Skip to content

Speed-up to O(1) from O(N) of the computation of each return in REINFORCE #1083

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions reinforcement_learning/reinforce.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import gym
import numpy as np
from itertools import count

from collections import deque
import torch
import torch.nn as nn
import torch.nn.functional as F
Expand Down Expand Up @@ -62,10 +62,10 @@ def select_action(state):
def finish_episode():
R = 0
policy_loss = []
returns = []
returns = deque()
for r in policy.rewards[::-1]:
R = r + args.gamma * R
returns.insert(0, R)
returns.appendleft(R)
returns = torch.tensor(returns)
returns = (returns - returns.mean()) / (returns.std() + eps)
for log_prob, R in zip(policy.saved_log_probs, returns):
Expand Down