-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathutils.py
210 lines (174 loc) · 6.23 KB
/
utils.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
import theano
import numpy as np
from theano import config
from time import time
import cPickle
import sys
import cPickle as pkl
def checkIfQuarter(idx, n):
if idx == round(n / 4.) or idx == round(n / 2.) or idx == round(3 * n / 4.):
return True
return False
def prepare_data(list_of_seqs,list_of_pos):
lengths = [len(s) for s in list_of_seqs]
n_samples = len(list_of_seqs)
maxlen = np.max(lengths)
x = np.zeros((n_samples, maxlen)).astype('int32')
x_pos = np.zeros((n_samples, maxlen)).astype('int32')
x_mask = np.zeros((n_samples, maxlen)).astype(theano.config.floatX)
for idx, s in enumerate(list_of_seqs):
x[idx, :lengths[idx]] = s
x_mask[idx, :lengths[idx]] = 1.
for idx, s in enumerate(list_of_pos):
x_pos[idx, :lengths[idx]] = s
x_mask = np.asarray(x_mask, dtype=config.floatX)
return x, x_pos, x_mask
def get_minibatches_idx(n, minibatch_size, shuffle=False):
idx_list = np.arange(n, dtype="int32")
if shuffle:
np.random.shuffle(idx_list)
minibatches = []
minibatch_start = 0
for i in range(n // minibatch_size):
minibatches.append(idx_list[minibatch_start:
minibatch_start + minibatch_size])
minibatch_start += minibatch_size
if (minibatch_start != n):
minibatches.append(idx_list[minibatch_start:])
return zip(range(len(minibatches)), minibatches)
def getDataSim(batch, nout):
g1, g1_pos = [], []
g2, g2_pos = [], []
for i in batch:
g1.append(i[0].embeddings)
g2.append(i[1].embeddings)
g1_pos.append(i[0].pos_embeddings)
g2_pos.append(i[1].pos_embeddings)
g1x, g1x_pos, g1x_mask = prepare_data(g1, g1_pos)
g2x, g2x_pos, g2x_mask = prepare_data(g2, g2_pos)
scores = []
for i in batch:
temp = np.zeros(nout)
score = float(i[2])
ceil, fl = int(np.ceil(score)), int(np.floor(score))
if ceil == fl:
temp[fl - 1] = 1
else:
temp[fl - 1] = ceil - score
temp[ceil - 1] = score - fl
scores.append(temp)
scores = np.matrix(scores) + 0.000001
scores = np.asarray(scores, dtype=config.floatX)
return (scores, g1x, g1x_pos, g1x_mask, g2x, g2x_pos, g2x_mask)
def getDataSim2(batch, nout):
g1, g1_pos = [], []
g2, g2_pos = [], []
for i in batch:
g1.append(i[0].embeddings)
g2.append(i[1].embeddings)
g1_pos.append(i[0].pos_embeddings)
g2_pos.append(i[1].pos_embeddings)
g1x, g1x_pos, g1x_mask = prepare_data(g1, g1_pos)
g2x, g2x_pos, g2x_mask = prepare_data(g2, g2_pos)
scores = []
for i in batch:
scores.append(float(i[2]))
scores = np.asarray(scores, dtype=config.floatX)
return (scores, g1x, g1x_pos, g1x_mask, g2x, g2x_pos, g2x_mask)
def getDataEntailment(batch):
g1 = []; g2 = []
g1_pos = []; g2_pos = []
for i in batch:
g1.append(i[0].embeddings)
g2.append(i[1].embeddings)
g1_pos.append(i[0].pos_embeddings)
g2_pos.append(i[1].pos_embeddings)
g1x, g1x_pos = prepare_data(g1, g1_pos)
g2x, g2x_pos = prepare_data(g2, g2_pos)
scores = []
for i in batch:
temp = np.zeros(3)
label = i[2].strip().lower()
if label == "contradiction":
temp[0]=1
if label == "neutral":
temp[1]=1
if label == "entailment":
temp[2]=1
scores.append(temp)
scores = np.matrix(scores)+0.000001
scores = np.asarray(scores,dtype=config.floatX)
return (scores,g1x,g1x_pos,g2x,g2x_pos)
def getDataParaphrase(batch):
g1 = []; g2 = []
g1_pos = []; g2_pos = []
for i in batch:
g1.append(i[0].embeddings)
g2.append(i[1].embeddings)
g1_pos.append(i[0].pos_embeddings)
g2_pos.append(i[1].pos_embeddings)
g1x, g1x_pos = prepare_data(g1, g1_pos)
g2x, g2x_pos = prepare_data(g2, g2_pos)
scores = []
for i in batch:
temp = np.zeros(2)
label = i[2].strip()
if label == "0":
temp[0]=1
if label == "1":
temp[1]=1
scores.append(temp)
scores = np.matrix(scores)+0.000001
scores = np.asarray(scores,dtype=config.floatX)
return (scores,g1x,g1x_pos,g2x,g2x_pos)
def getDataSentiment(batch):
g1 = []
g1_pos = []
for i in batch:
g1.append(i[0].embeddings)
g1_pos.append(i[0].pos_embeddings)
g1x, g1x_pos = prepare_data(g1,g1_pos)
scores = []
for i in batch:
temp = np.zeros(2)
label = i[1].strip()
if label == "0":
temp[0]=1
if label == "1":
temp[1]=1
scores.append(temp)
scores = np.matrix(scores)+0.000001
scores = np.asarray(scores,dtype=config.floatX)
return (scores,g1x,g1x_pos)
def train(model, train_data, words, pos_vocab, params):
start_time = time()
best_val = 0
best_p = None
try:
for eidx in xrange(params.epochs):
kf = get_minibatches_idx(len(train_data), params.batchsize, shuffle=True)
uidx = 0
for _, train_index in kf:
uidx += 1
batch = [train_data[t] for t in train_index]
for i in batch:
i[0].populate_embeddings(words, pos_vocab)
i[1].populate_embeddings(words, pos_vocab)
(scores,g1x,g1x_pos,g1x_mask,g2x,g2x_pos,g2x_mask) = getDataSim(batch, model.nout)
cost = model.train_function(scores, g1x, g1x_pos, g2x, g2x_pos)
if np.isnan(cost) or np.isinf(cost):
print 'NaN detected'
#print 'Epoch ', (eidx+1), 'Update ', (uidx+1), 'Cost ', cost
#undo batch to save RAM
for i in batch:
i[0].representation = None
i[1].representation = None
i[0].unpopulate_embeddings()
i[1].unpopulate_embeddings()
pkl.dump(model.all_params, open('%s.pkl'% params.outfile, 'wb'))
print 'Epoch ', (eidx+1), 'Cost ', cost
sys.stdout.flush()
except KeyboardInterrupt:
print "Training interupted"
end_time = time()
print "total time:", (end_time - start_time)