Last active
January 21, 2020 17:21
-
-
Save piEsposito/558e9ca1fb2c2abba275781b64623a3b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def update_policy(policy_network, rewards, log_probs): | |
discounted_rewards = [] | |
for t in range(len(rewards)): | |
Gt = 0 | |
pw = 0 | |
for r in rewards[t:]: | |
Gt = Gt + GAMMA**pw * r | |
pw += 1 | |
discounted_rewards.append(Gt) | |
discounted_rewards = torch.tensor(discounted_rewards, device=device) | |
discounted_rewards = (discounted_rewards - discounted_rewards.mean()) / (discounted_rewards.std()) | |
policy_gradient = [] | |
for log_prob, Gt in zip(log_probs, discounted_rewards): | |
policy_gradient.append(-log_prob * Gt) | |
policy_network.optimizer.zero_grad() | |
policy_gradient_ = torch.stack(policy_gradient).sum() | |
policy_gradient_.backward(retain_graph=True) | |
policy_network.optimizer.step() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment