forked from OpenNMT/OpenNMT-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
umt.py
367 lines (289 loc) · 12.2 KB
/
umt.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import division
import random, argparse, os, sys
import torch
import torch.nn as nn
from torch import cuda
from torch.autograd import Variable
import onmt
import onmt.io
import onmt.Models
import onmt.ModelConstructor
import onmt.modules
from onmt.Utils import use_gpu
import opts
import gc
from preprocess import *
def parse_args():
parser = argparse.ArgumentParser(
description='umt.py',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
opts.add_md_help_argument(parser)
opts.model_opts(parser)
opts.preprocess_opts(parser)
opts.train_opts(parser)
opt = parser.parse_args()
torch.manual_seed(opt.seed)
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 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)
return opt
def build_model(model_opt, opt, lang_src, lang_tgt, checkpoint):
print('Building model...')
model = onmt.ModelConstructor.make_base_model(model_opt, lang_src, lang_tgt,
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 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 check_save_model_path(opt):
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 build_optim(opt, 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,
sgd_momentum=opt.sgd_momentum,
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.parameters())
return optim
def make_loss_compute(model, tgt_vocab, opt):
"""
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)
else:
compute = onmt.Loss.NMTLossCompute(
model.generator, tgt_vocab,
label_smoothing=opt.label_smoothing)
if use_gpu(opt):
compute.cuda()
return compute
def train_model(model, optim, model_opt, opt, train_src_seqs, train_tgt_seqs, \
valid_src_seqs, valid_tgt_seqs, vocab_src, vocab_tgt):
train_loss = make_loss_compute(model, vocab_tgt, opt)
valid_loss = make_loss_compute(model, vocab_tgt, opt)
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,
norm_method, grad_accum_count)
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)
pad_token = vocab_src.stoi['<pad>']
for epoch in range(opt.start_epoch, opt.epochs + 1):
# 1. Train for one epoch on the training set.
train_iter = DatasetIterator(train_src_seqs, train_tgt_seqs, pad_token, opt)
train_stats = trainer.train(train_iter, epoch, opt, report_func)
print('Train perplexity: %g' % train_stats.ppl())
print('Train accuracy: %g' % train_stats.accuracy())
# 2. Validate on the validation set.
valid_iter = DatasetIterator(valid_src_seqs, valid_tgt_seqs, pad_token, opt, \
is_inference=True)
valid_stats = trainer.validate(valid_iter)
print('Validation perplexity: %g' % valid_stats.ppl())
print('Validation accuracy: %g' % valid_stats.accuracy())
# 3. Update the learning rate
trainer.epoch_step(valid_stats.ppl(), epoch)
# 4. Drop a checkpoint if needed.
if epoch >= opt.start_checkpoint_at:
trainer.drop_checkpoint(model_opt, vocab_src, vocab_tgt, epoch, valid_stats)
class Batch(object):
def __init__(self, src, tgt, src_lens, tgt_lens, indices=None, src_maps=None):
self.src = src
self.tgt = tgt
self.src_lengths = src_lens
self.tgt_lengths = tgt_lens
self.batch_size = len(src_lens)
self.indices = indices
self.src_maps = src_maps
class DatasetIterator(object):
def __init__(self, src_seqs, tgt_seqs, pad_token, opt, src_maps=None, is_inference=False, is_test=False):
self.src_seqs = src_seqs
self.tgt_seqs = tgt_seqs
self.src_maps = src_maps
self.pad_token = pad_token
self.is_cuda = False if is_test else opt.gpuid
self.is_inference = is_inference
self.is_test = is_test
if is_test:
self.batch_size = opt.batch_size
else:
self.batch_size = opt.valid_batch_size if is_inference else opt.batch_size
self.num_batches = len(self.src_seqs) // self.batch_size
if len(self.src_seqs) % self.batch_size != 0:
self.num_batches += 1
def __iter__(self):
order = range(self.num_batches)
if not self.is_test: random.shuffle(order)
for i in order:
start = i*self.batch_size
ln = self.batch_size
if start+ln>len(self.src_seqs):
ln = len(self.src_seqs)-start
src = self.src_seqs[start:start+ln]
tgt = self.tgt_seqs[start:start+ln]
src_lens = [len(seq) for seq in src]
tgt_lens = [len(seq) for seq in tgt]
if self.is_test:
indices = range(start, start+ln)
src_maps = self.src_maps[start:start+ln]
sorted_materials = sorted(zip(src, tgt, src_lens, tgt_lens, indices, src_maps),
key=lambda p: p[2], reverse=True)
src, tgt, src_lens, tgt_lens, indices, src_maps = zip(*sorted_materials)
else:
sorted_materials = sorted(zip(src, tgt, src_lens, tgt_lens),
key=lambda p: p[2], reverse=True)
src, tgt, src_lens, tgt_lens = zip(*sorted_materials)
max_src_len = src_lens[0]
max_tgt_len = max(tgt_lens)
src_padded = [pad_seq(seq, max_src_len, self.pad_token) for seq in src]
tgt_padded = [pad_seq(seq, max_tgt_len, self.pad_token) for seq in tgt]
src_var = Variable(torch.LongTensor(src_padded).transpose(0,1), \
volatile=self.is_inference)
tgt_var = Variable(torch.LongTensor(tgt_padded).transpose(0,1), \
volatile=self.is_inference)
# if not self.is_test:
# tgt_var = Variable(tgt_var, volatile=self.is_inference)
# print(src_var.size())
src_lens = torch.LongTensor(src_lens)
tgt_lens = torch.LongTensor(tgt_lens)
if self.is_cuda:
src_var = src_var.cuda()
tgt_var = tgt_var.cuda()
src_lens = src_lens.cuda()
tgt_lens = tgt_lens.cuda()
if self.is_test:
indices = Variable(torch.LongTensor(indices), volatile=self.is_inference)
src_maps = self.make_src(src_maps)
if self.is_cuda:
indices = indices.cuda()
src_maps = src_maps.cuda()
yield Batch(src_var, tgt_var, src_lens, tgt_lens, indices, src_maps)
else:
yield Batch(src_var, tgt_var, src_lens, tgt_lens)
def __len__(self):
return self.num_batches
def pad_seq(self, seq, max_length, PAD_token):
seq += [PAD_token for i in range(max_length - len(seq))]
return seq
# From TextDataset.py
def make_src(self, data):
src_size = max([t.size(0) for t in data])
src_vocab_size = max([t.max() for t in data]) + 1
alignment = torch.zeros(src_size, len(data), src_vocab_size)
for i, sent in enumerate(data):
for j, t in enumerate(sent):
alignment[j, i, t] = 1
return alignment
def report_func(epoch, batch, num_batches,
start_time, lr, report_stats, opt):
"""
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)
report_stats = onmt.Statistics()
return report_stats
def main():
opt = parse_args()
print(opt)
train_src_seqs, train_tgt_seqs, valid_src_seqs, valid_tgt_seqs, \
src_vocab, tgt_vocab = prepare_train_data(opt)
# 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
# Build model.
model = build_model(model_opt, opt, src_vocab, tgt_vocab, checkpoint)
tally_parameters(model)
check_save_model_path(opt)
# # Build optimizer.
optim = build_optim(opt, model, checkpoint)
# # Do training.
train_model(model, optim, model_opt, opt, \
train_src_seqs, train_tgt_seqs, \
valid_src_seqs, valid_tgt_seqs, \
src_vocab, tgt_vocab)
if __name__=="__main__":
main()