-
Notifications
You must be signed in to change notification settings - Fork 0
/
actorcritic.py
48 lines (38 loc) · 1.35 KB
/
actorcritic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import torch
import torch.nn as nn
from torch.distributions import Categorical
device = torch.device('cuda:0')
class ActorCritic(nn.Module):
def __init__(self, state_dim, action_dim):
super(ActorCritic, self).__init__()
self.actor = nn.Sequential(
nn.Linear(state_dim, 512),
nn.Tanh(),
nn.Linear(512, 256),
nn.Tanh(),
nn.Linear(256, action_dim),
nn.Softmax(-1)
)
self.critic = nn.Sequential(
nn.Linear(state_dim, 512),
nn.Tanh(),
nn.Linear(512, 256),
nn.Tanh(),
nn.Linear(256, 1)
)
def forward(self):
raise Exception("cannot call forward() on actor critic")
def act(self, state):
action_probs = self.actor(state)
dist = Categorical(action_probs)
action = dist.sample()
action_logprob = dist.log_prob(action)
state_val = self.critic(state)
return action.detach(), action_logprob.detach(), state_val.detach()
def evaluate(self, state, action):
action_probs = self.actor(state)
dist = Categorical(action_probs)
action_logprobs = dist.log_prob(action)
dist_entropy = dist.entropy()
state_values = self.critic(state)
return action_logprobs, state_values, dist_entropy