-
Notifications
You must be signed in to change notification settings - Fork 11
/
models.py
44 lines (36 loc) · 1.36 KB
/
models.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
import torch
import torch.nn as nn
import torch.nn.functional as F
"""
the input x in both networks should be [o, g], where o is the observation and g is the goal.
"""
# define the actor network
class actor(nn.Module):
def __init__(self, env_params):
super(actor, self).__init__()
self.max_action = env_params['action_max']
self.fc1 = nn.Linear(env_params['obs'] + env_params['goal'], 256)
self.fc2 = nn.Linear(256, 256)
self.fc3 = nn.Linear(256, 256)
self.action_out = nn.Linear(256, env_params['action'])
def forward(self, x):
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = F.relu(self.fc3(x))
actions = self.max_action * torch.tanh(self.action_out(x))
return actions
class critic(nn.Module):
def __init__(self, env_params):
super(critic, self).__init__()
self.max_action = env_params['action_max']
self.fc1 = nn.Linear(env_params['obs'] + env_params['goal'] + env_params['action'], 256)
self.fc2 = nn.Linear(256, 256)
self.fc3 = nn.Linear(256, 256)
self.q_out = nn.Linear(256, 1)
def forward(self, x, actions):
x = torch.cat([x, actions / self.max_action], dim=1)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = F.relu(self.fc3(x))
q_value = self.q_out(x)
return q_value