-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmodels.py
184 lines (140 loc) · 5.06 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
from copy import deepcopy
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from util import to_numpy
if torch.cuda.is_available():
FloatTensor = torch.cuda.FloatTensor
else:
FloatTensor = torch.FloatTensor
class RLNN(nn.Module):
def __init__(self, state_dim, action_dim, max_action):
super(RLNN, self).__init__()
self.state_dim = state_dim
self.action_dim = action_dim
self.max_action = max_action
def set_params(self, params):
"""
Set the params of the network to the given parameters
"""
cpt = 0
for param in self.parameters():
tmp = np.product(param.size())
if torch.cuda.is_available():
param.data.copy_(torch.from_numpy(
params[cpt:cpt + tmp]).view(param.size()).cuda())
else:
param.data.copy_(torch.from_numpy(
params[cpt:cpt + tmp]).view(param.size()))
cpt += tmp
def get_params(self):
"""
Returns parameters of the actor
"""
return deepcopy(np.hstack([to_numpy(v).flatten() for v in
self.parameters()]))
def get_grads(self):
"""
Returns the current gradient
"""
return deepcopy(np.hstack([to_numpy(v.grad).flatten() for v in self.parameters()]))
def get_size(self):
"""
Returns the number of parameters of the network
"""
return self.get_params().shape[0]
def load_model(self, filename, net_name):
"""
Loads the model
"""
if filename is None:
return
self.load_state_dict(
torch.load('{}/{}.pkl'.format(filename, net_name),
map_location=lambda storage, loc: storage)
)
def save_model(self, output, net_name):
"""
Saves the model
"""
torch.save(
self.state_dict(),
'{}/{}.pkl'.format(output, net_name)
)
class Actor(RLNN):
def __init__(self, state_dim, action_dim, max_action, layer_norm=False, init=True):
super(Actor, self).__init__(state_dim, action_dim, max_action)
self.l1 = nn.Linear(state_dim, 400)
self.l2 = nn.Linear(400, 300)
self.l3 = nn.Linear(300, action_dim)
if layer_norm:
self.n1 = nn.LayerNorm(400)
self.n2 = nn.LayerNorm(300)
self.layer_norm = layer_norm
def forward(self, x):
if not self.layer_norm:
x = torch.tanh(self.l1(x))
x = torch.tanh(self.l2(x))
x = self.max_action * torch.tanh(self.l3(x))
else:
x = torch.tanh(self.n1(self.l1(x)))
x = torch.tanh(self.n2(self.l2(x)))
x = self.max_action * torch.tanh(self.l3(x))
return x
class Critic(RLNN):
def __init__(self, state_dim, action_dim, layer_norm=False):
super(Critic, self).__init__(state_dim, action_dim, 1)
self.l1 = nn.Linear(state_dim + action_dim, 400)
self.l2 = nn.Linear(400, 300)
self.l3 = nn.Linear(300, 1)
if layer_norm:
self.n1 = nn.LayerNorm(400)
self.n2 = nn.LayerNorm(300)
self.layer_norm = layer_norm
def forward(self, x, u):
if not self.layer_norm:
x = torch.leaky_relu(self.l1(torch.cat([x, u], 1)))
x = torch.leaky_relu(self.l2(x))
x = self.l3(x)
else:
x = torch.leaky_relu(self.n1(self.l1(torch.cat([x, u], 1))))
x = torch.leaky_relu(self.n2(self.l2(x)))
x = self.l3(x)
return x
class CriticTD3(RLNN):
def __init__(self, state_dim, action_dim, layer_norm=False):
super(CriticTD3, self).__init__(state_dim, action_dim, 1)
# Q1 architecture
self.l1 = nn.Linear(state_dim + action_dim, 400)
self.l2 = nn.Linear(400, 300)
self.l3 = nn.Linear(300, 1)
if layer_norm:
self.n1 = nn.LayerNorm(400)
self.n2 = nn.LayerNorm(300)
# Q2 architecture
self.l4 = nn.Linear(state_dim + action_dim, 400)
self.l5 = nn.Linear(400, 300)
self.l6 = nn.Linear(300, 1)
if layer_norm:
self.n4 = nn.LayerNorm(400)
self.n5 = nn.LayerNorm(300)
self.layer_norm = layer_norm
def forward(self, x, u):
if not self.layer_norm:
x1 = torch.leaky_relu(self.l1(torch.cat([x, u], 1)))
x1 = torch.leaky_relu(self.l2(x1))
x1 = self.l3(x1)
else:
x1 = torch.leaky_relu(self.n1(self.l1(torch.cat([x, u], 1))))
x1 = torch.leaky_relu(self.n2(self.l2(x1)))
x1 = self.l3(x1)
if not self.layer_norm:
x2 = torch.leaky_relu(self.l4(torch.cat([x, u], 1)))
x2 = torch.leaky_relu(self.l5(x2))
x2 = self.l6(x2)
else:
x2 = torch.leaky_relu(self.n4(self.l4(torch.cat([x, u], 1))))
x2 = torch.leaky_relu(self.n5(self.l5(x2)))
x2 = self.l6(x2)
return x1, x2