MCPcopy Create free account
hub / github.com/AgentMaker/Paddle-RLBooks / Policy

Class Policy

policy_gradient/policy_gradient_basic.py:19–41  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

17paddle.seed(1)
18
19class 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
43policy = Policy()
44optimizer = optim.Adam(parameters=policy.parameters(), learning_rate=1e-2)

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected