-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluation_tests.py
133 lines (101 loc) · 3.88 KB
/
evaluation_tests.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
import pandas as pd
from tqdm import tqdm
from question_recommend import QuestionRecommendation, TfIdfSearch, MinHashSearch
from semantic_sim import SimServer
TEST_QUESTIONS = 'data/test_questions.txt'
TEST_DATASET = 'data/test_dataset.txt'
def save_test_questions():
df = pd.read_csv('data/quora-question-pairs/train.csv', index_col=0)
test_questions = []
test_dataset = []
num_test_questions = 1000
num_dataset_questions = 400_000
for i, row in df.iterrows():
if row['is_duplicate'] == 1 and num_test_questions != 0:
test_questions.append((row['question1'],
len(test_dataset)))
num_test_questions -= 1
else:
test_dataset.append(row['question1'])
num_dataset_questions -= 1
test_dataset.append(row['question2'])
num_dataset_questions -= 1
if num_dataset_questions <= 0:
break
with open(TEST_QUESTIONS, 'w') as f:
for q in test_questions:
print(f"{q[0]}\t{q[1]}", file=f)
with open(TEST_DATASET, 'w') as f:
for q in test_dataset:
print(f"{q}", file=f)
def save_bert_questions():
df = pd.read_csv('data/quora-question-pairs/train.csv', index_col=0)
test_questions = []
test_dataset = []
num_test_questions = 100
num_dataset_questions = 10_000
for i, row in df.iterrows():
if row['is_duplicate'] == 1 and num_test_questions != 0:
test_questions.append((row['question1'],
len(test_dataset)))
num_test_questions -= 1
else:
test_dataset.append(row['question1'])
num_dataset_questions -= 1
test_dataset.append(row['question2'])
num_dataset_questions -= 1
if num_dataset_questions <= 0:
break
with open('data/bert_questions.txt', 'w') as f:
for q in test_questions:
print(f"{q[0]}\t{q[1]}", file=f)
with open('data/bert_dataset.txt', 'w') as f:
for q in test_dataset:
print(f"{q}", file=f)
def evaluate(engine, k, limit=None):
num_questions = 0
num_correct_3 = 0
num_correct_5 = 0
num_correct_10 = 0
num_correct = 0
with open(TEST_QUESTIONS) as f:
for line in tqdm(f):
q, dupl = line.strip().split('\t')
retrieved = engine.search(q, k=k)
if int(dupl) in retrieved[:1000]:
num_correct_10 += 1
if int(dupl) in retrieved[:5]:
num_correct_5 += 1
if int(dupl) in retrieved[:3]:
num_correct_3 += 1
if int(dupl) in retrieved[:1]:
num_correct += 1
num_questions += 1
if num_questions == limit:
break
print(f"\nDetection @1: {100 * num_correct/num_questions} %")
print(f"Detection @3: {100 * num_correct_3/num_questions} %")
print(f"Detection @5: {100 * num_correct_5/num_questions} %")
print(f"Detection @10: {100 * num_correct_10/num_questions} %")
if __name__ == '__main__':
se = QuestionRecommendation(TEST_DATASET, SimServer.UNIV_SENT_ENCODER)
se1 = QuestionRecommendation(TEST_DATASET, SimServer.USE_QA)
se2 = QuestionRecommendation(TEST_DATASET, SimServer.USE_MULTILINGUAL)
se3 = QuestionRecommendation(TEST_DATASET, SimServer.USE_WITH_DAN)
tf = TfIdfSearch(TEST_DATASET)
lsh = MinHashSearch(TEST_DATASET)
print("Loaded indices", flush=True)
print("Standard USE: ")
evaluate(se, 20)
print("USE per Question Answering: ")
evaluate(se1, 20)
print("USE advanced multilingual: ")
evaluate(se2, 20)
print("Standard USE with DAN network: ")
evaluate(se3, 20)
print("TF-IDF based search : ")
evaluate(tf, 20, limit=500)
print("LSH based search : ")
evaluate(lsh, 1000)
print("BERT model based search: ")
# evaluate_bert_qqp(TEST_DATASET)