-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalogies.py
188 lines (139 loc) · 5.1 KB
/
analogies.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
import time
import gc
import pickle
import numpy as np
from tqdm import tqdm
RAM_SIZE = 100
CHUNK_SIZE = 1000
DIM = 100
def analogy_query(kdtree, w2v, word_a, word_b, word_c):
exclude_w = [word_a, word_b, word_c]
exclude_e = [w2v[w] for w in exclude_w]
print(word_b, "-", word_a, "+", word_c)
analogy_vector = np.array(w2v[word_b]) - np.array(w2v[word_a]) + np.array(w2v[word_c])
if not kdtree:
result = None
min = np.inf
for w, e in tqdm(w2v.items()):
if w in exclude_w:
continue
dist = np.linalg.norm(e - analogy_vector)
if dist < min:
min = dist
result = e
return result
elif kdtree.__class__.__module__ == "kdtree":
return kdtree.nearest_neighbor(analogy_vector, exclude_e)[0]
elif kdtree.__class__.__module__ == "sklearn.neighbors._kd_tree":
embeddings = list(w2v.values())
dist, ind = kdtree.query(analogy_vector.reshape(1, -1), k=4)
for i in ind[0]:
if embeddings[i] not in exclude_e:
result = embeddings[i]
break
del embeddings
gc.collect()
return result
def read_file(filename, encoding="latin-1"):
with open(filename, "r", encoding=encoding) as fin:
next(fin)
n = 1
while True:
word_embeddings = {}
for _ in range(RAM_SIZE):
for _ in range(CHUNK_SIZE):
line = fin.readline()
if line == "":
with open(f"data/embeddings-{n}.pkl", "wb") as fout:
pickle.dump(word_embeddings, fout)
print("Clearing memory")
del word_embeddings
gc.collect()
return
content = line.strip().split(" ")
try:
w = content[0]
w = w.encode("latin-1").decode("utf-8")
except UnicodeError as ue:
print(f"{ue} encountered on word: {w}")
continue
e = [float(c) for c in content[1:]]
d = len(e)
if d == DIM:
word_embeddings[w] = e
else:
print(
f"Embedding dimension missing on word {w}: "
f"expected {DIM}, actual: {d}"
)
print(f"Dumping to: embeddings-{n}.pkl")
with open(f"embeddings-{n}.pkl", "wb") as fout:
pickle.dump(word_embeddings, fout)
print("Clearing memory")
del word_embeddings
gc.collect()
n += 1
def eval(eval_file, kdtree, w2v, v2w):
with open(eval_file, "r") as f:
correct = 0
total = 0
times = []
for line in f:
line = line.strip().split(" ")
word_a = line[0]
word_b = line[1]
word_c = line[2]
y_true = line[3]
words = w2v.keys()
if any(w not in words for w in line):
print(f"Missing words: {[w for w in line if w not in words]}")
continue
start_time = time.time()
result = tuple(
analogy_query(kdtree, w2v, word_a, word_b, word_c)
)
end_time = time.time()
elapsed_time = end_time - start_time
times.append(elapsed_time)
print(f"Time taken: {elapsed_time:.6f} seconds")
print(f"Predicted: {v2w[result]}, expected: {y_true}")
if v2w[result] == y_true:
correct += 1
total += 1
acc = correct / total
return acc, np.array(times)
def build_kdtree(w2v, implementation=None):
if not implementation:
return None
embeddings = np.array(list(w2v.values()))
if implementation == "local":
from kdtree import KDTree as MyKDTree
kdtree = MyKDTree(embeddings)
elif implementation == "sklearn":
from sklearn.neighbors import KDTree as SklKDTree
kdtree = SklKDTree(embeddings)
del embeddings
gc.collect()
return kdtree
def run_experiment(embeddings_file, eval_file):
with open(embeddings_file, "rb") as f:
w2v = pickle.load(f)
v2w = {tuple(vec): word for word, vec in w2v.items()}
accs = []
for implementation in [None, "local", "sklearn"]:
if implementation:
times_file = f"times/times-{implementation}.npy"
else:
times_file = "times/times-exhaustive.npy"
kdtree = build_kdtree(w2v, implementation=implementation)
acc, times = eval(eval_file, kdtree, w2v, v2w)
print("Accuracy:", acc)
print(f"Saving times to {times_file}")
accs.append(acc)
np.save(times_file, times)
assert len(set(accs)) == 1
def main():
# read_file("data/model.txt")
run_experiment("data/embeddings-1.pkl", "data/analogies.txt")
if __name__ == "__main__":
main()