-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathmodel.py
40 lines (33 loc) · 1.25 KB
/
model.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
import torch as th
import torch.nn as nn
import torch.nn.functional as F
class Critic(nn.Module):
def __init__(self, n_agent, dim_observation, dim_action):
super(Critic, self).__init__()
self.n_agent = n_agent
self.dim_observation = dim_observation
self.dim_action = dim_action
obs_dim = dim_observation * n_agent
act_dim = self.dim_action * n_agent
self.FC1 = nn.Linear(obs_dim, 1024)
self.FC2 = nn.Linear(1024+act_dim, 512)
self.FC3 = nn.Linear(512, 300)
self.FC4 = nn.Linear(300, 1)
# obs: batch_size * obs_dim
def forward(self, obs, acts):
result = F.relu(self.FC1(obs))
combined = th.cat([result, acts], 1)
result = F.relu(self.FC2(combined))
return self.FC4(F.relu(self.FC3(result)))
class Actor(nn.Module):
def __init__(self, dim_observation, dim_action):
super(Actor, self).__init__()
self.FC1 = nn.Linear(dim_observation, 500)
self.FC2 = nn.Linear(500, 128)
self.FC3 = nn.Linear(128, dim_action)
# action output between -2 and 2
def forward(self, obs):
result = F.relu(self.FC1(obs))
result = F.relu(self.FC2(result))
result = F.tanh(self.FC3(result))
return result