-
Notifications
You must be signed in to change notification settings - Fork 64
/
train.py
190 lines (150 loc) · 8.99 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
#!/usr/bin/env python
# coding: utf-8
import json
import math
import os
import time
from collections import OrderedDict
import tensorflow as tf
from utils import SEP_TOKEN, PAD_TOKEN, VOCAB_SIZE, MODEL_DIR
from data_utils import gen_batch_train_data
from model import Seq2SeqModel
from word2vec import get_word_embedding
# Data loading parameters
tf.app.flags.DEFINE_boolean('cangtou_data', False, 'Use cangtou training data')
tf.app.flags.DEFINE_boolean('rev_data', True, 'Use reversed training data')
tf.app.flags.DEFINE_boolean('align_data', True, 'Use aligned training data')
tf.app.flags.DEFINE_boolean('prev_data', True, 'Use training data with previous sentences')
tf.app.flags.DEFINE_boolean('align_word2vec', True, 'Use aligned word2vec model')
# Network parameters
tf.app.flags.DEFINE_string('cell_type', 'lstm', 'RNN cell for encoder and decoder, default: lstm')
tf.app.flags.DEFINE_string('attention_type', 'bahdanau', 'Attention mechanism: (bahdanau, luong), default: bahdanau')
tf.app.flags.DEFINE_integer('hidden_units', 128, 'Number of hidden units in each layer')
tf.app.flags.DEFINE_integer('depth', 4, 'Number of layers in each encoder and decoder')
tf.app.flags.DEFINE_integer('embedding_size', 128, 'Embedding dimensions of encoder and decoder inputs')
tf.app.flags.DEFINE_integer('num_encoder_symbols', 30000, 'Source vocabulary size')
tf.app.flags.DEFINE_integer('num_decoder_symbols', 30000, 'Target vocabulary size')
# NOTE(sdsuo): We used the same vocab for source and target
tf.app.flags.DEFINE_integer('vocab_size', VOCAB_SIZE, 'General vocabulary size')
tf.app.flags.DEFINE_boolean('use_residual', True, 'Use residual connection between layers')
tf.app.flags.DEFINE_boolean('attn_input_feeding', False, 'Use input feeding method in attentional decoder')
tf.app.flags.DEFINE_boolean('use_dropout', True, 'Use dropout in each rnn cell')
tf.app.flags.DEFINE_float('dropout_rate', 0.3, 'Dropout probability for input/output/state units (0.0: no dropout)')
# Training parameters
tf.app.flags.DEFINE_float('learning_rate', 0.0002, 'Learning rate')
tf.app.flags.DEFINE_float('max_gradient_norm', 1.0, 'Clip gradients to this norm')
tf.app.flags.DEFINE_integer('batch_size', 64, 'Batch size')
tf.app.flags.DEFINE_integer('max_epochs', 10000, 'Maximum # of training epochs')
tf.app.flags.DEFINE_integer('max_load_batches', 20, 'Maximum # of batches to load at one time')
tf.app.flags.DEFINE_integer('max_seq_length', 50, 'Maximum sequence length')
tf.app.flags.DEFINE_integer('display_freq', 100, 'Display training status every this iteration')
tf.app.flags.DEFINE_integer('save_freq', 100, 'Save model checkpoint every this iteration')
tf.app.flags.DEFINE_integer('valid_freq', 1150000, 'Evaluate model every this iteration: valid_data needed')
tf.app.flags.DEFINE_string('optimizer', 'adam', 'Optimizer for training: (adadelta, adam, rmsprop)')
tf.app.flags.DEFINE_string('model_dir', MODEL_DIR, 'Path to save model checkpoints')
tf.app.flags.DEFINE_string('summary_dir', 'model/summary', 'Path to save model summary')
tf.app.flags.DEFINE_string('model_name', 'translate.ckpt', 'File name used for model checkpoints')
tf.app.flags.DEFINE_boolean('shuffle_each_epoch', True, 'Shuffle training dataset for each epoch')
tf.app.flags.DEFINE_boolean('sort_by_length', True, 'Sort pre-fetched minibatches by their target sequence lengths')
tf.app.flags.DEFINE_boolean('use_fp16', False, 'Use half precision float16 instead of float32 as dtype')
tf.app.flags.DEFINE_boolean('bidirectional', True, 'Use bidirectional encoder')
tf.app.flags.DEFINE_string('train_mode', 'ground_truth', 'Decode helper to use for training')
tf.app.flags.DEFINE_string('sampling_probability', 0.1, 'Probability of sampling from decoder output instead of using ground truth')
# TODO(sdsuo): Make start token and end token more robust
tf.app.flags.DEFINE_integer('start_token', SEP_TOKEN, 'Start token')
tf.app.flags.DEFINE_integer('end_token', PAD_TOKEN, 'End token')
# Runtime parameters
tf.app.flags.DEFINE_boolean('allow_soft_placement', True, 'Allow device soft placement')
tf.app.flags.DEFINE_boolean('log_device_placement', False, 'Log placement of ops on devices')
FLAGS = tf.app.flags.FLAGS
def load_or_create_model(sess, model, saver, FLAGS):
ckpt = tf.train.get_checkpoint_state(FLAGS.model_dir)
if ckpt and tf.train.checkpoint_exists(ckpt.model_checkpoint_path):
print 'Reloading model parameters...'
model.restore(sess, saver, ckpt.model_checkpoint_path)
else:
if not os.path.exists(FLAGS.model_dir):
os.makedirs(FLAGS.model_dir)
print 'Created new model parameters...'
sess.run(tf.global_variables_initializer())
def train():
config_proto = tf.ConfigProto(
allow_soft_placement=FLAGS.allow_soft_placement,
log_device_placement=FLAGS.log_device_placement,
gpu_options=tf.GPUOptions(allow_growth=True)
)
with tf.Session(config=config_proto) as sess:
# Build the model
config = OrderedDict(sorted(FLAGS.__flags.items()))
model = Seq2SeqModel(config, 'train')
# Create a log writer object
log_writer = tf.summary.FileWriter(FLAGS.model_dir, graph=sess.graph)
# Create a saver
# Using var_list = None returns the list of all saveable variables
saver = tf.train.Saver(var_list=None)
# Initiaize global variables or reload existing checkpoint
load_or_create_model(sess, model, saver, FLAGS)
# Load word2vec embedding
embedding = get_word_embedding(FLAGS.hidden_units, alignment=FLAGS.align_word2vec)
model.init_vars(sess, embedding=embedding)
step_time, loss = 0.0, 0.0
sents_seen = 0
start_time = time.time()
print 'Training...'
for epoch_idx in xrange(FLAGS.max_epochs):
if model.global_epoch_step.eval() >= FLAGS.max_epochs:
print 'Training is already complete.', \
'Current epoch: {}, Max epoch: {}'.format(model.global_epoch_step.eval(), FLAGS.max_epochs)
break
# Prepare batch training data
# TODO(sdsuo): Make corresponding changes in data_utils
for source, source_len, target, target_len in gen_batch_train_data(FLAGS.batch_size,
prev=FLAGS.prev_data,
rev=FLAGS.rev_data,
align=FLAGS.align_data,
cangtou=FLAGS.cangtou_data):
step_loss, summary = model.train(
sess,
encoder_inputs=source,
encoder_inputs_length=source_len,
decoder_inputs=target,
decoder_inputs_length=target_len
)
loss += float(step_loss) / FLAGS.display_freq
sents_seen += float(source.shape[0]) # batch_size
# Display information
if model.global_step.eval() % FLAGS.display_freq == 0:
avg_perplexity = math.exp(float(loss)) if loss < 300 else float("inf")
time_elapsed = time.time() - start_time
step_time = time_elapsed / FLAGS.display_freq
sents_per_sec = sents_seen / time_elapsed
print 'Epoch ', model.global_epoch_step.eval(), 'Step ', model.global_step.eval(), \
'Perplexity {0:.2f}'.format(avg_perplexity), 'Step-time ', step_time, \
'{0:.2f} sents/s'.format(sents_per_sec)
loss = 0
sents_seen = 0
start_time = time.time()
# Record training summary for the current batch
log_writer.add_summary(summary, model.global_step.eval())
# Save the model checkpoint
if model.global_step.eval() % FLAGS.save_freq == 0:
print 'Saving the model..'
checkpoint_path = os.path.join(FLAGS.model_dir, FLAGS.model_name)
model.save(sess, saver, checkpoint_path, global_step=model.global_step)
json.dump(model.config,
open('%s-%d.json' % (checkpoint_path, model.global_step.eval()), 'wb'),
indent=2)
# Increase the epoch index of the model
model.increment_global_epoch_step_op.eval()
print 'Epoch {0:} DONE'.format(model.global_epoch_step.eval())
print 'Saving the last model'
checkpoint_path = os.path.join(FLAGS.model_dir, FLAGS.model_name)
model.save(sess, saver, checkpoint_path, global_step=model.global_step)
json.dump(model.config,
open('%s-%d.json' % (checkpoint_path, model.global_step.eval()), 'wb'),
indent=2)
print 'Training terminated'
def main(_):
train()
if __name__ == '__main__':
tf.app.run()