forked from Jamesyang2333/SAM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
origin_train_model.py
459 lines (393 loc) · 16.5 KB
/
origin_train_model.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
452
453
454
455
456
457
458
459
"""Model training."""
import argparse
import os
import time
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import common
import datasets
import made
DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
print('Device', DEVICE)
parser = argparse.ArgumentParser()
# Training.
parser.add_argument('--dataset', type=str, default='dmv-tiny', help='Dataset.')
parser.add_argument('--num-gpus', type=int, default=0, help='#gpus.')
parser.add_argument('--bs', type=int, default=1024, help='Batch size.')
parser.add_argument(
'--warmups',
type=int,
default=0,
help='Learning rate warmup steps. Crucial for Transformer.')
parser.add_argument('--epochs',
type=int,
default=20,
help='Number of epochs to train for.')
parser.add_argument('--constant-lr',
type=float,
default=None,
help='Constant LR?')
parser.add_argument(
'--column-masking',
action='store_true',
help='Column masking training, which permits wildcard skipping'\
' at querying time.')
# MADE.
parser.add_argument('--fc-hiddens',
type=int,
default=128,
help='Hidden units in FC.')
parser.add_argument('--layers', type=int, default=4, help='# layers in FC.')
parser.add_argument('--residual', action='store_true', help='ResMade?')
parser.add_argument('--direct-io', action='store_true', help='Do direct IO?')
parser.add_argument(
'--inv-order',
action='store_true',
help='Set this flag iff using MADE and specifying --order. Flag --order '\
'lists natural indices, e.g., [0 2 1] means variable 2 appears second.'\
'MADE, however, is implemented to take in an argument the inverse '\
'semantics (element i indicates the position of variable i). Transformer'\
' does not have this issue and thus should not have this flag on.')
parser.add_argument(
'--input-encoding',
type=str,
default='binary',
help='Input encoding for MADE/ResMADE, {binary, one_hot, embed}.')
parser.add_argument(
'--output-encoding',
type=str,
default='one_hot',
help='Iutput encoding for MADE/ResMADE, {one_hot, embed}. If embed, '
'then input encoding should be set to embed as well.')
# Ordering.
parser.add_argument('--num-orderings',
type=int,
default=1,
help='Number of orderings.')
parser.add_argument(
'--order',
nargs='+',
type=int,
required=False,
help=
'Use a specific ordering. '\
'Format: e.g., [0 2 1] means variable 2 appears second.'
)
parser.add_argument('--cuda-num',
type=int,
default=None,
help='the number of cuda used')
args = parser.parse_args()
def Entropy(name, data, bases=None):
import scipy.stats
s = 'Entropy of {}:'.format(name)
ret = []
for base in bases:
assert base == 2 or base == 'e' or base is None
e = scipy.stats.entropy(data, base=base if base != 'e' else None)
ret.append(e)
unit = 'nats' if (base == 'e' or base is None) else 'bits'
s += ' {:.4f} {}'.format(e, unit)
print(s)
return ret
def RunEpoch(split,
model,
opt,
train_data,
val_data=None,
batch_size=100,
upto=None,
epoch_num=None,
verbose=False,
log_every=10,
return_losses=False,
table_bits=None):
torch.set_grad_enabled(split == 'train')
model.train() if split == 'train' else model.eval()
dataset = train_data if split == 'train' else val_data
losses = []
loader = torch.utils.data.DataLoader(dataset,
batch_size=batch_size,
shuffle=(split == 'train'))
# How many orderings to run for the same batch?
nsamples = 1
if hasattr(model, 'orderings'):
nsamples = len(model.orderings)
for step, (idx, xb) in enumerate(loader):
if split == 'train':
base_lr = 8e-4
for param_group in opt.param_groups:
if args.constant_lr:
lr = args.constant_lr
elif args.warmups:
t = args.warmups
d_model = model.embed_size
global_steps = len(loader) * epoch_num + step + 1
lr = (d_model**-0.5) * min(
(global_steps**-.5), global_steps * (t**-1.5))
else:
lr = 1e-2
param_group['lr'] = lr
if upto and step >= upto:
break
xb = xb.to(DEVICE).to(torch.float32)
# Forward pass, potentially through several orderings.
xbhat = None
model_logits = []
num_orders_to_forward = 1
if split == 'test' and nsamples > 1:
# At test, we want to test the 'true' nll under all orderings.
num_orders_to_forward = nsamples
for i in range(num_orders_to_forward):
if hasattr(model, 'update_masks'):
# We want to update_masks even for first ever batch.
model.update_masks()
model_out = model(xb)
model_logits.append(model_out)
if xbhat is None:
xbhat = torch.zeros_like(model_out)
xbhat += model_out
if xbhat.shape == xb.shape:
if mean:
xb = (xb * std) + mean
loss = F.binary_cross_entropy_with_logits(
xbhat, xb, size_average=False) / xbhat.size()[0]
else:
if model.input_bins is None:
# NOTE: we have to view() it in this order due to the mask
# construction within MADE. The masks there on the output unit
# determine which unit sees what input vars.
xbhat = xbhat.view(-1, model.nout // model.nin, model.nin)
# Equivalent to:
loss = F.cross_entropy(xbhat, xb.long(), reduction='none') \
.sum(-1).mean()
else:
if num_orders_to_forward == 1:
loss = model.nll(xbhat, xb).mean()
else:
# Average across orderings & then across minibatch.
#
# p(x) = 1/N sum_i p_i(x)
# log(p(x)) = log(1/N) + log(sum_i p_i(x))
# = log(1/N) + logsumexp ( log p_i(x) )
# = log(1/N) + logsumexp ( - nll_i (x) )
#
# Used only at test time.
logps = [] # [batch size, num orders]
assert len(model_logits) == num_orders_to_forward, len(
model_logits)
for logits in model_logits:
# Note the minus.
logps.append(-model.nll(logits, xb))
logps = torch.stack(logps, dim=1)
logps = logps.logsumexp(dim=1) + torch.log(
torch.tensor(1.0 / nsamples, device=logps.device))
loss = (-logps).mean()
losses.append(loss.item())
if step % log_every == 0:
if split == 'train':
print(
'Epoch {} Iter {}, {} entropy gap {:.4f} bits (loss {:.3f}, data {:.3f}) {:.5f} lr'
.format(epoch_num, step, split,
loss.item() / np.log(2) - table_bits,
loss.item() / np.log(2), table_bits, lr))
else:
print('Epoch {} Iter {}, {} loss {:.4f} nats / {:.4f} bits'.
format(epoch_num, step, split, loss.item(),
loss.item() / np.log(2)))
if split == 'train':
opt.zero_grad()
loss.backward()
opt.step()
if verbose:
print('%s epoch average loss: %f' % (split, np.mean(losses)))
if return_losses:
return losses
return np.mean(losses)
def ReportModel(model, blacklist=None):
ps = []
for name, p in model.named_parameters():
if blacklist is None or blacklist not in name:
ps.append(np.prod(p.size()))
num_params = sum(ps)
mb = num_params * 4 / 1024 / 1024
print('Number of model parameters: {} (~= {:.1f}MB)'.format(num_params, mb))
print(model)
return mb
def InvertOrder(order):
if order is None:
return None
# 'order'[i] maps nat_i -> position of nat_i
# Inverse: position -> natural idx. This it the 'true' ordering -- it's how
# heuristic orders are generated + (less crucially) how Transformer works.
nin = len(order)
inv_ordering = [None] * nin
for natural_idx in range(nin):
inv_ordering[order[natural_idx]] = natural_idx
return inv_ordering
def MakeMade(scale, cols_to_train, seed, fixed_ordering=None):
if args.inv_order:
print('Inverting order!')
fixed_ordering = InvertOrder(fixed_ordering)
model = made.MADE(
nin=len(cols_to_train),
hidden_sizes=[scale] *
args.layers if args.layers > 0 else [512, 256, 512, 128, 1024],
nout=sum([c.DistributionSize() for c in cols_to_train]),
input_bins=[c.DistributionSize() for c in cols_to_train],
input_encoding=args.input_encoding,
output_encoding=args.output_encoding,
embed_size=32,
seed=seed,
do_direct_io_connections=args.direct_io,
natural_ordering=False if seed is not None and seed != 0 else True,
residual_connections=args.residual,
fixed_ordering=fixed_ordering,
column_masking=args.column_masking,
).to(DEVICE)
return model
def InitWeight(m):
if type(m) == made.MaskedLinear or type(m) == nn.Linear:
nn.init.xavier_uniform_(m.weight)
nn.init.zeros_(m.bias)
if type(m) == nn.Embedding:
nn.init.normal_(m.weight, std=0.02)
def TrainTask(seed=0):
torch.manual_seed(0)
np.random.seed(0)
if args.cuda_num is not None:
torch.cuda.set_device(args.cuda_num)
os.environ['CUDA_VISIBLE_DEVICES'] = str(args.cuda_num)
assert args.dataset in ['dmv-tiny', 'dmv', 'covtype','kddcup', 'poker', 'census' , 'dmv-full', 'tpc', 'cup98']
if args.dataset == 'dmv-tiny':
table = datasets.LoadDmv('dmv-tiny.csv')
elif args.dataset == 'dmv':
table = datasets.LoadDmv()
elif args.dataset == 'covtype':
table = datasets.LoadCovtype()
elif args.dataset == 'kddcup':
table = datasets.LoadKddcup()
elif args.dataset == 'poker':
table = datasets.LoadPoker()
elif args.dataset == 'census':
table = datasets.LoadCensus()
elif args.dataset == 'dmv-full':
table = datasets.LoadDmvFull()
elif args.dataset == 'tpc':
table = datasets.LoadTpc()
elif args.dataset == 'cup98':
table = datasets.LoadCup98()
table_bits = Entropy(
table,
table.data.fillna(value=0).groupby([c.name for c in table.columns
]).size(), [2])[0]
fixed_ordering = None
if args.order is not None:
print('Using passed-in order:', args.order)
fixed_ordering = args.order
print(table.data.info())
table_train = table
if args.dataset in ['dmv-tiny', 'dmv', 'covtype', 'kddcup', 'poker', 'census', 'dmv-full', 'tpc', 'cup98']:
model = MakeMade(
scale=args.fc_hiddens,
cols_to_train=table.columns,
seed=seed,
fixed_ordering=fixed_ordering,
)
else:
assert False, args.dataset
mb = ReportModel(model)
print('Applying InitWeight()')
model.apply(InitWeight)
opt = torch.optim.Adam(list(model.parameters()), 2e-4)
bs = args.bs
log_every = 200
train_data = common.TableDataset(table_train)
train_losses = []
train_start = time.time()
for epoch in range(args.epochs):
mean_epoch_train_loss = RunEpoch('train',
model,
opt,
train_data=train_data,
val_data=train_data,
batch_size=bs,
epoch_num=epoch,
log_every=log_every,
table_bits=table_bits)
if epoch % 1 == 0:
print('epoch {} train loss {:.4f} nats / {:.4f} bits'.format(
epoch, mean_epoch_train_loss,
mean_epoch_train_loss / np.log(2)))
since_start = time.time() - train_start
print('time since start: {:.1f} secs'.format(since_start))
train_losses.append(mean_epoch_train_loss)
if (epoch+1) % 5 == 0:
all_losses = RunEpoch('test',
model,
train_data=train_data,
val_data=train_data,
opt=None,
batch_size=1024,
log_every=500,
table_bits=table_bits,
return_losses=True)
model_nats = np.mean(all_losses)
model_bits = model_nats / np.log(2)
model.model_bits = model_bits
if fixed_ordering is None:
if seed is not None:
PATH = 'models/{}-{:.1f}MB-model{:.3f}-data{:.3f}-{}-{}epochs-seed{}.pt'.format(
args.dataset, mb, model.model_bits, table_bits, model.name(),
epoch+1, seed)
else:
PATH = 'models/{}-{:.1f}MB-model{:.3f}-data{:.3f}-{}-{}epochs-seed{}-{}.pt'.format(
args.dataset, mb, model.model_bits, table_bits, model.name(),
epoch + 1, seed, time.time())
else:
annot = ''
if args.inv_order:
annot = '-invOrder'
PATH = 'models/{}-{:.1f}MB-model{:.3f}-data{:.3f}-{}-{}epochs-seed{}-order{}{}.pt'.format(
args.dataset, mb, model.model_bits, table_bits, model.name(),
epoch + 1, seed, '_'.join(map(str, fixed_ordering)), annot)
os.makedirs(os.path.dirname(PATH), exist_ok=True)
torch.save(model.state_dict(), PATH)
print('Saved to:')
print(PATH)
print('Training done; evaluating likelihood on full data:')
all_losses = RunEpoch('test',
model,
train_data=train_data,
val_data=train_data,
opt=None,
batch_size=1024,
log_every=500,
table_bits=table_bits,
return_losses=True)
model_nats = np.mean(all_losses)
model_bits = model_nats / np.log(2)
model.model_bits = model_bits
if fixed_ordering is None:
if seed is not None:
PATH = 'data_models/{}-{:.1f}MB-model{:.3f}-data{:.3f}-{}-{}epochs-seed{}.pt'.format(
args.dataset, mb, model.model_bits, table_bits, model.name(),
args.epochs, seed)
else:
PATH = 'data_models/{}-{:.1f}MB-model{:.3f}-data{:.3f}-{}-{}epochs-seed{}-{}.pt'.format(
args.dataset, mb, model.model_bits, table_bits, model.name(),
args.epochs, seed, time.time())
else:
annot = ''
if args.inv_order:
annot = '-invOrder'
PATH = 'data_models/{}-{:.1f}MB-model{:.3f}-data{:.3f}-{}-{}epochs-seed{}-order{}{}.pt'.format(
args.dataset, mb, model.model_bits, table_bits, model.name(),
args.epochs, seed, '_'.join(map(str, fixed_ordering)), annot)
os.makedirs(os.path.dirname(PATH), exist_ok=True)
torch.save(model.state_dict(), PATH)
print('Saved to:')
print(PATH)
TrainTask()