ca907342507c1139696f542de0a3351d7a382eee,reinforcement_learning/actor_critic.py,,finish_episode,#,65

Before Change


        policy_loss -= (log_prob * reward).sum()
        value_loss += F.smooth_l1_loss(value, Variable(torch.Tensor([r])))
    optimizer.zero_grad()
    (policy_loss + value_loss).backward()
    optimizer.step()
    del model.rewards[:]
    del model.saved_actions[:]

After Change


    R = 0
    saved_actions = model.saved_actions
    policy_losses = []
    value_losses = []
    rewards = []
    for r in model.rewards[::-1]:
        R = r + args.gamma * R
        rewards.insert(0, R)
    rewards = torch.Tensor(rewards)
    rewards = (rewards - rewards.mean()) / (rewards.std() + np.finfo(np.float32).eps)
    for (log_prob, value), r in zip(saved_actions, rewards):
        reward = r - value.data[0, 0]
        policy_losses.append(-log_prob * reward)
        value_losses.append(F.smooth_l1_loss(value, Variable(torch.Tensor([r]))))
    optimizer.zero_grad()
    loss = torch.cat(policy_losses).sum() + torch.cat(value_losses).sum()
    loss.backward()
    optimizer.step()
    del model.rewards[:]
    del model.saved_actions[:]
Italian Trulli
In pattern: SUPERPATTERN

Frequency: 3

Non-data size: 5

Instances


Project Name: pytorch/examples
Commit Name: ca907342507c1139696f542de0a3351d7a382eee
Time: 2017-12-04
Author: sgross@fb.com
File Name: reinforcement_learning/actor_critic.py
Class Name:
Method Name: finish_episode


Project Name: neubig/nn4nlp-code
Commit Name: f31488d65a98107e03f2045f3c5e2f927dab067d
Time: 2019-01-18
Author: mysteryvaibhav@gmail.com
File Name: 03-wordemb-pytorch/wordemb-skip.py
Class Name:
Method Name: calc_sent_loss


Project Name: neubig/nn4nlp-code
Commit Name: f31488d65a98107e03f2045f3c5e2f927dab067d
Time: 2019-01-18
Author: mysteryvaibhav@gmail.com
File Name: 03-wordemb-pytorch/wordemb-cbow.py
Class Name:
Method Name: calc_sent_loss