-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtrain.py
195 lines (159 loc) · 7.25 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
191
192
193
194
195
"""Script for training the skip-thoughts model."""
import argparse
import itertools
import os
import time
import tensorflow as tf
from gensim.models import KeyedVectors
from skip_thoughts import SkipThoughts
parser = argparse.ArgumentParser()
argparse_bool = lambda s: s.lower() in ['true', 't', 'yes', '1']
# Hyperparameter args
parser.add_argument('--initial_lr', type=float, default=1e-3,
help="Initial learning rate.")
parser.add_argument('--vocabulary_size', type=int, default=20000,
help="Keep only the n most common words of the training data.")
parser.add_argument('--batch_size', type=int, default=128,
help="Stochastic gradient descent minibatch size.")
parser.add_argument('--output_size', type=int, default=512,
help="Number of hidden units for the encoder and decoder GRUs.")
parser.add_argument('--max_sequence_length', type=int, default=40,
help="Truncate input and output sentences to maximum length n.")
parser.add_argument('--max_grad_norm', type=float, default=5.,
help="Clip gradients to the specified maximum norm.")
parser.add_argument('--concat', type=argparse_bool, default=False,
help="Set to true to concatenate rather than add the biRNN outputs. "
"Note this doubles the dimension of the output vectors.")
parser.add_argument('--softmax_samples', type=int, default=0,
help="Set to n > 0 to use sampled softmax with n candidate samples.")
parser.add_argument('--optimizer', type=str, default='adam',
help="Currently supports 'adam' and 'sgd'.")
parser.add_argument('--train_word_embeddings', type=argparse_bool,
default=False, help="Set to backpropagate over the word embeddings.")
parser.add_argument('--train_special_embeddings', type=argparse_bool,
default=False, help="Set to backpropagate over the special token embeddings.")
parser.add_argument('--eos_token', type=argparse_bool, default=True,
help="Set to use the end-of-string token when running on inference.")
# Performance args
parser.add_argument('--time_major', type=argparse_bool, default=True,
help="Set to feed time-major batches to the RNNs.")
parser.add_argument('--cuda', type=argparse_bool, default=False,
help="Set to False to forcefully disable the use of Cudnn ops.")
parser.add_argument('--benchmark', type=int, default=0,
help="Set to n > 0 to estimate running time by executing n steps.")
# Configuration args
parser.add_argument('--embeddings_path', type=str, default="word2vecModel",
help="Path to the pre-trained word embeddings model.")
parser.add_argument('--input', type=str, default="data/books_tf",
help="Path to the directory containing the dataset TFRecord files.")
parser.add_argument('--model_name', type=str, default="default",
help="Will save/restore model in ./output/[model_name].")
parser.add_argument('--num_steps_per_save', type=int, default=5000,
help="Save the model's trainable variables every n steps.")
FLAGS = parser.parse_args()
def parse_and_pad(seq):
# Extract features from `tf.SequenceExample`
sequence_features = {
"tokens": tf.FixedLenSequenceFeature([], dtype=tf.int64),
}
_, sequence_parsed = tf.parse_single_sequence_example(
serialized=seq, sequence_features=sequence_features)
# Pad the sequence
t = sequence_parsed["tokens"]
if FLAGS.eos_token:
t = tf.pad(t, [[0, 1]], constant_values=3)
return tf.pad(t, [[0, FLAGS.max_sequence_length - tf.shape(t)[0]]])
def train_iterator(filenames):
"""Build the input pipeline for training.."""
dataset = tf.data.TFRecordDataset(filenames)
dataset = dataset.map(parse_and_pad)
dataset = dataset.apply(tf.contrib.data.sliding_window_batch(
window_size=FLAGS.batch_size, stride=1))
dataset = dataset.batch(FLAGS.batch_size).map(lambda x: x[:3])
# dataset = dataset.apply(tf.contrib.data.sliding_window_batch(
# window_size=3, stride=FLAGS.batch_size))
if FLAGS.time_major:
dataset = dataset.map(lambda x: tf.transpose(x, perm=[0, 2, 1]))
dataset = dataset.prefetch(1)
return dataset.make_one_shot_iterator().get_next()
if __name__ == '__main__':
print("Loading word vector model...")
start = time.time()
w2v_model = KeyedVectors.load(FLAGS.embeddings_path, mmap='r')
duration = time.time() - start
print("Done ({:0.4f}s).".format(duration))
print("Building computational graph...")
start = time.time()
graph = tf.Graph()
with graph.as_default():
filenames = [os.path.join(FLAGS.input, f) for f in os.listdir(FLAGS.input)]
iterator = train_iterator(filenames)
cuda = FLAGS.cuda
gpu_available = tf.test.is_gpu_available(cuda_only=True)
if cuda and not (FLAGS.time_major or gpu_available):
print("WARNING: disabling CUDA ops. GPU must be available and time "
"major mode must be enabled.")
cuda = False
if FLAGS.optimizer == 'adam':
optimizer = tf.train.AdamOptimizer
else:
optimizer = tf.train.GradientDescentOptimizer
m = SkipThoughts(w2v_model, train=iterator,
vocabulary_size=FLAGS.vocabulary_size,
batch_size=FLAGS.batch_size,
output_size=FLAGS.output_size,
max_sequence_length=FLAGS.max_sequence_length,
learning_rate=FLAGS.initial_lr,
max_grad_norm=FLAGS.max_grad_norm,
concat=FLAGS.concat, optimizer=optimizer,
softmax_samples=FLAGS.softmax_samples,
train_special_embeddings=FLAGS.train_special_embeddings,
train_word_embeddings=FLAGS.train_word_embeddings,
time_major=FLAGS.time_major, cuda=cuda)
duration = time.time() - start
print("Done ({:0.4f}s).".format(duration))
with tf.Session(graph=graph) as sess:
saver = tf.train.Saver(max_to_keep=1)
output_dir = os.path.join('output', FLAGS.model_name)
if not m.restore(output_dir):
print("Initializing model...")
start = time.time()
sess.run(tf.global_variables_initializer())
duration = time.time() - start
print(
"Initialized model at", output_dir,
"({:0.4f}s).".format(duration))
# Avoid crashes due to directory not existing.
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# Used for benchmarking running time.
i = 0
min_duration = float('inf') # infinity
average_duration = 0
# Training loop.
while True:
start = time.time()
loss_, _ = sess.run([m.loss, m.train_op])
duration = time.time() - start
current_step = sess.run(m.global_step)
# Only benchmark running time if requested.
if FLAGS.benchmark:
i += 1
min_duration = min(duration, min_duration)
average_duration = (duration + (i - 1) * average_duration ) / i
if i >= FLAGS.benchmark:
print("Running time benchmarks for", FLAGS.benchmark, "steps:")
print(" Average:", average_duration)
print(" Minimum:", min_duration)
exit()
else:
continue
print(
"Step", current_step,
"(loss={:0.4f}, time={:0.4f}s)".format(loss_, duration))
if current_step % FLAGS.num_steps_per_save == 0:
print("Saving model...")
saver.save(
sess,
os.path.join('output', FLAGS.model_name, 'checkpoint.ckpt'),
global_step=m.global_step)