-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel_def.py
364 lines (299 loc) · 13.1 KB
/
model_def.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import torch
import torch.nn as nn
from functions import TernaryTanh
from torch.autograd import Variable
import tools as tl
class HxQBNet(nn.Module):
"""
Quantized Bottleneck Network(QBN) for hidden states of GRU
"""
def __init__(self, input_size, x_features):
super(HxQBNet, self).__init__()
self.bhx_size = x_features
f1, f2 = int(8 * x_features), int(4 * x_features)
self.encoder = nn.Sequential(nn.Linear(input_size, f1),
nn.Tanh(),
nn.Linear(f1, f2),
nn.Tanh(),
nn.Linear(f2, x_features),
TernaryTanh())
self.decoder = nn.Sequential(nn.Linear(x_features, f2),
nn.Tanh(),
nn.Linear(f2, f1),
nn.Tanh(),
nn.Linear(f1, input_size),
nn.Tanh())
def forward(self, x):
x = self.encode(x)
return self.decode(x), x
def encode(self, x):
return self.encoder(x)
def decode(self, x):
return self.decoder(x)
class MMNet(nn.Module):
"""
Moore Machine Network(MMNet) definition.
"""
def __init__(self, net, hx_qbn=None, obs_qbn=None):
super(MMNet, self).__init__()
self.bhx_units = hx_qbn.bhx_size if hx_qbn is not None else None
self.gru_units = net.gru_units
self.obx_net = obs_qbn
self.gru_net = net
self.bhx_net = hx_qbn
self.actor_linear = self.gru_net.get_action_linear
def init_hidden(self, batch_size=1):
return self.gru_net.init_hidden(batch_size)
def forward(self, x, inspect=False):
if inspect:
x, hx = x
critic, actor, hx, (ghx, bhx, input_c, input_x, input_tanh) = self.gru_net((x, hx), input_fn=self.obx_net, hx_fn=self.bhx_net, inspect=True)
return critic, actor, hx, (ghx, bhx), (input_c, input_x, input_tanh)
else:
input_c = self.gru_net(x, input_fn=self.obx_net, hx_fn=self.bhx_net, inspect=False)
return input_c
def get_action_linear(self, state, decode=False):
if decode:
hx = self.bhx_net.decode(state)
else:
hx = state
return self.actor_linear(hx)
def transact(self, o_x, hx_x):
hx_x = self.gru_net.transact(self.obx_net.decode(o_x), self.bhx_net.decode(hx_x))
_, hx_x = self.bhx_net(hx_x)
return hx_x
def state_encode(self, state):
return self.bhx_net.encode(state)
def obs_encode(self, obs, hx=None):
if hx is None:
hx = Variable(torch.zeros(1, self.gru_units))
if next(self.parameters()).is_cuda:
hx = hx.cuda()
_, _, _, (_, _, _, input_x) = self.gru_net((obs, hx), input_fn=self.obx_net, hx_fn=self.bhx_net, inspect=True)
return input_x
class ObsQBNet(nn.Module):
"""
Quantized Bottleneck Network(QBN) for observation features.
"""
def __init__(self, input_size, x_features):
super(ObsQBNet, self).__init__()
self.bhx_size = x_features
f1 = int(8 * x_features)
self.encoder = nn.Sequential(nn.Linear(input_size, f1),
nn.Tanh(),
nn.Linear(f1, x_features),
TernaryTanh())
self.decoder = nn.Sequential(nn.Linear(x_features, f1),
nn.Tanh(),
nn.Linear(f1, input_size),
nn.ReLU6())
def forward(self, x):
encoded, before_ttanh = self.encode(x)
decoded = self.decode(encoded)
return decoded, encoded, before_ttanh
def encode(self, x):
linear1 = self.encoder[0](x)
tanh = self.encoder[1](linear1)
linear2 = self.encoder[2](tanh)
ttanh = self.encoder[3](linear2)
return self.encoder(x), linear2
def decode(self, x):
return self.decoder(x)
class GRUNet(nn.Module):
"""
Gated Recurrent Unit Network(GRUNet) definition.
"""
def __init__(self, input_size, gru_cells, total_actions):
super(GRUNet, self).__init__()
self.gru_units = gru_cells
self.noise = False
self.conv1 = nn.Conv2d(input_size, 32, 3, stride=2, padding=1)
self.conv2 = nn.Conv2d(32, 32, 3, stride=2, padding=1)
self.conv3 = nn.Conv2d(32, 16, 3, stride=2, padding=1)
self.conv4 = nn.Conv2d(16, 8, 3, stride=2, padding=1)
self.input_ff = nn.Sequential(self.conv1, nn.ReLU(),
self.conv2, nn.ReLU(),
self.conv3, nn.ReLU(),
self.conv4, nn.ReLU6())
self.input_c_features = 8 * 5 * 5
self.input_c_shape = (8, 5, 5)
self.gru = nn.GRUCell(self.input_c_features, gru_cells)
self.critic_linear = nn.Linear(gru_cells, 1)
self.actor_linear = nn.Linear(gru_cells, total_actions)
self.apply(tl.weights_init)
self.actor_linear.weight.data = tl.normalized_columns_initializer(self.actor_linear.weight.data, 0.01)
self.actor_linear.bias.data.fill_(0)
self.critic_linear.weight.data = tl.normalized_columns_initializer(self.critic_linear.weight.data, 1.0)
self.critic_linear.bias.data.fill_(0)
self.gru.bias_ih.data.fill_(0)
self.gru.bias_hh.data.fill_(0)
def forward(self, input, input_fn=None, hx_fn=None, inspect=False):
if inspect:
input, hx = input
c_input = self.input_ff(input)
c_input = c_input.view(-1, self.input_c_features)
input, input_x, linear2 = input_fn(c_input) if input_fn is not None else (c_input, c_input)
ghx = self.gru(input, hx)
hx, bhx = hx_fn(ghx) if hx_fn is not None else (ghx, ghx)
return self.critic_linear(hx), self.actor_linear(hx), hx, (ghx, bhx, c_input, input_x, linear2)
else:
c_input = self.input_ff(input)
c_input = c_input.view(-1, self.input_c_features)
input, input_x, linear2 = input_fn(c_input) if input_fn is not None else (c_input, c_input)
return c_input, input_x, linear2
def init_hidden(self, batch_size=1):
return torch.zeros(batch_size, self.gru_units)
def get_action_linear(self, state):
return self.actor_linear(state)
def transact(self, o_x, hx):
hx = self.gru(o_x, hx)
return hx
class ControlGRUNet(nn.Module):
"""
Gated Recurrent Unit Network(GRUNet) definition
"""
def __init__(self, input_size, gru_cells, total_actions):
super(ControlGRUNet, self).__init__()
self.gru_units = gru_cells
self.noise = False
self.input_ff = nn.Sequential(nn.Linear(input_size, 16),
nn.ELU(),
nn.Linear(16, 8),
nn.ReLU6())
self.input_flat_size = 8
self.input_c_features = self.input_flat_size
self.gru = nn.GRUCell(self.input_flat_size, 32)
self.critic_linear = nn.Linear(gru_cells, 1)
self.actor_linear = nn.Linear(gru_cells, total_actions)
self.apply(tl.weights_init)
self.actor_linear.weight.data = tl.normalized_columns_initializer(self.actor_linear.weight.data, 0.01)
self.actor_linear.bias.data.fill_(0)
self.critic_linear.weight.data = tl.normalized_columns_initializer(self.critic_linear.weight.data, 1.0)
self.critic_linear.bias.data.fill_(0)
self.gru.bias_ih.data.fill_(0)
self.gru.bias_hh.data.fill_(0)
def forward(self, input, input_fn=None, hx_fn=None, inspect=False):
if inspect:
input, hx = input
c_input = self.input_ff(input)
c_input = c_input.view(-1, self.input_flat_size)
input, input_x, input_tanh = input_fn(c_input) if input_fn is not None else (c_input, c_input, None)
ghx = self.gru(input, hx)
hx, bhx = hx_fn(ghx) if hx_fn is not None else (ghx, ghx)
return self.critic_linear(hx), self.actor_linear(hx), hx, (ghx, bhx, c_input, input_x, input_tanh)
else:
c_input = self.input_ff(input)
c_input = c_input.view(-1, self.input_c_features)
input, input_x, input_tanh = input_fn(c_input) if input_fn is not None else (c_input, c_input)
return c_input, input_x, input_tanh
def init_hidden(self, batch_size=1):
return torch.zeros(batch_size, self.gru_units)
def get_action_linear(self, state):
return self.actor_linear(state)
def transact(self, o_x, hx):
hx = self.gru(o_x, hx)
return hx
class ControlObsQBNet(nn.Module):
"""
Quantized Bottleneck Network(QBN) for observation features
"""
def __init__(self, input_size, x_features):
super(ControlObsQBNet, self).__init__()
self.bhx_size = x_features
f1 = int(8 * x_features)
self.encoder = nn.Sequential(nn.Linear(input_size, f1),
nn.Tanh(),
nn.Linear(f1, x_features),
TernaryTanh())
self.decoder = nn.Sequential(nn.Linear(x_features, f1),
nn.Tanh(),
nn.Linear(f1, input_size),
nn.ReLU6())
def forward(self, x):
encoded, before_ttanh = self.encode(x)
# encoded = self.encode(x)
decoded = self.decode(encoded)
return decoded, encoded, before_ttanh
# return decoded, encoded
def encode(self, x):
linear1 = self.encoder[0](x)
tanh = self.encoder[1](linear1)
linear2 = self.encoder[2](tanh)
ttanh = self.encoder[3](linear2)
return self.encoder(x), linear2
# return self.encoder(x)
def decode(self, x):
return self.decoder(x)
class ControlHxQBNet(nn.Module):
"""
Quantized Bottleneck Network(QBN) for hidden states of GRU
"""
def __init__(self, input_size, x_features):
super(ControlHxQBNet, self).__init__()
self.bhx_size = x_features
f1 = int(8 * x_features)
self.encoder = nn.Sequential(nn.Linear(input_size, f1),
nn.Tanh(),
nn.Linear(f1, x_features),
TernaryTanh())
self.decoder = nn.Sequential(nn.Linear(x_features, f1),
nn.Tanh(),
nn.Linear(f1, input_size),
nn.Tanh())
def forward(self, x):
# encoded, before_ttanh = self.encode(x)
encoded = self.encode(x)
decoded = self.decode(encoded)
# return decoded, encoded, before_ttanh
return decoded, encoded
def encode(self, x):
linear1 = self.encoder[0](x)
tanh = self.encoder[1](linear1)
linear2 = self.encoder[2](tanh)
ttanh = self.encoder[3](linear2)
# return self.encoder(x), linear2
return self.encoder(x)
def decode(self, x):
return self.decoder(x)
class ControlMMNet(nn.Module):
"""
Moore Machine Network(MMNet) definition
"""
def __init__(self, net, hx_qbn=None, obs_qbn=None):
super(ControlMMNet, self).__init__()
self.bhx_units = hx_qbn.bhx_size if hx_qbn is not None else None
self.gru_units = net.gru_units
self.obx_net = obs_qbn
self.gru_net = net
self.bhx_net = hx_qbn
self.actor_linear = self.gru_net.get_action_linear
def init_hidden(self, batch_size=1):
return self.gru_net.init_hidden(batch_size)
def forward(self, x, inspect=False):
if inspect:
x, hx = x
critic, actor, hx, (ghx, bhx, input_c, input_x, input_tanh) = self.gru_net((x, hx), input_fn=self.obx_net,
hx_fn=self.bhx_net, inspect=True)
return critic, actor, hx, (ghx, bhx), (input_c, input_x, input_tanh)
else:
input_c = self.gru_net(x, input_fn=self.obx_net, hx_fn=self.bhx_net, inspect=False)
return input_c
def get_action_linear(self, state, decode=False):
if decode:
hx = self.bhx_net.decode(state)
else:
hx = state
return self.actor_linear(hx)
def transact(self, o_x, hx_x):
hx_x = self.gru_net.transact(self.obx_net.decode(o_x), self.bhx_net.decode(hx_x))
_, hx_x = self.bhx_net(hx_x)
return hx_x
def state_encode(self, state):
return self.bhx_net.encode(state)
def obs_encode(self, obs, hx=None):
if hx is None:
hx = Variable(torch.zeros(1, self.gru_units))
if next(self.parameters()).is_cuda:
hx = hx.cuda()
_, _, _, (_, _, _, input_x, _, _) = self.gru_net((obs, hx), input_fn=self.obx_net, hx_fn=self.bhx_net, inspect=True)
return input_x