-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
301 lines (248 loc) · 9.66 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import torch
import torch.nn as nn
import torch.nn.functional as F
from distributions import get_distribution
def weights_init(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1 or classname.find('Linear') != -1:
nn.init.orthogonal_(m.weight.data)
if m.bias is not None:
m.bias.data.fill_(0)
class Flatten(nn.Module):
def forward(self, x):
return x.view(x.size(0), -1)
class HistoryCell(nn.Module):
def __init__(self,num_inputs,input_size,hid_size,batch_size=1):
super(HistoryCell, self).__init__()
'''
self.hsc_main = nn.Sequential(
nn.Conv2d(num_inputs, 16, 8, stride=4),
nn.ReLU(),
nn.Conv2d(16, 16, 4, stride=2),
nn.ReLU(),
nn.Conv2d(16, 16, 3, stride=1),
nn.ReLU(),
Flatten(),
nn.Linear(16 * 7 * 7,input_size),
nn.ReLU()
)
'''
self.batch_size=batch_size
self.hsc_lstm=nn.LSTMCell(input_size,hid_size)
self.hid_size=hid_size
def forward(self, input,h,c):
#x=self.hsc_main(input)
h,c=self.hsc_lstm(input,(h,c))
return h,c
class FutureCell(nn.Module):
def __init__(self,num_inputs,input_size,hid_size,batch_size=1):
super(FutureCell, self).__init__()
'''
self.ftc_main = nn.Sequential(
nn.Conv2d(num_inputs, 16, 8, stride=4),
nn.ReLU(),
nn.Conv2d(16, 16, 4, stride=2),
nn.ReLU(),
nn.Conv2d(16, 16, 3, stride=1),
nn.ReLU(),
Flatten(),
nn.Linear(16 * 7 * 7,input_size),
nn.ReLU()
)
'''
self.batch_size=batch_size
self.ftc_lstm=nn.LSTMCell(input_size,hid_size)
self.hid_size=hid_size
def forward(self, input,h,c):
#x=self.ftc_main(input)
h,c=self.ftc_lstm(input,(h,c))
return h,c
class History(nn.Module):
def __init__(self,num_inputs,input_size,hid_size,num_layers,batch_size):
super(History, self).__init__()
'''
self.hs_main = nn.Sequential(
nn.Conv2d(num_inputs, 16, 8, stride=4),
nn.ReLU(),
nn.Conv2d(16, 16, 4, stride=2),
nn.ReLU(),
nn.Conv2d(16, 16, 3, stride=1),
nn.ReLU(),
Flatten(),
nn.Linear(16 * 7 * 7,input_size),
nn.ReLU()
)
'''
self.hs_lstm=nn.LSTM(input_size,hid_size,num_layers)
self.hs_h0=torch.zeros(num_layers,batch_size,hid_size).cuda()
self.hs_c0=torch.zeros(num_layers,batch_size,hid_size).cuda()
self.hid_size=hid_size
def forward(self, input):
#x=self.hs_main(input)
x=input.unsqueeze(1)
out,(h,c)=self.hs_lstm(x,(self.hs_h0,self.hs_c0))
return h.view(1,2*self.hid_size)
class Future(nn.Module):
def __init__(self,num_inputs,input_size,hid_size,num_layers,batch_size):
super(Future, self).__init__()
'''
self.ft_main = nn.Sequential(
nn.Conv2d(num_inputs,16, 8, stride=4),
nn.ReLU(),
nn.Conv2d(16, 16, 4, stride=2),
nn.ReLU(),
nn.Conv2d(16, 16, 3, stride=1),
nn.ReLU(),
Flatten(),
nn.Linear(16 * 7 * 7,input_size),
nn.ReLU()
)
'''
self.ft_lstm=nn.LSTM(input_size,hid_size,num_layers)
self.ft_h0=torch.zeros(num_layers,batch_size,hid_size).cuda()
self.ft_c0=torch.zeros(num_layers,batch_size,hid_size).cuda()
self.hid_size=hid_size
def forward(self, input):
#x=self.ft_main(input)
x=input.unsqueeze(1)
out,(h,c)=self.ft_lstm(x,(self.ft_h0,self.ft_c0))
return h.view(1,2*self.hid_size)
class Policy(nn.Module):
def __init__(self):
super(Policy, self).__init__()
"""
All classes that inheret from Policy are expected to have
a feature exctractor for actor and critic (see examples below)
and modules called linear_critic and dist. Where linear_critic
takes critic features and maps them to value and dist
represents a distribution of actions.
"""
def forward(self, inputs, states, masks):
raise NotImplementedError
def get_feat(self,inputs):
hidden_feat= self(inputs)
return hidden_feat
def cat(self,hidden_feat,hs_info):
#hidden_feat = torch.cat((hidden_feat, hs_info), 1)
return hidden_feat
def act(self, hidden_feat, states, deterministic=False):
#hidden_critic, hidden_actor, states = self(inputs,hs_info, states, masks)
action = self.dist.sample(hidden_feat, deterministic=deterministic)
action_log_probs, dist_entropy = self.dist.logprobs_and_entropy(hidden_feat, action)
value = self.critic_linear(hidden_feat)
return value, action, action_log_probs, states
def get_value(self, hidden_feat):
#hidden_critic, _, states = self(hidden_feat, hs_info,states, masks)
value = self.critic_linear(hidden_feat)
return value
def evaluate_actions(self, hidden_feat, states,actions):
#hidden_critic, hidden_actor, states = self(inputs, hs_info,states, masks)
action_log_probs, dist_entropy = self.dist.logprobs_and_entropy(hidden_feat, actions)
value = self.critic_linear(hidden_feat)
return value, action_log_probs, dist_entropy, states
class CNNPolicy(Policy):
def __init__(self, num_inputs, action_space, hid_size,feat_size,use_gru):
super(CNNPolicy, self).__init__()
self.hid_size=hid_size
self.feat_size=feat_size
self.main = nn.Sequential(
nn.Conv2d(num_inputs, 32, 8, stride=4),
nn.ReLU(),
nn.Conv2d(32, 64, 4, stride=2),
nn.ReLU(),
nn.Conv2d(64, 32, 3, stride=1),
nn.ReLU(),
Flatten(),
nn.Linear(32 * 7 * 7, self.feat_size),
nn.ReLU()
)
if use_gru:
self.gru = nn.GRUCell(self.feat_size+self.hid_size*2, self.feat_size+self.hid_size*2)
#self.critic_linear = nn.Linear(self.feat_size+self.hid_size*2, 1)
self.critic_linear = nn.Linear(self.feat_size, 1)
#self.dist = get_distribution(self.feat_size+self.hid_size*2, action_space)
self.dist = get_distribution(self.feat_size, action_space)
self.train()
self.reset_parameters()
@property
def state_size(self):
if hasattr(self, 'gru'):
return self.feat_size
else:
return 1
@property
def hidden_size(self):
return self.hid_size
def reset_parameters(self):
self.apply(weights_init)
def mult_gain(m):
relu_gain = nn.init.calculate_gain('relu')
classname = m.__class__.__name__
if classname.find('Conv') != -1 or classname.find('Linear') != -1:
m.weight.data.mul_(relu_gain)
self.main.apply(mult_gain)
if hasattr(self, 'gru'):
nn.init.orthogonal_(self.gru.weight_ih.data)
nn.init.orthogonal_(self.gru.weight_hh.data)
self.gru.bias_ih.data.fill_(0)
self.gru.bias_hh.data.fill_(0)
if self.dist.__class__.__name__ == "DiagGaussian":
self.dist.fc_mean.weight.data.mul_(0.01)
def forward(self, inputs):
x = self.main(inputs / 255.0)
'''
if hasattr(self, 'gru'):
if inputs.size(0) == states.size(0):
x = states = self.gru(x, states * masks)
else:
x = x.view(-1, states.size(0), x.size(1))
masks = masks.view(-1, states.size(0), 1)
outputs = []
for i in range(x.size(0)):
hx = states = self.gru(x[i], states * masks[i])
outputs.append(hx)
x = torch.cat(outputs, 0)
'''
#print(x.shape)
#print(hs_info.shape)
#print(hs_info.type())
#print(x.shape)
return x
def weights_init_mlp(m):
classname = m.__class__.__name__
if classname.find('Linear') != -1:
m.weight.data.normal_(0, 1)
m.weight.data *= 1 / torch.sqrt(m.weight.data.pow(2).sum(1, keepdim=True))
if m.bias is not None:
m.bias.data.fill_(0)
class MLPPolicy(Policy):
def __init__(self, num_inputs, action_space):
super(MLPPolicy, self).__init__()
self.action_space = action_space
self.actor = nn.Sequential(
nn.Linear(num_inputs, 64),
nn.Tanh(),
nn.Linear(64, 64),
nn.Tanh()
)
self.critic = nn.Sequential(
nn.Linear(num_inputs, 64),
nn.Tanh(),
nn.Linear(64, 64),
nn.Tanh()
)
self.critic_linear = nn.Linear(64, 1)
self.dist = get_distribution(64, action_space)
self.train()
self.reset_parameters()
@property
def state_size(self):
return 1
def reset_parameters(self):
self.apply(weights_init_mlp)
if self.dist.__class__.__name__ == "DiagGaussian":
self.dist.fc_mean.weight.data.mul_(0.01)
def forward(self, inputs, states, masks):
hidden_critic = self.critic(inputs)
hidden_actor = self.actor(inputs)
return hidden_critic, hidden_actor, states