MCPcopy Create free account
hub / github.com/pytorch/examples / main

Function main

reinforcement_learning/actor_critic.py:138–177  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

136
137
138def main():
139 running_reward = 10
140
141 # run infinitely many episodes
142 for i_episode in count(1):
143
144 # reset environment and episode reward
145 state, _ = env.reset()
146 ep_reward = 0
147
148 # for each episode, only run 9999 steps so that we don't
149 # infinite loop while learning
150 for t in range(1, 10000):
151
152 # select action from policy
153 action = select_action(state)
154
155 # take the action
156 state, reward, terminated, truncated, _ = env.step(action)
157
158 model.rewards.append(reward)
159 ep_reward += reward
160 if terminated or truncated:
161 break
162
163 # update cumulative reward
164 running_reward = 0.05 * ep_reward + (1 - 0.05) * running_reward
165
166 # perform backprop
167 finish_episode()
168
169 # log results
170 if i_episode % args.log_interval == 0:
171 print(f'Episode {i_episode}\tLast reward: {ep_reward:.2f}\tAverage reward: {running_reward:.2f}')
172
173 # check if we have "solved" the cart pole problem
174 if running_reward > env.spec.reward_threshold:
175 print(f"Solved! Running reward is now {running_reward} and "
176 f"the last episode runs to {t} time steps!")
177 break
178
179
180if __name__ == '__main__':

Callers 1

actor_critic.pyFile · 0.70

Calls 3

resetMethod · 0.80
select_actionFunction · 0.70
finish_episodeFunction · 0.70

Tested by

no test coverage detected