forked from tensorflow/nmt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nmt.py
478 lines (420 loc) · 18.2 KB
/
nmt.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
# Copyright 2017 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""TensorFlow NMT model implementation."""
from __future__ import print_function
import argparse
import os
import random
import sys
# import matplotlib.image as mpimg
import numpy as np
import tensorflow as tf
from . import inference
from . import train
from .utils import evaluation_utils
from .utils import misc_utils as utils
from .utils import vocab_utils
utils.check_tensorflow_version()
FLAGS = None
def add_arguments(parser):
"""Build ArgumentParser."""
parser.register("type", "bool", lambda v: v.lower() == "true")
# network
parser.add_argument("--num_units", type=int, default=32, help="Network size.")
parser.add_argument("--num_layers", type=int, default=2,
help="Network depth.")
parser.add_argument("--encoder_type", type=str, default="uni", help="""\
uni | bi | gnmt. For bi, we build num_layers/2 bi-directional layers.For
gnmt, we build 1 bi-directional layer, and (num_layers - 1) uni-
directional layers.\
""")
parser.add_argument("--residual", type="bool", nargs="?", const=True,
default=False,
help="Whether to add residual connections.")
parser.add_argument("--time_major", type="bool", nargs="?", const=True,
default=True,
help="Whether to use time-major mode for dynamic RNN.")
# attention mechanisms
parser.add_argument("--attention", type=str, default="", help="""\
luong | scaled_luong | bahdanau | normed_bahdanau or set to "" for no
attention\
""")
parser.add_argument(
"--attention_architecture",
type=str,
default="standard",
help="""\
standard | gnmt | gnmt_v2.
standard: use top layer to compute attention.
gnmt: GNMT style of computing attention, use previous bottom layer to
compute attention.
gnmt_v2: similar to gnmt, but use current bottom layer to compute
attention.\
""")
parser.add_argument(
"--pass_hidden_state", type="bool", nargs="?", const=True,
default=True,
help="""\
Whether to pass encoder's hidden state to decoder when using an attention
based model.\
""")
# optimizer
parser.add_argument("--optimizer", type=str, default="sgd", help="sgd | adam")
parser.add_argument("--learning_rate", type=float, default=1.0,
help="Learning rate. Adam: 0.001 | 0.0001")
parser.add_argument("--start_decay_step", type=int, default=0,
help="When we start to decay")
parser.add_argument("--decay_steps", type=int, default=10000,
help="How frequent we decay")
parser.add_argument("--decay_factor", type=float, default=0.98,
help="How much we decay.")
parser.add_argument(
"--num_train_steps", type=int, default=12000, help="Num steps to train.")
parser.add_argument("--colocate_gradients_with_ops", type="bool", nargs="?",
const=True,
default=True,
help=("Whether try colocating gradients with "
"corresponding op"))
# data
parser.add_argument("--src", type=str, default=None,
help="Source suffix, e.g., en.")
parser.add_argument("--tgt", type=str, default=None,
help="Target suffix, e.g., de.")
parser.add_argument("--train_prefix", type=str, default=None,
help="Train prefix, expect files with src/tgt suffixes.")
parser.add_argument("--dev_prefix", type=str, default=None,
help="Dev prefix, expect files with src/tgt suffixes.")
parser.add_argument("--test_prefix", type=str, default=None,
help="Test prefix, expect files with src/tgt suffixes.")
parser.add_argument("--out_dir", type=str, default=None,
help="Store log/model files.")
# Vocab
parser.add_argument("--vocab_prefix", type=str, default=None, help="""\
Vocab prefix, expect files with src/tgt suffixes.If None, extract from
train files.\
""")
parser.add_argument("--sos", type=str, default="<s>",
help="Start-of-sentence symbol.")
parser.add_argument("--eos", type=str, default="</s>",
help="End-of-sentence symbol.")
parser.add_argument("--share_vocab", type="bool", nargs="?", const=True,
default=False,
help="""\
Whether to use the source vocab and embeddings for both source and
target.\
""")
# Sequence lengths
parser.add_argument("--src_max_len", type=int, default=50,
help="Max length of src sequences during training.")
parser.add_argument("--tgt_max_len", type=int, default=50,
help="Max length of tgt sequences during training.")
parser.add_argument("--src_max_len_infer", type=int, default=None,
help="Max length of src sequences during inference.")
parser.add_argument("--tgt_max_len_infer", type=int, default=None,
help="""\
Max length of tgt sequences during inference. Also use to restrict the
maximum decoding length.\
""")
# Default settings works well (rarely need to change)
parser.add_argument("--unit_type", type=str, default="lstm",
help="lstm | gru | layer_norm_lstm")
parser.add_argument("--forget_bias", type=float, default=1.0,
help="Forget bias for BasicLSTMCell.")
parser.add_argument("--dropout", type=float, default=0.2,
help="Dropout rate (not keep_prob)")
parser.add_argument("--max_gradient_norm", type=float, default=5.0,
help="Clip gradients to this norm.")
parser.add_argument("--init_weight", type=float, default=0.1,
help="Initial weights from [-this, this].")
parser.add_argument("--source_reverse", type="bool", nargs="?", const=True,
default=False, help="Reverse source sequence.")
parser.add_argument("--batch_size", type=int, default=128, help="Batch size.")
parser.add_argument("--steps_per_stats", type=int, default=100,
help=("How many training steps to do per stats logging."
"Save checkpoint every 10x steps_per_stats"))
parser.add_argument("--max_train", type=int, default=0,
help="Limit on the size of training data (0: no limit).")
parser.add_argument("--num_buckets", type=int, default=5,
help="Put data into similar-length buckets.")
# BPE
parser.add_argument("--bpe_delimiter", type=str, default=None,
help="Set to @@ to activate BPE")
# Misc
parser.add_argument("--num_gpus", type=int, default=1,
help="Number of gpus in each worker.")
parser.add_argument("--log_device_placement", type="bool", nargs="?",
const=True, default=False, help="Debug GPU allocation.")
parser.add_argument("--metrics", type=str, default="bleu",
help=("Comma-separated list of evaluations "
"metrics (bleu,rouge,accuracy)"))
parser.add_argument("--steps_per_external_eval", type=int, default=None,
help="""\
How many training steps to do per external evaluation. Automatically set
based on data if None.\
""")
parser.add_argument("--scope", type=str, default=None,
help="scope to put variables under")
parser.add_argument("--hparams_path", type=str, default=None,
help=("Path to standard hparams json file that overrides"
"hparams values from FLAGS."))
parser.add_argument("--random_seed", type=int, default=None,
help="Random seed (>0, set a specific seed).")
# Inference
parser.add_argument("--ckpt", type=str, default="",
help="Checkpoint file to load a model for inference.")
parser.add_argument("--inference_input_file", type=str, default=None,
help="Set to the text to decode.")
parser.add_argument("--inference_list", type=str, default=None,
help=("A comma-separated list of sentence indices "
"(0-based) to decode."))
parser.add_argument("--infer_batch_size", type=int, default=32,
help="Batch size for inference mode.")
parser.add_argument("--inference_output_file", type=str, default=None,
help="Output file to store decoding results.")
parser.add_argument("--inference_ref_file", type=str, default=None,
help=("""\
Reference file to compute evaluation scores (if provided).\
"""))
parser.add_argument("--beam_width", type=int, default=0,
help=("""\
beam width when using beam search decoder. If 0 (default), use standard
decoder with greedy helper.\
"""))
parser.add_argument("--length_penalty_weight", type=float, default=0.0,
help="Length penalty for beam search.")
# Job info
parser.add_argument("--jobid", type=int, default=0,
help="Task id of the worker.")
parser.add_argument("--num_workers", type=int, default=1,
help="Number of workers (inference only).")
def create_hparams():
"""Create training hparams."""
return tf.contrib.training.HParams(
# Data
src=FLAGS.src,
tgt=FLAGS.tgt,
train_prefix=FLAGS.train_prefix,
dev_prefix=FLAGS.dev_prefix,
test_prefix=FLAGS.test_prefix,
vocab_prefix=FLAGS.vocab_prefix,
out_dir=FLAGS.out_dir,
# Networks
num_units=FLAGS.num_units,
num_layers=FLAGS.num_layers,
dropout=FLAGS.dropout,
unit_type=FLAGS.unit_type,
encoder_type=FLAGS.encoder_type,
residual=FLAGS.residual,
time_major=FLAGS.time_major,
# Attention mechanisms
attention=FLAGS.attention,
attention_architecture=FLAGS.attention_architecture,
pass_hidden_state=FLAGS.pass_hidden_state,
# Train
optimizer=FLAGS.optimizer,
num_train_steps=FLAGS.num_train_steps,
batch_size=FLAGS.batch_size,
init_weight=FLAGS.init_weight,
max_gradient_norm=FLAGS.max_gradient_norm,
learning_rate=FLAGS.learning_rate,
start_decay_step=FLAGS.start_decay_step,
decay_factor=FLAGS.decay_factor,
decay_steps=FLAGS.decay_steps,
colocate_gradients_with_ops=FLAGS.colocate_gradients_with_ops,
# Data constraints
num_buckets=FLAGS.num_buckets,
max_train=FLAGS.max_train,
src_max_len=FLAGS.src_max_len,
tgt_max_len=FLAGS.tgt_max_len,
source_reverse=FLAGS.source_reverse,
# Inference
src_max_len_infer=FLAGS.src_max_len_infer,
tgt_max_len_infer=FLAGS.tgt_max_len_infer,
infer_batch_size=FLAGS.infer_batch_size,
beam_width=FLAGS.beam_width,
length_penalty_weight=FLAGS.length_penalty_weight,
# Vocab
sos=FLAGS.sos if FLAGS.sos else vocab_utils.SOS,
eos=FLAGS.eos if FLAGS.eos else vocab_utils.EOS,
bpe_delimiter=FLAGS.bpe_delimiter,
# Misc
forget_bias=FLAGS.forget_bias,
num_gpus=FLAGS.num_gpus,
epoch_step=0, # record where we were within an epoch.
steps_per_stats=FLAGS.steps_per_stats,
steps_per_external_eval=FLAGS.steps_per_external_eval,
share_vocab=FLAGS.share_vocab,
metrics=FLAGS.metrics.split(","),
log_device_placement=FLAGS.log_device_placement,
random_seed=FLAGS.random_seed,
)
def extend_hparams(hparams):
"""Extend training hparams."""
# Sanity checks
if hparams.encoder_type == "bi" and hparams.num_layers % 2 != 0:
raise ValueError("For bi, num_layers %d should be even" %
hparams.num_layers)
if (hparams.attention_architecture in ["gnmt"] and
hparams.num_layers < 2):
raise ValueError("For gnmt attention architecture, "
"num_layers %d should be >= 2" % hparams.num_layers)
# Flags
utils.print_out("# hparams:")
utils.print_out(" src=%s" % hparams.src)
utils.print_out(" tgt=%s" % hparams.tgt)
utils.print_out(" train_prefix=%s" % hparams.train_prefix)
utils.print_out(" dev_prefix=%s" % hparams.dev_prefix)
utils.print_out(" test_prefix=%s" % hparams.test_prefix)
utils.print_out(" out_dir=%s" % hparams.out_dir)
# Set num_residual_layers
if hparams.residual and hparams.num_layers > 1:
if hparams.encoder_type == "gnmt":
# The first unidirectional layer (after the bi-directional layer) in
# the GNMT encoder can't have residual connection due to the input is
# the concatenation of fw_cell and bw_cell's outputs.
num_residual_layers = hparams.num_layers - 2
else:
num_residual_layers = hparams.num_layers - 1
else:
num_residual_layers = 0
hparams.add_hparam("num_residual_layers", num_residual_layers)
## Vocab
# Get vocab file names first
if hparams.vocab_prefix:
src_vocab_file = hparams.vocab_prefix + "." + hparams.src
tgt_vocab_file = hparams.vocab_prefix + "." + hparams.tgt
else:
raise ValueError("hparams.vocab_prefix must be provided.")
# Source vocab
src_vocab_size, src_vocab_file = vocab_utils.check_vocab(
src_vocab_file,
hparams.out_dir,
sos=hparams.sos,
eos=hparams.eos,
unk=vocab_utils.UNK)
# Target vocab
if hparams.share_vocab:
utils.print_out(" using source vocab for target")
tgt_vocab_file = src_vocab_file
tgt_vocab_size = src_vocab_size
else:
tgt_vocab_size, tgt_vocab_file = vocab_utils.check_vocab(
tgt_vocab_file,
hparams.out_dir,
sos=hparams.sos,
eos=hparams.eos,
unk=vocab_utils.UNK)
hparams.add_hparam("src_vocab_size", src_vocab_size)
hparams.add_hparam("tgt_vocab_size", tgt_vocab_size)
hparams.add_hparam("src_vocab_file", src_vocab_file)
hparams.add_hparam("tgt_vocab_file", tgt_vocab_file)
# Check out_dir
if not tf.gfile.Exists(hparams.out_dir):
utils.print_out("# Creating output directory %s ..." % hparams.out_dir)
tf.gfile.MakeDirs(hparams.out_dir)
# Evaluation
for metric in hparams.metrics:
hparams.add_hparam("best_" + metric, 0) # larger is better
best_metric_dir = os.path.join(hparams.out_dir, "best_" + metric)
hparams.add_hparam("best_" + metric + "_dir", best_metric_dir)
tf.gfile.MakeDirs(best_metric_dir)
return hparams
def ensure_compatible_hparams(hparams, default_hparams):
"""Make sure the loaded hparams is compatible with new changes."""
default_hparams = utils.maybe_parse_standard_hparams(
default_hparams, FLAGS.hparams_path)
# For compatible reason, if there are new fields in default_hparams,
# we add them to the current hparams
default_config = default_hparams.values()
config = hparams.values()
for key in default_config:
if key not in config:
hparams.add_hparam(key, default_config[key])
# Make sure that the loaded model has latest values for the below keys
updated_keys = [
"out_dir", "num_gpus", "test_prefix", "beam_width",
"length_penalty_weight", "num_train_steps"
]
for key in updated_keys:
if key in default_config and getattr(hparams, key) != default_config[key]:
utils.print_out("# Updating hparams.%s: %s -> %s" %
(key, str(getattr(hparams, key)), str(default_config[key])))
setattr(hparams, key, default_config[key])
return hparams
def create_or_load_hparams(out_dir, default_hparams):
"""Create hparams or load hparams from out_dir."""
hparams = utils.load_hparams(out_dir)
if not hparams:
hparams = default_hparams
hparams = utils.maybe_parse_standard_hparams(
hparams, FLAGS.hparams_path)
hparams = extend_hparams(hparams)
else:
hparams = ensure_compatible_hparams(hparams, default_hparams)
# Save HParams
utils.save_hparams(out_dir, hparams)
for metric in hparams.metrics:
utils.save_hparams(getattr(hparams, "best_" + metric + "_dir"), hparams)
# Print HParams
utils.print_hparams(hparams)
return hparams
def main(unused_argv):
# Job
jobid = FLAGS.jobid
num_workers = FLAGS.num_workers
utils.print_out("# Job id %d" % jobid)
# Random
random_seed = FLAGS.random_seed
if random_seed is not None and random_seed > 0:
utils.print_out("# Set random seed to %d" % random_seed)
random.seed(random_seed + jobid)
np.random.seed(random_seed + jobid)
## Train / Decode
out_dir = FLAGS.out_dir
if not tf.gfile.Exists(out_dir): tf.gfile.MakeDirs(out_dir)
# Load hparams.
default_hparams = create_hparams()
hparams = create_or_load_hparams(out_dir, default_hparams)
if FLAGS.inference_input_file:
# Inference indices
hparams.inference_indices = None
if FLAGS.inference_list:
(hparams.inference_indices) = (
[int(token) for token in FLAGS.inference_list.split(",")])
# Inference
trans_file = FLAGS.inference_output_file
ckpt = FLAGS.ckpt
if not ckpt:
ckpt = tf.train.latest_checkpoint(out_dir)
inference.inference(ckpt, FLAGS.inference_input_file,
trans_file, hparams, num_workers, jobid)
# Evaluation
ref_file = FLAGS.inference_ref_file
if ref_file and tf.gfile.Exists(trans_file):
for metric in hparams.metrics:
score = evaluation_utils.evaluate(
ref_file,
trans_file,
metric,
hparams.bpe_delimiter)
utils.print_out(" %s: %.1f" % (metric, score))
else:
# Train
train.train(hparams)
if __name__ == "__main__":
nmt_parser = argparse.ArgumentParser()
add_arguments(nmt_parser)
FLAGS, unparsed = nmt_parser.parse_known_args()
tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)