-
Notifications
You must be signed in to change notification settings - Fork 9
/
data_utils.py
349 lines (306 loc) · 10.9 KB
/
data_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
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
from __future__ import absolute_import
from __future__ import division
import os
import re
import numpy as np
import csv
from collections import defaultdict
import random
import numpy as np
from sklearn.metrics import confusion_matrix
import itertools
import pandas as pd
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
def find_common_range_of_relation(target):
word_list = []
for e1, r, e2 in kb:
if r in target:
for e in e2.split('_'):
if e not in stop_words:
word_list.append(e)
dict = {}
for word in word_list:
if len(word) > 0:
dict[word] = dict.get(word, 0) + 1
type_counts = sorted(dict.items(), key = lambda x: x[1], reverse = True)
common_types = list(filter(lambda x:x[1] > 50 and len(x[0]) > 1, type_counts))[:10]
return list(map(lambda x:x[0], common_types))
def find_common_domain_of_relation(target):
word_list = []
for e1, r, e2 in kb:
if r in target:
for e in e1.split('_'):
if e not in stop_words:
word_list.append(e)
dict = {}
for word in word_list:
if len(word) > 0:
dict[word] = dict.get(word, 0) + 1
type_counts = sorted(dict.items(), key = lambda x: x[1], reverse = True)
common_types = list(filter(lambda x:x[1] > 80 and len(x[0]) > 1, type_counts))[:10]
return list(map(lambda x:x[0], common_types))
def get_model_answers(preds, data):
model_answers = []
real_answers = []
i = 0
for _, e1, r, e2, l in data:
q = e1 + r
score = preds[i][0]
model_answers.append([q, e2, score])
y = 1 if l[0] == '+' else 0
real_answers.append([q, e2, y])
i += 1
return model_answers, real_answers
def get_real_answers(filename):
with open(filename) as f:
test_data = []
for line in f.readlines():
e1, rest = line.replace("thing$concept_", "").split(",")
e2, sign = rest.split(":")
sign = sign.replace("\n", "").strip()
label = 0
if sign == "+":
label = 1
test_data.append([e1, e2, label])
return test_data
def sigmoid(x, derivative=False):
return x*(1-x) if derivative else 1/(1+np.exp(-x))
def nell_eval(model_answers, correct_answers):
test_data = correct_answers
# load prediction scores
preds = {}
for line in model_answers:
e1, e2, score = line
score = float(score)
if (e1, e2) not in preds:
preds[(e1, e2)] = score
else:
if preds[(e1, e2)] < score:
preds[(e1, e2)] = score
def get_pred_score(e1, e2):
if (e1, e2) in preds:
return preds[(e1, e2)]
else:
return -np.inf
test_pairs = defaultdict(lambda : defaultdict(int))
for e1, e2, label in test_data:
test_pairs[e1][e2] = label
aps = []
score_all = []
# calculate MAP
for e1 in test_pairs:
y_true = []
y_score = []
for e2 in test_pairs[e1]:
score = get_pred_score(e1, e2)
score_all.append(score)
y_score.append(score)
y_true.append(test_pairs[e1][e2])
count = list(zip(y_score, y_true))
count.sort(key=lambda x: x[0], reverse=True)
ranks = []
correct = 0
for idx_, item in enumerate(count):
if item[1] == 1:
correct += 1
ranks.append(correct / (1.0 + idx_))
if len(ranks) == 0:
ranks.append(0)
aps.append(np.mean(ranks))
mean_ap = np.mean(aps)
print('{0} queries evaluated'.format(len(aps)))
return mean_ap
from collections import defaultdict
def PR_curve(model_answers, correct_answers):
test_data = correct_answers
# load prediction scores
preds = {}
for line in model_answers:
e1, e2, score = line
score = float(score)
if (e1, e2) not in preds:
preds[(e1, e2)] = score
else:
if preds[(e1, e2)] < score:
preds[(e1, e2)] = score
def get_pred_score(e1, e2):
if (e1, e2) in preds:
return preds[(e1, e2)]
else:
return -np.inf
test_pairs = defaultdict(lambda : defaultdict(int))
for e1, e2, label in test_data:
test_pairs[e1][e2] = label
aps = []
score_all = []
# calculate MAP
precisions = []
recalls = []
for e1 in test_pairs:
y_true = []
y_score = []
for e2 in test_pairs[e1]:
score = get_pred_score(e1, e2)
score_all.append(score)
y_score.append(score)
y_true.append(test_pairs[e1][e2])
count = list(zip(y_score, y_true))
count.sort(key=lambda x: x[0], reverse=True)
correct = 0
found = 0
prec = []
rec = []
num_corrects = len(list(filter(lambda x: x[1], count)))
for idx_, item in enumerate(count):
if item[1] == 1:
correct += 1
prec.append(correct / (1.0 + idx_))
rec.append(correct / num_corrects)
found = 1
precisions.append(sum(prec)/len(prec))
recalls.append(sum(rec)/len(rec))
return [precisions, recalls, np.mean(np.array(precisions))]
def eval_mrr(model_answers, correct_answers):
test_data = correct_answers
# load prediction scores
preds = {}
for line in model_answers:
e1, e2, score = line
score = float(score)
if (e1, e2) not in preds:
preds[(e1, e2)] = score
else:
if preds[(e1,e2)] < score:
preds[(e1,e2)] = score
def get_pred_score(e1, e2):
if (e1, e2) in preds:
return preds[(e1,e2)]
else:
return -np.inf
test_pairs = defaultdict(lambda : defaultdict(int))
for e1, e2, label in test_data:
test_pairs[e1][e2] = label
mrr_ranks = []
score_all = []
hits_at1 = []
hits_at3 = []
hits_at10 = []
ranks_list = []
# calculate MRR
for e1 in test_pairs:
query_ranks = []
y_true = []
y_score = []
for e2 in test_pairs[e1]:
score = get_pred_score(e1, e2)
score_all.append(score)
y_score.append(score)
y_true.append(test_pairs[e1][e2])
count = list(zip(y_score, y_true))
count.sort(key=lambda x: x[0], reverse=True)
mrr_rank = 0.0
found_rank = 0.0
answer_rank = []
found = 0
for idx_, item in enumerate(count):
if item[1] == 1 and found_rank == 0:
mrr_rank = 1.0 / (1.0 + idx_)
found_rank = 1
if item[1] == 1 and found == 0:
answer_rank.append(idx_ + 1.0)
found = 1
mrr_ranks.append(mrr_rank)
ranks_list.append(answer_rank)
hits_at1 = np.sum( [ (np.sum(np.array(ranks) <= 1)) for ranks in ranks_list] )/len(test_pairs)
hits_at3 = np.sum( [ (np.sum(np.array(ranks) <= 3)) for ranks in ranks_list] )/len(test_pairs)
hits_at10= np.sum( [ (np.sum(np.array(ranks) <= 10)) for ranks in ranks_list] )/len(test_pairs)
mrr = np.mean(mrr_ranks)
return (mrr, hits_at1, hits_at3, hits_at10)
def print_epoch(epoch, num_epoch, train_loss, val_loss, train_acc, val_acc):
print("Epoch: {0:2d}/{1}\tloss: {2: 0.7f}\tacc: {3: 0.4f}\tval_loss: {4: 0.7f}\tval_acc: {5: 0.4f}".format(epoch,
num_epoch, train_loss, train_acc, val_loss, val_acc))
def display_data(data):
story = data[0]
question = data[1]
answer = data[2]
print("Story:")
for line in story:
print(" ".join(line))
print("Question:")
print(" ".join(question) + "?")
print("Answer:", answer)
def load_paths(data_dir, only_supporting=False):
'''Load the nth task. There are 20 tasks in total.
Returns a tuple containing the training and testing data for the task.
'''
files = os.listdir(data_dir)
files = [os.path.join(data_dir, f) for f in files]
train_file = [f for f in files if 'train.txt' in f][0]
test_file = [f for f in files if 'test.txt' in f][0]
train_data = get_stories(train_file, only_supporting)
test_data = get_stories(test_file, only_supporting)
return train_data, test_data
def tokenize(sent):
'''Return the tokens of a sentence including punctuation.
>>> tokenize('Bob dropped the apple. Where is the apple?')
['Bob', 'dropped', 'the', 'apple', '.', 'Where', 'is', 'the', 'apple', '?']
'''
return [x.strip() for x in re.split('(\S+)?', sent) if x.strip()]
def parse_stories(lines, only_supporting=False):
'''Parse stories provided in the bAbI tasks format
If only_supporting is true, only the sentences that support the answer are kept.
'''
data = []
story = []
nid = 0
for line in lines:
line = str.lower(line).replace("\n", "")
if nid == 1:
story = []
if '\t' in line: # query
nid = 1
e1, r, e2, l = line.split('\t')
substory = [x for x in story if x]
data.append((substory, e1, r, e2, l))
story = []
else: # regular sentence
# remove periods
nid = 0
sent = tokenize(line)
if sent[-1] == ".":
sent = sent[:-1]
story.append(sent)
label = ['context', 'e1', 'r', 'e2', 'label']
df = pd.DataFrame.from_records(data, columns=label)
return df
def get_stories(f, only_supporting=False):
'''Given a file name, read the file, retrieve the stories, and then convert the sentences into a single story.
If max_length is supplied, any stories longer than max_length tokens will be discarded.
'''
with open(f) as f:
return parse_stories(f.readlines(), only_supporting=only_supporting)
def vectorize_data(data, word_idx, SENT_SIZE, MEMORY_SIZE):
"""
Vectorize stories and queries.
If a sentence length < sentence_size, the sentence will be padded with 0's.
If a story length < memory_size, the story will be padded with empty memories.
Empty memories are 1-D arrays of length sentence_size filled with 0's.
The answer array is returned as a one-hot encoding.
"""
S = []
R = []
L = []
for story, _, relation, _, answer in data.values.tolist():
cxt = []
for i, path in enumerate(story, 1):
ls = max(0, SENT_SIZE - len(path))
cxt.append([0] * ls + [word_idx[w] for w in path])
# pad to memory_size
for _ in range(max(0, MEMORY_SIZE - len(cxt))):
cxt.append([0] * SENT_SIZE)
S.append(cxt)
# C.append(char_cxt)
R.append(word_idx[relation])
L.append(1 if answer[0] == '+' else 0)
return np.array(S), np.array(R), np.array(L)