-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathppdb_utils.py
252 lines (213 loc) · 6.53 KB
/
ppdb_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
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
import numpy as np
from tree import tree
import time
from random import randint
from random import choice
from scipy.spatial.distance import pdist
from scipy.spatial.distance import squareform
import utils
import sys
from evaluate import evaluate_all
def lookupIDX(words,w):
w = w.lower()
if len(w) > 1 and w[0] == '#':
w = w.replace("#","")
if w in words:
return words[w]
else:
return words['UUUNKKK']
def getPPDBData(f,words):
data = open(f,'r')
lines = data.readlines()
examples = []
for i in lines:
i=i.strip()
if(len(i) > 0):
i=i.split('\t')
if len(i) == 2:
e = (tree(i[0], words), tree(i[1], words))
examples.append(e)
else:
print i
return examples
def getPosVocab(f):
data = open(f, 'r')
lines = data.readlines()
pos_vocab = {}
pos_vocab['null_000'] = 0
num = 1
for i in lines:
i = i.lower()
i = i.strip().split('\t')
if len(i) == 3:
for j in [ii.split('_')[1] for ii in i[1].split() if len(ii.split('_'))==2]:
if not j in pos_vocab:
pos_vocab[j] = num
num += 1
for j in [ii.split('_')[1] for ii in i[2].split() if len(ii.split('_'))==2]:
if not j in pos_vocab:
pos_vocab[j] = num
num += 1
pos_vocab['UUUNKKK'] = num
return pos_vocab
def getSimEntDataset(f,words,pos_vocab):
data = open(f,'r')
lines = data.readlines()
examples = []
for i in lines:
i=i.lower()
i=i.strip()
if(len(i) > 0):
i=i.split('\t')
if len(i) == 3:
e = (tree(i[1], words, pos_vocab), tree(i[2], words, pos_vocab), float(i[0]))
examples.append(e)
else:
print i
return examples
def getSentimentDataset(f,words,pos_vocab):
data = open(f,'r')
lines = data.readlines()
examples = []
for i in lines:
i=i.lower()
i=i.strip()
if(len(i) > 0):
i=i.split('\t')
if len(i) == 2:
e = (tree(i[0], words, pos_vocab), i[1])
examples.append(e)
else:
print i
return examples
def getWordmap(textfile):
words={}
We = []
f = open(textfile,'r')
lines = f.readlines()
words['null_000'] = 0 #for batches
We.append(300*[0.]) #300 is dimension of word embedding
for (n,i) in enumerate(lines):
i=i.split()
if i[0] in words:
#print i[0]
continue
j = 1
v = []
while j < len(i):
v.append(float(i[j]))
j += 1
words[i[0]]=n+1
We.append(v)
return (words, np.array(We))
def getPairRand(d,idx):
wpick = None
ww = None
while(wpick == None or (idx == ww)):
ww = choice(d)
ridx = randint(0,1)
wpick = ww[ridx]
return wpick
def getPairMixScore(d,idx,maxpair):
r1 = randint(0,1)
if r1 == 1:
return maxpair
else:
return getPairRand(d,idx)
def getPairsFast(d, type):
X = []
T = []
pairs = []
for i in range(len(d)):
(p1,p2) = d[i]
X.append(p1.representation)
X.append(p2.representation)
T.append(p1)
T.append(p2)
arr = pdist(X,'cosine')
arr = squareform(arr)
for i in range(len(arr)):
arr[i,i]=1
if i % 2 == 0:
arr[i,i+1] = 1
else:
arr[i,i-1] = 1
arr = np.argmin(arr,axis=1)
for i in range(len(d)):
(t1,t2) = d[i]
p1 = None
p2 = None
if type == "MAX":
p1 = T[arr[2*i]]
p2 = T[arr[2*i+1]]
if type == "RAND":
p1 = getPairRand(d,i)
p2 = getPairRand(d,i)
if type == "MIX":
p1 = getPairMixScore(d,i,T[arr[2*i]])
p2 = getPairMixScore(d,i,T[arr[2*i+1]])
pairs.append((p1,p2))
return pairs
def getpairs(model, batch, params):
g1 = []
g2 = []
for i in batch:
g1.append(i[0].embeddings)
g2.append(i[1].embeddings)
g1x, g1mask = utils.prepare_data(g1)
g2x, g2mask = utils.prepare_data(g2)
embg1 = model.feedforward_function(g1x, g1mask)
embg2 = model.feedforward_function(g2x, g2mask)
for idx, i in enumerate(batch):
i[0].representation = embg1[idx, :]
i[1].representation = embg2[idx, :]
pairs = getPairsFast(batch, params.type)
p1 = []
p2 = []
for i in pairs:
p1.append(i[0].embeddings)
p2.append(i[1].embeddings)
p1x, p1mask = utils.prepare_data(p1)
p2x, p2mask = utils.prepare_data(p2)
return (g1x, g1mask, g2x, g2mask, p1x, p1mask, p2x, p2mask)
def train(model, data, words, params):
start_time = time.time()
counter = 0
try:
for eidx in xrange(params.epochs):
kf = utils.get_minibatches_idx(len(data), params.batchsize, shuffle=True)
uidx = 0
for _, train_index in kf:
uidx += 1
batch = [data[t] for t in train_index]
for i in batch:
i[0].populate_embeddings(words)
i[1].populate_embeddings(words)
(g1x, g1mask, g2x, g2mask, p1x, p1mask, p2x, p2mask) = getpairs(model, batch, params)
cost = model.train_function(g1x, g2x, p1x, p2x, g1mask, g2mask, p1mask, p2mask)
if np.isnan(cost) or np.isinf(cost):
print 'NaN detected'
if (utils.checkIfQuarter(uidx, len(kf))):
if (params.save):
counter += 1
utils.saveParams(model, params.outfile + str(counter) + '.pickle')
if (params.evaluate):
evaluate_all(model, words)
sys.stdout.flush()
#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()
#print 'Epoch ', (eidx+1), 'Update ', (uidx+1), 'Cost ', cost
if (params.save):
counter += 1
utils.saveParams(model, params.outfile + str(counter) + '.pickle')
if (params.evaluate):
evaluate_all(model, words)
print 'Epoch ', (eidx + 1), 'Cost ', cost
except KeyboardInterrupt:
print "Training interupted"
end_time = time.time()
print "total time:", (end_time - start_time)