| 12 | |
| 13 | |
| 14 | class 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 | |
| 35 | def finish_episode(optimizer, policy, config): |