forked from MultiPath/attention_is_all_you_need
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seq2seq.py
303 lines (253 loc) · 11.3 KB
/
seq2seq.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
# encoding: utf-8
import argparse
import json
import os.path
from nltk.translate import bleu_score
import numpy
import six
import chainer
from chainer import cuda
from chainer.dataset import convert
from chainer import reporter
from chainer import training
from chainer.training import extensions
import europal
import net
from subfuncs import VaswaniRule
def seq2seq_pad_concat_convert(xy_batch, device, eos_id=0):
"""
Args:
xy_batch (list of tuple of two numpy.ndarray-s or cupy.ndarray-s):
xy_batch[i][0] is an array
of token ids of i-th input sentence in a minibatch.
xy_batch[i][1] is an array
of token ids of i-th target sentence in a minibatch.
The shape of each array is `(sentence length, )`.
device (int or None): Device ID to which an array is sent. If it is
negative value, an array is sent to CPU. If it is positive, an
array is sent to GPU with the given ID. If it is ``None``, an
array is left in the original device.
Returns:
Tuple of Converted array.
(input_sent_batch_array, target_sent_batch_input_array,
target_sent_batch_output_array).
The shape of each array is `(batchsize, max_sentence_length)`.
All sentences are padded with -1 to reach max_sentence_length.
"""
x_seqs, y_seqs = zip(*xy_batch)
x_block = convert.concat_examples(x_seqs, device, padding=-1)
y_block = convert.concat_examples(y_seqs, device, padding=-1)
xp = cuda.get_array_module(x_block)
# add eos
x_block = xp.pad(x_block, ((0, 0), (0, 1)),
'constant', constant_values=-1)
for i_batch, seq in enumerate(x_seqs):
x_block[i_batch, len(seq)] = eos_id
y_out_block = xp.pad(y_block, ((0, 0), (0, 1)),
'constant', constant_values=-1)
for i_batch, seq in enumerate(y_seqs):
y_out_block[i_batch, len(seq)] = eos_id
y_in_block = xp.pad(y_block, ((0, 0), (1, 0)),
'constant', constant_values=eos_id)
return (x_block, y_in_block, y_out_block)
def source_pad_concat_convert(x_seqs, device, eos_id=0):
x_block = convert.concat_examples(x_seqs, device, padding=-1)
xp = cuda.get_array_module(x_block)
# add eos
x_block = xp.pad(x_block, ((0, 0), (0, 1)),
'constant', constant_values=-1)
for i_batch, seq in enumerate(x_seqs):
x_block[i_batch, len(seq)] = eos_id
return x_block
class CalculateBleu(chainer.training.Extension):
trigger = 1, 'epoch'
priority = chainer.training.PRIORITY_WRITER
def __init__(
self, model, test_data, key, batch=50, device=-1, max_length=50):
self.model = model
self.test_data = test_data
self.key = key
self.batch = batch
self.device = device
self.max_length = max_length
def __call__(self, trainer):
print('## Calculate BLEU')
with chainer.no_backprop_mode():
with chainer.using_config('train', False):
references = []
hypotheses = []
for i in range(0, len(self.test_data), self.batch):
sources, targets = zip(*self.test_data[i:i + self.batch])
references.extend([[t.tolist()] for t in targets])
sources = [
chainer.dataset.to_device(self.device, x) for x in sources]
ys = [y.tolist()
for y in self.model.translate(sources, self.max_length)]
hypotheses.extend(ys)
bleu = bleu_score.corpus_bleu(
references, hypotheses,
smoothing_function=bleu_score.SmoothingFunction().method1) * 100
print('BLEU:', bleu)
reporter.report({self.key: bleu})
def main():
parser = argparse.ArgumentParser(
description='Chainer example: convolutional seq2seq')
parser.add_argument('--batchsize', '-b', type=int, default=32,
help='Number of images in each mini-batch')
parser.add_argument('--epoch', '-e', type=int, default=100,
help='Number of sweeps over the dataset to train')
parser.add_argument('--gpu', '-g', type=int, default=-1,
help='GPU ID (negative value indicates CPU)')
parser.add_argument('--unit', '-u', type=int, default=512,
help='Number of units')
parser.add_argument('--layer', '-l', type=int, default=6,
help='Number of layers')
parser.add_argument('--input', '-i', type=str, default='./',
help='Input directory')
parser.add_argument('--source', '-s', type=str,
default='europarl-v7.fr-en.en',
help='Filename of train data for source language')
parser.add_argument('--target', '-t', type=str,
default='europarl-v7.fr-en.fr',
help='Filename of train data for target language')
parser.add_argument('--source-valid', '-svalid', type=str,
default='dev/newstest2013.en',
help='Filename of validation data for source language')
parser.add_argument('--target-valid', '-tvalid', type=str,
default='dev/newstest2013.fr',
help='Filename of validation data for target language')
parser.add_argument('--out', '-o', default='result',
help='Directory to output the result')
parser.add_argument('--source-vocab', type=int, default=40000,
help='Vocabulary size of source language')
parser.add_argument('--target-vocab', type=int, default=40000,
help='Vocabulary size of target language')
args = parser.parse_args()
print(json.dumps(args.__dict__, indent=4))
# Check file
en_path = os.path.join(args.input, args.source)
source_vocab = ['<eos>', '<unk>'] + \
europal.count_words(en_path, args.source_vocab)
source_data = europal.make_dataset(en_path, source_vocab)
fr_path = os.path.join(args.input, args.target)
target_vocab = ['<eos>', '<unk>'] + \
europal.count_words(fr_path, args.target_vocab)
target_data = europal.make_dataset(fr_path, target_vocab)
assert len(source_data) == len(target_data)
print('Original training data size: %d' % len(source_data))
train_data = [(s, t)
for s, t in six.moves.zip(source_data, target_data)
if 0 < len(s) < 50 and 0 < len(t) < 50]
print('Filtered training data size: %d' % len(train_data))
en_path = os.path.join(args.input, args.source_valid)
source_data = europal.make_dataset(en_path, source_vocab)
fr_path = os.path.join(args.input, args.target_valid)
target_data = europal.make_dataset(fr_path, target_vocab)
assert len(source_data) == len(target_data)
test_data = [(s, t) for s, t in six.moves.zip(source_data, target_data)
if 0 < len(s) and 0 < len(t)]
source_ids = {word: index for index, word in enumerate(source_vocab)}
target_ids = {word: index for index, word in enumerate(target_vocab)}
target_words = {i: w for w, i in target_ids.items()}
source_words = {i: w for w, i in source_ids.items()}
# Define Model
model = net.Seq2seq(
args.layer, len(source_ids), len(target_ids), args.unit)
if args.gpu >= 0:
chainer.cuda.get_device(args.gpu).use()
model.to_gpu(args.gpu)
# Setup Optimizer
optimizer = chainer.optimizers.Adam(
alpha=args.unit ** (-0.5),
beta1=0.9,
beta2=0.98,
eps=1e-9
)
optimizer.setup(model)
# Setup Trainer
train_iter = chainer.iterators.SerialIterator(train_data, args.batchsize)
test_iter = chainer.iterators.SerialIterator(test_data, args.batchsize,
repeat=False, shuffle=False)
iter_per_epoch = len(train_data) // args.batchsize
print('Number of iter/epoch =', iter_per_epoch)
updater = training.StandardUpdater(
train_iter, optimizer,
converter=seq2seq_pad_concat_convert, device=args.gpu)
trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=args.out)
# TODO lengthen this interval
# If you want to change a logging interval, change this number
log_trigger = (min(100, iter_per_epoch // 2), 'iteration')
def floor_step(trigger):
floored = trigger[0] - trigger[0] % log_trigger[0]
if floored <= 0:
floored = trigger[0]
return (floored, trigger[1])
# Validation every half epoch
eval_trigger = floor_step((iter_per_epoch // 2, 'iteration'))
record_trigger = training.triggers.MinValueTrigger(
'val/main/perp', eval_trigger)
evaluator = extensions.Evaluator(
test_iter, model,
converter=seq2seq_pad_concat_convert,
device=args.gpu)
evaluator.default_name = 'val'
trainer.extend(evaluator, trigger=eval_trigger)
# Use Vaswan's magical rule of learning rate (Eq. 3 in the paper)
trainer.extend(VaswaniRule('alpha', d=args.unit, warmup_steps=4000),
trigger=(1, 'iteration'))
trainer.extend(extensions.observe_lr(observation_key='alpha'),
trigger=(1, 'iteration'))
# Only if a model gets best validation score,
# save the model
trainer.extend(extensions.snapshot_object(
model, 'model_iter_{.updater.iteration}.npz'),
trigger=record_trigger)
def translate_one(source, target):
words = europal.split_sentence(source)
print('# source : ' + ' '.join(words))
x = model.xp.array(
[source_ids.get(w, 1) for w in words], 'i')
ys = model.translate([x])[0]
words = [target_words[y] for y in ys]
print('# result : ' + ' '.join(words))
print('# expect : ' + target)
@chainer.training.make_extension(trigger=(200, 'iteration'))
def translate(trainer):
translate_one(
'Who are we ?',
'Qui sommes-nous?')
translate_one(
'And it often costs over a hundred dollars ' +
'to obtain the required identity card .',
'Or, il en coûte souvent plus de cent dollars ' +
'pour obtenir la carte d\'identité requise.')
source, target = test_data[numpy.random.choice(len(test_data))]
source = ' '.join([source_words[i] for i in source])
target = ' '.join([target_words[i] for i in target])
translate_one(source, target)
# Gereneration Test
trainer.extend(
translate,
trigger=(min(200, iter_per_epoch), 'iteration'))
# Calculate BLEU every half epoch
trainer.extend(
CalculateBleu(
model, test_data, 'val/main/bleu',
device=args.gpu, batch=args.batchsize // 4),
trigger=floor_step((iter_per_epoch // 2, 'iteration')))
# Log
trainer.extend(extensions.LogReport(trigger=log_trigger),
trigger=log_trigger)
trainer.extend(extensions.PrintReport(
['epoch', 'iteration',
'main/loss', 'val/main/loss',
'main/perp', 'val/main/perp',
'main/acc', 'val/main/acc',
'val/main/bleu',
'alpha',
'elapsed_time']),
trigger=log_trigger)
print('start training')
trainer.run()
if __name__ == '__main__':
main()