-
Notifications
You must be signed in to change notification settings - Fork 2
/
setup.py
398 lines (295 loc) · 13.3 KB
/
setup.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torchvision
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
import numpy as np
import math
import yaml
import pickle
import pprint
import os
import logging
import sys
import data_loaders
import nets
import losses
import utils
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
torch.manual_seed(787)
torch.cuda.manual_seed(787)
def get_data(name : str, **params):
'''
Helper method to get the dataloaders given parameters for the dataset
The parameters come from the yaml file for the experiment
name : string that gives you the experiment type
'''
global savepath
path = os.path.join(savepath, 'data.pkl')
print("PATH")
print(path)
if name == 'bair':
train_dataset = data_loaders.RobotPushDataset(train=True)
test_dataset = data_loaders.RobotPushDataset(train=False)
elif name == 'balls':
'''
synthetic is the yellow dot experiment
'''
dataset = data_loaders.BallDataset(path=path, **params)
elif name == 'Mball':
'''
synthetic is the yellow dot experiment
'''
dataset = data_loaders.MBallDataset(path=path, **params)
elif name == 'smnist-d':
'''
smnist-d is the mnist digits dataset
'''
dataset = data_loaders.SMNISTDynamicDataset(path=path, **params)
elif name == 'wass':
'''
wass is the wasserstein dataset between two images in the COIL20 dataset
'''
dataset = data_loaders.COILDataset(load_path=path, **params)
elif name == 'ballwass':
'''
ballwass is the wasserstein dataset between two images in the COIL20 dataset
with two balls
'''
dataset = data_loaders.BallWassDataset(path=path, **params)
elif name == 'stocks':
'''
stocks is the S&P 500 dataset
'''
dataset = data_loaders.SP()
elif name == 'vector':
dataset = data_loaders.VectorDataset(**params)
elif 'dna' in name:
'''
dna is the microscopy dataset
'''
dataset = data_loaders.DNADataset(path=path, name=name, **params)
else:
raise NotImplementedError
# plot the scaled data
plt.plot(dataset.xt)
plt.savefig(os.path.join(savepath, 'original_seq_scaled.pdf'))
plt.close('all')
# plot the original data
if name != 'stocks':
orig_plots = [dataset.xt_orig[:,i] for i in range(dataset.xt_orig.shape[1])]
plot_titles = ['component {}'.format(i) for i in range(dataset.xt_orig.shape[1])]
utils.plot_subplots(orig_plots, plot_titles, os.path.join(savepath, 'original_seq.pdf'), plot_type='plot', axis=True)
# train val test split
train_inds = range(len(dataset) - params['n_test'] * 2)
val_inds = range(len(dataset) - params['n_test'] * 2, len(dataset) - params['n_test'])
test_inds = range(len(dataset) - params['n_test'], len(dataset))
train_dataset = torch.utils.data.Subset(dataset, train_inds)
val_dataset = torch.utils.data.Subset(dataset, val_inds)
test_dataset = torch.utils.data.Subset(dataset, test_inds)
train_dataloader = torch.utils.data.DataLoader(train_dataset, \
num_workers = 0, batch_size = params['batch_size'], drop_last=True)
val_dataloader = torch.utils.data.DataLoader(val_dataset, \
num_workers = 0, batch_size = params['batch_size'])
test_dataloader = torch.utils.data.DataLoader(test_dataset, \
num_workers = 0, batch_size = params['batch_size'])
return train_dataloader, val_dataloader, test_dataloader
def setup(cfg, sp):
global savepath
savepath = sp
optim_params = cfg['optimizer']
dataset_params = cfg['dataset']
ae_params = cfg['ae']
sde_params = cfg['sde']
dataset = dataset_params['name']
dt = ( dataset_params['tn'] - dataset_params['t0'] ) / dataset_params['n_points']
if dataset == 'balls':
fcn_sigma = '[1, 0, 0, 1]'
x_init = np.array([0, 0])
elif dataset == 'Mball':
fcn_sigma = list(np.eye(10).reshape(1,-1)[0])
x_init = np.random.randn(10)
#x_init = np.array([0,0,1,1,-1,-1,1,-1,-1,1])
elif dataset == 'smnist' or dataset == 'smnist-d':
fcn_sigma = list(np.eye(4).reshape(1,-1)[0])
x_init = np.array([1, 1, -1,- 1])
elif dataset == 'wass':
fcn_sigma = '[1]'
x_init = np.array([1])
elif dataset == 'ballwass':
fcn_sigma = np.array([1, 0, 0, 0, 1, 0, 0, 0, 1])
x_init = np.array([1,0,-1])
elif 'dna' in dataset:
fcn_sigma = '[1, 0, 0, 1]'
x_init = np.array([0, 0])
#fcn_sigma = np.array([1, 0, 0, 0, 1, 0, 0, 0, 1])
#x_init = np.array([1,0,-1])
elif 'vector' in dataset:
x_init = np.random.randn(3)
fcn_sigma = np.array([1, 0, 0, 0, 1, 0, 0, 0, 1])
else:
fcn_mu = None
fcn_sigma = None
dt = 0.5
try:
x_init = dataset_params['x_init']
fcn_sigma = dataset_params['fcn_sigma']
except KeyError:
pass
finally:
print(fcn_sigma)
fcn_params = {'x_init': x_init, 'fcn_sigma': fcn_sigma, 'dt': dt}
train_dataloader, val_dataloader, test_dataloader = get_data(**{**dataset_params, **fcn_params})
# Setup the networks
lr = optim_params['lr']
z_dim = ae_params['net']['latent_dim']
# Mu/sigma hyperparameters
net_type = sde_params['type']
width = sde_params['width']
depth = sde_params['depth']
act = sde_params['act']
# load from an existing path if necessary
ae_path = ae_params['path']
ae_net_params = ae_params['net']
ae = nets.StochConvAE(**ae_net_params).to(device)
lr_ae = ae_params['lr']
lr_mu = sde_params['lr_mu']
lr_sigma = sde_params['lr_sigma']
if ae_path:
try:
ae.load_state_dict(torch.load(os.path.join(savepath,'saved_nets/{}'.format(ae_path))))
except FileNotFoundError:
print('trained AE not found, using random initialization')
print('this is usually a bad error...')
total_params = sum(p.numel() for p in ae.parameters())
print('Total number of parameters in autoencoder: ' + str(total_params))
# load the latent space functions
if net_type == 'mlp':
tri_inds = torch.tril_indices(z_dim,z_dim)
upper_tri = torch.eye(z_dim)[tri_inds[0,:], tri_inds[1,:]]
mu = nets.MLP(z_dim + 1,width,depth,z_dim,activation=act).to(device)
sigma = nets.MLP(z_dim + 1,width,depth,int((z_dim+1)*z_dim/2),activation=act).to(device)
total_params = sum(p.numel() for p in mu.parameters())
print('Total number of parameters in mu: ' + str(total_params))
total_params = sum(p.numel() for p in sigma.parameters())
print('Total number of parameters in sigma: ' + str(total_params))
nn.init.zeros_(mu.out.weight.data)
nn.init.ones_(mu.out.bias.data)
nn.init.zeros_(sigma.out.weight.data)
with torch.no_grad():
sigma.out.bias = nn.Parameter(upper_tri.to(device), requires_grad=True)
opt_params = [{'params': mu.parameters(), 'lr': lr_mu},
{'params': sigma.parameters(), 'lr': lr_sigma},
{'params': ae.parameters(), 'lr': lr_ae}]
elif net_type == 'const':
mu = nn.Parameter(torch.randn(z_dim).to(device),requires_grad=True)
if ae_net_params['sigma_type'] == 'diag':
sigma = nn.Parameter(torch.ones(z_dim).to(device),requires_grad=True)
else:
tri_inds = torch.tril_indices(z_dim,z_dim)
upper_tri = torch.eye(z_dim)[tri_inds[0,:], tri_inds[1,:]]
sigma = nn.Parameter(upper_tri.to(device),requires_grad=True)
opt_params = [{'params': [mu], 'lr': lr_mu},
{'params': [sigma], 'lr': lr_sigma},
{'params': ae.parameters(), 'lr': lr_ae}]
elif net_type == 'const-sig':
tri_inds = torch.tril_indices(z_dim,z_dim)
upper_tri = torch.eye(z_dim)[tri_inds[0,:], tri_inds[1,:]]
mu = nets.MLP(z_dim + 1, width, depth, z_dim, activation=act).to(device)
if sde_params['path']:
mu.load_state_dict(torch.load(os.path.join(savepath, 'saved_nets/{}'.format(sde_params['path']))))
if ae_net_params['sigma_type'] == 'diag':
sigma = nn.Parameter(torch.ones(z_dim).to(device),requires_grad=True)
else:
sigma = nn.Parameter(upper_tri.to(device),requires_grad=True)
opt_params = [{'params': mu.parameters(),'lr': lr_mu},
{'params': [sigma], 'lr': lr_sigma},
{'params': ae.parameters(), 'lr': lr_ae}]
elif net_type == 'const-sig-nt':
tri_inds = torch.tril_indices(z_dim,z_dim)
upper_tri = torch.eye(z_dim)[tri_inds[0,:], tri_inds[1,:]]
mu = nets.MLP(z_dim, width, depth, z_dim, activation=act).to(device)
#nn.init.ones_(mu.out.bias.data)
#nn.init.zeros_(mu.out.weight.data)
#nn.init.ones_(mu.out.bias.data)
if ae_net_params['sigma_type'] == 'diag':
sigma = nn.Parameter(torch.ones(z_dim).to(device),requires_grad=True)
else:
sigma = nn.Parameter(upper_tri.to(device),requires_grad=True)
if ae_net_params['loss'] == 'mc':
opt_params = [{'params': mu.parameters(),'lr': lr_mu, 'eps':1e-7},#, 'betas':(0.5, 0.99), 'eps': 1e-5}, #-7
{'params': [sigma], 'lr': lr_sigma},
{'params': ae.parameters(), 'lr': lr_ae, 'betas':(0.5, 0.99), 'eps': 1e-5}] # -5
elif ae_net_params['loss'] == 'exact':
#nn.init.ones_(mu.out.bias.data)
nn.init.zeros_(mu.out.weight.data)
nn.init.ones_(mu.out.bias.data)
opt_params = [{'params': mu.parameters(),'lr': lr_mu, 'eps':1e-3, 'weight_decay': 1e-5},#, 'betas':(0.5, 0.99), 'eps': 1e-5}, #-7
{'params': [sigma], 'lr': lr_sigma},
{'params': ae.parameters(), 'lr': lr_ae, 'betas':(0.5, 0.99), 'eps': 1e-5, 'weight_decay': 1e-5}] # -5
else:
opt_params = [{'params': mu.parameters(),'lr': lr_mu, 'eps':1e-3},#, 'betas':(0.5, 0.99), 'eps': 1e-5}, #-7
{'params': [sigma], 'lr': lr_sigma},
{'params': ae.parameters(), 'lr': lr_ae, 'betas':(0.5, 0.99), 'eps': 1e-5}] # -5
if sde_params['path']:
try:
mu.load_state_dict(torch.load(os.path.join(savepath, 'saved_nets/{}'.format(sde_params['path']))))
except FileNotFoundError:
print('mu not found, using random initialization')
try:
suffix = 'latest'
if sde_params['path']:
with open(os.path.join(savepath,'sigma_{}.pkl'.format(suffix)),'rb') as f:
sigma = pickle.load(f)
except:
if ae_net_params['sigma_type'] == 'diag':
sigma = nn.Parameter(torch.ones(z_dim).to(device),requires_grad=True)
else:
sigma = nn.Parameter(upper_tri.to(device),requires_grad=True)
elif net_type == 'linear':
tri_inds = torch.tril_indices(z_dim,z_dim)
upper_tri = torch.eye(z_dim)[tri_inds[0,:], tri_inds[1,:]]
mu = nets.Lin(z_dim).to(device)
if sde_params['path']:
mu.load_state_dict(torch.load(os.path.join(savepath, 'saved_nets/{}'.format(sde_params['path']))))
if ae_net_params['sigma_type'] == 'diag':
sigma = nn.Parameter(torch.ones(z_dim).to(device),requires_grad=True)
else:
sigma = nn.Parameter(upper_tri.to(device),requires_grad=True)
opt_params = [{'params': mu.parameters(),'lr': lr_mu},
{'params': [sigma], 'lr': lr_sigma},
{'params': ae.parameters(), 'lr': lr_ae}]
elif net_type == 'well':
tri_inds = torch.tril_indices(z_dim,z_dim)
upper_tri = torch.eye(z_dim)[tri_inds[0,:], tri_inds[1,:]]
mu = nets.Well(z_dim).to(device)
if sde_params['path']:
mu.load_state_dict(torch.load(os.path.join(savepath, 'saved_nets/{}'.format(sde_params['path']))))
if ae_net_params['sigma_type'] == 'diag':
sigma = nn.Parameter(torch.ones(z_dim).to(device),requires_grad=True)
else:
sigma = nn.Parameter(upper_tri.to(device),requires_grad=True)
opt_params = [{'params': mu.parameters(),'lr': lr_mu},
{'params': [sigma], 'lr': lr_sigma},
{'params': ae.parameters(), 'lr': lr_ae}] #'betas':(0.5, 0.99), 'eps': 1e-3}]
optimizer = getattr(optim, optim_params['name'])(opt_params)
if optim_params['sched']:
scheduler = getattr(optim.lr_scheduler, optim_params['sched'])(optimizer, **optim_params['sched_param'])
else:
scheduler = None
initialized = {'ae' : ae,
'mu' : mu,
'sigma' : sigma,
'dt' : dt,
'train_data' : train_dataloader,
'val_data' : val_dataloader,
'test_data' : test_dataloader,
'optimizer' : optimizer,
'scheduler' : scheduler,
'n_epochs' : optim_params['n_epochs'],
'data_params': dataset_params
}
return initialized