-
Notifications
You must be signed in to change notification settings - Fork 0
/
alignmental_trainer.py
executable file
·489 lines (406 loc) · 16.7 KB
/
alignmental_trainer.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
#!/usr/bin/env python
# coding=utf-8
# Copyright 2018 The THUMT Authors
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import argparse
import os
import six
import numpy as np
import tensorflow as tf
import thumt.data.cache as cache
import src_cons_dataset as dataset
import thumt.data.record as record
import thumt.data.vocab as vocabulary
#import thumt.models as models
import src_cons_transformer_train
import thumt.utils.hooks as hooks
import thumt.utils.inference as inference
import thumt.utils.optimize as optimize
import thumt.utils.parallel as parallel
def parse_args(args=None):
parser = argparse.ArgumentParser(
description="Training neural machine translation models",
usage="trainer.py [<args>] [-h | --help]"
)
# input files
parser.add_argument("--input", type=str, nargs=2,
help="Path of source and target corpus")
parser.add_argument("--record", type=str,
help="Path to tf.Record data")
parser.add_argument("--output", type=str, default="train",
help="Path to saved models")
parser.add_argument("--vocabulary", type=str, nargs=2,
help="Path of source and target vocabulary")
parser.add_argument("--validation", type=str,
help="Path of validation file")
parser.add_argument("--references", type=str, nargs="+",
help="Path of reference files")
parser.add_argument("--checkpoint", type=str,
help="Path to pre-trained checkpoint")
# model and configuration
parser.add_argument("--model", type=str, required=True,
help="Name of the model")
parser.add_argument("--parameters", type=str, default="",
help="Additional hyper parameters")
parser.add_argument("--align_layer", type=int, default=5,
help="layer to be guided by alignment")
parser.add_argument("--align_head", type=int, default=1,
help="head to be guided by alignment")
parser.add_argument("--align_loss_model", type=str, default="square-mean",
help="align_loss_model-square-mean or cross-entropy")
return parser.parse_args(args)
def default_parameters():
params = tf.contrib.training.HParams(
#新版的参数
input=["", ""],
output="",
record="",
model="transformer",
vocab=["", ""],
# Default training hyper parameters
num_threads=6,
batch_size=4096,
max_length=256,
length_multiplier=1,
mantissa_bits=2,
warmup_steps=4000,
train_steps=100000,
buffer_size=10000,
constant_batch_size=False,
device_list=[0],
update_cycle=1,
initializer="uniform_unit_scaling",
initializer_gain=1.0,
optimizer="Adam",
adam_beta1=0.9,
adam_beta2=0.999,
adam_epsilon=1e-8,
clip_grad_norm=5.0,
learning_rate=1.0,
learning_rate_decay="linear_warmup_rsqrt_decay",
learning_rate_boundaries=[0],
learning_rate_values=[0.0],
keep_checkpoint_max=20,
keep_top_checkpoint_max=5,
# Validation
eval_steps=2000,
eval_secs=0,
eval_batch_size=32,
top_beams=1,
beam_size=4,
decode_alpha=0.6,
decode_length=50,
validation="",
references=[""],
save_checkpoint_secs=0,
save_checkpoint_steps=1000,
# Setting this to True can save disk spaces, but cannot restore
# training using the saved checkpoint
only_save_trainable=False,
)
return params
def import_params(model_dir, model_name, params):
model_dir = os.path.abspath(model_dir)
p_name = os.path.join(model_dir, "params.json")
m_name = os.path.join(model_dir, model_name + ".json")
if not tf.gfile.Exists(p_name) or not tf.gfile.Exists(m_name):
return params
with tf.gfile.Open(p_name) as fd:
tf.logging.info("Restoring hyper parameters from %s" % p_name)
json_str = fd.readline()
params.parse_json(json_str)
with tf.gfile.Open(m_name) as fd:
tf.logging.info("Restoring model parameters from %s" % m_name)
json_str = fd.readline()
params.parse_json(json_str)
return params
def export_params(output_dir, name, params):
if not tf.gfile.Exists(output_dir):
tf.gfile.MkDir(output_dir)
# Save params as params.json
filename = os.path.join(output_dir, name)
with tf.gfile.Open(filename, "w") as fd:
fd.write(params.to_json())
def collect_params(all_params, params):
collected = tf.contrib.training.HParams()
for k in params.values().iterkeys():
collected.add_hparam(k, getattr(all_params, k))
return collected
def merge_parameters(params1, params2):
params = tf.contrib.training.HParams()
for (k, v) in params1.values().iteritems():
params.add_hparam(k, v)
params_dict = params.values()
for (k, v) in params2.values().iteritems():
if k in params_dict:
# Override
setattr(params, k, v)
else:
params.add_hparam(k, v)
return params
def override_parameters(params, args):
params.model = args.model
params.input = args.input or params.input
params.output = args.output or params.output
params.record = args.record or params.record
params.vocab = args.vocabulary or params.vocab
params.validation = args.validation or params.validation
params.references = args.references or params.references
params.parse(args.parameters)
params.vocabulary = {
"source": vocabulary.load_vocabulary(params.vocab[0]),
"target": vocabulary.load_vocabulary(params.vocab[1])
}
params.vocabulary["source"] = vocabulary.process_vocabulary(
params.vocabulary["source"], params
)
params.vocabulary["target"] = vocabulary.process_vocabulary(
params.vocabulary["target"], params
)
control_symbols = [params.pad, params.bos, params.eos, params.unk]
params.mapping = {
"source": vocabulary.get_control_mapping(
params.vocabulary["source"],
control_symbols
),
"target": vocabulary.get_control_mapping(
params.vocabulary["target"],
control_symbols
)
}
params.align_loss_model = args.align_loss_model or params.align_loss_model
params.align_layer = args.align_layer or params.align_layer
params.align_head = args.align_head or params.align_head
return params
def get_initializer(params):
if params.initializer == "uniform":
max_val = params.initializer_gain
return tf.random_uniform_initializer(-max_val, max_val)
elif params.initializer == "normal":
return tf.random_normal_initializer(0.0, params.initializer_gain)
elif params.initializer == "normal_unit_scaling":
return tf.variance_scaling_initializer(params.initializer_gain,
mode="fan_avg",
distribution="normal")
elif params.initializer == "uniform_unit_scaling":
return tf.variance_scaling_initializer(params.initializer_gain,
mode="fan_avg",
distribution="uniform")
else:
raise ValueError("Unrecognized initializer: %s" % params.initializer)
def get_learning_rate_decay(learning_rate, global_step, params):
if params.learning_rate_decay in ["linear_warmup_rsqrt_decay", "noam"]:
step = tf.to_float(global_step)
warmup_steps = tf.to_float(params.warmup_steps)
multiplier = params.hidden_size ** -0.5
decay = multiplier * tf.minimum((step + 1) * (warmup_steps ** -1.5),
(step + 1) ** -0.5)
return learning_rate * decay
elif params.learning_rate_decay == "piecewise_constant":
return tf.train.piecewise_constant(tf.to_int32(global_step),
params.learning_rate_boundaries,
params.learning_rate_values)
elif params.learning_rate_decay == "none":
return learning_rate
else:
raise ValueError("Unknown learning_rate_decay")
def session_config(params):
optimizer_options = tf.OptimizerOptions(opt_level=tf.OptimizerOptions.L1,
do_function_inlining=True)
graph_options = tf.GraphOptions(optimizer_options=optimizer_options)
config = tf.ConfigProto(allow_soft_placement=True,
graph_options=graph_options)
if params.device_list:
device_str = ",".join([str(i) for i in params.device_list])
config.gpu_options.visible_device_list = device_str
return config
def decode_target_ids(inputs, params):
decoded = []
vocab = params.vocabulary["target"]
for item in inputs:
syms = []
for idx in item:
if isinstance(idx, six.integer_types):
sym = vocab[idx]
else:
sym = idx
if sym == params.eos:
break
if sym == params.pad:
break
syms.append(sym)
decoded.append(syms)
return decoded
def restore_variables(checkpoint):
if not checkpoint:
return tf.no_op("restore_op")
# Load checkpoints
tf.logging.info("Loading %s" % checkpoint)
var_list = tf.train.list_variables(checkpoint)
reader = tf.train.load_checkpoint(checkpoint)
values = {}
for (name, shape) in var_list:
tensor = reader.get_tensor(name)
name = name.split(":")[0]
values[name] = tensor
var_list = tf.trainable_variables()
ops = []
for var in var_list:
name = var.name.split(":")[0]
if name in values:
tf.logging.info("Restore %s" % var.name)
ops.append(tf.assign(var, values[name]))
return tf.group(*ops, name="restore_op")
def main(args):
tf.logging.set_verbosity(tf.logging.INFO)
#model_cls = models.get_model(args.model)
model_cls = src_cons_transformer_train.Transformer
params = default_parameters()
# Import and override parameters
# Priorities (low -> high):
# default -> saved -> command
params = merge_parameters(params, model_cls.get_parameters())
params = import_params(args.output, args.model, params)
override_parameters(params, args)
# Export all parameters and model specific parameters
export_params(params.output, "params.json", params)
export_params(
params.output,
"%s.json" % args.model,
collect_params(params, model_cls.get_parameters())
)
# Build Graph
with tf.Graph().as_default():
# Create global step
global_step = tf.train.get_or_create_global_step()
if not params.record:
# Build input queue
features = dataset.get_training_input_with_alignment(params.input, params)
else:
features = record.get_input_features(
os.path.join(params.record, "*train*"), "train", params
)
features, init_op = cache.cache_features(features,
params.update_cycle)
# Build model
initializer = get_initializer(params)
model = model_cls(params)
# Multi-GPU setting
sharded_losses = parallel.parallel_model(
model.get_training_func(initializer),
features,
params.device_list
)
loss = tf.add_n(sharded_losses) / len(sharded_losses)
# Print parameters
all_weights = {v.name: v for v in tf.trainable_variables()}
total_size = 0
for v_name in sorted(list(all_weights)):
v = all_weights[v_name]
tf.logging.info("%s\tshape %s", v.name[:-2].ljust(80),
str(v.shape).ljust(20))
v_size = np.prod(np.array(v.shape.as_list())).tolist()
total_size += v_size
tf.logging.info("Total trainable variables size: %d", total_size)
learning_rate = get_learning_rate_decay(params.learning_rate,
global_step, params)
learning_rate = tf.convert_to_tensor(learning_rate, dtype=tf.float32)
tf.summary.scalar("learning_rate", learning_rate)
# Create optimizer
if params.optimizer == "Adam":
opt = tf.train.AdamOptimizer(learning_rate,
beta1=params.adam_beta1,
beta2=params.adam_beta2,
epsilon=params.adam_epsilon)
elif params.optimizer == "LazyAdam":
opt = tf.contrib.opt.LazyAdamOptimizer(learning_rate,
beta1=params.adam_beta1,
beta2=params.adam_beta2,
epsilon=params.adam_epsilon)
else:
raise RuntimeError("Optimizer %s not supported" % params.optimizer)
loss, ops = optimize.create_train_op(loss, opt, global_step, params)
restore_op = restore_variables(args.checkpoint)
# Validation
if params.validation and params.references[0]:
files = [params.validation] + list(params.references)
eval_inputs = dataset.sort_and_zip_files(files)
eval_input_fn = dataset.get_evaluation_input
else:
eval_input_fn = None
# Add hooks
save_vars = tf.trainable_variables() + [global_step]
saver = tf.train.Saver(
var_list=save_vars if params.only_save_trainable else None,
max_to_keep=params.keep_checkpoint_max,
sharded=False
)
tf.add_to_collection(tf.GraphKeys.SAVERS, saver)
train_hooks = [
tf.train.StopAtStepHook(last_step=params.train_steps),
tf.train.NanTensorHook(loss),
tf.train.LoggingTensorHook(
{
"step": global_step,
"loss": loss,
},
every_n_iter=1
),
tf.train.CheckpointSaverHook(
checkpoint_dir=params.output,
save_secs=params.save_checkpoint_secs or None,
save_steps=params.save_checkpoint_steps or None,
saver=saver
)
]
config = session_config(params)
if eval_input_fn is not None:
train_hooks.append(
hooks.EvaluationHook(
lambda f: inference.create_inference_graph(
[model.get_inference_func()], f, params
),
lambda: eval_input_fn(eval_inputs, params),
lambda x: decode_target_ids(x, params),
params.output,
config,
params.keep_top_checkpoint_max,
eval_secs=params.eval_secs,
eval_steps=params.eval_steps
)
)
def restore_fn(step_context):
step_context.session.run(restore_op)
def step_fn(step_context):
# Bypass hook calls
step_context.session.run([init_op, ops["zero_op"]])
for i in range(params.update_cycle):
step_context.session.run(ops["collect_op"])
step_context.session.run(ops["scale_op"])
return step_context.run_with_hooks(ops["train_op"])
# Create session, do not use default CheckpointSaverHook
with tf.train.MonitoredTrainingSession(
checkpoint_dir=params.output, hooks=train_hooks,
save_checkpoint_secs=None, config=config) as sess:
# Restore pre-trained variables
sess.run_step_fn(restore_fn)
while not sess.should_stop():
sess.run_step_fn(step_fn)
# # 为了和 1.4以下的兼容
# # Create session, do not use default CheckpointSaverHook
#
# with tf.train.MonitoredTrainingSession(
# checkpoint_dir=params.output, hooks=train_hooks,
# save_checkpoint_secs=None, config=config) as sess:
#
# sess._tf_sess().run(restore_op)
#
# while not sess.should_stop():
# sess._tf_sess().run([init_op, ops["zero_op"]])
# for i in range(params.update_cycle):
# sess._tf_sess().run(ops["collect_op"])
# sess.run(ops["train_op"])
if __name__ == "__main__":
main(parse_args())