-
Notifications
You must be signed in to change notification settings - Fork 15
/
multiprobe.go
273 lines (247 loc) · 6.63 KB
/
multiprobe.go
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
270
271
272
273
package lsh
import (
"container/heap"
"math/rand"
)
type perturbSet map[int]bool
func (ps perturbSet) isValid(m int) bool {
for key := range ps {
// At most one perturbation on same index.
if _, ok := ps[2*m+1-key]; ok {
return false
}
// No keys larger than 2m.
if key > 2*m {
return false
}
}
return true
}
func (ps perturbSet) shift() perturbSet {
next := make(perturbSet)
max := 0
for k := range ps {
if k > max {
max = k
}
next[k] = true
}
delete(next, max)
next[max+1] = true
return next
}
func (ps perturbSet) expand() perturbSet {
next := make(perturbSet)
max := 0
for k := range ps {
if k > max {
max = k
}
next[k] = true
}
next[max+1] = true
return next
}
// A pair of perturbation set and its score.
type perturbSetPair struct {
ps perturbSet
score float64
}
// perturbSetHeap is a min-heap of perturbSetPairs.
type perturbSetHeap []perturbSetPair
func (h perturbSetHeap) Len() int { return len(h) }
func (h perturbSetHeap) Less(i, j int) bool { return h[i].score < h[j].score }
func (h perturbSetHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *perturbSetHeap) Push(x interface{}) {
// Push and Pop use pointer receivers because they modify the slice's length,
// not just its contents.
*h = append(*h, x.(perturbSetPair))
}
func (h *perturbSetHeap) Pop() interface{} {
old := *h
n := len(old)
x := old[n-1]
*h = old[0 : n-1]
return x
}
// MultiprobeLsh implements the Multi-probe LSH algorithm by Qin Lv et.al.
// The Multi-probe LSH does not support k-NN query directly.
type MultiprobeLsh struct {
*BasicLsh
// The size of our probe sequence.
t int
// The scores of perturbation values.
scores []float64
perturbSets []perturbSet
// Each hash table has a list of perturbation vectors
// each perturbation vector is list of -+ 1 or 0 that will
// be applied to the hashTableKey of the query hash value
// t x l x m
perturbVecs [][][]int
}
// NewMultiprobeLsh creates a new Multi-probe LSH for L2 distance.
// dim is the diminsionality of the data, l is the number of hash
// tables to use, m is the number of hash values to concatenate to
// form the key to the hash tables, and w is the slot size for the
// family of LSH functions.
// t is the number of perturbation vectors that will be applied to
// each query.
// Increasing t increases the running time of the Query function.
func NewMultiprobeLsh(dim, l, m int, w float64, t int) *MultiprobeLsh {
index := &MultiprobeLsh{
BasicLsh: NewBasicLsh(dim, l, m, w),
t: t,
}
index.initProbeSequence()
return index
}
func (index *MultiprobeLsh) initProbeSequence() {
m := index.m
index.scores = make([]float64, 2*m)
// Use j's starting from 1 to match the paper.
for j := 1; j <= m; j++ {
index.scores[j-1] = float64(j*(j+1)) / float64(4*(m+1)*(m+2))
}
for j := m + 1; j <= 2*m; j++ {
index.scores[j-1] = 1 - float64(2*m+1-j)/float64(m+1) + float64((2*m+1-j)*(2*m+2-j))/float64(4*(m+1)*(m+2))
}
index.genPerturbSets()
index.genPerturbVecs()
}
func (index *MultiprobeLsh) getScore(ps *perturbSet) float64 {
score := 0.0
for j := range *ps {
score += index.scores[j-1]
}
return score
}
func (index *MultiprobeLsh) genPerturbSets() {
setHeap := make(perturbSetHeap, 1)
start := perturbSet{1: true}
setHeap[0] = perturbSetPair{
ps: start,
score: index.getScore(&start),
}
heap.Init(&setHeap)
index.perturbSets = make([]perturbSet, index.t)
m := index.m
for i := 0; i < index.t; i++ {
for counter := 0; true; counter++ {
currentTop := heap.Pop(&setHeap).(perturbSetPair)
nextShift := currentTop.ps.shift()
heap.Push(&setHeap, perturbSetPair{
ps: nextShift,
score: index.getScore(&nextShift),
})
nextExpand := currentTop.ps.expand()
heap.Push(&setHeap, perturbSetPair{
ps: nextExpand,
score: index.getScore(&nextExpand),
})
if currentTop.ps.isValid(m) {
index.perturbSets[i] = currentTop.ps
break
}
if counter >= 2*m {
panic("too many iterations, probably infinite loop!")
}
}
}
}
func (index *MultiprobeLsh) genPerturbVecs() {
// First we need to generate the permutation tables
// that maps the ids of the unit perturbation in each
// perturbation set to the index of the unit hash
// value
perms := make([][]int, index.l)
for i := range index.tables {
random := rand.New(rand.NewSource(int64(i)))
perm := random.Perm(index.m)
perms[i] = make([]int, index.m*2)
for j := 0; j < index.m; j++ {
perms[i][j] = perm[j]
}
for j := 0; j < index.m; j++ {
perms[i][j+index.m] = perm[index.m-1-j]
}
}
// Generate the vectors
index.perturbVecs = make([][][]int, len(index.perturbSets))
for i, ps := range index.perturbSets {
perTableVecs := make([][]int, index.l)
for j := range perTableVecs {
vec := make([]int, index.m)
for k := range ps {
mapped_ind := perms[j][k-1]
if k > index.m {
// If it is -1
vec[mapped_ind] = -1
} else {
// if it is +1
vec[mapped_ind] = 1
}
}
perTableVecs[j] = vec
}
index.perturbVecs[i] = perTableVecs
}
}
func (index *MultiprobeLsh) queryHelper(tableKeys []hashTableKey, out chan<- string) {
// Apply hash functions
hvs := index.toBasicHashTableKeys(tableKeys)
// Lookup in each table.
for i, table := range index.tables {
if candidates, exist := table[hvs[i]]; exist {
for _, id := range candidates {
out <- id
}
}
}
}
// perturb returns the result of applying perturbation on each baseKey.
func (index *MultiprobeLsh) perturb(baseKey []hashTableKey, perturbation [][]int) []hashTableKey {
if len(baseKey) != len(perturbation) {
panic("Number tables does not match with number of perturb vecs")
}
perturbedTableKeys := make([]hashTableKey, len(baseKey))
for i, p := range perturbation {
perturbedTableKeys[i] = make(hashTableKey, index.m)
for j, h := range baseKey[i] {
perturbedTableKeys[i][j] = h + p[j]
}
}
return perturbedTableKeys
}
// Query finds the ids of nearest neighbour candidates,
// given the query point
func (index *MultiprobeLsh) Query(q Point) []string {
// Hash
baseKey := index.hash(q)
// Query
results := make(chan string)
go func() {
defer close(results)
for i := 0; i < len(index.perturbVecs)+1; i++ {
perturbedTableKeys := baseKey
if i != 0 {
// Generate new hash key based on perturbation.
perturbedTableKeys = index.perturb(baseKey, index.perturbVecs[i-1])
}
// Perform lookup.
index.queryHelper(perturbedTableKeys, results)
}
}()
seen := make(map[string]bool)
for id := range results {
if _, exist := seen[id]; exist {
continue
}
seen[id] = true
}
// Collect results
ids := make([]string, 0, len(seen))
for id := range seen {
ids = append(ids, id)
}
return ids
}