-
Notifications
You must be signed in to change notification settings - Fork 6
/
train_toy_no_mdn.py
273 lines (205 loc) · 9.52 KB
/
train_toy_no_mdn.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
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
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
parser = argparse.ArgumentParser()
parser.add_argument('--infer', type=str, default='true_data', choices=['noise', 'true_data'])
parser.add_argument('--data', type=str, default='mixture_1')
parser.add_argument('--covar', type=str, default='fixed_diagonal_covar1')
parser.add_argument('--n_train_points', type=int, default=int(1e5))
parser.add_argument('--n_test_points', type=int, default=int(1e3))
parser.add_argument('--n_eval_points', type=int, default=int(1e3))
parser.add_argument('--n_kl_points', type=int, default=int(1e4))
parser.add_argument('--eval_based_scheduler', type=str, default='20,30,40,50')
parser.add_argument('--lr', type=float, default=5e-3)
parser.add_argument('--seed', type=int, default=42)
parser.add_argument('--batch_size', type=int, default=512)
parser.add_argument('--test_batch_size', type=int, default=100)
parser.add_argument('--gpu', type=int, default=0)
parser.add_argument('--dir', type=str, default=None)
parser.add_argument('--name', type=str, default=None)
parser.add_argument('--flow_steps_prior', type=int, default=5)
parser.add_argument('--flow_steps_posterior', type=int, default=5)
parser.add_argument('--posterior_context_size', type=int, default=2) #this is just dim when we use w
parser.add_argument('--n_epochs', type=int, default=int(1e6))
parser.add_argument('--objective', type=str, default='elbo', choices=['elbo', 'iwae', 'iwae_sumo'])
parser.add_argument('--K', type=int, default=1, help='# of samples for objective')
parser.add_argument('--viz_freq', type=int, default=10)
parser.add_argument('--test_freq', type=int, default=10)
parser.add_argument('--maf_features', type=int, default=64)
parser.add_argument('--maf_hidden_blocks', type=int, default=2)
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.FloatTensor')
torch.cuda.set_device(args.gpu)
if args.dir is None:
args.dir = 'toy/' + str(args.infer) + '/' + str(args.objective) + '/' + str(args.data) + '/' + str(args.covar) + '/'
if not os.path.exists(args.dir):
os.makedirs(args.dir)
if args.name is None:
name = 'n_train_points_' + str(args.n_train_points) + \
'_K_' + str(args.K) + \
'_batch_size_' + str(args.batch_size) + \
'_fs_posterior_' + str(args.flow_steps_posterior) + \
'_seed_' + str(args.seed)
# if os.path.isfile(args.dir + 'logs/' + name + '.log'):
# raise ValueError('This file already exists.')
if not os.path.exists(args.dir + 'logs/'):
os.makedirs(args.dir + 'logs/')
if not os.path.exists(args.dir + 'out/'):
os.makedirs(args.dir + 'out/')
if not os.path.exists(args.dir + 'models/'):
os.makedirs(args.dir + 'models/')
logger = get_logger(logpath=(args.dir + 'logs/' + name + '.log'), filepath=os.path.abspath(__file__))
logger.info(args)
torch.manual_seed(args.seed)
np.random.seed(args.seed)
def lr_scheduler(n_epochs_not_improved, optimzer, scheduler, logger):
lr = args.lr
for i in range(len(scheduler) - 1):
if n_epochs_not_improved > scheduler[i]:
lr *= 0.1
for param_group in optimzer.param_groups:
param_group['lr'] = lr
message = 'New learning rate: %f' % lr
logger.info(message)
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():
train_covar = covar_gen(args.covar, args.n_train_points).astype(np.float32)
train_data_clean = data_gen(args.data, args.n_train_points)[0].astype(np.float32)
# plt.scatter(train_data_clean[:, 0], train_data_clean[:, 1])
train_data = np.zeros_like(train_data_clean)
for i in range(args.n_train_points):
train_data[i] = train_data_clean[i] + np.random.multivariate_normal(mean=np.zeros((2,)), cov=train_covar[i])
# plt.scatter(train_data[:, 0], train_data[:, 1])
# plt.show()
train_covar = torch.from_numpy(train_covar)
train_data = torch.from_numpy(train_data.astype(np.float32))
train_dataset = DeconvDataset(train_data, train_covar)
train_loader = DataLoader(train_dataset, batch_size=args.batch_size, shuffle=True)
test_data_clean = torch.from_numpy(data_gen(args.data, args.n_test_points)[0].astype(np.float32))
eval_covar = covar_gen(args.covar, args.n_eval_points).astype(np.float32)
eval_data_clean = data_gen(args.data, args.n_eval_points)[0].astype(np.float32)
eval_data = np.zeros_like(eval_data_clean)
for i in range(args.n_eval_points):
eval_data[i] = eval_data_clean[i] + np.random.multivariate_normal(mean=np.zeros((2,)), cov=eval_covar[i])
eval_covar = torch.from_numpy(eval_covar)
eval_data = torch.from_numpy(eval_data.astype(np.float32))
eval_dataset = DeconvDataset(eval_data, eval_covar)
eval_loader = DataLoader(eval_dataset, batch_size=args.test_batch_size, shuffle=False)
if args.infer == 'true_data':
model = SVIFlowToy(dimensions=2,
objective=args.objective,
posterior_context_size=args.posterior_context_size,
batch_size=args.batch_size,
device=device,
maf_steps_prior=args.flow_steps_prior,
maf_steps_posterior=args.flow_steps_posterior,
maf_features=args.maf_features,
maf_hidden_blocks=args.maf_hidden_blocks,
K=args.K)
else:
model = SVIFlowToyNoise(dimensions=2,
objective=args.objective,
posterior_context_size=args.posterior_context_size,
batch_size=args.batch_size,
device=device,
maf_steps_prior=args.flow_steps_prior,
maf_steps_posterior=args.flow_steps_posterior,
maf_features=args.maf_features,
maf_hidden_blocks=args.maf_hidden_blocks,
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=args.lr)
#training
scheduler = list(map(int, args.eval_based_scheduler.split(',')))
epoch = 0
best_model = copy.deepcopy(model.state_dict())
best_eval_loss = compute_eval_loss(model, eval_loader, device, args.n_eval_points)
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()
eval_loss = compute_eval_loss(model, eval_loader, device, args.n_eval_points)
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
lr_scheduler(n_epochs_not_improved, optimizer, scheduler, logger)
if (epoch + 1) % args.test_freq == 0:
if args.infer == 'true_data':
test_loss_clean = -model.model._prior.log_prob(test_data_clean.to(device)).mean()
else:
test_loss_clean = -model.model._likelihood.log_prob(test_data_clean.to(device)).mean()
message = 'Epoch %s:' % (epoch + 1), 'train loss = %.5f' % loss, 'eval loss = %.5f' % eval_loss, 'test loss (clean) = %.5f' % test_loss_clean
logger.info(message)
else:
message = 'Epoch %s:' % (epoch + 1), 'train loss = %.5f' % loss, 'eval loss = %.5f' % eval_loss
logger.info(message)
if (epoch + 1) % args.viz_freq == 0:
if args.infer == 'true_data':
samples = model.model._prior.sample(1000).detach().cpu().numpy()
else:
samples = model.model._likelihood.sample(1000).detach().cpu().numpy()
corner.hist2d(samples[:, 0], samples[:, 1])
fig_filename = args.dir + 'out/' + name + '_corner_fig_' + str(epoch + 1) + '.png'
plt.savefig(fig_filename)
plt.close()
plt.scatter(samples[:, 0], samples[:, 1])
fig_filename = args.dir + 'out/' + name + '_scatter_fig_' + str(epoch + 1) + '.png'
plt.savefig(fig_filename)
plt.close()
model.train()
epoch += 1
model.load_state_dict(best_model)
model.eval()
if args.infer == 'true_data':
test_loss_clean = -model.model._prior.log_prob(test_data_clean.to(device)).mean()
else:
test_loss_clean = -model.model._likelihood.log_prob(test_data_clean.to(device)).mean()
message = 'Final test loss (clean) = %.5f' % test_loss_clean
logger.info(message)
torch.save(model.state_dict(), args.dir + 'models/' + name + '.model')
logger.info('Training has finished.')
if args.data.split('_')[0] == 'mixture' or args.data.split('_')[0] == 'gaussian':
kl_points = data_gen(args.data, args.n_kl_points)[0].astype(np.float32)
if args.infer == 'true_data':
model_log_prob = model.model._prior.log_prob(torch.from_numpy(kl_points.astype(np.float32)).to(device)).mean()
else:
model_log_prob = model.model._likelihood.log_prob(torch.from_numpy(kl_points.astype(np.float32)).to(device)).mean()
data_log_prob = compute_data_ll(args.data, kl_points).mean()
approximate_KL = data_log_prob - model_log_prob
message = 'KL div %.5f:' % approximate_KL
logger.info(message)
if __name__ == '__main__':
main()