On Symmetric and Asymmetric LSHs for Inner Product Search
Behnam Neyshabur, Nathan Srebro
Introduction
MIPS problems of the form (1) arise, e.g. when using matrix-factorization based recommendation systems (Koren et al., 2009; Srebro et al., 2005; Cremonesi et al., 2010), in multi-class prediction (Dean et al., 2013; Jain et al., 2009) and structural SVM (Joachims, 2006; Joachims et al., 2009) problems and in vision problems when scoring filters based on their activations (Dean et al., 2013) (see Shrivastava and Li, 2014a, for more about MIPS). In order to efficiently find approximate MIPS solutions, Shrivastava and Li (2014a) suggest constructing a Locality Sensitive Hash (LSH) for inner product “similarity”.
Several tree-based methods have also been proposed for inner product search (Ram and Gray, 2012; Koenigstein et al., 2012; Curtin et al., 2013). Shrivastava and Li (2014a) argue that tree-based methods, such as cone trees, are impractical in high dimensions while the performance of LSH-based methods is in a way independent of dimension of the data. Although the exact regimes under which LSH-based methods are superior to tree-based methods and vice versa are not fully established yet, the goal of this paper is to analyze different LSH methods and compare them with each other, rather than comparing to tree-based methods, so as to understand which LSH to use and why, in those regimes where tree-based methods are not practical.
As mentioned above, our study also yields an LSH for MIPS, which we refer to as simple-lsh, which is not only symmetric but also parameter-free and enjoys significantly better theoretical and empirical compared to l2-alsh(sl) proposed by Shrivastava and Li (2014a). In Appendix A we show that all of our theoretical observations about l2-alsh(sl) apply also to the alternative hash sign-lsh(sl) put forth by Shrivastava and Li (2014b).
The transformation at the root of simple-lsh was also recently proposed by Bachrach et al. (2014), who used it in a PCA-Tree data structure for speeding up the Xbox recommender system. Here, we study the transformation as part of an LSH scheme, investigate its theoretical properties, and compare it to ls-alsh(sl).
Locality Sensitive Hashing
A hash of a set of objects is a random mapping from to some alphabet , i.e. a distribution over functions . The hash is sometimes thought of as a “family” of functions, where the distribution over the family is implicit.
A hash is said to be a -LSH for a similarity function sim over the pair of spaces if for any and :
When , we say simply “over the space ”.
Here is a threshold of interest, and for efficient approximate nearest neighbor search, we need and . In particular, given an -LSH, a data structure for finding -similar objects for query points when -similar objects exist in the database can be constructed in time and space where . This quantity is therefore of particular interest, as we are interested in an LSH with minimum possible , and we refer to it as the hashing quality.
An asymmetric hash is said to be an -ALSH for a similarity function sim over if for any and :
Referring to either of the above definitions, we also say that a hash is an -LSH (or ALSH) if there exists such that it is an -LSH (or ALSH). And we say it is a universal LSH (or ALSH) if for every it is an -LSH (or ALSH).
For any (to be set later), define the matrix as follows:
where denotes element-wise (Hadamard) product. Now, for a sign matrix , the margin complexity of is defined as (see Srebro and Shraibman, 2005, and also for the definition of the max-norm ), and we know that the margin complexity of an triangular matrix is bounded by (Forster et al., 2003), implying
Furthermore, any collision probability matrix has max-norm (Neyshabur et al., 2014), and shifting the matrix by changes the max-norm by at most , implying , which combined with (5) implies . For any , selecting a large enough we get a contradiction. ∎
For completeness, we also include in Appendix B a full definition of the max-norm and margin complexity, as well as the bounds on the max-norm and margin complexity used in the proof above.
Maximum Inner Product Search
The query is normalized: Since given a vector , the norm does not affect the argmax in (1), we can assume always.
But first, we review l2-alsh(sl) and note that it is not universal—it depends on three parameters and no setting of the parameters works for all thresholds . We also compare our simple-lsh to l2-alsh(sl) (and to the recently suggested sign-alsh(sl)) both in terms of the hashing quality and empirically of movie recommendation data sets.
For an integer parameter , and real valued parameters and , consider the following pair of mappings:
combined with the standard hash function
Shrivastava and Li (2014a) establishShrivastava and Li (2014a) have the scaling by as a separate step, and state their hash as an -ALSH over , where the threshold is also scaled by . This is equivalent to the presentation here which integrates the pre-scaling step, which also scales the threshold, into the hash. that for any and , there exists , , , such that l2-alsh(sl) is an -ALSH over . They furthermore calculate the hashing quality as a function of and , and numerically find the optimal over a grid of possible values for and , for each choice of .
Before moving on to presenting a symmetric hash for the problem, we note that l2-alsh(sl) is not universal (as defined at the end of Section 2). That is, not only might the optimal and depend on , but in fact there is no choice of the parameters and that yields an ALSH for all , or even for all ratios for some specific threshold or for all thresholds for some specific ratio . This is unfortunate, since in MIPS problems, the relevant threshold is the maximal inner product (or the threshold inner product if we are interested in the “top-” hits), which typically varies with the query. It is therefore desirable to have a single hash that works for all thresholds.
l2-alsh(sl) is not an -ALSH for inner product similarity over and .
Assume for contradiction that it is an -ALSH. For any query point , let be a vector s.t. and and let , so that . We have that:
where is a monotonically decreasing function of (Datar et al., 2004). To get a contradiction it is therefor enough to show that . We have:
For any and , l2-alsh(sl) is not a universal ALSH for inner product similarity over and . Furthermore, for any , and any choice of there exists for which l2-alsh(sl) is not an -ALSH over , and for any and any choice of there exists for which l2-alsh(sl) is not an -ALSH over .
In Appendix A, we show a similar non-universality result also for sign-alsh(sl).
2 SIMPLE-LSH
We propose here a simpler, parameter-free, symmetric LSH, which we call simple-lsh.
For any we have , and for any , since , we have:
Now, to define the hash simple-lsh, take a spherical random vector and consider the following random mapping into the binary alphabet :
simple-lsh given in (11) is a universal LSH over . That is, for every and , it is an -LSH over . Furthermore, it has hashing quality:
For any and we have (Goemans and Williamson, 1995):
Since for any , is a monotonically increasing function, this gives us an LSH. ∎
3 Theoretical Comparison
Earlier we discussed that an LSH with the smallest possible hashing quality is desirable. In this Section, we compare the best achievable hashing quality and show that simple-lsh allows for much better hashing quality compared to l2-alsh(sl), as well as compared to the improved hash sign-lsh(sl).
For l2-alsh(sl) and sign-alsh(sl), for each desired threshold and ratio , one can optimize over the parameters and , and for l2-alsh(sl) also , to find the hash with the best . This is a non-convex optimization problem and Shrivastava and Li (2014a) suggest using grid search to find a bound on the optimal . We followed the procedure, and grid, as suggested by Shrivastava and Li (2014a)We actually used a slightly tighter bound—a careful analysis shows the denominator in equation 19 of Shrivastava and Li (2014a) can be . For simple-lsh no parameters need to be tuned, and for each the hashing quality is given by Theorem 5.3. In Figure 1 we compare the optimal hashing quality for the three methods, for different values of and . It is clear that the simple-lsh dominates the other methods.
4 Empirical Evaluation
We also compared the hash functions empirically, following the exact same protocol as Shrivastava and Li (2014a), using two collaborative filtering datasets, Netflix and Movielens 10M.
For a given user-item matrix , we followed the pureSVD procedure suggested by Cremonesi et al. (2010): we first subtracted the overall average rating from each individual rating and created the matrix with these average-subtracted ratings for observed entries and zeros for unobserved entries. We then take a rank- approximation (top singular components, for Movielens and for Netflix) and define so that . We can think of each row of as the vector presentation of a user and each row of as the presentation for an item.
The database consists of all rows of (corresponding to movies) and we use each row of (corresponding to users) as a query. That is, for each user we would like to find the top movies, i.e. the movies with highest , for different values of .
To do so, for each hash family, we generate hash codes of length , for varying lengths , for all movies and a random selection of 60000 users (queries). For each user, we sort movies in ascending order of hamming distance between the user hash and movie hash, breaking up ties randomly. For each of several values of and we calculate precision-recall curves for recalling the top movies, averaging the precision-recall values over the 60000 randomly selected users.
In Figures 2 and 3, we plot precision-recall curves of retrieving top items by hash code of length for Netflix and Movielens datasets where and . For l2-alsh(sl) we used , suggested by the authors and used in their empirical evaluation. For sign-alsh(sl) we used two different settings of the parameters suggested by Shrivastava and Li (2014b): and . simple-lsh does not require any parameters.
As can be seen in the Figures, simple-lsh shows a dramatic empirical improvement over l2-alsh(sl). Following the presentation of simple-lsh and the comparison with l2-alsh(sl), Shrivastava and Li (2014b) suggested the modified hash sign-alsh(sl), which is based on random projections, as is simple-lsh, but with an asymmetric transform similar to that in l2-alsh(sl). Perhaps not surprising, sign-alsh(sl) does indeed perform almost the same as simple-lsh (simple-lsh has only a slight advantage on Movielens), however: (1) simple-lsh is simpler, and uses a single symmetric lower-dimensional transformation ; (2) simpler-lsh is universal and parameter free, while sign-alsh(sl) requires tuning two parameters (its authors suggest two different parameter settings for use). Therefor, we see no reason to prefer sign-alsh(sl) over the simpler symmetric option.
Unnormalized Queries
In the previous Section, we exploited asymmetry in the MIPS problem formulation, and showed that with such asymmetry, there is no need for the hash itself to be asymmetric. In this Section, we consider LSH for inner product similarity in a more symmetric setting, where we assume no normalization and only boundedness. That is, we ask whether there is an LSH or ALSH for inner product similarity over . Beyond a theoretical interest in the need for asymmetry in this fully symmetric setting, the setting can also be useful if we are interested in using sets and interchangeably as query and data sets. In user-item setting for example, one might be also interested in retrieving the top users interested in a given item without the need to create a separate hash for this task.
We first observe that there is no symmetric LSH for this setting. We therefore consider asymmetric hashes. Unfortunately, we show that neither l2-alsh(sl) (nor sign-alsh(sl)) are ALSH over . Instead, we propose a parameter-free asymmetric extension of simple-lsh, which we call simple-alsh, and show that it is a universal ALSH for inner product similarity over .
To summarize the situation, if we consider the problem asymmetrically, as in the previous Section, there is no need for the hash to be asymmetric, and we can use a single hash function. But if we insist on considering the problem symmetrically, we do indeed have to use an asymmetric hash.
We first show we do not have a symmetric LSH:
For any and there is no -LSH (by Definition 1) for inner product similarity over .
2 L2-ALSH(SL)
We might hope l2-alsh(sl) is a valid ALSH here. Unfortunately, whenever , and so in particular for all , it is not:
For any and any , there are no and such that l2-alsh(sl) is an -ALSH for inner product similarity over .
Let and be unit vectors such that . Let be a unit vector and define . For any and :
where the inequality follows from . Now, the same arguments as in Lemma 1 using monotonicity of collision probabilities in establish ls-alsh(sl) is not an -ALSH. ∎
In Appendix A, we show a stronger negative result for sign-alsh(sl): for any and , there are no such that sign-alsh(sl) is an .
3 SIMPLE-ALSH
Fortunately, we can define a variant of simple-lsh, which we refer to as simple-alsh, for this more general case where queries are not normalized. We use the pair of transformations:
and the random mappings , , where is as in (11). It is clear that by these definitions, we always have that for all , and .
simple-alsh is a universal ALSH over . That is, for every , it is an -ALSH over .
Shrivastava and Li (2015) also showed how a modification of simple-alsh can be used for searching similarity measures such as set containment and weighted Jaccard similarity.
Conclusion
We provide a complete characterization of when symmetric and asymmetric LSH are possible for inner product similarity: {itemize*}
For the MIPS setting, with normalized queries and bounded database vectors , a universal symmetric LSH is possible.
It is important to emphasize that even though in the MIPS setting an asymmetric hash, as we define here, is not needed, an asymmetric view of the problem is required. In particular, to use a symmetric hash, one must normalize the queries but not the database vectors, which can legitimately be viewed as an asymmetric operation which is part of the hash (though then the hash would not be, strictly speaking, an ALSH). In this regard Shrivastava and Li (2014a) do indeed successfully identify the need for an asymmetric view of MIPS, and provide the first practical ALSH for the problem.
This research was partially funded by NSF award IIS-1302662.
References
Appendix A Another variant
To benefit from the empirical advantages of random projection hashing, Shrivastava and Li (2014b) also proposed a modified asymmetric LSH, which we refer to here as sign-alsh(sl). sign-alsh(sl) uses two different mappings , , similar to those of l2-alsh(sl), but then uses a random projection hash , as is the one used by simple-lsh, instead of the quantized hash used in l2-alsh(sl). In this appendix we show that our theoretical observations about l2-alsh(sl) are also valid for sign-alsh(sl).
where and are parameters, as in l2-alsh(sl). sign-alsh(ls) is then given by , , where is the random projection hash given in (11). sign-alsh(ls) therefor depends on two parameters, and uses a binary alphabet .
In this section, we show that, like l2-alsh(ls), sign-alsh(ls) is not a universal ALSH over , and moreover for any and it is not an -ALSH over :
sign-alsh(sl) is not an -ALSH for inner product similarity over and .
and sign-alsh(sl) is an -ALSH. For any query point , let be a vector s.t. and and let , so that . We have that:
The monotonicity of establishes a contradiction. To get the other bound on , let and assume for contradiction that:
and sign-alsh(sl) is an -ALSH. For any query point , let be a vector s.t. and and let . By the monotonicity of , to get a contradiction is enough to show that
For any and , sign-alsh(sl) is not a universal ALSH for inner product similarity over and . Furthermore, for any , and any choice of there exists for which sign-alsh(sl) is not an -ALSH over , and for any and any choice of there exists for which sign-alsh(sl) is not an -ALSH over .
For any and there are no and such that sign-alsh(sl) is an -ALSH for inner product similarity over .
Similar to the proof of Theorem 5.2, for any and , let and be unit vectors such that . Let be a unit vector and define . For any and :
Now, the same arguments as in Lemma 1 using monotonicity of collision probabilities in establish sign-alsh(sl) is not an -ALSH. ∎
Appendix B Max-norm and margin complexity
For any two sets of objects and hashes over them, if is the collision probability matrix, then .
For each and , define the following biclustering matrix:
For any function , let be the indicator of the values of function :
Margin complexity
For any sign matrix , the margin complexity of is defined as:
Let be a sign matrix with +1 on and above the diagonal and -1 below it. Forster et al. (2003) prove that the margin complexity of matrix is .