MCPcopy Create free account
hub / github.com/BIT-MCS/DRL-eFresh / Policy

Class Policy

utils/main_mp_github.py:14–32  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

12
13
14class Policy(nn.Module):
15 def __init__(self, agent):
16 super(Policy, self).__init__()
17 self.agent = agent
18
19 self.saved_log_probs = []
20 self.rewards = []
21
22 def forward(self, observation):
23 """Sample action from agents output distribution over actions.
24 """
25 # Unsqueeze to give a batch size of 1.
26 state = torch.from_numpy(observation).float().unsqueeze(0)
27 action_scores, _ = self.agent(state)
28 action_probs = F.softmax(action_scores, dim=-1)
29 dist = torch.distributions.Categorical(action_probs)
30 action = dist.sample()
31 self.saved_log_probs.append(dist.log_prob(action))
32 return action.item()
33
34
35def finish_episode(optimizer, policy, config):

Callers 1

trainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected