-
Notifications
You must be signed in to change notification settings - Fork 2
/
train_simple_te_model_h.py
235 lines (213 loc) · 9.58 KB
/
train_simple_te_model_h.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
import atexit
import json
import os
import pickle
import random
from argparse import ArgumentParser
import numpy as np
import tensorflow as tf
from tensorflow.python.ops.rnn_cell_impl import DropoutWrapper
from datasets import load_te_dataset
from embeddings import load_glove, glove_embeddings_initializer
from utils import Progbar
from utils import batch
from utils import start_logger, stop_logger
def build_simple_te_model_relu_h(premise_input,
hypothesis_input,
dropout_input,
num_tokens,
num_labels,
embeddings,
embeddings_size,
train_embeddings,
rnn_hidden_size,
classification_hidden_size):
hypothesis_length = tf.cast(
tf.reduce_sum(
tf.cast(tf.not_equal(hypothesis_input, tf.zeros_like(hypothesis_input, dtype=tf.int32)), tf.int64),
1
),
tf.int32
)
if embeddings is not None:
embedding_matrix = tf.get_variable(
"embedding_matrix",
shape=(num_tokens, embeddings_size),
initializer=glove_embeddings_initializer(embeddings),
trainable=train_embeddings
)
print("Loaded GloVe embeddings!")
else:
embedding_matrix = tf.get_variable(
"embedding_matrix",
shape=(num_tokens, embeddings_size),
initializer=tf.random_normal_initializer(stddev=0.05),
trainable=train_embeddings
)
hypothesis_embeddings = tf.nn.embedding_lookup(embedding_matrix, hypothesis_input)
lstm_cell = DropoutWrapper(
tf.nn.rnn_cell.LSTMCell(rnn_hidden_size),
input_keep_prob=dropout_input,
output_keep_prob=dropout_input
)
hypothesis_outputs, hypothesis_final_states = tf.nn.dynamic_rnn(
cell=lstm_cell,
inputs=hypothesis_embeddings,
sequence_length=hypothesis_length,
dtype=tf.float32
)
first_layer = tf.nn.dropout(
tf.contrib.layers.fully_connected(hypothesis_final_states.h, classification_hidden_size),
keep_prob=dropout_input
)
second_layer = tf.nn.dropout(
tf.contrib.layers.fully_connected(first_layer, classification_hidden_size),
keep_prob=dropout_input
)
third_layer = tf.nn.dropout(
tf.contrib.layers.fully_connected(second_layer, classification_hidden_size),
keep_prob=dropout_input
)
return tf.contrib.layers.fully_connected(
third_layer,
num_labels,
activation_fn=None
)
if __name__ == "__main__":
random_seed = 12345
os.environ["PYTHONHASHSEED"] = str(random_seed)
random.seed(random_seed)
np.random.seed(random_seed)
tf.set_random_seed(random_seed)
parser = ArgumentParser()
parser.add_argument("--train_filename", type=str, required=True)
parser.add_argument("--dev_filename", type=str, required=True)
parser.add_argument("--vectors_filename", type=str, required=True)
parser.add_argument("--model_save_filename", type=str, required=True)
parser.add_argument("--max_vocab", type=int, default=300000)
parser.add_argument("--embeddings_size", type=int, default=300)
parser.add_argument("--train_embeddings", type=bool, default=True)
parser.add_argument("--rnn_hidden_size", type=int, default=512)
parser.add_argument("--dropout_ratio", type=float, default=0.5)
parser.add_argument("--classification_hidden_size", type=int, default=512)
parser.add_argument("--batch_size", type=int, default=256)
parser.add_argument("--num_epochs", type=int, default=100)
parser.add_argument("--learning_rate", type=float, default=0.001)
parser.add_argument("--l2_reg", type=float, default=0.000005)
parser.add_argument("--patience", type=int, default=3)
args = parser.parse_args()
start_logger(args.model_save_filename + ".train_log")
atexit.register(stop_logger)
print("-- Building vocabulary")
embeddings, token2id, id2token = load_glove(args.vectors_filename, args.max_vocab, args.embeddings_size)
label2id = {"neutral": 0, "entailment": 1, "contradiction": 2}
id2label = {v: k for k, v in label2id.items()}
num_tokens = len(token2id)
num_labels = len(label2id)
print("Number of tokens: {}".format(num_tokens))
print("Number of labels: {}".format(num_labels))
with open(args.model_save_filename + ".params", mode="w") as out_file:
json.dump(vars(args), out_file)
print("Params saved to: {}".format(args.model_save_filename + ".params"))
with open(args.model_save_filename + ".index", mode="wb") as out_file:
pickle.dump(
{
"token2id": token2id,
"id2token": id2token,
"label2id": label2id,
"id2label": id2label
},
out_file
)
print("Index saved to: {}".format(args.model_save_filename + ".index"))
print("-- Loading training set")
train_labels, train_premises, train_hypotheses, _, _ = load_te_dataset(args.train_filename, token2id, label2id)
print("-- Loading development set")
dev_labels, dev_premises, dev_hypotheses, _, _ = load_te_dataset(args.dev_filename, token2id, label2id)
print("-- Building model")
premise_input = tf.placeholder(tf.int32, (None, None), name="premise_input")
hypothesis_input = tf.placeholder(tf.int32, (None, None), name="hypothesis_input")
label_input = tf.placeholder(tf.int32, (None,), name="label_input")
dropout_input = tf.placeholder(tf.float32, name="dropout_input")
logits = build_simple_te_model_relu_h(
premise_input,
hypothesis_input,
dropout_input,
num_tokens,
num_labels,
embeddings,
args.embeddings_size,
args.train_embeddings,
args.rnn_hidden_size,
args.classification_hidden_size
)
loss_function = tf.losses.sparse_softmax_cross_entropy(label_input, logits)
train_step = tf.train.AdamOptimizer(learning_rate=args.learning_rate).minimize(loss_function)
saver = tf.train.Saver()
num_examples = train_labels.shape[0]
num_batches = num_examples // args.batch_size
dev_best_accuracy = -1
stopping_step = 0
best_epoch = None
should_stop = False
with tf.Session(config=tf.ConfigProto(inter_op_parallelism_threads=1)) as session:
session.run(tf.global_variables_initializer())
for epoch in range(args.num_epochs):
if should_stop:
break
print("\n==> Online epoch # {0}".format(epoch + 1))
progress = Progbar(num_batches)
batches_indexes = np.arange(num_examples)
np.random.shuffle(batches_indexes)
batch_index = 1
epoch_loss = 0
for indexes in batch(batches_indexes, args.batch_size):
batch_premises = train_premises[indexes]
batch_hypotheses = train_hypotheses[indexes]
batch_labels = train_labels[indexes]
loss, _ = session.run([loss_function, train_step], feed_dict={
premise_input: batch_premises,
hypothesis_input: batch_hypotheses,
label_input: batch_labels,
dropout_input: args.dropout_ratio
})
progress.update(batch_index, [("Loss", loss)])
epoch_loss += loss
batch_index += 1
print("Current mean training loss: {}\n".format(epoch_loss / num_batches))
print("-- Validating model")
dev_num_examples = dev_labels.shape[0]
dev_batches_indexes = np.arange(dev_num_examples)
dev_num_correct = 0
for indexes in batch(dev_batches_indexes, args.batch_size):
dev_batch_premises = dev_premises[indexes]
dev_batch_hypotheses = dev_hypotheses[indexes]
dev_batch_labels = dev_labels[indexes]
predictions = session.run(
tf.argmax(logits, axis=1),
feed_dict={
premise_input: dev_batch_premises,
hypothesis_input: dev_batch_hypotheses,
dropout_input: 1.0
}
)
dev_num_correct += (predictions == dev_batch_labels).sum()
dev_accuracy = dev_num_correct / dev_num_examples
print("Current mean validation accuracy: {}".format(dev_accuracy))
if dev_accuracy > dev_best_accuracy:
stopping_step = 0
best_epoch = epoch + 1
dev_best_accuracy = dev_accuracy
saver.save(session, args.model_save_filename + ".ckpt")
print("Best mean validation accuracy: {} (reached at epoch {})".format(dev_best_accuracy, best_epoch))
print("Best model saved to: {}".format(args.model_save_filename))
else:
stopping_step += 1
print("Current stopping step: {}".format(stopping_step))
if stopping_step >= args.patience:
print("Early stopping at epoch {}!".format(epoch + 1))
print("Best mean validation accuracy: {} (reached at epoch {})".format(dev_best_accuracy, best_epoch))
should_stop = True
if epoch + 1 >= args.num_epochs:
print("Stopping at epoch {}!".format(epoch + 1))
print("Best mean validation accuracy: {} (reached at epoch {})".format(dev_best_accuracy, best_epoch))