-
Notifications
You must be signed in to change notification settings - Fork 6
/
evaluate_candidates.py
151 lines (112 loc) · 4.92 KB
/
evaluate_candidates.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
__author__ = 'rwechsler'
import cPickle as pickle
import itertools
import random
from annoy import AnnoyIndex
import multiprocessing as mp
import sys
import argparse
import time
import datetime
import numpy as np
import gensim
def timestamp():
return datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
def load_candidate_dump(file_name):
return pickle.load(open(file_name, "rb"))
def load_annoy_tree(model_file_name, vector_dims):
tree = AnnoyIndex(vector_dims)
tree.load(model_file_name)
return tree
def load_word2vecmodel(file_name):
return gensim.models.Word2Vec.load_word2vec_format(file_name, binary=True)
def annoy_knn(annoy_tree, vector, true_index, k=100):
neighbours = annoy_tree.get_nns_by_vector(list(vector), k)
if true_index in neighbours:
return True
else:
return False
def test_pair(pair1, pair2, word2vec_model, k=100, show=30):
"""
Only used in interactive mode so far.
:param pair1:
:param pair2:
:param word2vec_model:
:param k:
:param show:
:return:
"""
prefix = pair1[0]
fl1 = pair1[1]
tail1 = pair1[2]
prefix2 = pair2[0]
fl2 = pair2[1]
tail2 = pair2[2]
assert prefix == prefix2
diff = word2vec_model[prefix + fl2 + tail2.lower()] - word2vec_model[tail2]
predicted = word2vec_model[tail1] + diff
true_word = prefix + fl1 + tail1.lower()
neighbours = word2vec_model.most_similar([predicted], topn=k)
print neighbours[:show]
neighbours, _ = zip(*neighbours)
print "Found: ", true_word in neighbours
def candidate_generator(candidates, rank_threshold):
for prefix in candidates:
yield (prefix, candidates[prefix], rank_threshold)
def mp_wrapper_evaluate_set(argument):
return evaluate_set(*argument)
if __name__ == "__main__":
#### Default Parameters-------------------------------------------####
rank_threshold = 100
sample_set_size = 500
n_processes = 2
####End-Parametes-------------------------------------------------####
parser = argparse.ArgumentParser(description='Evaluate candidates')
parser.add_argument('-w', action='store', dest="word2vec_file", required=True)
parser.add_argument('-d', action="store", dest="vector_dims", type=int, required=True)
parser.add_argument('-t', action="store", dest="annoy_tree_file", required=True)
parser.add_argument('-c', action="store", dest="candidates_index_file", required=True)
parser.add_argument('-o', action="store", dest="result_output_file", required=True)
parser.add_argument('-p', action="store", dest="n_processes", type=int, default=n_processes)
parser.add_argument('-s', action="store", dest="sample_set_size", type=int, default=sample_set_size)
parser.add_argument('-r', action="store", dest="rank_threshold", type=int, default=rank_threshold)
arguments = parser.parse_args(sys.argv[1:])
print timestamp(), "loading candidates"
candidates = load_candidate_dump(arguments.candidates_index_file)
print timestamp(), "loading word2vec model"
word2vec_model = load_word2vecmodel(arguments.word2vec_file)
print timestamp(), "preprocess candidates"
# only store vectors that we need. And sample already.
word2vec_vectors = dict()
for cand in candidates:
if len(candidates[cand]) > arguments.sample_set_size:
candidates[cand] = set(random.sample(candidates[cand], arguments.sample_set_size))
for (i,j) in candidates[cand]:
word2vec_vectors[i] = np.array(word2vec_model.syn0[i])
word2vec_vectors[j] = np.array(word2vec_model.syn0[j])
del word2vec_model
print timestamp(), "number of vectors: ", len(word2vec_vectors)
print timestamp(), "load annoy tree"
# global annoy_tree
annoy_tree = load_annoy_tree(arguments.annoy_tree_file, arguments.vector_dims)
def evaluate_set(prefix, tails, rank_threshold=100):
global annoy_tree
global word2vec_vectors
counts = dict()
counts[True] = 0
counts[False] = 0
#print mp.current_process().name, id(annoy_tree), id(word2vec_vectors), prefix.encode('utf-8')
#sys.stdout.flush()
for (comp1, tail1), (comp2, tail2) in itertools.combinations(tails, 2):
diff = word2vec_vectors[comp2]- word2vec_vectors[tail2]
predicted = word2vec_vectors[tail1] + diff
result = annoy_knn(annoy_tree, predicted, comp1, rank_threshold)
counts[result] += 1
return (prefix, float(counts[True]) / (counts[True] + counts[False])) if counts[True] + counts[False] > 0 else (prefix, 0.0)
print timestamp(), "evaluating candidates"
pool = mp.Pool(processes=arguments.n_processes)
params = candidate_generator(candidates, arguments.rank_threshold)
results = pool.map(mp_wrapper_evaluate_set, params)
print timestamp(), "pickling"
pickle.dump(results, open(arguments.result_output_file, "wb"))
print timestamp(), "done"