-
Notifications
You must be signed in to change notification settings - Fork 9
/
airfoil_regression3.py
451 lines (372 loc) · 17.8 KB
/
airfoil_regression3.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""@author: Riashat Islam
"""
import argparse
import os
import sys
import time
import numpy
np = numpy
#np.random.seed(1) # TODO
import math
import os
import random
import pandas as pd
from BHNs_MLP_Regression import MLPWeightNorm_BHN, MCdropout_MLP, Backprop_MLP
from get_regression_data import get_regression_dataset
from ops import load_mnist
from utils import log_normal, log_laplace
import lasagne
import theano
import theano.tensor as T
from lasagne.random import set_rng
from theano.tensor.shared_randomstreams import RandomStreams
import matplotlib.pyplot as plt
import scipy
lrdefault = 1e-3
n_mc = 20
def save_list(path, ll):
thefile = open(path, 'w')
for item in ll:
thefile.write("%s\n" % item)
def get_LL(y_hat, y, tau):
n_mc = len(y_hat)
return scipy.misc.logsumexp(-.5*tau*(y_hat-y)**2) - np.log(n_mc) - .5*np.log(2*np.pi) - .5*np.log(tau**-1)
def rmse(predictions, targets):
return np.sqrt(((predictions - targets) ** 2).mean())
def get_lbda(tau, length_scale, drop_prob=None):
# this is eqn (7) from https://arxiv.org/pdf/1506.02142.pdf (Gal)
lbda = length_scale**2 / tau # we don't divide by the 2 * N(=dataset size) as in Gal, since our prior handles this scaling
if drop_prob is not None:
lbda *= (1-drop_prob)
lbda = np.cast['float32'](lbda)
return lbda
def train_model(model, save_path, save_,
X,Y,Xv,Yv,
Xt, Yt, # TODO: default to None
lr0=0.1,lrdecay=1,bs=32,epochs=50,anneal=0,name='0',
e0=0,rec=0, tau=None):
#save_=True):
print 'trainset X.shape:{}, Y.shape:{}'.format(X.shape,Y.shape)
N = X.shape[0]
tr_RMSEs = list()
tr_LLs = list()
va_RMSEs = list()
va_LLs = list()
te_RMSEs = list()
te_LLs = list()
t = 0
for e in range(epochs):
if e <= e0:
continue
if lrdecay:
lr = lr0 * 10**(-e/float(epochs-1))
else:
lr = lr0
if anneal:
w = min(1.0,0.001+e/(epochs/2.))
else:
w = 1.0
for i in range(N/bs):
x = X[i*bs:(i+1)*bs]
y = Y[i*bs:(i+1)*bs]
loss = model.train_func(x,y,N,lr,w)
#print ("Loss", loss)
if 0:#t%100==0:
#print 'epoch: {} {}, loss:{}'.format(e,t,loss)
tr_rmse = rmse(model.predict(X), Y)
va_rmse = rmse(model.predict(Xv), Yv)
# print '\ttrain rmse: {}'.format(tr_rmse)
# print '\tvalid rmse: {}'.format(va_rmse)
t+=1
tr_rmse, tr_LL = evaluate_model(model.predict, X, Y, n_mc=n_mc, tau=tau)
va_rmse, va_LL = evaluate_model(model.predict,Xv,Yv, n_mc=n_mc, tau=tau)
te_rmse, te_LL = evaluate_model(model.predict,Xt,Yt, n_mc=n_mc, tau=tau)
if e % 5 == 0:
if 0: #verbose
print '\n'
print 'tr LL at epochs {}: {}'.format(e,tr_LL)
print 'tr rmse at epochs {}: {}'.format(e,tr_rmse)
print 'va LL at epochs {}: {}'.format(e,va_LL)
#print 'va rmse at epochs {}: {}'.format(e,va_rmse)
tr_RMSEs.append(tr_rmse)
tr_LLs.append(tr_LL)
va_RMSEs.append(va_rmse)
va_LLs.append(va_LL)
te_RMSEs.append(te_rmse)
te_LLs.append(te_LL)
if va_LL > rec and save_:
print '.... save best model .... '
model.save(save_path,[e])
rec = va_LL
#print '\n\n'
return tr_LLs, tr_RMSEs, va_LLs, va_RMSEs, te_LLs, te_RMSEs
def evaluate_model(predict,X,Y,n_mc=100,max_n=100, tau=None):
MCt = np.zeros((n_mc,X.shape[0],1))
N = X.shape[0]
num_batches = np.ceil(N / float(max_n)).astype(int)
for i in range(n_mc):
for j in range(num_batches):
x = X[j*max_n:(j+1)*max_n]
MCt[i,j*max_n:(j+1)*max_n] = predict(x)
Y_pred = MCt.mean(0)
Y_true = Y
RMSE = rmse(Y_pred, Y_true)
LL = get_LL(Y_pred, Y_true, tau)
return RMSE, LL
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--perdatapoint',default=0,type=int)
parser.add_argument('--lrdecay',default=0.0,type=int)
parser.add_argument('--lr0',default=0.001,type=float) # .1
parser.add_argument('--coupling',default=4,type=int)
parser.add_argument('--lbda',default=None,type=float)
parser.add_argument('--size',default=2000,type=int)
parser.add_argument('--bs',default=32,type=int)
parser.add_argument('--epochs',default=1000,type=int)
parser.add_argument('--prior',default='log_normal',type=str)
parser.add_argument('--model',default='BHN',type=str, choices=['BHN', 'MCDropout', 'Backprop'])
parser.add_argument('--anneal',default=0,type=int)
parser.add_argument('--n_hiddens',default=1,type=int) # 1
parser.add_argument('--n_trials',default=10,type=int)
parser.add_argument('--n_units',default=50,type=int) # 50
parser.add_argument('--totrain',default=1,type=int)
parser.add_argument('--seed',default=None,type=int)
parser.add_argument('--override',default=1,type=int)
parser.add_argument('--reinit',default=1,type=int)
parser.add_argument('--dataset',default='airfoil',type=str, choices=['airfoil', 'boston', 'concrete', 'energy', 'kin8nm', 'naval', 'power', 'protein', 'wine', 'yacht', 'year'])
parser.add_argument('--flow',default='IAF',type=str, choices=['RealNVP', 'IAF'])
parser.add_argument('--save_dir',default=None, type=str)
parser.add_argument('--cross_validate',default=0, type=int)
parser.add_argument('--drop_prob',default=0.005, type=float)
parser.add_argument('--length_scale',default=1e-3, type=float)
parser.add_argument('--tau',default=1e2, type=float)
parser.add_argument('--grid_search',default=0, type=int)
#parser.add_argument('--save_results',default='./results/',type=str)
args = parser.parse_args()
print args
args_dict = args.__dict__
flags = [flag.lstrip('--') for flag in sys.argv[1:] if not flag.startswith('--save_dir')]
exp_description = '_'.join(flags)
if args_dict['save_dir'] is None:
save_ = False
print "\n\n\n\t\t\t\t WARNING: save_dir is None! Results will not be saved! \n\n\n"
else:
save_ = True
# save_dir = filename + PROVIDED parser arguments
save_dir = os.path.join(args_dict.pop('save_dir'), os.path.basename(__file__) + '___' + '_'.join(flags))
print("\t\t save_dir=", save_dir)
# make directory for results
if not os.path.exists(save_dir):
os.makedirs(save_dir)
# save ALL parser arguments
with open (os.path.join(save_dir,'exp_settings.txt'), 'w') as f:
for key in sorted(args_dict):
f.write(key+'\t'+str(args_dict[key])+'\n')
locals().update(args_dict)
assert lbda is None
assert size == 2000
if seed is None:
seed = np.random.randint(2**32 - 1)
set_rng(np.random.RandomState(seed))
np.random.seed(seed+1000)
input_dim, train_x, train_y, valid_x, valid_y, test_x , test_y = get_regression_dataset(dataset, data_path=os.environ['HOME'] + '/BayesianHypernetCW/')
datasets = [get_regression_dataset(dataset, data_path=os.environ['HOME'] + '/BayesianHypernetCW/') for _ in range(n_trials)]
# SET HPARAMS FOR SEARCH (override grid search if provided as flag)
if grid_search:
# TODO: better grid...
length_scales = 1000.**np.arange(-3,1)#length_scales = [.1, .01, .001] # length scale should be smaller!
taus = 3.**np.arange(4,7)# tau should be larger (still)!
lr0s = [.01, .003, .001]#lr0s = [.01, .001, .0001]
drop_probs = [.01]#[.1, .05, .02, .01, .005, .002, .001]
for trial, dataset in enumerate(datasets):
input_dim, train_x, train_y, valid_x, valid_y, test_x , test_y = dataset
for length_scale in length_scales:
for tau in taus:
for lr0 in lr0s:
# DROPOUT
if model == 'MCDropout':
for drop_prob in drop_probs:
t0 = time.time()
lbda = get_lbda(tau, length_scale, drop_prob)
print drop_prob, lbda
network = MCdropout_MLP(n_hiddens=n_hiddens,
n_units=n_units,
lbda=lbda,
#srng = RandomStreams(seed=seed+2000),
input_dim=input_dim)
path = save_dir
name = '{}/airfoil_regression_m{}p{}c{}lr0{}seed{}reinit{}flow{}trial{}tau{}l{}'.format(
path,
model,
drop_prob,
coupling,
lr0,
seed,
reinit,
flow,
trial,
tau,
length_scale
)
e0 = 0
rec = 0
tr_LLs, tr_RMSEs, va_LLs, va_RMSEs, te_LLs, te_RMSEs = train_model(network, name, save_,
train_x[:size],train_y[:size],
valid_x,valid_y,
test_x, test_y,
lr0,lrdecay,bs,epochs,anneal,name,
e0,rec, tau)
if save_:
save_list(name + "_tr_RMSE", tr_RMSEs)
save_list(name + "_tr_LL", tr_LLs)
save_list(name + "_va_RMSE", va_RMSEs)
save_list(name + "_va_LL", va_LLs)
save_list(name + "_te_RMSE", te_RMSEs)
save_list(name + "_te_LL", te_LLs)
print "time=", time.time() - t0
# BHN
elif model == 'BHN':
lbda = get_lbda(tau, length_scale)
prior = log_normal
for coupling in [4]:#,12]:
for flow in ['IAF']:#, 'RealNVP']:
for reinit in [1]:#,0]:
t0 = time.time()
print lbda
if reinit:
init_batch_size = 64
init_batch = train_x[:size][-init_batch_size:]
else:
init_batch = None
network = MLPWeightNorm_BHN(lbda=lbda,
perdatapoint=perdatapoint,
srng = RandomStreams(seed=seed+2000),
prior=prior,
coupling=coupling,
n_hiddens=n_hiddens,
n_units=n_units,
input_dim=input_dim,
flow=flow,
init_batch=init_batch)
path = save_dir
name = '{}/airfoil_regression_m{}p{}c{}lr0{}seed{}reinit{}flow{}trial{}tau{}l{}'.format(
path,
model,
drop_prob,
coupling,
lr0,
seed,
reinit,
flow,
trial,
tau,
length_scale
)
e0 = 0
rec = 0
tr_LLs, tr_RMSEs, va_LLs, va_RMSEs, te_LLs, te_RMSEs = train_model(network, name, save_,
train_x[:size],train_y[:size],
valid_x,valid_y,
test_x, test_y,
lr0,lrdecay,bs,epochs,anneal,name,
e0,rec, tau)
if save_:
save_list(name + "_tr_RMSE", tr_RMSEs)
save_list(name + "_tr_LL", tr_LLs)
save_list(name + "_va_RMSE", va_RMSEs)
save_list(name + "_va_LL", va_LLs)
save_list(name + "_te_RMSE", te_RMSEs)
save_list(name + "_te_LL", te_LLs)
print "time=", time.time() - t0
else:
input_dim, train_x, train_y, valid_x, valid_y, test_x , test_y = get_regression_dataset(dataset, data_path=os.environ['HOME'] + '/BayesianHypernetCW/')
trial = 0
if model == 'MCDropout':
t0 = time.time()
lbda = get_lbda(tau, length_scale, drop_prob)
print drop_prob, lbda
network = MCdropout_MLP(n_hiddens=n_hiddens,
n_units=n_units,
lbda=lbda,
#srng = RandomStreams(seed=seed+2000),
input_dim=input_dim)
elif model == 'BHN':
lbda = get_lbda(tau, length_scale)
prior = log_normal
t0 = time.time()
print lbda
if reinit:
init_batch_size = 64
init_batch = train_x[:size][-init_batch_size:]
else:
init_batch = None
network = MLPWeightNorm_BHN(lbda=lbda,
perdatapoint=perdatapoint,
srng = RandomStreams(seed=seed+2000),
prior=prior,
coupling=coupling,
n_hiddens=n_hiddens,
n_units=n_units,
input_dim=input_dim,
flow=flow,
init_batch=init_batch)
path = save_dir
name = '{}/airfoil_regression_m{}p{}c{}lr0{}seed{}reinit{}flow{}trial{}tau{}l{}'.format(
path,
model,
drop_prob,
coupling,
lr0,
seed,
reinit,
flow,
trial,
tau,
length_scale
)
e0 = 0
rec = 0
result = train_model(network, name, save_,
train_x[:size],train_y[:size],
valid_x,valid_y,
test_x, test_y,
lr0,lrdecay,bs,epochs,anneal,name,
e0,rec, tau)
tr_LLs, tr_RMSEs, va_LLs, va_RMSEs, te_LLs, te_RMSEs = result
if save_:
save_list(name + "_tr_RMSE", tr_RMSEs)
save_list(name + "_tr_LL", tr_LLs)
save_list(name + "_va_RMSE", va_RMSEs)
save_list(name + "_va_LL", va_LLs)
save_list(name + "_te_RMSE", te_RMSEs)
save_list(name + "_te_LL", te_LLs)
print "time=", time.time() - t0
# post-experiment analysis
print "tr/va LL:", max(tr_LLs), max(va_LLs)
print "tr/va RMSE:", max(tr_RMSEs), max(va_RMSEs)
print "lambda", lbda
# for these 2 lines to work, you need to run this script interactively in ipython with run -i SCRIPTNAME, and define flagz = []; results = [] in the interactive session
#flagz = []; results = []
flagz.append(flags)
results.append(result)
desc= exp_description + ' lbda=' + str(lbda)
figure(1)
subplot(121)
plot(tr_LLs, label=desc+'_TR')
plot(va_LLs, label=desc)
subplot(122)
plot(tr_RMSEs, label=desc+'_TR')
plot(va_RMSEs, label=desc)
legend()
figure()
suptitle(desc)
subplot(121)
plot(tr_LLs)
plot(va_LLs)
subplot(122)
plot(tr_RMSEs)
plot(va_RMSEs)
show()