-
Notifications
You must be signed in to change notification settings - Fork 0
/
regression_run_manyrun_singlemodel.py
388 lines (293 loc) · 15.5 KB
/
regression_run_manyrun_singlemodel.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import numpy as np
import torch
from torch.optim import lr_scheduler
from swag.posteriors import swag as swag
import time
from load_andi_dataset import *
from LSTM_Neural_Network import *
from swag_lr_scheduler import *
modelint = int(input("Which model (0-4) of [attm,ctrw,fbm,lw,sbm]?"))
modelstring = ["attm","ctrw","fbm","lw","sbm"]
while modelint not in [0,1,2,3,4]:
print("Please type an integer for model choice!")
modelint = int(input("Which model (0-4) of [attm,ctrw,fbm,lw,sbm]?"))
"""
modelint = [2,4]
modelnames = ["attm","ctrw","fbm","lw","sbm"]
for i in range(len(modelint)):
print("Using model "+ modelnames[modelint[i]])
"""
Ts = input("Which length? Leave blank for all of (10,100,500)")
#Ts = [10,25,50,100,250,500,999]
if Ts != "":
Ts = [int(Ts)]
else:
Ts = [10,100,500]
for T in Ts:
print(f"Training models for T={T}...")
print("Loading data...")
#setup data using super dataset
#T = 100
noise_T = T
N_train = int(3e5)
N_test = 10000
dim = 1
use_increments = True
N_save = [16000,16000,10000,16000,10000]
#loading from saved trajectories, allows for only one dataset of trajectories usable for all trajectory lenghts
train_path = "datasets/trajectories/"
train_dataset = SingleModel_dataset_from_saved_trajs(path = train_path, task = 1, dim = dim, N_total = N_train,
T = T, N_save = N_save[modelint],
use_increments = use_increments,model=modelint)
test_path = "datasets/trajectories/testset/"
test_dataset = SingleModel_dataset_from_saved_trajs(path = test_path, task = 1, dim = dim, N_total = N_test,
T = T, N_save = 500, use_increments = use_increments,model=modelint)
print(len(train_dataset),len(test_dataset))
print("Finished loading data")
for manyrunning in range(25):
#randomly choose hyperparameters i want to observe
swag_lr = 2e-4#5e-4#np.random.choice([1e-3,1e-4,5e-4])#np.random.choice([1e-3,1e-4,5e-4])#4e-4#np.random.choice([0.03,0.01,0.005])
#lr_init = np.random.choice([1e-3,5e-4])
cyclic = True
# Device configuration, run on gpu if available
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# Hyper-parameters
input_dim = 1 # 1D input sequence
LSTM_size = [128,128,64]#64
output_dim = 2 #output size is 2, one for anomalous exponent, 1 for its log variance
num_epochs = 80
batch_size = 512
#optimizer hyper-paras
lr_init = 2e-3#2e-3 #initial learning rate
momentum = 0.9 #contribution of earlier gradient to next gradient
weight_decay = 1e-4 #contribution L2 norm of weights to loss
cyclic_multiplier = 4 #multiplier for the time it takes for one half of a cyclic period: e.g. 2 episodes
#parameter choices for swag
swag_start = 65 #when to start swag epoches (needs to be smaller than num epochs)
swag_update_freq = 5 #number of times swag estimate is updated per epoch(x updates per epoch)
#swag_lr = 0.005 #swag learning rate
# In[4]:
# Data loader
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
batch_size=batch_size,
shuffle=True, pin_memory=False, num_workers = 3)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
batch_size=batch_size,
shuffle=False, pin_memory=False, num_workers = 3)
# In[6]:
############## TENSORBOARD ########################
#to view the tensorboard you need to run "tensorboard --logdir=runs" in console
#while in the directory of this file and open the link!
import sys
import torch.nn.functional as F
from torch.utils.tensorboard import SummaryWriter
import os
#specify path for tensorboard
i = 0
while os.path.exists(f"runs/aleatoric/{T}lenght/final_andiregress_"+modelstring[modelint]+f"_{swag_start}-{num_epochs}_{lr_init:.4f}-{swag_lr:.5f}_mom{momentum:.2f}_batch{batch_size}swag%s" % i):
i += 1
tensorboardpath = f"runs/aleatoric/{T}lenght/final_andiregress_"+modelstring[modelint]+f"_{swag_start}-{num_epochs}_{lr_init:.4f}-{swag_lr:.5f}_mom{momentum:.2f}_batch{batch_size}swag%s" % i
#overwritten path (for custom name)
#tensorboardpath = "runs/{N_test}lenght/aleatoric/andiregress_swagrun_25-35_lr0.15-0.02"
# default `log_dir` is "runs" - we'll be more specific here
writer = SummaryWriter(tensorboardpath)
###################################################
# In[8]:
#define model
model = LSTM_Regression_aleatoric(input_dim, output_dim=output_dim, LSTM_size=LSTM_size).to(device)
#define swag model
swag_model = swag.SWAG(LSTM_Regression_aleatoric, subspace_type = 'covariance',
subspace_kwargs={'max_rank': 20}, num_input_features = input_dim,
output_dim = output_dim, LSTM_size=LSTM_size)
swag_model.to(device)
#loss and optimizer
criterion = torch.nn.GaussianNLLLoss()#my_gnll_loss #gaussian negative log likelihood loss
#SGD with momentum and weight decay; or adam
#optimizer = torch.optim.SGD(model.parameters(), lr=lr_init, momentum=momentum, weight_decay=weight_decay)
optimizer = torch.optim.Adam(model.parameters(), lr = lr_init,weight_decay=weight_decay)
#learing rate scheduler, using custom swag lr scheduler, which decays the lr to the swag_lr
learnrate_scheduler = swag_lr_scheduler(optimizer, lr_init, num_epochs,
swag = True, swag_start = swag_start, swag_lr = swag_lr, anneal_start = 0.01)
#learnrate_scheduler = lr_scheduler.StepLR(optimizer, step_size = 5, gamma = 0.25)
cyclic_learnrate_scheduler = torch.optim.lr_scheduler.CyclicLR(optimizer, base_lr=swag_lr,
max_lr=lr_init, step_size_up = cyclic_multiplier*len(train_loader),
cycle_momentum=False, mode = "triangular2")
#cyclic = True
swaganneal_learnrate_scheduler = torch.optim.swa_utils.SWALR(optimizer,
anneal_strategy="linear",anneal_epochs=2,swa_lr=swag_lr)
#optional load model:
"""
something like this:
if args.resume is not None:
print('Resume training from %s' % args.resume)
checkpoint = torch.load(args.resume)
start_epoch = checkpoint['epoch']
model.load_state_dict(checkpoint['state_dict'])
optimizer.load_state_dict(checkpoint['optimizer'])"""
#possible load swag to add here
"""
something like this:
if args.swag and args.swag_resume is not None:
checkpoint = torch.load(args.swag_resume)
swag_model.subspace.rank = torch.tensor(0)
swag_model.load_state_dict(checkpoint['state_dict'])
"""
# In[ ]:
print(f"starting training on model {manyrunning}")
# Train the model
running_loss = 0.0
n_total_steps = len(train_loader)
for epoch in range(num_epochs):
starttime = time.process_time()
running_loss = 0.0
#print(f"Current learning rate: {optimizer.param_groups[0]['lr']}")
for i, (traj,target) in enumerate(train_loader):
traj = traj.to(device, non_blocking=True)
target = target.to(device, non_blocking=True)
# Forward pass
output = model(traj)
loss = criterion(output[:,0].view(-1,1), target, output[:,1].view(-1,1))
#print(output)
# Backward and optimize
optimizer.zero_grad()
#print(loss)
loss.backward()
optimizer.step()
#calc accumulated loss
running_loss += loss.item()
if epoch+1 >= swag_start and (i+1)%int(n_total_steps/swag_update_freq) == 0:
#print("updating SWAG estimate")
swag_model.collect_model(model)
if (i+1) % int(n_total_steps/5) == 0:
#print (f'Epoch [{epoch+1}/{num_epochs}], Step [{i+1}/{n_total_steps}], Loss: {running_loss/200:.4f}')
#print(output)
############## TENSORBOARD ########################
#adding scalars for plots of loss function or similar
#add_scalar('name', value (y-axis), iteration number (x-axis))
#tensorboard smoothes the values, the opaque line is the unsmoothed one!
writer.add_scalar('training loss', running_loss / int(n_total_steps/5), epoch * n_total_steps + i)
running_loss = 0.0
###################################################
if epoch+1 < swag_start and cyclic == True:
cyclic_learnrate_scheduler.step()
if epoch+1 < swag_start:
if cyclic == False:
learnrate_scheduler.step()
else:
swaganneal_learnrate_scheduler.step()
#print("Time needed for epoch "+str(time.process_time()-starttime))
#short test after each epoch
with torch.no_grad():
n_test_steps = len(test_loader)
n_abserr = 0
n_samples = 0
acc_loss = 0
for traj, targets in test_loader:
traj = traj.to(device)
targets = targets.to(device)
if epoch+1 >= swag_start:
number_mc_samples = 20
output_samples = torch.ones(number_mc_samples, len(traj), 1, dtype=torch.float32).to(device)
variance_samples = torch.ones(number_mc_samples, len(traj), 1, dtype=torch.float32).to(device)
for i in range(number_mc_samples):
swag_model.sample()
model_output = swag_model(traj)
output_samples[i] = model_output[:,0].view(-1,1)
variance_samples[i] = model_output[:,1].view(-1,1)
output_exp = output_samples.mean(0)
outputted_var = variance_samples.mean(0)
combined_var = output_samples.var(0) + outputted_var
outputs = torch.cat((output_exp,outputted_var),1)
"""number_mc_samples = 20
output_samples = torch.ones(number_mc_samples, len(traj), 2, dtype=torch.float32).to(device)
for j in range(number_mc_samples):
swag_model.sample()
output_samples[j] = swag_model(traj)
outputs = output_samples.mean(0)"""
#swag_model.set_swa()
#outputs = swag_model(traj)
else:
outputs = model(traj)
acc_loss += criterion(outputs[:,0].view(-1,1), targets, outputs[:,1].view(-1,1)).item()
n_samples += targets.size(0)
n_abserr += (outputs[:,0].view(-1,1)-targets).abs().sum().item()
MAE = n_abserr / n_samples
mean_loss = acc_loss/n_test_steps
#print(f'MAE of the network on the 10000 test trajectories: {MAE}')
############## TENSORBOARD ########################
#adding scalars for plots of loss function or similar
#add_scalar('name', value (y-axis), iteration number (x-axis))
#tensorboard smoothes the values, the opaque line is the unsmoothed one!
writer.add_scalar('testing MAE', MAE, epoch * n_total_steps)
writer.add_scalar('testing loss', mean_loss, epoch * n_total_steps)
###################################################
#todo:
#calc MAE - done looking good
#why adam so much better? -higher LR helped!
# In[ ]:
# In[ ]:
#test swag model
number_mc_samples = 20
MSELoss = torch.nn.MSELoss()
with torch.no_grad():
n_test_steps = len(test_loader)
n_abserr = 0
n_samples = 0
acc_loss = 0
acc_pred_var = 0
acc_mse = 0
for traj, targets in test_loader:
traj = traj.to(device)
targets = targets.to(device)
output_samples = torch.ones(number_mc_samples, len(traj), 1, dtype=torch.float32).to(device)
variance_samples = torch.ones(number_mc_samples, len(traj), 1, dtype=torch.float32).to(device)
for i in range(number_mc_samples):
swag_model.sample()
model_output = swag_model(traj)
output_samples[i] = model_output[:,0].view(-1,1)
variance_samples[i] = model_output[:,1].view(-1,1)
outputs = output_samples.mean(0)
outputted_var = variance_samples.mean(0)
combined_var = output_samples.var(0) + outputted_var
acc_pred_var += combined_var.sum().item()
"""if epoch+1 >= swag_start:
swag_model.set_swa()
outputs = swag_model(traj)
else:
outputs = model(traj)"""
acc_loss += criterion(outputs, targets, outputted_var).item()
acc_mse += MSELoss(outputs,targets)
n_samples += targets.size(0)
n_abserr += (outputs-targets).abs().sum().item()
MAE = n_abserr / n_samples
mean_loss = acc_loss/n_test_steps
mean_pred_var = acc_pred_var/n_samples
mean_mse = acc_mse/n_test_steps
print(f'MAE of the network on the 10000 test trajectories: {MAE}')
print(f'Mean loss is: {mean_loss}')
print(f'Mean Variance predicted by SWAG is: {mean_pred_var}')
print(f'Mean Squared Error is: {mean_mse}')
# In[ ]:
#saving
#get time and date
import time#, os, fnmatch, shutil
t = time.localtime()
timestamp = time.strftime('%b-%d-%Y_%H%M', t)
#save standardmodel
name = "modelcheckpoint_" + timestamp
directory = f"saves/aleatoric/"+modelstring[modelint]+f"/{T}_lenght/" #f"saves/aleatoric/"+ modelnames[modelint] + f"/{T}_lenght/"
path = directory + name
import os
try:
os.mkdir(directory)
except:
print("directory exists")
torch.save(model.state_dict(),path)
#save swag model
name = "swag_modelcheckpoint_" + timestamp
directory = f"saves/aleatoric/"+modelstring[modelint]+f"/{T}_lenght/" #f"saves/aleatoric/"+ modelnames[modelint] + f"/{T}_lenght/"
path = directory + name
torch.save(swag_model.state_dict(),path)