Sample action from agents output distribution over actions.
(self, observation)
| 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): |