Pytorch 中的策略梯度损失

时间:2021-01-16 13:26:34

标签: deep-learning pytorch reinforcement-learning policy-gradient-descent

版本 1

y = episode_a.argmax(-1)   # episode_a is in shape [T, n_actions]
action_preds = self.net(ep_s)  # action_preds is logits before softmax
neg_log_like = self.loss_fn(action_preds, y) 
loss = torch.mean(r * neg_log_like)   # r is reward

版本 2

y = torch.tensor(episode_a, requires_grad=True)
action_preds = model(ep_s)
neg_log_like = -y * torch.log(action_preds)
loss = torch.sum(neg_log_like, 1).mean()

版本 1 和 2 似乎具有相同的损失值。不同之处在于,y 在版本 2 中不需要 grad。但它就像一个监督学习反向传播操作,y 应该不需要 require_grad。我不明白为什么版本 1 不能训练策略而版本 2 可以?

0 个答案:

没有答案