-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsnli_reasoning_attention.py
345 lines (306 loc) · 13.8 KB
/
snli_reasoning_attention.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
# coding: utf-8
# In[1]:
from __future__ import print_function
import pickle
import sys
import numpy
import numpy as np
import pandas as pd
import theano
import theano.tensor as T
import lasagne
import time
from custom_layers import CustomEmbedding, CustomLSTMEncoder, CustomDense, CustomLSTMDecoder
# In[2]:
def prepare(df):
seqs_premise = []
seqs_hypothesis = []
for cc in df['sentence1']:
seqs_premise.append(cc)
for cc in df['sentence2']:
seqs_hypothesis.append(cc)
seqs_p = seqs_premise
seqs_h = seqs_hypothesis
lengths_p = [len(s) for s in seqs_p]
lengths_h = [len(s) for s in seqs_h]
n_samples = len(seqs_p)
maxlen_p = numpy.max(lengths_p) + 1
maxlen_h = numpy.max(lengths_h) + 1
premise = numpy.zeros((n_samples, maxlen_p))
hypothesis = numpy.zeros((n_samples, maxlen_h))
premise_masks = numpy.zeros((n_samples, maxlen_p))
hypothesis_masks = numpy.zeros((n_samples, maxlen_h))
for idx, [s_t, s_h] in enumerate(zip(seqs_p, seqs_h)):
assert lengths_h[idx] == len(s_h)
premise[idx, :lengths_p[idx]] = s_t
premise_masks[idx, :lengths_p[idx]] = 1
hypothesis[idx, :lengths_h[idx]] = s_h
hypothesis_masks[idx, :lengths_h[idx]] = 1
labels = []
for gl in df['gold_label']:
if gl == 'entailment':
value = 2
elif gl == 'contradiction':
value = 1
elif gl == 'neutral':
value = 0
else:
raise ValueError('unknown gold_label {0}'.format(gl))
labels.append(value)
labels = np.array(labels)
return (premise.astype('int32'),
premise_masks.astype('int32'),
hypothesis.astype('int32'),
hypothesis_masks.astype('int32'),
labels.astype('int32'))
# In[3]:
print('Loading data ...')
train_df, dev_df, test_df = (None, None, None)
with open('./snli/converted_train.pkl', 'rb') as f:
print('Loading train ...')
train_df = pickle.load(f)
print(len(train_df))
filtered_s2 = train_df.sentence2.apply(lambda s2: len(s2) != 0)
train_df = train_df[filtered_s2]
print(len(train_df))
train_df = train_df[train_df.gold_label != '-']
train_df = train_df.reset_index()
print(len(train_df))
# prepare(train_df)
with open('./snli/converted_dev.pkl', 'rb') as f:
print('Loading dev ...')
dev_df = pickle.load(f)
print(len(dev_df))
filtered_s2 = dev_df.sentence2.apply(lambda s2: len(s2) != 0)
dev_df = dev_df[filtered_s2]
print(len(dev_df))
dev_df = dev_df[dev_df.gold_label != '-']
dev_df = dev_df.reset_index()
print(len(dev_df))
with open('./snli/converted_test.pkl', 'rb') as f:
print('Loading test ...')
test_df = pickle.load(f)
print(len(test_df))
filtered_s2 = test_df.sentence2.apply(lambda s2: len(s2) != 0)
test_df = test_df[filtered_s2]
print(len(test_df))
test_df = test_df[test_df.gold_label != '-']
test_df = test_df.reset_index()
print(len(test_df))
# In[7]:
premise_max = 82 + 1
hypothesis_max = 62 + 1
# In[8]:
def main(num_epochs=10, k=100, batch_size=128,
display_freq=100,
save_freq=1000,
load_previous=False,
attention=True,
word_by_word=True, mode='word_by_word'):
print('num_epochs: {}'.format(num_epochs))
print('k: {}'.format(k))
print('batch_size: {}'.format(batch_size))
print('display_frequency: {}'.format(display_freq))
print('save_frequency: {}'.format(save_freq))
print('load previous: {}'.format(load_previous))
print('attention: {}'.format(attention))
print('word_by_word: {}'.format(word_by_word))
save_filename = './snli/{}_model.npz'.format(mode)
print("Building network ...")
premise_var = T.imatrix('premise_var')
premise_mask = T.imatrix('premise_mask')
hypo_var = T.imatrix('hypo_var')
hypo_mask = T.imatrix('hypo_mask')
unchanged_W = pickle.load(open('./snli/unchanged_W.pkl', 'rb'))
unchanged_W = unchanged_W.astype('float32')
unchanged_W_shape = unchanged_W.shape
oov_in_train_W = pickle.load(open('./snli/oov_in_train_W.pkl', 'rb'))
oov_in_train_W = oov_in_train_W.astype('float32')
oov_in_train_W_shape = oov_in_train_W.shape
print('unchanged_W.shape: {0}'.format(unchanged_W_shape))
print('oov_in_train_W.shape: {0}'.format(oov_in_train_W_shape))
# best hypoparameters
p = 0.2
learning_rate = 0.001
# learning_rate = 0.0003
# l2_weight = 0.0003
l2_weight = 0.
l_premise = lasagne.layers.InputLayer(shape=(None, premise_max), input_var=premise_var)
l_premise_mask = lasagne.layers.InputLayer(shape=(None, premise_max), input_var=premise_mask)
l_hypo = lasagne.layers.InputLayer(shape=(None, hypothesis_max), input_var=hypo_var)
l_hypo_mask = lasagne.layers.InputLayer(shape=(None, hypothesis_max), input_var=hypo_mask)
premise_embedding = CustomEmbedding(l_premise, unchanged_W, unchanged_W_shape,
oov_in_train_W, oov_in_train_W_shape,
p=p)
# weights shared with premise_embedding
hypo_embedding = CustomEmbedding(l_hypo, unchanged_W=premise_embedding.unchanged_W,
unchanged_W_shape=unchanged_W_shape,
oov_in_train_W=premise_embedding.oov_in_train_W,
oov_in_train_W_shape=oov_in_train_W_shape,
p=p,
dropout_mask=premise_embedding.dropout_mask)
l_premise_linear = CustomDense(premise_embedding, k,
nonlinearity=lasagne.nonlinearities.linear)
l_hypo_linear = CustomDense(hypo_embedding, k,
W=l_premise_linear.W, b=l_premise_linear.b,
nonlinearity=lasagne.nonlinearities.linear)
encoder = CustomLSTMEncoder(l_premise_linear, k, peepholes=False, mask_input=l_premise_mask)
decoder = CustomLSTMDecoder(l_hypo_linear, k, cell_init=encoder, peepholes=False, mask_input=l_hypo_mask,
encoder_mask_input=l_premise_mask,
attention=attention,
word_by_word=word_by_word
)
if p > 0.:
print('apply dropout rate {} to decoder'.format(p))
decoder = lasagne.layers.DropoutLayer(decoder, p)
l_softmax = lasagne.layers.DenseLayer(
decoder, num_units=3,
nonlinearity=lasagne.nonlinearities.softmax)
if load_previous:
print('loading previous saved model ...')
# And load them again later on like this:
with np.load(save_filename) as f:
param_values = [f['arr_%d' % i] for i in range(len(f.files))]
lasagne.layers.set_all_param_values(l_softmax, param_values)
target_var = T.ivector('target_var')
# lasagne.layers.get_output produces a variable for the output of the net
prediction = lasagne.layers.get_output(l_softmax, deterministic=False)
# The network output will have shape (n_batch, 3);
loss = lasagne.objectives.categorical_crossentropy(prediction, target_var)
cost = loss.mean()
if l2_weight > 0.:
# apply l2 regularization
print('apply l2 penalty to all layers, weight: {}'.format(l2_weight))
regularized_layers = {encoder: l2_weight,
decoder: l2_weight}
l2_penalty = lasagne.regularization.regularize_network_params(l_softmax,
lasagne.regularization.l2) * l2_weight
cost += l2_penalty
# Retrieve all parameters from the network
all_params = lasagne.layers.get_all_params(l_softmax, trainable=True)
# Compute adam updates for training
print("Computing updates ...")
updates = lasagne.updates.adam(cost, all_params, learning_rate=learning_rate)
test_prediction = lasagne.layers.get_output(l_softmax, deterministic=True)
test_loss = lasagne.objectives.categorical_crossentropy(test_prediction,
target_var)
test_loss = test_loss.mean()
# lasagne.objectives.categorical_accuracy()
# As a bonus, also create an expression for the classification accuracy:
test_acc = T.mean(T.eq(T.argmax(test_prediction, axis=1), target_var),
dtype=theano.config.floatX)
# Theano functions for training and computing cost
print("Compiling functions ...")
train_fn = theano.function([premise_var, premise_mask, hypo_var, hypo_mask, target_var],
cost, updates=updates)
val_fn = theano.function([premise_var, premise_mask, hypo_var, hypo_mask, target_var],
[test_loss, test_acc])
print("Training ...")
print('train_df.shape: {0}'.format(train_df.shape))
print('dev_df.shape: {0}'.format(dev_df.shape))
print('test_df.shape: {0}'.format(test_df.shape))
try:
# Finally, launch the training loop.
print("Starting training...")
# We iterate over epochs:
for epoch in range(num_epochs):
# In each epoch, we do a full pass over the training data:
shuffled_train_df = train_df.reindex(np.random.permutation(train_df.index))
train_err = 0
train_acc = 0
train_batches = 0
start_time = time.time()
display_at = time.time()
save_at = time.time()
for start_i in range(0, len(shuffled_train_df), batch_size):
batched_df = shuffled_train_df[start_i:start_i + batch_size]
ps, p_masks, hs, h_masks, labels = prepare(batched_df)
train_err += train_fn(ps, p_masks, hs, h_masks, labels)
err, acc = val_fn(ps, p_masks, hs, h_masks, labels)
train_acc += acc
train_batches += 1
# display
if train_batches % display_freq == 0:
print("Seen {:d} samples, time used: {:.3f}s".format(
start_i + batch_size, time.time() - display_at))
print(" current training loss:\t\t{:.6f}".format(train_err / train_batches))
print(" current training accuracy:\t\t{:.6f}".format(train_acc / train_batches))
# do tmp save model
if train_batches % save_freq == 0:
print('saving to ..., time used {:.3f}s'.format(time.time() - save_at))
np.savez(save_filename,
*lasagne.layers.get_all_param_values(l_softmax))
save_at = time.time()
# And a full pass over the validation data:
val_err = 0
val_acc = 0
val_batches = 0
for start_i in range(0, len(dev_df), batch_size):
batched_df = dev_df[start_i:start_i + batch_size]
ps, p_masks, hs, h_masks, labels = prepare(batched_df)
err, acc = val_fn(ps, p_masks, hs, h_masks, labels)
val_err += err
val_acc += acc
val_batches += 1
# Then we print the results for this epoch:
print("Epoch {} of {} took {:.3f}s".format(
epoch + 1, num_epochs, time.time() - start_time))
print(" training loss:\t\t{:.6f}".format(train_err / train_batches))
print(" training accuracy:\t\t{:.2f} %".format(
train_acc / train_batches * 100))
print(" validation loss:\t\t{:.6f}".format(val_err / val_batches))
print(" validation accuracy:\t\t{:.2f} %".format(
val_acc / val_batches * 100))
# After training, we compute and print the test error:
test_err = 0
test_acc = 0
test_batches = 0
for start_i in range(0, len(test_df), batch_size):
batched_df = test_df[start_i:start_i + batch_size]
ps, p_masks, hs, h_masks, labels = prepare(batched_df)
err, acc = val_fn(ps, p_masks, hs, h_masks, labels)
test_err += err
test_acc += acc
test_batches += 1
# print("Final results:")
print(" test loss:\t\t\t{:.6f}".format(test_err / test_batches))
print(" test accuracy:\t\t{:.2f} %".format(
test_acc / test_batches * 100))
filename = './snli/{}_model_epoch{}.npz'.format(mode, epoch + 1)
print('saving to {}'.format(filename))
np.savez(filename,
*lasagne.layers.get_all_param_values(l_softmax))
# Optionally, you could now dump the network weights to a file like this:
# np.savez('model.npz', *lasagne.layers.get_all_param_values(network))
#
# And load them again later on like this:
# with np.load('model.npz') as f:
# param_values = [f['arr_%d' % i] for i in range(len(f.files))]
# lasagne.layers.set_all_param_values(network, param_values)
except KeyboardInterrupt:
print('exit ...')
# In[9]:
if __name__ == '__main__':
attention = True
word_by_word = True
mode = 'word_by_word'
if len(sys.argv) == 2:
mode = sys.argv[1]
if mode == 'condition':
attention = False
word_by_word = False
elif mode == 'attention':
word_by_word = False
elif mode == 'word_by_word':
attention = True
word_by_word = True
else:
print('doesn\'t recognize mode {}'.format(mode))
print('only supports [condition|attention|word_by_word]')
sys.exit(1)
main(num_epochs=20, batch_size=30,
display_freq=1000,
load_previous=False,
attention=attention,
word_by_word=word_by_word,
mode=mode)