Skip to content
Navigation Menu
{{ message }}
forked from tdebatty/java-LSH
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSuperBit.java
More file actions
312 lines (267 loc) · 9.31 KB
/
Copy pathSuperBit.java
File metadata and controls
312 lines (267 loc) · 9.31 KB
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
* The MIT License
*
* Copyright 2015 Thibault Debatty.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package info.debatty.java.lsh;
import info.debatty.java.utils.SparseDoubleVector;
import info.debatty.java.utils.SparseIntegerVector;
import java.io.Serializable;
import java.util.Random;
/**
* Implementation of Super-Bit Locality-Sensitive Hashing.
* Super-Bit is an improvement of Random Projection LSH.
* It computes an estimation of cosine similarity.
*
* Super-Bit Locality-Sensitive Hashing
* Jianqiu Ji, Jianmin Li, Shuicheng Yan, Bo Zhang, Qi Tian
* http://papers.nips.cc/paper/4847-super-bit-locality-sensitive-hashing.pdf
* Advances in Neural Information Processing Systems 25, 2012
*
* Supported input types:
* - SparseIntegerVector
* - double[]
* - others to come...
*
* @author Thibault Debatty
*/
public class SuperBit implements Serializable {
private double[][] hyperplanes;
private static final int DEFAULT_CODE_LENGTH = 10000;
/**
* Initialize SuperBit algorithm.
* Super-Bit depth n must be [1 .. d] and number of Super-Bit l in [1 ..
* The resulting code length k = n * l
* The K vectors are orthogonalized in L batches of N vectors
*
* @param d data space dimension
* @param n Super-Bit depth [1 .. d]
* @param l number of Super-Bit [1 ..
*/
public SuperBit(final int d, final int n, final int l) {
this(d, n, l, new Random());
}
/**
* Initialize SuperBit algorithm.
* Super-Bit depth n must be [1 .. d] and number of Super-Bit l in [1 ..
* The resulting code length k = n * l
* The K vectors are orthogonalized in L batches of N vectors
*
* @param d data space dimension
* @param n Super-Bit depth [1 .. d]
* @param l number of Super-Bit [1 ..
* @param seed to use for the random number generator
*/
public SuperBit(final int d, final int n, final int l, final long seed) {
this(d, n, l, new Random(seed));
}
private SuperBit(final int d, final int n, final int l, final Random rand) {
if (d <= 0) {
throw new IllegalArgumentException("Dimension d must be >= 1");
}
if (n < 1 || n > d) {
throw new IllegalArgumentException(
"Super-Bit depth N must be 1 <= N <= d");
}
if (l < 1) {
throw new IllegalArgumentException(
"Number of Super-Bit L must be >= 1");
}
// Input: Data space dimension d, Super-Bit depth 1 <= N <= d,
// number of Super-Bit L >= 1,
// resulting code length K = N * L
// Generate a random matrix H with each element sampled independently
// from the normal distribution
// N (0, 1), with each column normalized to unit length.
// Denote H = [v1, v2, ..., vK].
int code_length = n * l;
double[][] v = new double[code_length][d];
for (int i = 0; i < code_length; i++) {
double[] vector = new double[d];
for (int j = 0; j < d; j++) {
vector[j] = rand.nextGaussian();
}
normalize(vector);
v[i] = vector;
}
// for i = 0 to L - 1 do
// for j = 1 to N do
// w_{iN+j} = v_{iN+j}
// for k = 1 to j - 1 do
// w_{iN+j} = w_{iN+j} - w_{iN+k} w^T_{iN+k} v_{iN+j}
// end for
// wiN+j = wiN+j / | wiN+j |
// end for
// end for
// Output: H˜ = [w1, w2, ..., wK]
double[][] w = new double[code_length][d];
for (int i = 0; i <= l - 1; i++) {
for (int j = 1; j <= n; j++) {
java.lang.System.arraycopy(
v[i * n + j - 1],
0,
w[i * n + j - 1],
0,
d);
for (int k = 1; k <= (j - 1); k++) {
w[i * n + j - 1] = sub(
w[i * n + j - 1],
product(
dotProduct(
w[i * n + k - 1],
v[ i * n + j - 1]),
w[i * n + k - 1]));
}
normalize(w[i * n + j - 1]);
}
}
this.hyperplanes = w;
}
/**
* Initialize SuperBit algorithm.
* With code length K = 10000
* The K vectors are orthogonalized in d batches of 10000/d vectors
* The resulting mean error is 0.01
* @param d
*/
public SuperBit(final int d) {
this(d, d, DEFAULT_CODE_LENGTH / d);
}
/**
* Initialize SuperBit algorithm without parameters
* (used only for serialization).
*/
public SuperBit() {
}
/**
* Compute the signature of this vector.
* @param vector
* @return
*/
public final boolean[] signature(final SparseIntegerVector vector) {
boolean[] sig = new boolean[this.hyperplanes.length];
for (int i = 0; i < this.hyperplanes.length; i++) {
sig[i] = (vector.dotProduct(this.hyperplanes[i]) >= 0);
}
return sig;
}
/**
* Compute the signature of this vector.
* @param vector
* @return
*/
public final boolean[] signature(final SparseDoubleVector vector) {
boolean[] sig = new boolean[this.hyperplanes.length];
for (int i = 0; i < this.hyperplanes.length; i++) {
sig[i] = (vector.dotProduct(this.hyperplanes[i]) >= 0);
}
return sig;
}
/**
* Compute the signature of this vector.
* @param vector
* @return
*/
public final boolean[] signature(final double[] vector) {
boolean[] sig = new boolean[this.hyperplanes.length];
for (int i = 0; i < this.hyperplanes.length; i++) {
sig[i] = (dotProduct(this.hyperplanes[i], vector) >= 0);
}
return sig;
}
/**
* Compute the similarity between two signature, which is also an
* estimation of the cosine similarity between the two vectors.
*
* @param sig1
* @param sig2
* @return estimated cosine similarity
*/
public final double similarity(final boolean[] sig1, final boolean[] sig2) {
double agg = 0;
for (int i = 0; i < sig1.length; i++) {
if (sig1[i] == sig2[i]) {
agg++;
}
}
agg = agg / sig1.length;
return Math.cos((1 - agg) * Math.PI);
}
/**
* Get the hyperplanes coefficients used to compute signatures.
* @return
*/
public final double[][] getHyperplanes() {
return this.hyperplanes;
}
/* ---------------------- STATIC ---------------------- */
/**
* Computes the cosine similarity, computed as v1 dot v2 / (|v1| * |v2|).
* Cosine similarity of two vectors is the cosine of the angle between them.
* It ranges between -1 and +1
*
* @param v1
* @param v2
* @return
*/
public static double cosineSimilarity(final double[]v1, final double[] v2) {
return dotProduct(v1, v2) / (norm(v1) * norm(v2));
}
private static double[] product(final double x, final double[] v) {
double[] r = new double[v.length];
for (int i = 0; i < v.length; i++) {
r[i] = x * v[i];
}
return r;
}
private static double[] sub(final double[] a, final double[] b) {
double[] r = new double[a.length];
for (int i = 0; i < a.length; i++) {
r[i] = a[i] - b[i];
}
return r;
}
private static void normalize(final double[] vector) {
double norm = norm(vector);
for (int i = 0; i < vector.length; i++) {
vector[i] = vector[i] / norm;
}
}
/**
* Returns the norm L2. sqrt(sum_i(v_i^2))
* @param v
* @return
*/
private static double norm(final double[] v) {
double agg = 0;
for (int i = 0; i < v.length; i++) {
agg += (v[i] * v[i]);
}
return Math.sqrt(agg);
}
private static double dotProduct(final double[] v1, final double[] v2) {
double agg = 0;
for (int i = 0; i < v1.length; i++) {
agg += (v1[i] * v2[i]);
}
return agg;
}
}
You can’t perform that action at this time.
