-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
executable file
·308 lines (244 loc) · 9.39 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
302
303
304
305
306
307
308
import torch
import torch.nn as nn
import torch.nn.functional as F
from distributions import Categorical, DiagGaussian
from utils import orthogonal, att, maxout, lwta
def weights_init(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1 or classname.find('Linear') != -1:
orthogonal(m.weight.data)
if m.bias is not None:
m.bias.data.fill_(0)
class FFPolicy(nn.Module):
def __init__(self):
super(FFPolicy, self).__init__()
def forward(self, inputs, states, masks):
raise NotImplementedError
def act(self, inputs, states, masks, deterministic=False):
value, x, states = self(inputs, states, masks)
action = self.dist.sample(x, deterministic=deterministic)
action_log_probs, dist_entropy = self.dist.logprobs_and_entropy(x, action)
return value, action, action_log_probs, states
def evaluate_actions(self, inputs, states, masks, actions):
value, x, states = self(inputs, states, masks)
action_log_probs, dist_entropy = self.dist.logprobs_and_entropy(x, actions)
return value, action_log_probs, dist_entropy, states
class CNNPolicy(FFPolicy):
def __init__(self, num_inputs, action_space, use_gru, act_func):
super(CNNPolicy, self).__init__()
self.act_func = act_func
self.acti = None
############## SETTING ACTIVATION FUNCTION STUFF ###################
if act_func == 'relu':
C = 1
print(">> ||| USING RELU ACTIVATION FUNCTION ||| <<")
elif act_func == 'maxout':
C = 2
self.acti = maxout
print(">> ||| USING maxout ACTIVATION FUNCTION ||| <<")
elif act_func == 'lwta':
C = 1
self.acti = lwta
print(">> ||| USING LWTA ACTIVATION FUNCTION ||| <<")
print(C)
self.conv1 = nn.Conv2d(num_inputs, 32*C, 8, stride=4)
self.conv2 = nn.Conv2d(32, 64*C, 4, stride=2)
self.conv3 = nn.Conv2d(64, 32*C, 3, stride=1)
self.linear1 = nn.Linear(32 * 7 * 7, 512)
#if use_att:
# self.att = att(256, 256)
if use_gru:
self.gru = nn.GRUCell(512, 256)
self.critic_linear = nn.Linear(256, 1)
if action_space.__class__.__name__ == "Discrete":
# HARCODED CHAGING
num_outputs = action_space.n
self.dist = Categorical(256, num_outputs)
elif action_space.__class__.__name__ == "Box":
#print("Sampling from Box")
num_outputs = action_space.shape[0]
self.dist = DiagGaussian(256, num_outputs)
else:
raise NotImplementedError
self.train()
self.reset_parameters()
@property
def state_size(self):
if hasattr(self, 'gru'):
return 256
else:
return 1
def reset_parameters(self):
self.apply(weights_init)
relu_gain = nn.init.calculate_gain('relu')
self.conv1.weight.data.mul_(relu_gain)
self.conv2.weight.data.mul_(relu_gain)
self.conv3.weight.data.mul_(relu_gain)
self.linear1.weight.data.mul_(relu_gain)
if hasattr(self, 'gru'):
orthogonal(self.gru.weight_ih.data)
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, states, masks):
x = self.conv1(inputs / 255.0)
if self.act_func == "relu":
x = F.relu(x)
else:
x = self.acti(x)
x = self.conv2(x)
if self.act_func == "relu":
x = F.relu(x)
else:
x = self.acti(x)
x = self.conv3(x)
if self.act_func == "relu":
x = F.relu(x)
else:
x = self.acti(x)
if hasattr(self, 'att'):
#print("GO FOR ATTENTION","RECEIVEING FROM CONVOLUTION THIS ONE", x.size())
x = x.view(-1, 49, 256)
else:
x = x.view(-1, 32 * 7 * 7)
x = self.linear1(x)
x = F.relu(x)
if hasattr(self, 'gru'):
if inputs.size(0) == states.size(0):
if hasattr(self, 'att'):
#print("I AMMMM PAYING ATTENTION")
#print("BEFORE ATTEND",x.size())
x = self.att(x, states*masks)
#print("AFTER ATTEND",x.size())
#print(x)
x = states = self.gru(x, states * masks)
else:
x = states = self.gru(x, states * masks)
else:
if hasattr(self, 'att'):
x = x.view(-1, states.size(0), 49, 256)
masks = masks.view(-1, states.size(0) , 1)
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)):
if hasattr(self, 'att'):
#print("I AMMMM PAYING ATTENTION BUT DIFFERENTLY")
#print("BEFORE ATTEND",x[i].size())
#print("SOMETHING IS WRONG HERE", states.size(), masks[i].size())
X = self.att(x[i], states*masks[i])
#print(X)
#print("AFTER ATTEND",x[i].size())
hx = states = self.gru(X, states * masks[i])
outputs.append(hx)
else:
hx = states = self.gru(x[i], states * masks[i])
outputs.append(hx)
x = torch.cat(outputs, 0)
#print(x)
return self.critic_linear(x), x, states
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(FFPolicy):
def __init__(self, num_inputs, action_space, act_func, drop, num_updates):
super(MLPPolicy, self).__init__()
self.drop = drop
self.act_func = act_func
self.num_updates = num_updates
self.counter = num_updates
############## SETTING ACTIVATION FUNCTION STUFF ###################
if act_func == 'tanh':
C = 1
print(">> ||| USING tanh ACTIVATION FUNCTION ||| <<")
elif act_func == 'maxout':
C = 2
self.acti = maxout
print(">> ||| USING maxout ACTIVATION FUNCTION ||| <<")
elif act_func == 'lwta':
self.acti = lwta
C = 1
print(">> ||| USING LWTA ACTIVATION FUNCTION ||| <<")
elif act_func == 'relu':
self.acti = F.relu
C = 1
print(">> ||| USING RELU ACTIVATION FUNCTION ||| <<")
print(C)
self.action_space = action_space
self.a_fc1 = nn.Linear(num_inputs, 64*C)
self.a_fc2 = nn.Linear(64, 64*C)
self.v_fc1 = nn.Linear(num_inputs, 64*C)
self.v_fc2 = nn.Linear(64, 64*C)
self.v_fc3 = nn.Linear(64, 1)
if action_space.__class__.__name__ == "Discrete":
num_outputs = action_space.n
self.dist = Categorical(64, num_outputs)
elif action_space.__class__.__name__ == "Box":
num_outputs = action_space.shape[0]
self.dist = DiagGaussian(64, num_outputs)
else:
raise NotImplementedError
self.train()
self.reset_parameters()
@property
def state_size(self):
return 1
def reset_parameters(self):
self.apply(weights_init_mlp)
"""
tanh_gain = nn.init.calculate_gain('tanh')
self.a_fc1.weight.data.mul_(tanh_gain)
self.a_fc2.weight.data.mul_(tanh_gain)
self.v_fc1.weight.data.mul_(tanh_gain)
self.v_fc2.weight.data.mul_(tanh_gain)
"""
if self.dist.__class__.__name__ == "DiagGaussian":
self.dist.fc_mean.weight.data.mul_(0.01)
def forward(self, inputs, states, masks):
if self.counter < 0:
decay = 0
else:
decay = self.counter/self.num_updates
#print("Decay is:", decay, "And drop is:", self.drop)
x = self.v_fc1(inputs)
if self.act_func == "tanh":
x = F.tanh(x)
else:
x = self.acti(x)
#DROPOUT
#print(x.data[0, :5].numpy(), "BEFORE DROP")
x = F.dropout(x, self.drop * decay)
#print(x.data[0, :5].numpy(), "AFTER DROP")
x = self.v_fc2(x)
if self.act_func == "tanh":
x = F.tanh(x)
else:
x = self.acti(x)
#DROPOUT
x = F.dropout(x, self.drop * decay)
x = self.v_fc3(x)
value = x
x = self.a_fc1(inputs)
if self.act_func == "tanh":
x = F.tanh(x)
else:
x = self.acti(x)
#DROPOUT
x = F.dropout(x, self.drop * decay)
x = self.a_fc2(x)
#print("IN",x.size())
if self.act_func == "tanh":
x = F.tanh(x)
else:
x = self.acti(x)
#DROPOUT
x = F.dropout(x, self.drop * decay)
#print("OUT",x.size())
return value, x, states