-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcnnrank.py
368 lines (344 loc) · 12.8 KB
/
cnnrank.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
from scorer import Scorer
import numpy as np
import logging
import theano
import ipdb
import pdb, os, inflect, argparse
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Input, LSTM
from keras.layers import Embedding, Flatten, concatenate, Reshape
from keras.layers import Convolution1D, MaxPooling1D, Merge, GlobalMaxPooling1D, Convolution2D, MaxPooling2D, GlobalMaxPooling2D
from keras.layers.merge import Concatenate, Dot
from keras.layers import merge
from keras.models import Model
from keras.utils import np_utils
from gensim.models import Word2Vec
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report
from keras.models import model_from_json
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.metrics import mean_squared_error
EMB_DIM = 300
MAX_NB_WORDS=20000
MAX_LEN = 2000
nb_filter = 10
batch_size = 200
epochs = 7
TRAIN = 0.7
logging.basicConfig(level=logging.INFO)
class NNScorer(Scorer):
def __init__(self, topics, pairs, pairstest, mode):
super().__init__(topics, pairs, pairstest, mode)
#self.loadWord2Vec('/home/Btech13/sumit.cs13/GoogleNews-vectors-negative300.bin', True)
def get_single_instances(self, filename, fmap):
X = []
with open(filename) as fin:
for line in fin:
l = line.strip().split('\t')
try:
idx = 3
text = ''
fm = fmap
while fm > 0:
take = fm % 2
fm = int(fm / 2)
if take == 1:
text += l[idx]
idx += 1
X.append([l[0], l[1], text[50:]])
except:
ipdb.set_trace()
return X
def get_train_instances(self, X):
sz = len(X)
split = int(sz/2)
# take half as they are
X_train = X[0:split]
y = [1] * len(X_train)
i = split
# assign random profession to other half
# TODO incorporate some kind of profession dependency
while i < sz:
j = np.random.randint(0, len(self.topics))
if self.topics[j] == X[i][1]:
continue
samp = X[i]
X_train.append([samp[0], self.topics[j], samp[2]])
y.append(0)
i = i + 1
k = list(zip(X_train, y))
np.random.shuffle(k)
k = list(zip(*k))
X_train = k[0]
y = k[1]
return np.asarray(X_train), y
def vectorize_persons(self, pairs):
data = []
y = []
for p in pairs:
if 'opening_text' in self.persons[p[0]]:
data.append([p[0], p[1], p[2], self.persons[p[0]]['opening_text']])
y.append(p[2])
data = np.asarray(data)
X = self.text_to_vectors(data)
return data[:,0:3], X, np.asarray(y, dtype=np.int32)
def text_to_vectors(self, X):
f1 = pad_sequences(self.tokenizer.texts_to_sequences(X[:,3]), self.MAX_LEN)
f2 = pad_sequences(self.tokenizer.texts_to_sequences(X[:,1]), self.MAX_LENE)
return [f1,f2]
def fit_data(self, X, y):
texts = X[:,2].ravel().tolist()
entities = X[:,1].ravel().tolist()
#self.MAX_LEN = len(max(texts, key = lambda x:len(x)))
self.MAX_LEN = MAX_LEN
self.MAX_LENE = len(max(entities, key = lambda x:len(x)))
self.tokenizer = Tokenizer(num_words=MAX_NB_WORDS)
self.tokenizer.fit_on_texts(texts+entities)
self.word_index = self.tokenizer.word_index
self.X_train = []
self.logger.info("Found %s unique tokens", self.MAX_LEN)
self.X_train = []
f1 = pad_sequences(self.tokenizer.texts_to_sequences(X[:,2]), self.MAX_LEN)
f2 = pad_sequences(self.tokenizer.texts_to_sequences(X[:,1]), self.MAX_LENE)
#f = lambda x: self.get_vector(x)
#f2 = np.array(list(map(f, X[:,1])))
#for idx, x in enumerate(X):
# self.X_train.append(np.array([f1[idx], f2[idx]]))
#ipdb.set_trace()
self.X_train = [f1,f2]
self.y = np.array(y)
def get_train_test(self):
t = int(len(self.X_train) * TRAIN)
return self.X_train[0:t], self.y[0:t], self.X_train[t:], self.y[t:]
def get_embedding_matrix(self):
embedding_matrix = np.zeros((len(self.word_index) + 1, 300))
for word, i in self.word_index.items():
try:
embedding_vector = self.w2v[word]
embedding_matrix[i] = embedding_vector
except Exception as e:
pass
return embedding_matrix
def get_data(self, train = True):
pairs = self.pairs
if not train:
pairs = self.pairstest
X = np.empty((len(pairs), EMB_DIM * 2))
y = []
for idx, pair in enumerate(pairs):
vec1 = self.get_vector(pair[0])
vec2 = self.get_vector(pair[1])
v = np.append(vec1, vec2)
X[idx] = v
y.append(pair[2])
return X, y
def build_model(self):
X = self.get_single_instances('data/profession.one.sorted', 0b11)
X, y = self.get_train_instances(X)
self.fit_data(X,y)
self.loadWord2Vec('/home/Btech13/sumit.cs13/GoogleNews-vectors-negative300.bin', True)
embedding_matrix = self.get_embedding_matrix()
lim = int(len(self.X_train[0])*TRAIN)
input_shape = (self.MAX_LEN, EMB_DIM)
context_input = Input(shape=(self.MAX_LEN,))
emb = Embedding(len(self.word_index) + 1, EMB_DIM, input_length=self.MAX_LEN, weights=[embedding_matrix],name="embedding")(context_input)
emb = Dropout(0.2)(emb)
conv_blocks = []
for sz in [2,3]:
conv = Convolution1D(
filters=nb_filter,
kernel_size=sz,
padding='valid',
activation='relu',
strides=1,
input_shape=input_shape)(emb)
conv = GlobalMaxPooling1D()(conv)
#conv = Flatten()(conv)
conv_blocks.append(conv)
z = Concatenate(name='conv_layer')(conv_blocks) if len(conv_blocks) > 1 else conv_blocks[0]
#z = Dense(64, activation='relu', name='conv_dense')(z)
# define input for entity vector
#ent_inp = Input(shape=(300,), dtype='float32', name='ent_input')
ent_inp = Input(shape=(self.MAX_LENE,), dtype='float32', name='ent_input')
ent_emb = Embedding(len(self.word_index) + 1, EMB_DIM, input_length=self.MAX_LENE, weights=[embedding_matrix],name="ent_embedding")(ent_inp)
conv_ent = Convolution1D(
filters=3,
kernel_size=2,
padding='valid',
activation='relu',
strides=1,
input_shape=input_shape)(ent_emb)
conv_ent = GlobalMaxPooling1D()(conv_ent)
#ent = Dense(64, activation='relu', name='ent_dense')(ent_inp)
# merge entity vector input with CNN output on context
x = concatenate([z, conv_ent], name='merged_layer')
#x = Dot(1 , name='merged_layer')([z, ent])
# Stack a fully connected deep network
x = Dense(512, activation='relu', name='dense_one')(x)
x = Dropout(0.4)(x)
x = Dense(128, activation='relu', name='dense_two')(x)
x = Dropout(0.2)(x)
loss = Dense(1, activation='sigmoid', name='output')(x)
model = Model(inputs=[context_input, ent_inp], outputs=loss)
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
print(model.get_layer('conv_layer').output_shape)
print(model.get_layer('merged_layer').output_shape)
model.fit([self.X_train[0][0:lim], self.X_train[1][0:lim]], self.y[:lim],
batch_size=batch_size,
epochs=epochs,
validation_data=([self.X_train[0][lim:], self.X_train[1][lim:]], self.y[lim:])
)
ipdb.set_trace()
model_name = 'cnn_model'
model_json = model.to_json()
with open(model_name+".json", "w") as json_file:
json_file.write(model_json)
model.save_weights(model_name+".h5")
print("Saved model to disk")
def build_lstm(self):
X = self.get_single_instances('data/profession.one.sorted', 0b11)
X, y = self.get_train_instances(X)
self.fit_data(X,y)
self.loadWord2Vec('/home/Btech13/sumit.cs13/GoogleNews-vectors-negative300.bin', True)
embedding_matrix = self.get_embedding_matrix()
lim = int(len(self.X_train[0])*TRAIN)
input_shape = (self.MAX_LEN, EMB_DIM)
context_input = Input(shape=(self.MAX_LEN,))
emb = Embedding(len(self.word_index) + 1, EMB_DIM, input_length=self.MAX_LEN, weights=[embedding_matrix],name="embedding")(context_input)
emb = Dropout(0.2)(emb)
lstm_out = LSTM(32)(emb)
#auxiliary_output = Dense(1, activation='sigmoid', name='aux_output')(lstm_out)
ent_inp = Input(shape=(self.MAX_LENE,), name='ent_input')
ent_emb = Embedding(len(self.word_index) + 1, EMB_DIM, input_length=self.MAX_LENE, weights=[embedding_matrix],name="ent_embedding")(ent_inp)
conv_ent = Convolution1D(
filters=3,
kernel_size=2,
padding='valid',
activation='relu',
strides=1,
input_shape=input_shape)(ent_emb)
conv_ent = GlobalMaxPooling1D()(conv_ent)
x = concatenate([lstm_out, conv_ent])
x = Dense(64, activation='relu')(x)
x = Dense(64, activation='relu')(x)
main_output = Dense(1, activation='sigmoid', name='main_output')(x)
model = Model(inputs=[context_input, ent_inp], outputs=main_output)
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.fit([self.X_train[0][0:lim], self.X_train[1][0:lim]], self.y[:lim],
epochs=epochs, batch_size=batch_size,
validation_data=([self.X_train[0][lim:], self.X_train[1][lim:]],
self.y[lim:])
)
def test_cnn_model(self, model_name):
X = self.get_single_instances('data/profession.one.sorted', 0b11)
X, y = self.get_train_instances(X)
self.fit_data(X,y)
#self.loadWord2Vec('/home/Btech13/sumit.cs13/GoogleNews-vectors-negative300.bin', True)
#embedding_matrix = self.get_embedding_matrix()
lim = int(len(self.X_train[0])*TRAIN)
json_file = open(model_name+'.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
model = model_from_json(loaded_model_json)
model.load_weights(model_name+'.h5')
print("Loaded model from disk")
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
ipdb.set_trace()
labels = model.predict([self.X_train[0][lim:], self.X_train[1][lim:]])
print('predictions done')
truth = self.y[lim:]
t_l = int(len(truth)*TRAIN)
truth_train = truth[:t_l]
truth_test = truth[t_l:]
clf=None
score=0
print('fitting Logistic regression')
for i, C in enumerate((100, 1, 0.01)):
# turn down tolerance for short training time
clf_l1_LR = LogisticRegression(C=C, penalty='l1', tol=0.01)
clf_l2_LR = LogisticRegression(C=C, penalty='l2', tol=0.01)
clf_l1_LR.fit(labels[:t_l], truth_train)
clf_l2_LR.fit(labels[:t_l], truth_train)
l1=clf_l1_LR.score(labels[t_l:],truth_test)
l2=clf_l2_LR.score(labels[t_l:],truth_test)
if l1 > score:
clf = clf_l1_LR
score = l1
if l2 > score:
clf = clf_l2_LR
score = l2
print(l1, " ", l2)
preds = clf.predict(labels[t_l:]).astype(int)
y_test = truth_test.astype(int)
print(classification_report(y_test, preds))
ipdb.set_trace()
def regression(self, model_name):
X = self.get_single_instances('data/profession.one.sorted', 0b11)
X, y = self.get_train_instances(X)
self.fit_data(X,y)
lim = int(len(self.X_train[0])*TRAIN)
json_file = open(model_name+'.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
model = model_from_json(loaded_model_json)
model.load_weights(model_name+'.h5')
print("Loaded model from disk")
model.layers.pop()
model_new = Model(model.input, model.layers[-1].output)
model_new.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
self.persons = Scorer.getWikipediaTexts2(self.persons)
pairs_train, X_train, y_train = self.vectorize_persons(self.pairs)
pairs_test, X_test, y_test = self.vectorize_persons(self.pairstest)
X2_train = model_new.predict([X_train[0], X_train[1]])
X2_test = model_new.predict([X_test[0], X_test[1]])
est = GradientBoostingRegressor(n_estimators=100, learning_rate=0.1,
random_state=0, loss='lad').fit(X2_train, y_train)
pairstest = np.asarray(self.pairstest)
classes = est.predict(X2_test)
ipdb.set_trace()
classes = classes.reshape((len(classes),1))
pairstest = np.hstack((pairs_test, classes))
self.writeScore('preds', pairstest)
print(mean_squared_error(y_test, classes))
def get_vector(self, word):
vec = np.zeros((EMB_DIM,))
if not self.w2v:
return vec
words = word.split()
for w in words:
try:
vec += self.w2v[w]
except:
pass
return vec / len(word)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-i", help="Input directory", action="store_true")
parser.add_argument("input", help="Input directory value")
parser.add_argument("-o", help="Output directory", action="store_true")
parser.add_argument("output", help="Output directory value")
parser.add_argument("-pro", help="profession flag", action="store_true")
parser.add_argument("-nat", help="nationality flag", action="store_true")
args = parser.parse_args()
input = ""
output = ""
if args.i:
input = args.input
if args.o:
output = args.output
if args.pro:
logging.info("Getting topics")
topics = Scorer.getTopics(input+'/professions')
logging.info("Getting pairs")
pairs = NNScorer.getPersonPairs(input+'/profession.train')
pairstest = NNScorer.getPersonPairs(input+'/profession.test')
nns = NNScorer(topics, pairs, pairstest, 'profession')
#nns.build_model()
#nns.test_cnn_model('cnn_model2')
#nns.regression('cnn_model')
nns.build_lstm()
if __name__ == "__main__":
main()