forked from gamboviol/lsh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ann.py
269 lines (203 loc) · 6.99 KB
/
ann.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
"""
Nearest neighbour searches in sublinear time
using LSH Locality Sensitive Hashing
simple tutorial implementation based on
A. Andoni and P. Indyk, "Near-optimal hashing algorithms for approximate
nearest neighbor in high dimensions"
http://people.csail.mit.edu/indyk/p117-andoni.pdf
"""
from abc import abstractmethod
from itertools import izip, imap, chain
from math import sqrt
from random import gauss as random_gauss, \
uniform as random_uniform
from collections import defaultdict
from operator import itemgetter
from util import gapply, lapply, dot
''' Metrics
'''
def L1_norm(u, v):
"""L1 hash metric - Rectilinear (Manhattan) distance"""
return sum(abs(ui - vi) for ui, vi in izip(u, v))
def L2_norm(u, v):
"""L2 hash metric - Euclidean distance"""
return sqrt(sum((ui - vi) ** 2.0 for ui, vi in izip(u, v)))
def Cosine_norm(u, v):
"""Cosine hash metric - Angular distance"""
return 1.0 - dot(u, v) / sqrt(dot(u, u) * dot(v, v))
''' Hashes
'''
class Hash:
def __init__(self, r):
"""Initialize
:param r: a random vector
"""
self.r = list(r)
@abstractmethod
def hash(self, vec):
"""Hash a vector of integers"""
pass
class L1Hash(Hash):
def __init__(self, r, w):
"""
Initialize
:param r: a random vector
:param w: width of the quantization bin
"""
Hash.__init__(self, r)
self.w = w
def hash(self, vec):
float_gen = ((vec[idx] - s) / self.w
for idx, s in enumerate(self.r))
return hash(tuple(imap(int, float_gen)))
class L2Hash(Hash):
def __init__(self, r, b, w):
"""
Initialize
:param r: a random vector
:param b: a random variable uniformly distributed between 0 and w
:param w: width of the quantization bin
"""
Hash.__init__(self, r)
self.b = b
self.w = w
def hash(self, vec):
val = (dot(vec, self.r) + self.b) / self.w
return int(val)
class CosineHash(Hash):
def hash(self, vec):
return int(dot(vec, self.r) > 0)
''' Hash families
'''
class HashFamily:
def __init__(self, size):
self.size = int(size)
@abstractmethod
def get_hash_func(self):
pass
@abstractmethod
def get_projection(self):
pass
def combine(self, hashes):
""" combine hash values
:param hashes: an iterable representing a vector of hashes
"""
return hash(tuple(hashes))
class L1HashFamily(HashFamily):
def __init__(self, size, w):
"""
Initialize
:param size: size of hash family
:param w: width of the quantization bin
"""
HashFamily.__init__(self, size)
self.w = float(w)
def get_hash_func(self):
""" initialize each L1Hash with a different random
partition vector"""
return L1Hash(self.get_projection(), self.w)
def get_projection(self):
"""Return a vector of size d drawn from a uniform
distribution from 0 to w"""
return gapply(self.size, random_uniform, 0, self.w)
class L2HashFamily(HashFamily):
def __init__(self, size, w):
"""
Initialize
:param size: size of hash family
:param w: width of the quantization bin
"""
HashFamily.__init__(self, size)
self.w = float(w)
def get_hash_func(self):
"""initialize each L2Hash with a different random projection vector
and offset
"""
return L2Hash(self.get_projection(), random_uniform(0, self.w), self.w)
def get_projection(self):
"""Return a vector of size d drawn from a Gaussian
distribution with mean 0 and sigma 1
"""
return gapply(self.size, random_gauss, 0, 1)
class CosineHashFamily(HashFamily):
def get_hash_func(self):
"""initialize each CosineHash with a random projection vector"""
return CosineHash(self.get_projection())
def get_projection(self):
"""Random projection vector"""
return gapply(self.size, random_gauss, 0, 1)
def combine(self, hashes):
""" combine by treating as a bit-vector """
return sum(1 << idx if h > 0 else 0
for idx, h in enumerate(hashes))
''' Index
'''
class LSHIndex:
tot_touched = 0
num_queries = 0
points = []
def __init__(self, hash_family, k, L):
"""Initialize
:param hash_family: HashFamily instance
:param k: hash table size (increases selectivity)
:param L: number of hash tables (increases recall)
"""
self.hash_family = hash_family
self.k = k
self.L = 0
self.hash_tables = []
self.resize(L)
def resize(self, L):
""" update the number of hash tables to be used
:param L: number of hash tables (increases recall)
"""
if L < self.L:
self.hash_tables = self.hash_tables[:L]
else:
# initialise a new hash table for each hash function
hash_funcs = lapply(L - self.L, # rows
lapply, self.k, # columns
self.hash_family.get_hash_func)
self.hash_tables.extend((g, defaultdict(list))
for g in hash_funcs)
def hash(self, g, point):
"""
:param g: A vector of Hash instances
:param point: A point vector
:return: A combined hash digest
"""
return self.hash_family.combine(h.hash(point) for h in g)
def index(self, pts):
""" index the supplied points
:param pts: A list of points (represented as vectors)
"""
self.points = pts
for g, table in self.hash_tables:
for idx, p in enumerate(self.points):
table_idx = self.hash(g, p)
table[table_idx].append(idx)
# reset stats
self.tot_touched = 0
self.num_queries = 0
def query(self, query, metric, max_results):
""" find the max_results closest indexed points to query according
to the supplied metric
:param query: query vector
:param metric: distance metric to use
:param max_results: maximum number of results to return
:returns : sorted list of tuples of form <index, distance>
:rtype : list
"""
candidates = set(chain.from_iterable(
table.get(self.hash(g, query), [])
for g, table in self.hash_tables))
# update stats
self.tot_touched += len(candidates)
self.num_queries += 1
# re-rank candidates according to supplied metric
return sorted(((idx, metric(query, self.points[idx]))
for idx in candidates),
key=itemgetter(1))[:max_results]
def get_avg_touched(self):
""" mean number of candidates inspected per query """
return float(self.tot_touched) / float(self.num_queries)