Locality-sensitive hashing explained

In computer science, locality-sensitive hashing (LSH) is a fuzzy hashing technique that hashes similar input items into the same "buckets" with high probability.[1] (The number of buckets is much smaller than the universe of possible input items.) Since similar items end up in the same buckets, this technique can be used for data clustering and nearest neighbor search. It differs from conventional hashing techniques in that hash collisions are maximized, not minimized. Alternatively, the technique can be seen as a way to reduce the dimensionality of high-dimensional data; high-dimensional input items can be reduced to low-dimensional versions while preserving relative distances between items.

Hashing-based approximate nearest-neighbor search algorithms generally use one of two main categories of hashing methods: either data-independent methods, such as locality-sensitive hashing (LSH); or data-dependent methods, such as locality-preserving hashing (LPH).[2] [3]

Locality-preserving hashing was initially devised as a way to facilitate data pipelining in implementations of massively parallel algorithms that use randomized routing and universal hashing to reduce memory contention and network congestion.[4] [5]

Definitions

A finite family

lF

of functions

h\colonM\toS

is defined to be an LSH family[6] [7] for

lM=(M,d)

,

r>0

,

c>1

,

p1>p2

if it satisfies the following condition. For any two points

a,b\inM

and a hash function

h

chosen uniformly at random from

lF

:

d(a,b)\ler

, then

h(a)=h(b)

(i.e., and collide) with probability at least

p1

,

d(a,b)\gecr

, then

h(a)=h(b)

with probability at most

p2

.

Such a family

lF

is called

(r,cr,p1,p2)

-sensitive.

LSH with respect to a similarity measure

Alternatively[8] it is possible to define an LSH family on a universe of items endowed with a similarity function

\phi\colonU x U\to[0,1]

. In this setting, a LSH scheme is a family of hash functions coupled with a probability distribution over such that a function

h\inH

chosen according to satisfies

Pr[h(a)=h(b)]=\phi(a,b)

for each

a,b\inU

.

Amplification

Given a

(d1,d2,p1,p2)

-sensitive family

lF

, we can construct new families

lG

by either the AND-construction or OR-construction of

lF

.

To create an AND-construction, we define a new family

lG

of hash functions, where each function is constructed from random functions

h1,\ldots,hk

from

lF

. We then say that for a hash function

g\inlG

,

g(x)=g(y)

if and only if all

hi(x)=hi(y)

for

i=1,2,\ldots,k

. Since the members of

lF

are independently chosen for any

g\inlG

,

lG

is a

(d1,d2,

k,
p
1
k)
p
2
-sensitive family.

To create an OR-construction, we define a new family

lG

of hash functions, where each function is constructed from random functions

h1,\ldots,hk

from

lF

. We then say that for a hash function

g\inlG

,

g(x)=g(y)

if and only if

hi(x)=hi(y)

for one or more values of . Since the members of

lF

are independently chosen for any

g\inlG

,

lG

is a

(d1,d2,1-(1-

k,
p
1)

1-(1-

k)
p
2)
-sensitive family.

Applications

LSH has been applied to several problem domains, including:

Methods

Bit sampling for Hamming distance

One of the easiest ways to construct an LSH family is by bit sampling. This approach works for the Hamming distance over -dimensional vectors

\{0,1\}d

. Here, the family

lF

of hash functions is simply the family of all the projections of points on one of the

d

coordinates, i.e.,

{lF}=\{h\colon\{0,1\}d\to\{0,1\}\midh(x)=xiforsomei\in\{1,\ldots,d\}\}

, where

xi

is the

i

th coordinate of

x

. A random function

h

from

{lF}

simply selects a random bit from the input point. This family has the following parameters:

P1=1-R/d

,

P2=1-cR/d

.That is, any two vectors

x,y

with Hamming distance at most

R

collide under a random

h

with probability at least

P1

. Any

x,y

with Hamming distance at least

cR

collide with probability at most

P2

.

Min-wise independent permutations

See main article: MinHash.

Suppose is composed of subsets of some ground set of enumerable items and the similarity function of interest is the Jaccard index . If is a permutation on the indices of, for

A\subseteqS

let

h(A)=mina\{\pi(a)\}

. Each possible choice of defines a single hash function mapping input sets to elements of .

Define the function family to be the set of all such functions and let be the uniform distribution. Given two sets

A,B\subseteqS

the event that

h(A)=h(B)

corresponds exactly to the event that the minimizer of over

A\cupB

lies inside

A\capB

. As was chosen uniformly at random,

Pr[h(A)=h(B)]=J(A,B)

and

(H,D)

define an LSH scheme for the Jaccard index.

Because the symmetric group on elements has size !, choosing a truly random permutation from the full symmetric group is infeasible for even moderately sized . Because of this fact, there has been significant work on finding a family of permutations that is "min-wise independent" — a permutation family for which each element of the domain has equal probability of being the minimum under a randomly chosen . It has been established that a min-wise independent family of permutations is at least of size

\operatorname{lcm}\{1,2,\ldots,n\}\geen-o(n)

,[14] and that this bound is tight.[15]

Because min-wise independent families are too big for practical applications, two variant notions of min-wise independence are introduced: restricted min-wise independent permutations families, and approximate min-wise independent families.Restricted min-wise independence is the min-wise independence property restricted to certain sets of cardinality at most .[16] Approximate min-wise independence differs from the property by at most a fixed .[17]

Open source methods

Nilsimsa Hash

See main article: Nilsimsa Hash.

Nilsimsa is a locality-sensitive hashing algorithm used in anti-spam efforts.[18] The goal of Nilsimsa is to generate a hash digest of an email message such that the digests of two similar messages are similar to each other. The paper suggests that the Nilsimsa satisfies three requirements:

  1. The digest identifying each message should not vary significantly for changes that can be produced automatically.
  2. The encoding must be robust against intentional attacks.
  3. The encoding should support an extremely low risk of false positives.

Testing performed in the paper on a range of file types identified the Nilsimsa hash as having a significantly higher false positive rate when compared to other similarity digest schemes such as TLSH, Ssdeep and Sdhash.[19]

TLSH

TLSH is locality-sensitive hashing algorithm designed for a range of security and digital forensic applications.[13] The goal of TLSH is to generate hash digests for messages such that low distances between digests indicate that their corresponding messages are likely to be similar.

An implementation of TLSH is available as open-source software.[20]

Random projection

See main article: Random projection.

The random projection method of LSH due to Moses Charikar called SimHash (also sometimes called arccos[21]) uses an approximation of the cosine distance between vectors. The technique was used to approximate the NP-complete max-cut problem.

The basic idea of this technique is to choose a random hyperplane (defined by a normal unit vector) at the outset and use the hyperplane to hash input vectors.

Given an input vector and a hyperplane defined by, we let

h(v)=sgn(vr)

. That is,

h(v)=\pm1

depending on which side of the hyperplane lies. This way, each possible choice of a random hyperplane can be interpreted as a hash function

h(v)

.

For two vectors with angle

\theta(u,v)

between them, it can be shown that

Pr[h(u)=h(v)]=1-

\theta(u,v)
\pi

.

Since the ratio between

\theta(u,v)
\pi
and

1-\cos(\theta(u,v))

is at least 0.87856 when

\theta(u,v)\in[0,\pi]

,[22] the probability of two vectors being on the same side of the random hyperplane is approximately proportional to the cosine distance between them.

Stable distributions

The hash function[23]

ha,b(\boldsymbol{\upsilon}):l{R}d \tol{N}

maps a -dimensional vector

\boldsymbol{\upsilon}

onto the set of integers. Each hash functionin the family is indexed by a choice of random

a

and

b

where

a

is a -dimensional vector withentries chosen independently from a stable distribution and

b

isa real number chosen uniformly from the range [0,r]. For a fixed

a,b

the hash function

ha,b

isgiven by

ha,b(\boldsymbol{\upsilon})=\left\lfloor

a\boldsymbol{\upsilon
+b}{r}

\right\rfloor

.

Other construction methods for hash functions have been proposed to better fit the data. [24] In particular k-means hash functions are better in practice than projection-based hash functions, but without any theoretical guarantee.

Semantic hashing

Semantic hashing is a technique that attempts to map input items to addresses such that closer inputs have higher semantic similarity.[25] The hashcodes are found via training of an artificial neural network or graphical model.

Algorithm for nearest neighbor search

One of the main applications of LSH is to provide a method for efficient approximate nearest neighbor search algorithms. Consider an LSH family

lF

. The algorithm has two main parameters: the width parameter and the number of hash tables .

In the first step, we define a new family

lG

of hash functions, where each function is obtained by concatenating functions

h1,\ldots,hk

from

lF

, i.e.,

g(p)=[h1(p),\ldots,hk(p)]

. In other words, a random hash function is obtained by concatenating randomly chosen hash functions from

lF

. The algorithm then constructs hash tables, each corresponding to a different randomly chosen hash function .

In the preprocessing step we hash all -dimensional points from the data set into each of the hash tables. Given that the resulting hash tables have only non-zero entries, one can reduce the amount of memory used per each hash table to

O(n)

using standard hash functions.

Given a query point, the algorithm iterates over the hash functions . For each considered, it retrieves the data points that are hashed into the same bucket as . The process is stopped as soon as a point within distance from is found.

Given the parameters and, the algorithm has the following performance guarantees:

O(nLkt)

, where is the time to evaluate a function

h\inlF

on an input point ;

O(nL)

, plus the space for storing data points;
k))
O(L(kt+dnP
2
;

1-(1-

k
P
1

)L

;

For a fixed approximation ratio

c=1+\epsilon

and probabilities

P1

and

P2

, one can set

k=\left\lceil\tfrac{logn}{log1/P2}\right\rceil

and

L=\lceil

-k
P
1

\rceil=O(n\rho

-1
P
1

)

, where

\rho={\tfrac{logP1}{logP2}}

. Then one obtains the following performance guarantees:

O(n1+\rho

-1
P
1

kt)

;

O(n1+\rho

-1
P
1

)

, plus the space for storing data points;

O(n\rho

-1
P
1

(kt+d))

;

Improvements

When is large, it is possible to reduce the hashing time from

O(n\rho)

.This was shown by[26] and[27] which gave
2(1/P
O(tlog
2)/P

1+n\rho(d+1/P1))

;

O(n1+\rho/P1+

2(1/P
log
2)/P

1)

;

It is also sometimes the case that the factor

1/P1

can be very large.This happens for example with Jaccard similarity data, where even the most similar neighbor often has a quite low Jaccard similarity with the query.In[28] it was shown how to reduce the query time to
1-\rho
O(n
1

)

(not including hashing costs) and similarly the space usage.

See also

Further reading

External links

Notes and References

  1. Web site: Mining of Massive Datasets, Ch. 3.. Rajaraman. A.. Ullman. J.. Jeffrey Ullman. 2010.
  2. Zhao . Kang . Lu . Hongtao . Mei . Jincheng . Locality Preserving Hashing . AAAI Conference on Artificial Intelligence . 28 . 2014 . 2874–2880.
  3. Book: Tsai . Yi-Hsuan . Yang . Ming-Hsuan . 2014 IEEE International Conference on Image Processing (ICIP) . Locality preserving hashing . October 2014 . 2988–2992 . 10.1109/ICIP.2014.7025604 . 978-1-4799-5751-4 . 8024458 . 1522-4880.
  4. Chin . Andrew . 1991 . Complexity Issues in General Purpose Parallel Computing . 87–95 . DPhil . University of Oxford .
  5. Chin . Andrew . 1994 . Locality-Preserving Hash Functions for General Purpose Parallel Computation . Algorithmica . 12 . 2–3 . 170–181 . 10.1007/BF01185209. 18108051 .
  6. Gionis, A.. Piotr Indyk . Indyk, P. . Rajeev Motwani . Motwani, R.. 1999. Similarity Search in High Dimensions via Hashing. Proceedings of the 25th Very Large Database (VLDB) Conference.
  7. Indyk, Piotr. . Motwani, Rajeev. . 1998 . Approximate Nearest Neighbors: Towards Removing the Curse of Dimensionality. . http://people.csail.mit.edu/indyk/nndraft.ps . Proceedings of 30th Symposium on Theory of Computing. Symposium on Theory of Computing .
  8. Charikar. Moses S.. Moses Charikar . 2002 . Similarity Estimation Techniques from Rounding Algorithms . Proceedings of the 34th Annual ACM Symposium on Theory of Computing . 380–388 . 10.1145/509907.509965. 1-58113-495-9. 10.1.1.147.4064.
  9. .
  10. .
  11. .
  12. Chen. Beidi. Medini. Tharun. Farwell. James. Gobriel. Sameh. Tai. Charlie. Shrivastava. Anshumali. 2020-02-29. SLIDE : In Defense of Smart Algorithms over Hardware Acceleration for Large-Scale Deep Learning Systems. cs.DC. 1903.03129.
  13. Oliver, Jonathan. Cheng, Chun . Chen, Yanggui . TLSH - a locality sensitive hash . 4th Cybercrime and Trustworthy Computing Workshop . 2013 . 7–13 . 10.1109/CTC.2013.9 . 978-1-4799-3076-0.
  14. Broder . A.Z. . Andrei Broder . Charikar . M. . Moses Charikar . Frieze . A.M. . Alan M. Frieze. Mitzenmacher . M. . Michael Mitzenmacher . 1998 . Min-wise independent permutations . Proceedings of the Thirtieth Annual ACM Symposium on Theory of Computing . 327–336 . http://www.cs.princeton.edu/~moses/papers/minwise.ps . 2007-11-14 . 10.1145/276698.276781. 10.1.1.409.9220.
  15. An optimal construction of exactly min-wise independent permutations . Takei, Y. . Itoh, T. . Shinozaki, T. . Technical Report COMP98-62, IEICE, 1998.
  16. Matoušek, J. . Stojakovic, M. . 2002 . On Restricted Min-Wise Independence of Permutations . Preprint . 2007-11-14.
  17. Saks . M. . Michael Saks (mathematician) . Srinivasan. A.. Zhou. S. . Zuckerman . D. . 2000 . Low discrepancy sets yield approximate min-wise independent permutation families . Information Processing Letters . 73 . 1–2 . 29–32 . 2007-11-14 . 10.1016/S0020-0190(99)00163-5. 10.1.1.20.8264 .
  18. Web site: Damiani . et al. An Open Digest-based Technique for Spam Detection. 2004. 2013-09-01.
  19. Oliver. etal. TLSH - A Locality Sensitive Hash. 4th Cybercrime and Trustworthy Computing Workshop. 2013. 2015-06-04.
  20. Web site: TLSH . . 2014-04-10.
  21. Alexandr Andoni . Piotr Indyk. Indyk, P.. 2008. Near-Optimal Hashing Algorithms for Approximate Nearest Neighbor in High Dimensions. Communications of the ACM. 51. 1. 117–122. 10.1145/1327452.1327494. 10.1.1.226.6905. 6468963.
  22. Goemans . Michel X. . Williamson . David P. . Improved approximation algorithms for maximum cut and satisfiability problems using semidefinite programming . Journal of the ACM . Association for Computing Machinery (ACM) . 42 . 6 . 1995 . 0004-5411 . 10.1145/227683.227684 . 1115–1145. 15794408 . free .
  23. Datar, M. . Immorlica, N.. Nicole Immorlica . Piotr Indyk . Indyk, P. . Mirrokni, V.S. . 2004 . Locality-Sensitive Hashing Scheme Based on p-Stable Distributions . Proceedings of the Symposium on Computational Geometry.
  24. Pauleve, L. . Jegou, H. . Amsaleg, L. . 2010 . Locality sensitive hashing: A comparison of hash function types and querying mechanisms . Pattern Recognition Letters. 31 . 11 . 1348–1358 . 10.1016/j.patrec.2010.04.004 . 2010PaReL..31.1348P . 2666044 .
  25. Salakhutdinov. Ruslan. Hinton. Geoffrey. 2008. Semantic hashing. International Journal of Approximate Reasoning. en. 50. 7. 969–978. 10.1016/j.ijar.2008.11.006. free.
  26. Dahlgaard, Søren, Mathias Bæk Tejs Knudsen, and Mikkel Thorup. "Fast similarity sketching." 2017 IEEE 58th Annual Symposium on Foundations of Computer Science (FOCS). IEEE, 2017.
  27. Christiani, Tobias. "Fast locality-sensitive hashing frameworks for approximate near neighbor search." International Conference on Similarity Search and Applications. Springer, Cham, 2019.
  28. Ahle, Thomas Dybdahl. "On the Problem of
    -1
    p
    1
    in Locality-Sensitive Hashing." International Conference on Similarity Search and Applications. Springer, Cham, 2020.
  29. Gorman, James, and James R. Curran. "Scaling distributional similarity to large corpora." Proceedings of the 21st International Conference on Computational Linguistics and the 44th annual meeting of the Association for Computational Linguistics. Association for Computational Linguistics, 2006.