implements both actor and critic in one model
| 34 | |
| 35 | |
| 36 | class Policy(nn.Module): |
| 37 | """ |
| 38 | implements both actor and critic in one model |
| 39 | """ |
| 40 | def __init__(self): |
| 41 | super(Policy, self).__init__() |
| 42 | self.affine1 = nn.Linear(4, 128) |
| 43 | |
| 44 | # actor's layer |
| 45 | self.action_head = nn.Linear(128, 2) |
| 46 | |
| 47 | # critic's layer |
| 48 | self.value_head = nn.Linear(128, 1) |
| 49 | |
| 50 | # action & reward buffer |
| 51 | self.saved_actions = [] |
| 52 | self.rewards = [] |
| 53 | |
| 54 | def forward(self, x): |
| 55 | """ |
| 56 | forward of both actor and critic |
| 57 | """ |
| 58 | x = F.relu(self.affine1(x)) |
| 59 | |
| 60 | # actor: choses action to take from state s_t |
| 61 | # by returning probability of each action |
| 62 | action_prob = F.softmax(self.action_head(x), dim=-1) |
| 63 | |
| 64 | # critic: evaluates being in the state s_t |
| 65 | state_values = self.value_head(x) |
| 66 | |
| 67 | # return values for both actor and critic as a tuple of 2 values: |
| 68 | # 1. a list with the probability of each action over the action space |
| 69 | # 2. the value from state s_t |
| 70 | return action_prob, state_values |
| 71 | |
| 72 | |
| 73 | model = Policy() |