-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathana_eval_sparse.py
More file actions
169 lines (147 loc) · 6.33 KB
/
Copy pathana_eval_sparse.py
File metadata and controls
169 lines (147 loc) · 6.33 KB
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
# We reuse a fraction of code in http://bitbucket.org/omerlevy/hyperwords.
# Using the numpy and similarity matrix largely speed up the evaluation process,
# compared with evaluation scripts in word2vec and GloVe
import numpy as np
import argparse
import random
from scipy.sparse import dok_matrix, csr_matrix
def load_matrix(f_path):
with open(f_path, errors='ignore') as f:
row, col, data, iw = [], [], [], []
first_line = True
lines_num = 0
for line in f:
if first_line:
first_line = False
words_num = int(line.rstrip().split()[0])
dim = int(line.rstrip().split()[1])
continue
line = line.rstrip().split(' ')
word = line[0]
iw.append(word)
vector = line[1:]
for v in vector:
row.append(lines_num)
col.append(int(v.split(":")[0]))
data.append(float(v.split(":")[1]))
lines_num += 1
wi = {}
for i in range(len(iw)):
wi[iw[i]] = i
row = np.array(row)
col = np.array(col)
data = np.array(data)
matrix = csr_matrix((data, (row, col)), shape=(words_num, dim))
return matrix, iw, wi
def load_vocabulary(path):
with open(path) as f:
vocab = [line.strip().split()[0] for line in f if len(line) > 0]
return dict([(a, i) for i, a in enumerate(vocab)]), vocab
def read_analogy(path, iw):
analogy = {}
analogy_type = ""
with open(path) as f:
for line in f:
oov = 0
if line.strip().split()[0] == ':':
analogy_type = line.strip().split()[1]
analogy[analogy_type] = {}
analogy[analogy_type]["questions"] = []
analogy[analogy_type]["total"] = 0
analogy[analogy_type]["seen"] = 0
continue
analogy_question = line.strip().split()
for w in analogy_question[:3]:
if w not in iw:
oov = 1
if oov == 1:
analogy[analogy_type]["total"] += 1
continue
analogy[analogy_type]["total"] += 1
analogy[analogy_type]["seen"] += 1
analogy[analogy_type]["questions"].append(analogy_question)
for t in analogy:
analogy[t]['iw'] = []
analogy[t]['wi'] = {}
for question in analogy[t]["questions"]:
for w in question:
if w not in analogy[t]['iw']:
analogy[t]['iw'].append(w)
for i, w in enumerate(analogy[t]['iw']):
analogy[t]['wi'][w] = i
return analogy
def normalize(matrix):
matrix2 = matrix.copy()
matrix2.data **= 2
norm = np.reciprocal(np.sqrt(np.array(matrix2.sum(axis=1))[:, 0]))
normalizer = dok_matrix((len(norm), len(norm)))
normalizer.setdiag(norm)
matrix = normalizer.tocsr().dot(matrix)
return matrix
def guess(sims, analogy, analogy_type, iw, wi, word_a, word_b, word_c):
sim_a = sims[analogy[analogy_type]["wi"][word_a]]
sim_b = sims[analogy[analogy_type]["wi"][word_b]]
sim_c = sims[analogy[analogy_type]["wi"][word_c]]
add_sim = -sim_a+sim_b+sim_c
add_sim[wi[word_a]] = 0
add_sim[wi[word_b]] = 0
add_sim[wi[word_c]] = 0
guess_add = iw[np.nanargmax(add_sim)]
mul_sim = sim_b * sim_c * np.reciprocal(sim_a+0.01)
mul_sim[wi[word_a]] = 0
mul_sim[wi[word_b]] = 0
mul_sim[wi[word_c]] = 0
guess_mul = iw[np.nanargmax(mul_sim)]
return guess_add, guess_mul
def main():
neg = 1
vectors_path = "embedding_sample/sparse_small.txt"
analogy_path = "CA8/morphological.txt"
results = {}
myParser = argparse.ArgumentParser()
myParser.add_argument('-v', '--vectors', type=str, help="Vectors path")
myParser.add_argument('-a', '--analogy', type=str, help="Analogy benchmark path")
args = myParser.parse_args()
if args.vectors:
vectors_path = args.vectors
if args.analogy:
analogy_path = args.analogy
matrix, iw, wi = load_matrix(vectors_path) # Read matrix into the memory
matrix = normalize(matrix)
analogy = read_analogy(analogy_path, iw)
for analogy_type in analogy.keys(): # Calculate the accuracy for each relation type
correct_add_num, correct_mul_num = 0, 0
analogy_matrix = matrix[[wi[w] if w in wi else random.randint(0, len(wi)-1) for w in analogy[analogy_type]["iw"]]]
sims = analogy_matrix.dot(matrix.T)
sims = np.array(sims.todense())
for question in analogy[analogy_type]["questions"]: # Loop for each analogy question
word_a, word_b, word_c, word_d = question
guess_add, guess_mul = guess(sims, analogy, analogy_type, iw, wi, word_a, word_b, word_c)
if guess_add == word_d:
correct_add_num += 1
if guess_mul == word_d:
correct_mul_num += 1
cov = float(analogy[analogy_type]["seen"]) / analogy[analogy_type]["total"]
if analogy[analogy_type]["seen"] == 0:
acc_add = 0
acc_mul = 0
print (analogy_type + " add/mul: " + str(round(0.0, 3)) + "/" + str(round(0.0, 3)))
else:
acc_add = float(correct_add_num) / analogy[analogy_type]["seen"]
acc_mul = float(correct_mul_num) / analogy[analogy_type]["seen"]
print (analogy_type + " add/mul: " + str(round(acc_add, 3)) + "/" + str(round(acc_mul, 3)))
# Store the results
results[analogy_type] = {}
results[analogy_type]["coverage"] = [cov, analogy[analogy_type]["seen"], analogy[analogy_type]["total"]]
results[analogy_type]["accuracy_add"] = [acc_add, correct_add_num, analogy[analogy_type]["seen"]]
results[analogy_type]["accuracy_mul"] = [acc_mul, correct_mul_num, analogy[analogy_type]["seen"]]
correct_add_num, correct_mul_num, seen = 0, 0, 0
for analogy_type in results:
correct_add_num += results[analogy_type]["accuracy_add"][1]
correct_mul_num += results[analogy_type]["accuracy_mul"][1]
seen += results[analogy_type]["coverage"][1]
# print results
print("Total accuracy (add): " + str(round(float(correct_add_num)/seen, 3)))
print("Total accuracy (mul): " + str(round(float(correct_mul_num)/seen, 3)))
if __name__ == '__main__':
main()