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

Class Observer

distributed/rpc/rl/main.py:72–110  ·  view source on GitHub ↗

r""" An observer has exclusive access to its own environment. Each observer captures the state from its environment, and send the state to the agent to select an action. Then, the observer applies the action to its environment and reports the reward to the agent. It is true that

Source from the content-addressed store, hash-verified

70 return F.softmax(action_scores, dim=1)
71
72class Observer:
73 r"""
74 An observer has exclusive access to its own environment. Each observer
75 captures the state from its environment, and send the state to the agent to
76 select an action. Then, the observer applies the action to its environment
77 and reports the reward to the agent.
78
79 It is true that CartPole-v1 is a relatively inexpensive environment, and it
80 might be an overkill to use RPC to connect observers and trainers in this
81 specific use case. However, the main goal of this tutorial to how to build
82 an application using the RPC API. Developers can extend the similar idea to
83 other applications with much more expensive environment.
84 """
85 def __init__(self):
86 self.id = rpc.get_worker_info().id
87 self.env = gym.make('CartPole-v1')
88 self.env.reset(seed=args.seed)
89
90 def run_episode(self, agent_rref, n_steps):
91 r"""
92 Run one episode of n_steps.
93
94 Args:
95 agent_rref (RRef): an RRef referencing the agent object.
96 n_steps (int): number of steps in this episode
97 """
98 state, ep_reward = self.env.reset()[0], 0
99 for step in range(n_steps):
100 # send the state to the agent to get an action
101 action = _remote_method(Agent.select_action, agent_rref, self.id, state)
102
103 # apply the action to the environment, and get the reward
104 state, reward, terminated, truncated, _ = self.env.step(action)
105
106 # report the reward to the agent for training purpose
107 _remote_method(Agent.report_reward, agent_rref, self.id, reward)
108
109 if terminated or truncated:
110 break
111
112class Agent:
113 def __init__(self, world_size):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected