-
Notifications
You must be signed in to change notification settings - Fork 6
/
train_hypertuning.py
202 lines (154 loc) · 7.02 KB
/
train_hypertuning.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
import torch
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import logging
import copy
import os
import corner
from torch.utils.data import DataLoader
import argparse
from sklearn.utils import shuffle as util_shuffle
from sklearn.model_selection import KFold
from itertools import product
# matplotlib.use('agg')
from deconv.utils.make_2d_toy_data import data_gen
from deconv.utils.make_2d_toy_noise_covar import covar_gen
from deconv.utils.compute_2d_log_likelihood import compute_data_ll
from deconv.utils.misc import get_logger
from deconv.flow.svi_no_mdn import SVIFlowToy, SVIFlowToyNoise
from deconv.gmm.data import DeconvDataset
from sklearn.datasets import load_boston
parser = argparse.ArgumentParser()
parser.add_argument('--data', type=str, default='boston',
choices=['boston', 'white_wine', 'red_wine', 'ionosphere'])
parser.add_argument('--covar', type=float, default=0.1)
parser.add_argument('--seed', type=int, default=42)
parser.add_argument('--batch_size', type=int, default=100)
parser.add_argument('--gpu', type=int, default=0)
parser.add_argument('--n_epochs', type=int, default=int(1e4))
parser.add_argument('--objective', type=str, default='iwae',
choices=['elbo', 'iwae', 'iwae_sumo'])
parser.add_argument('--K', type=int, default=50,
help='# of samples for objective')
args = parser.parse_args()
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
if torch.cuda.is_available():
torch.set_default_tensor_type('torch.cuda.DoubleTensor')
torch.cuda.set_device(args.gpu)
else:
torch.set_default_tensor_type('torch.DoubleTensor')
args.dir = 'hypertuning/' + '/' + \
str(args.objective) + '/' + str(args.covar) + '/'
if not os.path.exists(args.dir):
os.makedirs(args.dir)
name = args.data + '_K_' + str(args.K)
if not os.path.exists(args.dir + 'logs/'):
os.makedirs(args.dir + 'logs/')
logger = get_logger(logpath=(args.dir + 'logs/' + name +
'.log'), filepath=os.path.abspath(__file__))
torch.manual_seed(args.seed)
np.random.seed(args.seed)
def compute_eval_loss(model, eval_loader, device, n_points):
loss = 0
for _, data in enumerate(eval_loader):
data[0] = data[0].to(device)
data[1] = data[1].to(device)
loss += -model.score(data).sum()
return loss / n_points
def main():
if args.data == 'boston':
data = np.load('data_small/boston_no_discrete.npy')
elif args.data == 'white_wine':
data = np.load('data_small/white_no_discrete_no_corr_0.98.npy')
elif args.data == 'red_wine':
data = np.load('data_small/red_no_discrete_no_corr_0.98.npy')
elif args.data == 'ionosphere':
data = np.load('data_small/ionosphere_no_discrete_no_corr_0.98.npy')
n_features = data.shape[1]
n_train = int(data.shape[0] * 0.9)
train_data_clean = data[:n_train]
covar = np.diag(args.covar * np.ones((n_features,)))
train_data = train_data_clean + \
np.random.multivariate_normal(mean=np.zeros(
(n_features,)), cov=covar, size=n_train)
kf = KFold(n_splits=5)
# 54 combinations
lr_list = [1e-3, 5e-4, 1e-4]
flow_steps_prior_list = [3, 4, 5]
flow_steps_posterior_list = [3, 4, 5]
maf_features_list = [64, 128]
maf_hidden_blocks_list = [1, 2]
n_combs = 0
for lr, fspr, fspo, maf_f, maf_h in product(lr_list, flow_steps_posterior_list, flow_steps_posterior_list, maf_features_list, maf_hidden_blocks_list):
n_combs += 1
print(n_combs, (lr, fspr, fspo, maf_f, maf_h))
best_eval = np.zeros((n_combs, 5))
counter = 0
for lr, fspr, fspo, maf_f, maf_h in product(lr_list, flow_steps_posterior_list, flow_steps_posterior_list, maf_features_list, maf_hidden_blocks_list):
logger.info((lr, fspr, fspo, maf_f, maf_h))
for i, (train_index, eval_index) in enumerate(kf.split(train_data)):
X_train, X_eval = train_data[train_index], train_data[eval_index]
train_covars = np.repeat(
covar[np.newaxis, :, :], X_train.shape[0], axis=0)
eval_covars = np.repeat(
covar[np.newaxis, :, :], X_eval.shape[0], axis=0)
train_dataset = DeconvDataset(X_train, train_covars)
train_loader = DataLoader(
train_dataset, batch_size=args.batch_size, shuffle=True)
eval_dataset = DeconvDataset(X_eval, eval_covars)
eval_loader = DataLoader(
eval_dataset, batch_size=args.batch_size, shuffle=False)
model = SVIFlowToy(dimensions=n_features,
objective=args.objective,
posterior_context_size=n_features,
batch_size=args.batch_size,
device=device,
maf_steps_prior=fspr,
maf_steps_posterior=fspo,
maf_features=maf_f,
maf_hidden_blocks=maf_h,
K=args.K)
message = 'Total number of parameters: %s' % (
sum(p.numel() for p in model.parameters()))
logger.info(message)
optimizer = torch.optim.Adam(params=model.parameters(), lr=lr)
# training
scheduler = [30] # stop after 30 epochs of no improvement
epoch = 0
model.eval()
with torch.no_grad():
best_eval_loss = compute_eval_loss(
model, eval_loader, device, X_eval.shape[0])
best_model = copy.deepcopy(model.state_dict())
n_epochs_not_improved = 0
model.train()
while n_epochs_not_improved < scheduler[-1] and epoch < args.n_epochs:
for batch_idx, data in enumerate(train_loader):
data[0] = data[0].to(device)
data[1] = data[1].to(device)
loss = -model.score(data).mean()
optimizer.zero_grad()
loss.backward(retain_graph=True)
optimizer.step()
model.eval()
with torch.no_grad():
eval_loss = compute_eval_loss(
model, eval_loader, device, X_eval.shape[0])
if eval_loss < best_eval_loss:
best_model = copy.deepcopy(model.state_dict())
best_eval_loss = eval_loss
n_epochs_not_improved = 0
else:
n_epochs_not_improved += 1
message = 'Epoch %s:' % (
epoch + 1), 'train loss = %.5f' % loss, 'eval loss = %.5f' % eval_loss
logger.info(message)
model.train()
epoch += 1
best_eval[counter, i] = best_eval_loss
np.save(args.data + '_hypertuning_results_tmp', best_eval)
counter += 1
np.save(args.data + '_hypertuning_results', best_eval)
if __name__ == '__main__':
main()