-
Notifications
You must be signed in to change notification settings - Fork 46
/
train.py
executable file
·454 lines (365 loc) · 15.5 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
#!/usr/bin/env python
from __future__ import division
import argparse
import glob
import os
import sys
import random
import torch
import torch.nn as nn
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 opts
parser = argparse.ArgumentParser(
description='train.py',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
# opts.py
opts.add_md_help_argument(parser)
opts.model_opts(parser)
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.brnn2 = (opt.encoder_type2 == "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, comment="Onmt")
def report_func(epoch, batch, num_batches,
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.
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, epoch * num_batches + batch)
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):
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.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:
for batch in self.cur_iter:
yield batch
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 sort_minibatch_key(self, ex):
""" Sort using length of source sentences and length of target sentence """
#Needed for packed sequence
return len(ex.src1), len(ex.tgt1)
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_key=self.sort_minibatch_key,
sort=False, sort_within_batch=True,
repeat=False)
def make_dataset_iter(datasets, fields, opt, is_train=True):
"""
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":
global max_src_in_batch, max_tgt_in_batch
def batch_size_fn(new, count, sofar):
global max_src_in_batch, max_tgt_in_batch
if count == 1:
max_src_in_batch = 0
max_tgt_in_batch = 0
max_src_in_batch = max(max_src_in_batch, len(new.src) + 2)
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 = opt.gpuid[0] if opt.gpuid else -1
return DatasetLazyIter(datasets, fields, batch_size, batch_size_fn,
device, is_train)
def make_loss_compute(model, tgt_vocab, opt, stage1=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 not stage1:
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, decoder_type=opt.decoder_type1)
if use_gpu(opt):
compute.cuda()
return compute
def train_model(model, model2, fields, optim, optim2, data_type, model_opt):
train_loss = make_loss_compute(model, fields["tgt1"].vocab, opt, stage1=True)
valid_loss = make_loss_compute(model, fields["tgt1"].vocab, opt, stage1=True)
train_loss2 = make_loss_compute(model2, fields["tgt2"].vocab, opt, stage1=False)
valid_loss2 = make_loss_compute(model2, fields["tgt2"].vocab, opt, stage1=False)
trunc_size = opt.truncated_decoder # Badly named...
shard_size = opt.max_generator_batches
norm_method = opt.normalization
grad_accum_count = opt.accum_count
cuda = False
if opt.gpuid:
cuda = True
trainer = onmt.Trainer(model, model2, train_loss, valid_loss, train_loss2, valid_loss2, optim, optim2,
trunc_size, shard_size, data_type,
norm_method, grad_accum_count, cuda)
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)
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)
train_stats, train_stats2 = trainer.train(train_iter, epoch, report_func)
print('Train perplexity: %g' % train_stats.ppl())
print('Train accuracy: %g' % train_stats.accuracy())
print('Train perplexity2: %g' % train_stats2.ppl())
print('Train accuracy2: %g' % train_stats2.accuracy())
# 2. Validate on the validation set.
valid_iter = make_dataset_iter(lazily_load_dataset("valid"),
fields, opt,
is_train=False)
valid_stats, valid_stats2 = trainer.validate(valid_iter)
print('Validation perplexity: %g' % valid_stats.ppl())
print('Validation accuracy: %g' % valid_stats.accuracy())
print('Validation perplexity2: %g' % valid_stats2.ppl())
print('Validation accuracy2: %g' % valid_stats2.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.ppl(), valid_stats2.ppl(), epoch)
# 5. Drop a checkpoint if needed.
if epoch >= opt.start_checkpoint_at:
trainer.drop_checkpoint(model_opt, epoch, fields, valid_stats, valid_stats2)
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' or data_type == 'box':
print(' * vocabulary size. source1 = %d; target1 = %d, source2 = %d; target2 = %d' %
(len(fields['src1'].vocab), len(fields['tgt1'].vocab), len(fields['src2'].vocab), len(fields['tgt2'].vocab)))
else:
assert False
print(' * vocabulary size. target = %d' %
(len(fields['tgt'].vocab)))
return fields
def collect_report_features(fields):
src_features = onmt.io.collect_features(fields, side='src1')
tgt_features = onmt.io.collect_features(fields, side='tgt1')
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...')
model1 = onmt.ModelConstructor.make_base_model(model_opt, fields,
use_gpu(opt), checkpoint, stage1=True)
model2 = onmt.ModelConstructor.make_base_model(model_opt, fields,
use_gpu(opt), checkpoint, stage1=False)
if len(opt.gpuid) > 1:
print('Multi gpu training: ', opt.gpuid)
model1 = nn.DataParallel(model1, device_ids=opt.gpuid, dim=1)
model2 = nn.DataParallel(model2, device_ids=opt.gpuid, dim=1)
print(model1)
print(model2)
return model1, model2
def build_optim(model, checkpoint):
if opt.train_from:
print('Loading optimizer from checkpoint.')
optim = checkpoint['optim']
optim.optimizer.load_state_dict(
checkpoint['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,
adagrad_accum=opt.adagrad_accumulator_init,
decay_method=opt.decay_method,
warmup_steps=opt.warmup_steps,
model_size=opt.rnn_size)
optim.set_parameters(model.named_parameters())
return optim
def main():
print('Experiment 22-4.4 using attn_dim of 64')
# 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
else:
checkpoint = None
model_opt = opt
# 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.
model1, model2 = build_model(model_opt, opt, fields, checkpoint)
tally_parameters(model1)
tally_parameters(model2)
check_save_model_path()
# Build optimizer.
optim1 = build_optim(model1, checkpoint)
optim2 = build_optim(model2, checkpoint)
# Do training.
train_model(model1, model2, fields, optim1, optim2, data_type, model_opt)
# If using tensorboard for logging, close the writer after training.
if opt.tensorboard:
writer.close()
if __name__ == "__main__":
main()