forked from harvardnlp/var-attn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
executable file
·569 lines (473 loc) · 20.7 KB
/
train.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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
#!/usr/bin/env python
from __future__ import division
import argparse
import glob
import os
import sys
import random
from datetime import datetime
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch import cuda
import onmt
import onmt.io
import onmt.Models
import onmt.ModelConstructor
import onmt.modules
from onmt.Utils import use_gpu
import onmt.opts
parser = argparse.ArgumentParser(
description='train.py',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
# onmt.opts.py
onmt.opts.add_md_help_argument(parser)
onmt.opts.model_opts(parser)
onmt.opts.train_opts(parser)
opt = parser.parse_args()
if opt.word_vec_size != -1:
opt.src_word_vec_size = opt.word_vec_size
opt.tgt_word_vec_size = opt.word_vec_size
if opt.layers != -1:
opt.enc_layers = opt.layers
opt.dec_layers = opt.layers
opt.brnn = (opt.encoder_type == "brnn")
if opt.seed > 0:
random.seed(opt.seed)
torch.manual_seed(opt.seed)
if opt.rnn_type == "SRU" and not opt.gpuid:
raise AssertionError("Using SRU requires -gpuid set.")
if torch.cuda.is_available() and not opt.gpuid:
print("WARNING: You have a CUDA device, should run with -gpuid 0")
if opt.gpuid:
cuda.set_device(opt.gpuid[0])
if opt.seed > 0:
torch.cuda.manual_seed(opt.seed)
if len(opt.gpuid) > 1:
sys.stderr.write("Sorry, multigpu isn't supported yet, coming soon!\n")
sys.exit(1)
# Set up the Crayon logging server.
if opt.exp_host != "":
from pycrayon import CrayonClient
cc = CrayonClient(hostname=opt.exp_host)
experiments = cc.get_experiment_names()
print(experiments)
if opt.exp in experiments:
cc.remove_experiment(opt.exp)
experiment = cc.create_experiment(opt.exp)
if opt.tensorboard:
from tensorboardX import SummaryWriter
writer = SummaryWriter(
opt.tensorboard_log_dir + datetime.now().strftime("/%b-%d_%H-%M-%S"),
comment="Onmt")
progress_step = 0
def report_func(epoch, batch, num_batches,
progress_step,
start_time, lr, report_stats):
"""
This is the user-defined batch-level traing progress
report function.
Args:
epoch(int): current epoch count.
batch(int): current batch count.
num_batches(int): total number of batches.
progress_step(int): the progress step.
start_time(float): last report time.
lr(float): current learning rate.
report_stats(Statistics): old Statistics instance.
Returns:
report_stats(Statistics): updated Statistics instance.
"""
if batch % opt.report_every == -1 % opt.report_every:
report_stats.output(epoch, batch + 1, num_batches, start_time)
if opt.exp_host:
report_stats.log("progress", experiment, lr)
if opt.tensorboard:
# Log the progress using the number of batches on the x-axis.
report_stats.log_tensorboard(
"progress", writer, lr, progress_step)
report_stats = onmt.Statistics()
return report_stats
class DatasetLazyIter(object):
""" An Ordered Dataset Iterator, supporting multiple datasets,
and lazy loading.
Args:
datsets (list): a list of datasets, which are lazily loaded.
fields (dict): fields dict for the datasets.
batch_size (int): batch size.
batch_size_fn: custom batch process function.
device: the GPU device.
is_train (bool): train or valid?
"""
def __init__(self, datasets, fields, batch_size, batch_size_fn,
device, is_train, random_state=None):
self.datasets = datasets
self.fields = fields
self.batch_size = batch_size
self.batch_size_fn = batch_size_fn
self.device = device
self.is_train = is_train
self.random_state = random_state
self.cur_iter = self._next_dataset_iterator(datasets)
# We have at least one dataset.
assert self.cur_iter is not None
def __iter__(self):
dataset_iter = (d for d in self.datasets)
while self.cur_iter is not None:
if self.random_state is not None:
self.cur_iter.random_shuffler._random_state = self.random_state
for batch in self.cur_iter:
yield batch
self.random_state = self.cur_iter.random_shuffler._random_state
self.cur_iter = self._next_dataset_iterator(dataset_iter)
def __len__(self):
# We return the len of cur_dataset, otherwise we need to load
# all datasets to determine the real len, which loses the benefit
# of lazy loading.
assert self.cur_iter is not None
return len(self.cur_iter)
def get_cur_dataset(self):
return self.cur_dataset
def _next_dataset_iterator(self, dataset_iter):
try:
self.cur_dataset = next(dataset_iter)
except StopIteration:
return None
# We clear `fields` when saving, restore when loading.
self.cur_dataset.fields = self.fields
# Sort batch by decreasing lengths of sentence required by pytorch.
# sort=False means "Use dataset's sortkey instead of iterator's".
return onmt.io.OrderedIterator(
dataset=self.cur_dataset, batch_size=self.batch_size,
batch_size_fn=self.batch_size_fn,
device=self.device, train=self.is_train,
sort=False, sort_within_batch=True,
repeat=False)
def make_dataset_iter(datasets, fields, opt, is_train=True, random_state=None):
"""
This returns user-defined train/validate data iterator for the trainer
to iterate over during each train epoch. We implement simple
ordered iterator strategy here, but more sophisticated strategy
like curriculum learning is ok too.
"""
batch_size = opt.batch_size if is_train else opt.valid_batch_size
batch_size_fn = None
if is_train and opt.batch_type == "tokens":
# In token batching scheme, the number of sequences is limited
# such that the total number of src/tgt tokens (including padding)
# in a batch <= batch_size
def batch_size_fn(new, count, sofar):
# Maintains the longest src and tgt length in the current batch
global max_src_in_batch, max_tgt_in_batch
# Reset current longest length at a new batch (count=1)
if count == 1:
max_src_in_batch = 0
max_tgt_in_batch = 0
# Src: <bos> w1 ... wN <eos>
max_src_in_batch = max(max_src_in_batch, len(new.src) + 2)
# Tgt: w1 ... wN <eos>
max_tgt_in_batch = max(max_tgt_in_batch, len(new.tgt) + 1)
src_elements = count * max_src_in_batch
tgt_elements = count * max_tgt_in_batch
return max(src_elements, tgt_elements)
device = 'cuda' if opt.gpuid else 'cpu'
return DatasetLazyIter(datasets, fields, batch_size, batch_size_fn,
device, is_train, random_state=random_state)
def make_loss_compute(model, tgt_vocab, opt, train=True):
"""
This returns user-defined LossCompute object, which is used to
compute loss in train/validate process. You can implement your
own *LossCompute class, by subclassing LossComputeBase.
"""
if opt.copy_attn:
compute = onmt.modules.CopyGeneratorLossCompute(
model.generator, tgt_vocab, opt.copy_attn_force,
opt.copy_loss_by_seqlength)
else:
compute = onmt.Loss.NMTLossCompute(
model.generator, tgt_vocab,
label_smoothing=opt.label_smoothing if train else 0.0,
train_baseline=opt.train_baseline > 0,
)
if use_gpu(opt):
compute.cuda()
return compute
def train_model(model, fields, optim, data_type, model_opt):
train_loss = make_loss_compute(model, fields["tgt"].vocab, opt)
valid_loss = make_loss_compute(model, fields["tgt"].vocab, opt,
train=False)
trunc_size = opt.truncated_decoder # Badly named...
shard_size = opt.max_generator_batches
norm_method = opt.normalization
grad_accum_count = opt.accum_count
trainer = onmt.Trainer(model, train_loss, valid_loss, optim,
trunc_size, shard_size, data_type,
norm_method, grad_accum_count)
if model_opt.eval_only > 0:
print("|Param|: {}".format(sum([p.norm()**2 for p in model.parameters()]).data[0]**0.5))
print("ELBO_q")
model.use_prior = False
valid_iter = make_dataset_iter(lazily_load_dataset("valid"),
fields, opt,
is_train=False)
valid_stats = trainer.validate(valid_iter, "enum")
print('Validation exp(elbo): %g' % valid_stats.expelbo())
print('Validation perplexity: %g' % valid_stats.ppl())
print('Validation xent: %g' % valid_stats.xent())
print('Validation kl: %g' % valid_stats.kl())
print('Validation accuracy: %g' % valid_stats.accuracy())
print("N validation words: {}".format(valid_stats._n_words))
print("p(x)")
model.use_prior = True
for k in range(6):
print("k-max: {}".format(k))
model.k = k
valid_iter = make_dataset_iter(lazily_load_dataset("valid"),
fields, opt,
is_train=False)
valid_stats = trainer.validate(valid_iter, "exact")
print('Validation exp(elbo): %g' % valid_stats.expelbo())
print('Validation perplexity: %g' % valid_stats.ppl())
print('Validation xent: %g' % valid_stats.xent())
print('Validation kl: %g' % valid_stats.kl())
print('Validation accuracy: %g' % valid_stats.accuracy())
print("N validation words: {}".format(valid_stats._n_words))
return 0
print('\nStart training...')
print(' * number of epochs: %d, starting from Epoch %d' %
(opt.epochs + 1 - opt.start_epoch, opt.start_epoch))
print(' * batch size: %d' % opt.batch_size)
random_state = None
for epoch in range(opt.start_epoch, opt.epochs + 1):
print('')
# 1. Train for one epoch on the training set.
train_iter = make_dataset_iter(lazily_load_dataset("train"),
fields, opt, random_state=random_state)
train_stats = trainer.train(train_iter, epoch, report_func)
random_state = train_iter.random_state
print('Train exp(elbo): %g' % train_stats.expelbo())
print('Train perplexity: %g' % train_stats.ppl())
print('Train xent: %g' % train_stats.xent())
print('Train kl: %g' % train_stats.kl())
print('Train accuracy: %g' % train_stats.accuracy())
# 2. Validate on the validation set.
valid_iter = make_dataset_iter(lazily_load_dataset("valid"),
fields, opt,
is_train=False)
if model.mode == 'sample' or model.mode == 'wsram':
val_mode = 'enum'
else:
val_mode = model.mode
valid_stats = trainer.validate(valid_iter, val_mode)
print('Validation exp(elbo): %g' % valid_stats.expelbo())
print('Validation perplexity: %g' % valid_stats.ppl())
print('Validation xent: %g' % valid_stats.xent())
print('Validation kl: %g' % valid_stats.kl())
print('Validation accuracy: %g' % valid_stats.accuracy())
# 3. Log to remote server.
if opt.exp_host:
train_stats.log("train", experiment, optim.lr)
valid_stats.log("valid", experiment, optim.lr)
if opt.tensorboard:
train_stats.log_tensorboard("train", writer, optim.lr, epoch)
train_stats.log_tensorboard("valid", writer, optim.lr, epoch)
# 4. Update the learning rate
trainer.epoch_step(valid_stats.expelbo(), epoch)
# 5. Drop a checkpoint if needed.
if epoch >= opt.start_checkpoint_at:
trainer.drop_checkpoint(model_opt, epoch, fields, valid_stats)
def check_save_model_path():
save_model_path = os.path.abspath(opt.save_model)
model_dirname = os.path.dirname(save_model_path)
if not os.path.exists(model_dirname):
os.makedirs(model_dirname)
def tally_parameters(model):
n_params = sum([p.nelement() for p in model.parameters()])
print('* number of parameters: %d' % n_params)
enc = 0
dec = 0
for name, param in model.named_parameters():
if 'encoder' in name:
enc += param.nelement()
elif 'decoder' or 'generator' in name:
dec += param.nelement()
print('encoder: ', enc)
print('decoder: ', dec)
def lazily_load_dataset(corpus_type):
"""
Dataset generator. Don't do extra stuff here, like printing,
because they will be postponed to the first loading time.
Args:
corpus_type: 'train' or 'valid'
Returns:
A list of dataset, the dataset(s) are lazily loaded.
"""
assert corpus_type in ["train", "valid"]
def lazy_dataset_loader(pt_file, corpus_type):
dataset = torch.load(pt_file)
print('Loading %s dataset from %s, number of examples: %d' %
(corpus_type, pt_file, len(dataset)))
return dataset
# Sort the glob output by file name (by increasing indexes).
pts = sorted(glob.glob(opt.data + '.' + corpus_type + '.[0-9]*.pt'))
if pts:
for pt in pts:
yield lazy_dataset_loader(pt, corpus_type)
else:
# Only one onmt.io.*Dataset, simple!
pt = opt.data + '.' + corpus_type + '.pt'
yield lazy_dataset_loader(pt, corpus_type)
def load_fields(dataset, data_type, checkpoint):
if checkpoint is not None:
print('Loading vocab from checkpoint at %s.' % opt.train_from)
fields = onmt.io.load_fields_from_vocab(
checkpoint['vocab'], data_type)
else:
fields = onmt.io.load_fields_from_vocab(
torch.load(opt.data + '.vocab.pt'), data_type)
fields = dict([(k, f) for (k, f) in fields.items()
if k in dataset.examples[0].__dict__])
if data_type == 'text':
print(' * vocabulary size. source = %d; target = %d' %
(len(fields['src'].vocab), len(fields['tgt'].vocab)))
else:
print(' * vocabulary size. target = %d' %
(len(fields['tgt'].vocab)))
return fields
def collect_report_features(fields):
src_features = onmt.io.collect_features(fields, side='src')
tgt_features = onmt.io.collect_features(fields, side='tgt')
for j, feat in enumerate(src_features):
print(' * src feature %d size = %d' % (j, len(fields[feat].vocab)))
for j, feat in enumerate(tgt_features):
print(' * tgt feature %d size = %d' % (j, len(fields[feat].vocab)))
def build_model(model_opt, opt, fields, checkpoint):
print('Building model...')
model = onmt.ModelConstructor.make_base_model(model_opt, fields,
use_gpu(opt), checkpoint)
if len(opt.gpuid) > 1:
print('Multi gpu training: ', opt.gpuid)
model = nn.DataParallel(model, device_ids=opt.gpuid, dim=1)
print(model)
return model
def build_optim(model, checkpoint):
saved_optimizer_state_dict = None
if opt.train_from:
print('Loading optimizer from checkpoint.')
optim = checkpoint['optim']
# We need to save a copy of optim.optimizer.state_dict() for setting
# the, optimizer state later on in Stage 2 in this method, since
# the method optim.set_parameters(model.parameters()) will overwrite
# optim.optimizer, and with ith the values stored in
# optim.optimizer.state_dict()
saved_optimizer_state_dict = optim.optimizer.state_dict()
else:
print('Making optimizer for training.')
optim = onmt.Optim(
opt.optim, opt.learning_rate, opt.max_grad_norm,
lr_decay=opt.learning_rate_decay,
start_decay_at=opt.start_decay_at,
beta1=opt.adam_beta1,
beta2=opt.adam_beta2,
eps=opt.adam_eps,
adagrad_accum=opt.adagrad_accumulator_init,
decay_method=opt.decay_method,
warmup_steps=opt.warmup_steps,
model_size=None)
# Stage 1:
# Essentially optim.set_parameters (re-)creates and optimizer using
# model.paramters() as parameters that will be stored in the
# optim.optimizer.param_groups field of the torch optimizer class.
# Importantly, this method does not yet load the optimizer state, as
# essentially it builds a new optimizer with empty optimizer state and
# parameters from the model.
optim.set_parameters(model.named_parameters())
print(
"Stage 1: Keys after executing optim.set_parameters" +
"(model.parameters())")
show_optimizer_state(optim)
if opt.train_from:
# Stage 2: In this stage, which is only performed when loading an
# optimizer from a checkpoint, we load the saved_optimizer_state_dict
# into the re-created optimizer, to set the optim.optimizer.state
# field, which was previously empty. For this, we use the optimizer
# state saved in the "saved_optimizer_state_dict" variable for
# this purpose.
# See also: https://github.com/pytorch/pytorch/issues/2830
optim.optimizer.load_state_dict(saved_optimizer_state_dict)
# Convert back the state values to cuda type if applicable
if use_gpu(opt):
for state in optim.optimizer.state.values():
for k, v in state.items():
if torch.is_tensor(v):
state[k] = v.cuda()
print(
"Stage 2: Keys after executing optim.optimizer.load_state_dict" +
"(saved_optimizer_state_dict)")
show_optimizer_state(optim)
# We want to make sure that indeed we have a non-empty optimizer state
# when we loaded an existing model. This should be at least the case
# for Adam, which saves "exp_avg" and "exp_avg_sq" state
# (Exponential moving average of gradient and squared gradient values)
if (optim.method == 'adam') and (len(optim.optimizer.state) < 1):
raise RuntimeError(
"Error: loaded Adam optimizer from existing model" +
" but optimizer state is empty")
return optim
# Debugging method for showing the optimizer state
def show_optimizer_state(optim):
print("optim.optimizer.state_dict()['state'] keys: ")
for key in optim.optimizer.state_dict()['state'].keys():
print("optim.optimizer.state_dict()['state'] key: " + str(key))
print("optim.optimizer.state_dict()['param_groups'] elements: ")
for element in optim.optimizer.state_dict()['param_groups']:
print("optim.optimizer.state_dict()['param_groups'] element: " + str(
element))
def main():
# Load checkpoint if we resume from a previous training.
if opt.train_from:
print('Loading checkpoint from %s' % opt.train_from)
checkpoint = torch.load(opt.train_from,
map_location=lambda storage, loc: storage)
model_opt = checkpoint['opt']
# I don't like reassigning attributes of opt: it's not clear.
opt.start_epoch = checkpoint['epoch'] + 1
elif opt.init_with:
print('Loading checkpoint from %s' % opt.init_with)
checkpoint = torch.load(opt.init_with,
map_location=lambda storage, loc: storage)
model_opt = opt
elif opt.eval_with:
print('Loading checkpoint from %s' % opt.eval_with)
checkpoint = torch.load(opt.eval_with,
map_location=lambda storage, loc: storage)
model_opt = checkpoint["opt"]
model_opt.eval_only = 1
else:
checkpoint = None
model_opt = opt
for k, v in vars(model_opt).items():
print("{}: {}".format(k, v))
# Peek the fisrt dataset to determine the data_type.
# (All datasets have the same data_type).
first_dataset = next(lazily_load_dataset("train"))
data_type = first_dataset.data_type
# Load fields generated from preprocess phase.
fields = load_fields(first_dataset, data_type, checkpoint)
# Report src/tgt features.
collect_report_features(fields)
# Build model.
model = build_model(model_opt, opt, fields, checkpoint)
# Remove bridge for tally params
#model.encoder.bridge = None
tally_parameters(model)
check_save_model_path()
# Build optimizer.
optim = build_optim(model, checkpoint)
# Do training.
train_model(model, fields, optim, data_type, model_opt)
# If using tensorboard for logging, close the writer after training.
if opt.tensorboard:
writer.close()
if __name__ == "__main__":
main()