Improved Asymmetric Locality Sensitive Hashing (ALSH) for Maximum Inner Product Search (MIPS)

Anshumali Shrivastava, Ping Li

Introduction

The MIPS problem is related to the problem of near neighbor search (NNS). For example, L2-NNS

These three problems are equivalent if the norm of every element x∈Sx\in\mathcal{S} is constant. Clearly, the value of the norm ∣∣q∣∣2||q||_{2} has no effect for the argmax. In many scenarios, MIPS arises naturally at places where the norms of the elements in S\mathcal{S} have significant variations . As reviewed in , examples of applications of MIPS include recommender system , large-scale object detection with DPM , structural SVM , and multi-class label prediction .

Asymmetric LSH (ALSH): Locality Sensitive Hashing (LSH) is popular in practice for efficiently solving NNS. In the prior work , the concept of “asymmetric LSH” (ALSH) was proposed that one can transform the input query Q(p)Q(p) and data in the collection P(x)P(x) independently, where the transformations QQ and PP are different. developed a particular set of transformations to convert MIPS into L2-NNS and then solved the problem by standard L2-hash . In this paper, we name the scheme in as L2-ALSH. Asymmetry in hashing has become popular recently, and it has been applied for hashing higher order similarity , data dependent hashing , sketching etc.

Our contribution: In this study, we propose another scheme for ALSH, by developing a new set of asymmetric transformations to convert MIPS into a problem of correlation-NNS, which is solved by “sign random projections” . We name this new scheme as Sign-ALSH. Our theoretical analysis and experimental study show that Sign-LSH is more advantageous than L2-ALSH for MIPS.

Review: Locality Sensitive Hashing (LSH)

The problem of efficiently finding nearest neighbors has been an active research since the very early days of computer science . Approximate versions of the near neighbor search problem were proposed to break the linear query time bottleneck. The following formulation for approximate near neighbor search is often adopted.

Locality Sensitive Hashing (LSH) is a family of functions, with the property that more similar items have a higher collision probability. LSH trades off query time with extra (one time) preprocessing cost and space. Existence of an LSH family translates into provably sublinear query time algorithm for c-NN problems.

if Sim(x,y)≥S0Sim(x,y)\geq S_{0} then PrH(h(x)=h(y))≥p1Pr_{\mathcal{H}}(h(x)=h(y))\geq p_{1}

if Sim(x,y)≤cS0Sim(x,y)\leq cS_{0} then PrH(h(x)=h(y))≤p2Pr_{\mathcal{H}}(h(x)=h(y))\leq p_{2}

For efficient approximate nearest neighbor search, p1>p2p_{1}>p_{2} and c<1c<1 is needed.

Fact 1: Given a family of (S0,cS0,p1,p2)(S_{0},cS_{0},p_{1},p_{2}) -sensitive hash functions, one can construct a data structure for cc-NN with O(nρlog⁡n)O(n^{\rho}\log{n}) query time and space O(n1+ρ)O(n^{1+\rho}), where ρ=log⁡p1log⁡p2<1\rho=\frac{\log{p_{1}}}{\log{p_{2}}}<1.

LSH is a generic framework and an implementation of LSH requires a concrete hash function.

presented an LSH family for L2L_{2} distances. Formally, given a fixed window size rr, we sample a random vector aa with each component from i.i.d. normal, i.e., ai∼N(0,1)a_{i}\sim N(0,1), and a scalar bb generated uniformly at random from [0,r][0,r]. The hash function is defined as:

where ⌊⌋\lfloor\rfloor is the floor operation. The collision probability under this scheme can be shown to be

where Φ(x)=∫−∞x12πe−x22dx\Phi(x)=\int_{-\infty}^{x}\frac{1}{\sqrt{2\pi}}e^{-\frac{x^{2}}{2}}dx and d=∣∣x−y∣∣2d=||x-y||_{2} is the Euclidean distance between the vectors xx and yy.

2 LSH for correlation

Another popular LSH family is the so-called “sign random projections” . Again, we choose a random vector aa with ai∼N(0,1)a_{i}\sim N(0,1). The hash function is defined as:

This hashing scheme is also popularly known as signed random projections (SRP)

Review of ALSH for MIPS and L2-ALSH

In , it was shown that the framework of locality sensitive hashing is restrictive for solving MIPS. The inherent assumption of the same hash function for both the transformation as well as the query was unnecessary in the classical LSH framework and it was the main hurdle in finding provable sub-linear algorithms for MIPS with LSH. For the theoretical guarantees of LSH to work there was no requirement of symmetry. Incorporating asymmetry in the hashing schemes was the key in solving MIPS efficiently.

if Sim(q,x)≥S0Sim(q,x)\geq S_{0} then PrH(h(Q(q)))=h(P(x)))≥p1Pr_{\mathcal{H}}(h(Q(q)))=h(P(x)))\geq p_{1}

if Sim(q,x)≤cS0Sim(q,x)\leq cS_{0} then PrH(h(Q(q))=h(P(x)))≤p2Pr_{\mathcal{H}}(h(Q(q))=h(P(x)))\leq p_{2}

Here xx is any point in the collection S\mathcal{S}.

Note that the query transformation QQ is only applied on the query and the pre-processing transformation PP is applied to x∈Sx\in\mathcal{S} while creating hash tables. By letting Q(x)=P(x)=xQ(x)=P(x)=x, we can recover the vanilla LSH. Using different transformations (i.e., Q≠PQ\neq P), it is possible to counter the fact that self similarity is not highest with inner products which is the main argument of failure of LSH. We only just need the probability of the new collision event {h(Q(q))=h(P(y))}\{h(Q(q))=h(P(y))\} to satisfy the conditions of definition of ALSH for Sim(q,y)=qTySim(q,y)=q^{T}y.

Given a family of hash function H\mathcal{H} and the associated query and preprocessing transformations PP and QQ, which is (S0,cS0,p1,p2)(S_{0},cS_{0},p_{1},p_{2}) -sensitive, one can construct a data structure for cc-NN with O(nρlog⁡n)O(n^{\rho}\log{n}) query time and space O(n1+ρ)O(n^{1+\rho}), where ρ=log⁡p1log⁡p2\rho=\frac{\log{p_{1}}}{\log{p_{2}}}.

also provided an explicit construction of ALSH, which we call L2-ALSH. Without loss of generality, one can always assume

where [;] is the concatenation. P(x)P(x) appends mm scalers of the form ∣∣x∣∣22i||x||_{2}^{2^{i}} at the end of the vector xx, while Q(x) simply appends mm “1/2” to the end of the vector xx. By observing

one can obtain the following key equality:

Since ∣∣xi∣∣2≤U<1||x_{i}||_{2}\leq U<1, we have ∣∣xi∣∣2m+1→0||x_{i}||^{2^{m+1}}\rightarrow 0 at the tower rate (exponential to exponential). Thus, as long as mm is not too small (e.g., m≥3m\geq 3 would suffice), we have

This scheme is the first connection between solving un-normalized MIPS and approximate near neighbor search. Transformations PP and QQ, when norms are less than 1, provide correction to the L2 distance ∣∣Q(q)−P(xi)∣∣2||Q(q)-P({x_{i}})||_{2} making it rank correlate with the (un-normalized) inner product. The general idea of ALSH was partially inspired by the work on three-way similarity search , where they applied different hashing functions for handling query and data in the repository.

Asymmetric transformations give us enough flexibility to modify norms without changing inner products. The transformation provided in used this flexibility to convert MIPS to standard near neighbor search in L2L_{2} space for which we have standard hash functions. Signed random projections are popular hash functions widely adopted for correlation or cosine similarity. We use asymmetric transformation to convert approximate MIPS into approximate maximum correlation search. The transformations and the collision probability of the hashing functions determines the efficiency of the obtained ALSH algorithm. We show that the new transformation with SRP is better suited for ALSH compared to the existing L2-ALSH. Note that in the recent work on coding for random projections , it was already shown that sign random projections (or 2-bit random projections) can outperform L2LSH.

The New Proposal: Sign-ALSH

Using ∣∣Q(q)∣∣22=∣∣q∣∣22=1||Q({q})||^{2}_{2}=||q||^{2}_{2}=1, Q(q)TP(xi)=qTxiQ(q)^{T}P({x_{i}})=q^{T}x_{i}, and

The term ∣∣xi∣∣2m+1→0,||x_{i}||^{2^{m+1}}\rightarrow 0, again vanishes at the tower rate. This means we have approximately

This provides another solution for solving MIPS using known methods for approximate correlation-NNS.

2 Fast Algorithms for MIPS Using Sign Random Projections

Eq. (16) shows that MIPS reduces to the standard approximate near neighbor search problem which can be efficiently solved by sign random projections, i.e., hSignh^{Sign} (defined by Eq. (6)). Formally, we can state the following theorem.

Given a cc-approximate instance of MIPS, i.e., Sim(q,x)=qTxSim(q,x)=q^{T}x, and a query qq such that ∣∣q∣∣2=1||q||_{2}=1 along with a collection S\mathcal{S} having ∣∣x∣∣2≤U<1||x||_{2}\leq U<1 ∀x∈S.\forall x\in\mathcal{S}. Let PP and QQ be the vector transformations defined in Eq. (13) and Eq. (14), respectively. We have the following two conditions for hash function hSignh^{Sign} (defined by Eq. (6))

where z∗=(m/22m+1−2)2−m−1z^{*}=\left(\frac{m/2}{2^{m+1}-2}\right)^{2^{-m-1}}.

Proof: When qTx≥S0q^{T}x\geq S_{0}, we have, according to Eq. (7)

When qTx≤cS0q^{T}x\leq cS_{0}, by noting that qTx≤∥x∥2q^{T}x\leq\|x\|_{2}, we have

For this one-dimensional function f(z)=za+zbf(z)=\frac{z}{\sqrt{a+z^{b}}}, where z=qTxz=q^{T}x, a=m/4a=m/4 and b=2m+1≥2b=2^{m+1}\geq 2, we know

One can also check that f′′(z)≤0f^{\prime\prime}(z)\leq 0 for 0<z<10<z<1, i.e., f(z)f(z) is a concave function. The maximum of f(z)f(z) is attained at z∗=(2ab−2)1/b=(m/22m+1−2)2−m−1z^{*}=\left(\frac{2a}{b-2}\right)^{1/b}=\left(\frac{m/2}{2^{m+1}-2}\right)^{2^{-m-1}}If z∗≥cS0z^{*}\geq cS_{0}, then we need to use f(cS0)f(cS_{0}) as the bound. \hfill□\hfill\Box

Therefore, we have obtained, in LSH terminology,

Theorem 1 allows us to construct data structures with worst case O(nρlog⁡n)O(n^{\rho}\log{n}) query time guarantees for cc-approximate MIPS, where ρ=log⁡p1log⁡p2\rho=\frac{\log p_{1}}{\log p_{2}}. For any given c<1c<1, there always exist U<1U<1 and mm such that ρ<1\rho<1. This way, we obtain a sublinear query time algorithm for MIPS. Because ρ\rho is a function of 2 parameters, the best query time chooses UU and mm, which minimizes the value of ρ\rho. For convenience, we define

See Figure 1 for the plots of ρ∗\rho^{*}, which also compares the optimal ρ\rho values for L2-ALSH in the prior work . The results show that Sign-ALSH is noticeably better.

3 Parameter Selection

Figure 2 presents the ρ\rho values for two sets of selected parameters: (m,U)=(2,0.75)(m,U)=(2,0.75) and (m,U)=(3,0.85)(m,U)=(3,0.85). We can see that even if we use fixed parameters, the performance would not degrade much. This essentially frees practitioners from the burden of choosing parameters.

Remove Dependence on Norm of Query

Changing norms of the query does not affect the arg⁡max⁡x∈CqTx\arg\max_{x\in\mathcal{C}}q^{T}x, and hence, in practice for retrieving top-kk, normalizing the query should not affect the performance. But for theoretical purposes, we want the runtime guarantee to be independent of ∣∣q∣∣2||q||_{2}. Note, both LSH and ALSH schemes solve the cc-approximate instance of the problem, which requires a threshold S0=qtxS_{0}=q^{t}x and an approximation ratio cc. For this given cc-approximate instance we choose optimal parameters KK and LL. If the queries have varying norms, which is likely the case in practical scenarios, then given a cc-approximate MIPS instance, normalizing the query will change the problem because it will change the threshold S0S_{0} and also the approximation ratio cc. The optimal parameters for the algorithm KK and LL, which are also the size of the data structure, change with S0S_{0} and cc. This will require re-doing the costly preprocessing with every change in query. Thus, the query time which is dependent on ρ\rho should be independent of the query.

Transformations PP and QQ were precisely meant to remove the dependency of correlation on the norms of xx but at the same time keeping the inner products same. Realizing the fact that we are allowed asymmetry, we can use the same idea to get rid of the norm of qq. Let MM be the upper bound on all the norms i.e. M=maxx∈C∣∣x∣∣2M=max_{x\in\mathcal{C}}||x||_{2}. In other words MM is the radius of the space.

Given the query qq and any data point xx, observe that the inner products between P(Q(T(q)))P(Q(T(q))) and Q(P(T(x)))Q(P(T(x))) is

P(Q(T(q)))P(Q(T(q))) appends first m zeros components to T(q)T(q) and then mm components of the form 1/2−∣∣q∣∣2i1/2-||q||^{2^{i}}. Q(P(T(q)))Q(P(T(q))) does the same thing but in a different order. Now we are working in D+2mD+2m dimensions. It is not difficult to see that the norms of P(Q(T(q)))P(Q(T(q))) and Q(P(T(q)))Q(P(T(q))) is given by

The transformations are very asymmetric but we know that it is necessary.

Therefore the correlation or the cosine similarity between P(Q(T(q)))P(Q(T(q))) and Q(P(T(x)))Q(P(T(x))) is

Note ∣∣T(q)∣∣22m+1,∣∣T(x)∣∣22m+1≤U<1||T(q)||_{2}^{2^{m+1}},||T(x)||_{2}^{2^{m+1}}\leq U<1, therefore both ∣∣T(q)∣∣22m+1||T(q)||_{2}^{2^{m+1}} and ∣∣T(x)∣∣22m+1||T(x)||_{2}^{2^{m+1}} converge to zero at a tower rate and we get approximate monotonicity of correlation with the inner products. We can apply sign random projections to hash P(Q(T(q)))P(Q(T(q))) and Q(P(T(q)))Q(P(T(q))).

Using the fact 0≤∣∣T(q)∣∣22m+1≤U0\leq||T(q)||_{2}^{2^{m+1}}\leq U and 0≤∣∣T(x)∣∣22m+1≤U0\leq||T(x)||_{2}^{2^{m+1}}\leq U, it is not difficult to get p1p_{1} and p2p_{2} for Sign-ALSH, without any conditions on any norms. Simplifying the expression, we get the following value of optimal ρu\rho_{u} (u for unrestricted).

With this value of ρu∗\rho_{u}^{*}, we can state our main theorem.

For the problem of cc-approximate MIPS in a bounded space, one can construct a data structure having O(nρu∗log⁡n)O(n^{\rho_{u}^{*}}\log{n}) query time and space O(n1+ρu∗)O(n^{1+\rho_{u}^{*}}), where ρu∗<1\rho_{u}^{*}<1 is the solution to constraint optimization (26).

Note, for all c<1c<1, we always have ρu∗<1\rho_{u}^{*}<1 because the constraint U2m+1<m(1−c)4cU^{2^{m+1}}<\frac{m(1-c)}{4c} is always true for big enough mm. The only assumption for efficiently solving MIPS that we need is that the space is bounded, which is always satisfied for any finite dataset. ρu∗\rho_{u}^{*} depends on MM, the radius of the space, which is expected.

Ranking Evaluations

In , the L2-ALSH scheme was shown to outperform the LSH for L2 distance in retrieving maximum inner products. Since our proposal is an improvement over L2-ALSH, we focus on comparisons with L2-ALSH. In this section, we compare L2-ALSH with Sign-ALSH based on ranking.

We use the two popular collaborative filtering datasets MovieLens 10M and Netflix, for the task of item recommendations. These are also the same datasets used in . Each dataset is a sparse user-item matrix RR, where R(i,j)R(i,j) indicates the rating of user ii for movie jj. For getting the latent feature vectors from user item matrix, we follow the methodology of . They use PureSVD procedure described in to generate user and item latent vectors, which involves computing the SVD of RR

where WW is nusers×fn_{users}\times f matrix and VV is nitem×fn_{item}\times f matrix for some chosen rank ff also known as latent dimension.

After the SVD step, the rows of matrix U=WΣU=W\Sigma are treated as the user characteristic vectors while rows of matrix VV correspond to the item characteristic vectors. This simple procedure has been shown to outperform other popular recommendation algorithms for the task of top item recommendations in , on these two datasets. We use the same choices for the latent dimension ff, i.e., f=150f=150 for Movielens and f=300f=300 for Netflix as .

2 Evaluations

In this section, we show how the ranking of the two ALSH schemes, L2-ALSH and Sign-ALSH, correlates with the top-TT inner products. Given a user ii and its corresponding user vector uiu_{i}, we compute the top-TT gold standard items based on the actual inner products uiTvju_{i}^{T}v_{j}, ∀j\forall j. We then generate KK different hash codes of the vector uiu_{i} and all the item vectors vjv_{j}s and then compute

For L2-ALSH, we used the same parameters used and recommended in . For Sign-ALSH, we used the two recommended choices shown in Section 4.3, which are U=0.75U=0.75, m=2m=2 and U=0.85U=0.85, m=3m=3. It should be noted that Sign-ALSH does not have the parameter rr.

We compute the precision and recall of the top-TT items for T∈{1,5,10}T\in\{1,5,10\}, obtained from the sorted list based on MatchesMatches. To compute this precision and recall, we start at the top of the ranked item list and walk down in order. Suppose we are at the kthk^{th} ranked item, we check if this item belongs to the gold standard top-TT list. If it is one of the top-TT gold standard item, then we increment the count of relevant seen by 1, else we move to k+1k+1. By kthk^{th} step, we have already seen kk items, so the total items seen is kk. The precision and recall at that point is then computed as:

We show performance for K∈{64,128,256,512}K\in\{64,128,256,512\}. Note that it is important to balance both precision and recall. The method which obtains higher precision at a given recall is superior. Higher precision indicates higher ranking of the relevant items. We report averaged precisions and recalls over 2000 randomly chosen users.

The plots for MovieLens and Netflix datasets are shown in Figure 3 and Figure 4 respectively. We can clearly see, that our proposed Sign-ALSH scheme gives significantly higher precision recall curves than the L2-ALSH scheme, indicating better correlation of the top neighbors under inner products with Sign-ALSH compared to L2-ALSH. In addition, there is not much difference in the two different combinations of the parameters UU and mm in Sign-ALSH. The results are very consistent across both datasets.

LSH Bucketing Experiments

In this section, we evaluate the actual savings in the number of inner product evaluations for recommending top-TT items for the MovieLens dataset. For this, we implemented the standard (K,L)(K,L) algorithms in , where KK is number of hashes in each hash table and LL is the total number of tables. For each query point, the returned results are the union of matches in all LL tables. To find the top-TT items, we need to compute the actual inner products only on the candidate items retrieved by the bucketing procedure.

In this experiment, we choose T∈{1,5,10}T\in\{1,5,10\} and compute the recall value for each combination of (T,K,L)(T,K,L) for every query. For example, given query qq and a (K,L)(K,L)-LSH scheme, if T=10T=10 and only 5 of the true top-10 data points are retrieved, the recall will be 50%50\% for this (T,K,L)(T,K,L). At the same time, we can also compute the FIP (fraction of inner products):

which is basically the total number of inner products evaluation (where K×LK\times L represents the cost of hashing), normalized by the total number of items in the repository. Thus, for each qq and (T,K,L)(T,K,L), we can compute two values: recall and FIP. We also need to figure out a way to aggregate the results for all queries.

Typically the performance of bucketing algorithm is very sensitive to the choice of hashing parameters KK and LL. Ideally, to find best KK and LL, we need to know the operating threshold S0S_{0} and the approximation ratio cc in advance. Unfortunately, the data and the queries are very diverse and therefore for retrieving top-TT near neighbors there is no common fixed threshold S0S_{0} and approximation ratio cc that works for different queries.

Our goal is to compare the hashing schemes, and minimize the effect of KK and LL on the evaluation. To get away with the effect of KK and LL, we perform rigorous evaluations of various KK and LL which includes optimal choices at various thresholds. For both the hashing schemes, we then select the best performing KK and LL and report the performance. This involves running the bucketing experiments for thousands of combinations and then choosing the best KK and LL to marginalize the effect of parameters in the comparisons. This all ensures that our evaluation is fair.

We choose the following scheme. For each (T,K,L)(T,K,L), we compute the averaged recall and averaged FIP, over all queries. Then for each “target” recall level (and TT), we can find the (K,L)(K,L) which produces the best (lowest) averaged FIP. This way, for each TT, we can compute a “FIP-recall” curve, which can be used to compare Sign-ALSH with L2-ALSH. We use K∈{4,5,..,20}K\in\{4,5,..,20\} and L∈{1,2,3,...,200}L\in\{1,2,3,...,200\}.

The results are summarized in Figure 5. We can clearly see from the plots that for achieving the same recall for top-TT, Sign-ALSH scheme needs to do less computations compared to L2-ALSH.

Conclusion

The MIPS (maximum inner product search) problem has numerous important applications in machine learning, databases, and information retrieval. developed the framework of Asymmetric LSH and provided an explicit scheme (L2-ALSH) for approximate MIPS in sublinear time. In this study, we present another asymmetric transformation scheme (Sign-ALSH) which converts the problem of maximum inner products into the problem of maximum correlation search, which is subsequently solved by sign random projections. Theoretical analysis and experimental study demonstrate that Sign-ALSH can be noticeably more advantageous than L2-ALSH.

Acknowledgement

The research is supported in part by ONR-N00014-13-1-0764, NSF-III-1360971, AFOSR-FA9550-13-1-0137, and NSF-Bigdata-1419210. The method and theoretical analysis for Sign-ALSH were conducted right after the initial submission of our first work on ALSH in February 2014. The intensive experiments (especially the LSH bucketing experiments), however, were not fully completed until June 2014 due to the demand of computational resources, because we exhaustively experimented a wide range of KK (number of hashes) and LL (number of tables) for implementing (K,L)(K,L)-LSH schemes. Here, we also would like to thank the computing supporting team (LCSR) at Rutgers CS department as well as the IT support staff at Rutgers Statistics department, for setting up the workstations especially the server with 1.5TB memory.

References