-
Notifications
You must be signed in to change notification settings - Fork 6
/
evaluate_candidates_mt.py
165 lines (121 loc) · 5.27 KB
/
evaluate_candidates_mt.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
__author__ = 'lqrz'
import cPickle as pickle
import gensim
import itertools
import random
from annoy import AnnoyIndex
import sys
import argparse
import time
import datetime
import numpy as np
import threading
import Queue
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_word2vecmodel(file_name):
return gensim.models.Word2Vec.load_word2vec_format(file_name, binary=True)
def load_annoy_tree(model_file_name, vector_dims):
tree = AnnoyIndex(vector_dims)
tree.load(model_file_name)
return tree
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, annoy_tree_file, vector_dims, rank_threshold, sample_size):
for prefix in candidates:
yield (prefix, candidates[prefix], annoy_tree_file, vector_dims, rank_threshold, sample_size)
def mp_wrapper_evaluate_set(argument):
return evaluate_set(*argument)
if __name__ == "__main__":
#lqrz
contentQueue = Queue.Queue()
indexQueue = Queue.Queue()
#### Default Parameters-------------------------------------------####
rank_threshold = 100
sample_set_size = 500
n_processes = 2
####End-Parametes-------------------------------------------------####
parser = argparse.ArgumentParser(description='Evaluate candidates')
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)
annoy_tree = load_annoy_tree(arguments.annoy_tree_file, arguments.vector_dims)
print 'Global annoy tree', id(annoy_tree)
def evaluate_set(contentQueue, indexQueue):
while not contentQueue.empty():
counts = dict()
counts[True] = 0
counts[False] = 0
t = contentQueue.get()
prefix = t[0]
tails = t[1]
annoy_tree= t[2]
rank_threshold = t[3]
sample_size = t[4]
print prefix, id(annoy_tree)
if len(tails) > sample_size:
tails = random.sample(tails, sample_size)
for (comp1, tail1), (comp2, tail2) in itertools.combinations(tails, 2):
diff = np.array(annoy_tree.get_item_vector(comp2))- np.array(annoy_tree.get_item_vector(tail2))
predicted = np.array(annoy_tree.get_item_vector(tail1)) + diff
result = annoy_knn(annoy_tree, predicted, comp1, rank_threshold)
counts[result] += 1
#place tuple into out queue
tOut = (prefix, float(counts[True]) / (counts[True] + counts[False])) if counts[True] + counts[False] > 0 else (prefix, 0.0)
indexQueue.put(tOut)
#signals to queue job is done
contentQueue.task_done()
for prefix in candidates:
contentQueue.put(((prefix, candidates[prefix], annoy_tree, arguments.rank_threshold, arguments.sample_set_size)))
print timestamp(), "evaluating candidates"
nThreads = arguments.n_processes
threads = [threading.Thread(target=evaluate_set, args=(contentQueue, indexQueue)) for _ in range(nThreads)]
for t in threads:
t.setDaemon(True)
t.start()
contentQueue.join()
# returns tuples in the form: (prefix, acc)
results = [indexQueue.get() for _ in range(len(candidates))]
# results = evaluate_candidates(candidates, arguments.annoy_tree_file, arguments.vector_dims, rank_threshold=arguments.rank_threshold,
# sample_size=arguments.sample_set_size, processes=arguments.n_processes)
print timestamp(), "pickling"
pickle.dump(results, open(arguments.result_output_file, "wb"))
print timestamp(), "done"
print results