| 17 | paddle.seed(1) |
| 18 | |
| 19 | class Policy(nn.Layer): |
| 20 | def __init__(self): |
| 21 | super(Policy, self).__init__() |
| 22 | self.fc1 = nn.Linear(4, 128) |
| 23 | self.fc2 = nn.Linear(128, 2) |
| 24 | |
| 25 | self.saved_log_probs = [] |
| 26 | self.rewards = [] |
| 27 | |
| 28 | def forward(self, inputs): |
| 29 | x = F.relu(F.dropout(self.fc1(inputs), 0.6)) |
| 30 | x = self.fc2(x) |
| 31 | |
| 32 | return F.softmax(x, -1) |
| 33 | |
| 34 | def select_action(self, inputs): |
| 35 | x = paddle.to_tensor(inputs).astype('float32').unsqueeze(0) |
| 36 | probs = self.forward(x) |
| 37 | m = Categorical(probs) |
| 38 | action = m.sample([1]) |
| 39 | self.saved_log_probs.append(m.log_prob(action)) |
| 40 | |
| 41 | return action |
| 42 | |
| 43 | policy = Policy() |
| 44 | optimizer = optim.Adam(parameters=policy.parameters(), learning_rate=1e-2) |
no outgoing calls
no test coverage detected