-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
452 lines (384 loc) · 16.5 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
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
import torch.nn.functional as F
import torch
import torch.nn as nn
from base import Reshape, conv_shape
import torch.autograd as autograd
import pdb
import numpy as np
import torch.nn.init as init
from einops import rearrange, repeat
from base import EncoderConv, DecoderConv, \
EncoderConv1, DecoderConv1, \
EncoderConv2, DecoderConv2, \
EncoderResNet, DecoderResNet, \
ContentMotion, DynamicalModel, \
Discriminator_V, TemporalBlock, \
EncoderMNIST, DecoderMNIST
class SubspaceVAE(nn.Module):
def __init__(self, config, data, device_ids=[0,1,2]):
super(SubspaceVAE, self).__init__()
self.config = config
self.device_ids = device_ids
if self.config['dynamics'] == 'symplecticform':
decoder_dim_V = int(self.config['v_dim']/2)
else:
decoder_dim_V = self.config['v_dim']
if not self.config['useZ']:
self.config['z_dim'] = 0
if data == 'rmnist':
self.encoder = EncoderMNIST(self.config['channels'],
self.config['h_dim']).to(self.device_ids[0])
self.decoder = DecoderMNIST(self.config['h_dim'],
decoder_dim_V,
self.config['z_dim'],
self.config['channels']).to(self.device_ids[1])
else:
if self.config['network'] == 'conv':
self.encoder = EncoderConv(self.config['channels'],
self.config['h_dim']).to(self.device_ids[0])
self.decoder = DecoderConv(self.config['h_dim'],
decoder_dim_V,
self.config['z_dim'],
self.config['channels']).to(self.device_ids[1])
elif self.config['network'] == 'conv1':
self.encoder = EncoderConv1(self.config['channels'],
self.config['h_dim']).to(self.device_ids[0])
self.decoder = DecoderConv1(self.config['h_dim'],
decoder_dim_V,
self.config['z_dim'],
self.config['channels']).to(self.device_ids[1])
elif self.config['network'] == 'conv2':
self.encoder = EncoderConv2(self.config['channels'],
self.config['h_dim']).to(self.device_ids[0])
self.decoder = DecoderConv2(self.config['h_dim'],
decoder_dim_V,
self.config['z_dim'],
self.config['channels']).to(self.device_ids[1])
elif self.config['network'] == 'resnetladder':
self.encoder = EncoderResNet(self.config['width'],
self.config['height'],
self.config['channels'],
self.config['h_dim']).to(self.device_ids[0])
self.decoder = DecoderResNet(self.config['width'],
self.config['height'],
self.config['channels'],
self.config['h_dim'],
decoder_dim_V,
self.config['z_dim']).to(self.device_ids[1])
else:
assert 0, f"Network Not implemented {self.config['network']}"
if self.config['sequential']:
self.contentMotion = ContentMotion(self.config['h_dim'],
self.config['z_dim'],
self.config['v_dim'],
self.config['u_dim'],
self.config['uselstmZ'],
self.config['condnSonU'],
self.config['useZ'],
self.config['dynamics']).to(self.device_ids[0])
if self.config['dynamics'] in ['Hamiltonian', 'SkewHamiltonian']:
self.W_p = TemporalBlock(self.config['v_dim'], self.config['v_dim'],
kernel_size=(4,), dropout=0, padding=3, dilation=1,
stride=1).to(self.device_ids[0])
self.mu_p = nn.Linear(self.config['v_dim'], self.config['v_dim']).to(self.device_ids[0])
self.sigma_p = nn.Linear(self.config['v_dim'], self.config['v_dim']).to(self.device_ids[0])
self.mu_p.apply(self.weights_init(init_type='orthogonal'))
self.sigma_p.apply(self.weights_init(init_type='orthogonal'))
#if self.config['useZ']:
# self.mu_z = nn.Linear(self.config['z_dim'], self.config['z_dim']).to(self.device_ids[0])
# self.sigma_z = nn.Linear(self.config['z_dim'], self.config['z_dim']).to(self.device_ids[0])
if self.config['dynamics'] == 'Fourier':
in_dim, out_dim = 1, 1
else:
in_dim, out_dim = self.config['v_dim'], self.config['v_dim']
self.mu_q = nn.Linear(in_dim, out_dim).to(self.device_ids[0])
self.sigma_q = nn.Linear(in_dim, out_dim).to(self.device_ids[0])
self.mu_q.apply(self.weights_init(init_type='orthogonal'))
self.sigma_q.apply(self.weights_init(init_type='orthogonal'))
if not self.config['condnSonU']:
self.config['u_dim'] = self.config['nm_operators']
if self.config['dynamics'] in ['Hamiltonian', 'SkewHamiltonian', 'RNN', 'Linear']:
self.lds = DynamicalModel(self.config['v_dim'],
self.config['u_dim'],
self.config['dynamics'],
self.config['condnSonU'],
self.config['projection']).to(self.device_ids[2])
else:
self.latent_layer = nn.Sequential(
nn.Linear(self.config['h_dim'], self.config['z_dim']),
nn.BatchNorm1d(self.config['z_dim']),
nn.LeakyReLU(0.2),
).to(self.device_ids[0])
if self.config['useZ']:
self.mu_z = nn.Linear(self.config['z_dim'], self.config['z_dim']).to(self.device_ids[0])
self.sigma_z = nn.Linear(self.config['z_dim'], self.config['z_dim']).to(self.device_ids[0])
self.mu_z.apply(self.weights_init(init_type='orthogonal'))
self.sigma_z.apply(self.weights_init(init_type='orthogonal'))
self.encoder.apply(self.weights_init(init_type='orthogonal'))
self.decoder.apply(self.weights_init(init_type='orthogonal'))
def weights_init(self, init_type='kaiming'):
def init_fun(m):
classname = m.__class__.__name__
if (classname.find('Conv') == 0 or classname.find('Linear') == 0) and hasattr(m, 'weight'):
if init_type == 'gaussian':
init.normal_(m.weight.data, 0.0, 0.02)
elif init_type == 'xavier':
init.xavier_normal_(m.weight.data, gain=np.sqrt(2))
elif init_type == 'kaiming':
init.kaiming_normal_(m.weight.data, a=0, mode='fan_in')
elif init_type == 'orthogonal':
init.orthogonal_(m.weight.data, gain=np.sqrt(2))
elif init_type == 'default':
pass
else:
assert 0, f"Initialization not implemented {init_type}"
if hasattr(m, 'bias') and m.bias is not None:
init.constant_(m.bias.data, 0.0)
return init_fun
def reparameterisation(self, x, space='v'):
if space == 'q':
mu, logsig = self.mu_q(x), self.sigma_q(x)
elif space == 'z':
mu, logsig = self.mu_z(x), self.sigma_z(x)
elif space == 'p':
mu, logsig = self.mu_p(x), self.sigma_p(x)
else:
assert 0, f"Not implemented {space}"
noise = torch.randn(*mu.shape).to(mu.device)
sig = torch.exp(0.5*logsig)
z = mu + noise * sig
return z, mu, logsig
def pretrainEncode(self, x):
h_t = self.encoder(x)
batch, time, z_dim = h_t.shape
h_t = rearrange(h_t, 'b t d -> (b t) d')
h_t = self.latent_layer(h_t)
z, mu_t, logsigma_t = self.reparameterisation(h_t, space='z')
z = rearrange(z, '(b t) d -> b t d', b=batch, t=time)
mu_t = rearrange(mu_t, '(b t) d -> b t d', b=batch, t=time)
logsigma_t = rearrange(logsigma_t, '(b t) d -> b t d', b=batch, t=time)
return z, mu_t, logsigma_t
def splitSubspace(self, v, mu_v, logsigma_v, batch, time):
v = rearrange(v, '(b t) v -> b t v', b=batch, t=time)
mu_v = rearrange(mu_v, '(b t) v -> b t v', b=batch, t=time)
logsigma_v = rearrange(logsigma_v, '(b t) v -> b t v', b=batch, t=time)
v = torch.stack(v.chunk(self.config['u_dim'], dim=2)).transpose(1,0).transpose(2,1)
mu_v = torch.stack(mu_v.chunk(self.config['u_dim'], dim=2)).transpose(1,0).transpose(2,1)
logsigma_v = torch.stack(logsigma_v.chunk(self.config['u_dim'], dim=2)).transpose(1,0).transpose(2,1)
v = v/(v.norm(dim=3).unsqueeze(3) + 1e-8)
return v, mu_v, logsigma_v
def encode(self, x, u_t=None):
h_t = self.encoder(x)
batch, time, h_dim = h_t.shape
if self.config['useZ']:
z, v_t = self.contentMotion(h_t, u_t)
z, mu_z, logsigma_z = self.reparameterisation(z, space='z')
else:
_, v_t = self.contentMotion(h_t, u_t)
z, mu_z, logsigma_z = None, None, None
if self.config['dynamics'] == 'Fourier':
q_t = rearrange(v_t, 'b t d -> (b t) d')
q_t, mu_qt, logsigma_qt = self.reparameterisation(q_t, space='q')
q_t = rearrange(q_t, '(b t) d -> b t d', b=batch, t=time)
mu_qt = rearrange(mu_qt, '(b t) d -> b t d', b=batch, t=time)
logsigma_qt = rearrange(logsigma_qt, '(b t) d -> b t d', b=batch, t=time)
p_t, mu_pt, logsigma_pt = None, None, None
elif self.config['dynamics'] in ['Linear', 'RNN']:
q_t = rearrange(v_t, 'b t d -> (b t) d')
q_t, mu_qt, logsigma_qt = self.reparameterisation(q_t, space='q')
if self.config['condnSonU']:
q_t, mu_qt, logsigma_qt = self.splitSubspace(q_t, mu_qt, logsigma_qt, batch, time)
else:
q_t = rearrange(q_t, '(b t) d -> b t 1 d', b=batch, t=time)
mu_qt = rearrange(mu_qt, '(b t) d -> b t 1 d', b=batch, t=time)
logsigma_qt = rearrange(logsigma_qt, '(b t) d -> b t 1 d', b=batch, t=time)
p_t, mu_pt, logsigma_pt = None, None, None
# p_t = rearrange(self.W_p(rearrange(v_t, 'b t d -> b d t')), 'b d t -> b t d')
# p_t = rearrange(p_t, 'b t d -> (b t) d')
# p_t, mu_pt, logsigma_pt = self.reparameterisation(p_t, space='p')
# p_t, mu_pt, logsigma_pt = self.splitSubspace(p_t, mu_pt, logsigma_pt, batch, time)
# q_t, mu_qt, logsigma_qt = None, None, None
else:
p_t = rearrange(self.W_p(rearrange(v_t, 'b t d -> b d t')), 'b d t -> b t d')
p_t = rearrange(p_t, 'b t d -> (b t) d')
p_t, mu_pt, logsigma_pt = self.reparameterisation(p_t, space='p')
q_t = rearrange(v_t, 'b t d -> (b t) d')
q_t, mu_qt, logsigma_qt = self.reparameterisation(q_t, space='q')
q_t, mu_qt, logsigma_qt = self.splitSubspace(q_t, mu_qt, logsigma_qt, batch, time)
p_t, mu_pt, logsigma_pt = self.splitSubspace(p_t, mu_pt, logsigma_pt, batch, time)
if self.config['condnSonU'] and self.config['projection']:
projection = torch.zeros(*q_t.shape).to(x.device)
actions = u_t.argmax(axis=1)
projection[range(len(actions)),:,actions,:] = 1.
if self.config['dynamics'] in ['RNN', 'Linear']:
q_t = projection * q_t
mu_qt = projection * mu_qt
logsigma_qt = projection * logsigma_qt
elif self.config['dynamics'] in ['Hamiltonian', 'SkewHamiltonian']:
q_t = projection * q_t
mu_qt = projection * mu_qt
logsigma_qt = projection * logsigma_qt
p_t = projection * p_t
mu_pt = projection * mu_pt
logsigma_pt = projection * logsigma_pt
else:
pass
return z, mu_z, logsigma_z, q_t, mu_qt, logsigma_qt, p_t, mu_pt, logsigma_pt
def dynamics(self, v_t, u_t, timesteps, test=False):
if self.config['dynamics'] == 'Fourier':
startidx = 2
T_i = torch.linspace(0, 1, timesteps-startidx).to(v_t.device)
T_i = v_t[:,startidx,0].reshape(-1,1) + T_i
T_i = torch.cat([v_t[:,:startidx,0], T_i], 1)
c_d = 2**(torch.linspace(0, int(self.config['v_dim']/2)-1, int(self.config['v_dim']/2)))
coords = torch.einsum('b t, d -> b t d', T_i, c_d.to(v_t.device))
v_t_seq = torch.cat([torch.sin(coords), torch.cos(coords)], 2)
v_t_seq = repeat(v_t_seq, 'b t v -> b t n v', n=1)
# Generate trajectory using Hamiltonian Operators
elif self.config['dynamics'] in ['RNN', 'Linear']:
startidx = 0
v_t_seq = self.lds(u_t.to(self.device_ids[2]), v_t.to(self.device_ids[2]), timesteps, startidx, test)
elif self.config['qv_x'] == 'qv_f_x1toT':
startidx = torch.randint(0, v_t.shape[1]-1, (1,))
v_f = v_t[:,startidx,:,:].squeeze(1)
v_t_seq = self.lds(u_t.to(self.device_ids[2]), v_f.to(self.device_ids[2]), timesteps, startidx)
elif self.config['qv_x'] == 'qv_x0':
startidx = 0
v_f = v_t[:,0,:,:]
v_t_seq = self.lds(u_t.to(self.device_ids[2]), v_f.to(self.device_ids[2]), timesteps, startidx)
else:
assert 0,f"Not impelemented {self.config['qv_x']}"
v_t_seq = v_t_seq.to(self.device_ids[0])
return v_t_seq, startidx
def decode(self, z):
return self.decoder(z.to(self.device_ids[1])).to(self.device_ids[0])
def vae_loss(self, mu, logsigma):
kld = -0.5 * torch.sum(1 +logsigma - mu.pow(2) - logsigma.exp())
return kld
def recon_loss(self, x, x_recon):
if self.config['losstype'] == 'mse':
recon_loss = F.mse_loss(x, x_recon, reduction='sum')
elif self.config['losstype'] == 'bce':
recon_loss = F.binary_cross_entropy(x_recon, x, reduction='sum')
else:
assert 0, f"{self.config['losstype']} not implemented"
return recon_loss
class VAE(nn.Module):
def __init__(self, config, data, device_ids=[2,3]):
super(VAE, self).__init__()
self.config = config
self.device_ids = device_ids
if data in ['kth','sprites', 'MUG']:
if self.config['network'] == 'conv':
self.encoder = EncoderConv(self.config['channels'],
self.config['h_dim'],
self.config['ht_dim'],
False,
sequential=False).to(self.device_ids[0])
self.decoder = DecoderConv(self.config['ht_dim'],
self.config['z_dim'],
self.config['channels'],
self.config['h_dim']).to(self.device_ids[1])
elif self.config['network'] == 'conv1':
self.encoder = EncoderConv1(self.config['channels'],
self.config['h_dim'],
self.config['ht_dim'],
False,
sequential=False).to(self.device_ids[0])
self.decoder = DecoderConv1(self.config['ht_dim'],
self.config['z_dim'],
self.config['channels'],
self.config['h_dim']).to(self.device_ids[1])
elif self.config['network'] == 'conv2':
self.encoder = EncoderConv2(self.config['channels'],
self.config['h_dim'],
self.config['ht_dim'],
False,
sequential=False).to(self.device_ids[0])
self.decoder = DecoderConv2(self.config['ht_dim'],
self.config['z_dim'],
self.config['channels'],
self.config['h_dim']).to(self.device_ids[1])
elif self.config['network'] == 'resnetladder':
self.encoder = EncoderResNet(self.config['width'],
self.config['height'],
self.config['channels'],
self.config['h_dim'],
self.config['ht_dim'],
False,
sequential=False).to(self.device_ids[0])
self.decoder = DecoderResNet(self.config['width'],
self.config['height'],
self.config['channels'],
self.config['ht_dim'],
self.config['z_dim'],
self.config['h_dim']).to(self.device_ids[1])
else:
assert 0, f"Network Not implemented {self.config['network']}"
else:
assert 0, f"Not implemented {self.config['data']}"
self.mu_t = nn.Linear(self.config['h_dim'], self.config['h_dim'] + self.config['z_dim']).to(self.device_ids[0])
self.mu_t.apply(self.weights_init(init_type='kaiming'))
self.sigma_t = nn.Linear(self.config['h_dim'], self.config['h_dim'] + self.config['z_dim']).to(self.device_ids[0])
self.sigma_t.apply(self.weights_init(init_type='kaiming'))
self.encoder.apply(self.weights_init(init_type='kaiming'))
self.decoder.apply(self.weights_init(init_type='kaiming'))
def weights_init(self, init_type='kaiming'):
def init_fun(m):
classname = m.__class__.__name__
if (classname.find('Conv') == 0 or classname.find('Linear') == 0) and hasattr(m, 'weight'):
if init_type == 'gaussian':
init.normal_(m.weight.data, 0.0, 0.02)
elif init_type == 'xavier':
init.xavier_normal_(m.weight.data, gain=np.sqrt(2))
elif init_type == 'kaiming':
init.kaiming_normal_(m.weight.data, a=0, mode='fan_in')
elif init_type == 'orthogonal':
init.orthogonal_(m.weight.data, gain=np.sqrt(2))
elif init_type == 'default':
pass
else:
assert 0, f"Initialization not implemented {init_type}"
if hasattr(m, 'bias') and m.bias is not None:
init.constant_(m.bias.data, 0.0)
return init_fun
def forward(self, x):
z_t, mu_t, logsigma_t = self.encode(x)
x_recon = self.decode(z_t)
return x_recon, z_t, mu_t, logsigma_t
def reparameterization(self, x):
mu = self.mu_t(x)
logsig = self.sigma_t(x)
noise = torch.randn(*mu.shape).to(mu.device)
sig = torch.exp(0.5*logsig)
z = mu + noise * sig
return z, mu, logsig
def encode(self, x):
h_t = self.encoder(x)
batch, time, hdim = h_t.shape
h_t = h_t.view(-1, hdim)
z_t, mu_t, logsigma_t = self.reparameterization(h_t)
zdim = z_t.shape[1]
z_t = z_t.view(batch, time, zdim)
mu_t = mu_t.view(batch, time, zdim)
logsigma_t = logsigma_t.view(batch, time, zdim)
return z_t, mu_t, logsigma_t
def decode(self, z):
return self.decoder(z.to(self.device_ids[1])).to(self.device_ids[0])
def vae_loss(self, mu, logsigma):
#kld = 0.0
kld = torch.mean(-0.5 * torch.sum(1 + logsigma - mu.pow(2) - logsigma.exp(), dim=1), dim=0)
return kld
def vae_loss_T(self, mu, logsigma):
kld = 0.0
for i in range(mu.shape[1]):
kld += -0.5 * torch.sum(1 + logsigma[:,i,:] - mu[:,i,:].pow(2) - logsigma[:,i,:].exp())
return kld
def recon_loss(self, x, x_recon):
if self.config['losstype'] == 'mse':
recon_loss = F.mse_loss(x, x_recon, reduction='sum')
elif self.config['losstype'] == 'bce':
recon_loss = F.binary_cross_entropy(x_recon, x, reduction='sum')
else:
assert 0, f"{self.config['losstype']} not implemented"
return recon_loss