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
of functions
is defined to be an
LSH family[6] [7] for
,
,
,
if it satisfies the following condition. For any two points
and a hash function
chosen uniformly at random from
:
, then
(i.e., and collide) with probability at least
,
, then
with probability at most
.
Such a family
is called
-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
. In this setting, a LSH scheme is a family of
hash functions coupled with a
probability distribution over such that a function
chosen according to satisfies
for each
.
Amplification
Given a
-sensitive family
, we can construct new families
by either the AND-construction or OR-construction of
.
To create an AND-construction, we define a new family
of hash functions, where each function is constructed from random functions
from
. We then say that for a hash function
,
if and only if all
for
. Since the members of
are independently chosen for any
,
is a
-sensitive family.
To create an OR-construction, we define a new family
of hash functions, where each function is constructed from random functions
from
. We then say that for a hash function
,
if and only if
for one or more values of . Since the members of
are independently chosen for any
,
is a
-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
. Here, the family
of hash functions is simply the family of all the projections of points on one of the
coordinates, i.e.,
{lF}=\{h\colon\{0,1\}d\to\{0,1\}\midh(x)=xiforsomei\in\{1,\ldots,d\}\}
, where
is the
th coordinate of
. A random function
from
simply selects a random bit from the input point. This family has the following parameters:
,
.That is, any two vectors
with Hamming distance at most
collide under a random
with probability at least
. Any
with Hamming distance at least
collide with probability at most
.
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
let
. 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
the event that
corresponds exactly to the event that the minimizer of over
lies inside
. As was chosen uniformly at random,
and
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:
- The digest identifying each message should not vary significantly for changes that can be produced automatically.
- The encoding must be robust against intentional attacks.
- 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
. That is,
depending on which side of the hyperplane lies. This way, each possible choice of a random hyperplane can be interpreted as a hash function
.
For two vectors with angle
between them, it can be shown that
Since the ratio between
and
is at least 0.87856 when
,
[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
onto the set of integers. Each hash functionin the family is indexed by a choice of random
and
where
is a -dimensional vector withentries chosen independently from a
stable distribution and
isa real number chosen uniformly from the range [0,r]. For a fixed
the hash function
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
. The algorithm has two main parameters: the width parameter and the number of hash tables .
In the first step, we define a new family
of hash functions, where each function is obtained by concatenating functions
from
, 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
. 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
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:
, where is the time to evaluate a function
on an input point ;
, plus the space for storing data points;
;
- the algorithm succeeds in finding a point within distance from (if there exists a point within distance) with probability at least
;
For a fixed approximation ratio
and probabilities
and
, one can set
k=\left\lceil\tfrac{logn}{log1/P2}\right\rceil
and
L=\lceil
\rceil=O(n\rho
)
, where
\rho={\tfrac{logP1}{logP2}}
. Then one obtains the following performance guarantees:
;
, plus the space for storing data points;
;
Improvements
When is large, it is possible to reduce the hashing time from
.This was shown by
[26] and
[27] which gave
;
;
It is also sometimes the case that the factor
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
(not including hashing costs) and similarly the space usage.
See also
Further reading
- Samet, H. (2006) Foundations of Multidimensional and Metric Data Structures. Morgan Kaufmann.
- Indyk . Piotr . Piotr Indyk. Motwani . Rajeev . Rajeev Motwani. Raghavan . Prabhakar. Vempala . Santosh . Santosh Vempala. Locality-preserving hashing in multidimensional spaces . 10.1145/258533.258656 . Proceedings of the twenty-ninth annual ACM symposium on Theory of computing. STOC '97. 618–625. 1997 . 978-0-89791-888-6. 10.1.1.50.4927. 15693787 .
- Chin . Andrew. Locality-preserving hash functions for general purpose parallel computation . 10.1007/BF01185209 . Algorithmica. 12 . 2–3 . 170–181 . 1994 . 18108051.
External links
Notes and References
- Web site: Mining of Massive Datasets, Ch. 3.. Rajaraman. A.. Ullman. J.. Jeffrey Ullman. 2010.
- Zhao . Kang . Lu . Hongtao . Mei . Jincheng . Locality Preserving Hashing . AAAI Conference on Artificial Intelligence . 28 . 2014 . 2874–2880.
- 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.
- Chin . Andrew . 1991 . Complexity Issues in General Purpose Parallel Computing . 87–95 . DPhil . University of Oxford .
- Chin . Andrew . 1994 . Locality-Preserving Hash Functions for General Purpose Parallel Computation . Algorithmica . 12 . 2–3 . 170–181 . 10.1007/BF01185209. 18108051 .
- 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.
- 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 .
- 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.
- .
- .
- .
- 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.
- 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.
- 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.
- An optimal construction of exactly min-wise independent permutations . Takei, Y. . Itoh, T. . Shinozaki, T. . Technical Report COMP98-62, IEICE, 1998.
- Matoušek, J. . Stojakovic, M. . 2002 . On Restricted Min-Wise Independence of Permutations . Preprint . 2007-11-14.
- 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 .
- Web site: Damiani . et al. An Open Digest-based Technique for Spam Detection. 2004. 2013-09-01.
- Oliver. etal. TLSH - A Locality Sensitive Hash. 4th Cybercrime and Trustworthy Computing Workshop. 2013. 2015-06-04.
- Web site: TLSH . . 2014-04-10.
- 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.
- 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 .
- 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.
- 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 .
- 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.
- 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.
- Christiani, Tobias. "Fast locality-sensitive hashing frameworks for approximate near neighbor search." International Conference on Similarity Search and Applications. Springer, Cham, 2019.
- Ahle, Thomas Dybdahl. "On the Problem of
in Locality-Sensitive Hashing." International Conference on Similarity Search and Applications. Springer, Cham, 2020.
- 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.