When using a stochastic function in the graph (such as torch.multinomial), you have to give it a reward before backproping through the graph.
Here’s an example:

  
      github.com
  
  
    pytorch/examples/blob/master/reinforcement_learning/reinforce.py#L58-L70 149



def finish_episode():
R = 0
policy_loss = []
rewards = []
for r in policy.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, reward in zip(policy.saved_log_probs, rewards):
    policy_loss.append(-log_prob * reward)
optimizer.zero_grad()