Algorithm Search⌘K

Techniqueresearch

Cross-domain similarity local scaling

Also called CSLS.

Retrieve translations by cosine similarity minus the mean similarity of each word to its k nearest neighbours, which reduces hubness.

Cross-domain similarity local scaling (CSLS) is a similarity score for nearest neighbour retrieval between two sets of vectors, such as mapped source words and target words in cross-lingual embedding mapping. In high dimension, a few target vectors, called hubs, are the nearest neighbours of many queries, and plain cosine retrieval returns them too often (hubness). CSLS subtracts from twice the cosine the mean similarity of each of the two vectors to its KK nearest neighbours in the other set:

CSLS(x⃗,y⃗)=2cos⁡(x⃗,y⃗)−rT(x⃗)−rS(y⃗).\mathrm{CSLS}(\vec x, \vec y) = 2\cos(\vec x, \vec y) - r_T(\vec x) - r_S(\vec y).

A hub has a high rSr_S, so its scores go down. Conneau, Lample, Ranzato, Denoyer and Jégou (2018) introduced it with K=10K = 10. On their English to Spanish test, it raised the precision at 1 of a supervised Procrustes map from 77.4 with plain nearest neighbour to 81.4.

In plain words

When you translate a word by finding the closest word in the other language, some words are close to almost everything. They sit in a crowded middle part of the space, and they keep winning, even when they are wrong.

CSLS makes the contest fairer. For every word, it measures how close the word usually is to its nearest few neighbours on the other side. A word that is close to everything has a high usual closeness. The score of a pair is its closeness, counted twice, minus the usual closeness of each of the two words.

A pair now scores high only when the two words are closer to each other than they are to their other neighbours. Crowded words lose their advantage, and isolated words get a fair chance.

When it applies

Use it when you retrieve, for each query from one set, its best match in a second set by cosine similarity, the vectors have a high dimension, and you can afford to compute the neighbourhoods of both sets once. The signals are retrieval between two domains (two languages, or images and text), the same few targets appearing as the answer to many queries, and a wish for a fixed rule without a tuned temperature. For retrieval inside one set, the same idea applies with both neighbourhoods in that set.

The template

The holes are the source vectors x⃗s\vec x_s (already mapped into the target space), the target vectors y⃗t\vec y_t, the similarity (cosine) and the size KK of the neighbourhoods. Let NT(x⃗)N_T(\vec x) be the KK target vectors nearest to x⃗\vec x and NS(y⃗)N_S(\vec y) the KK source vectors nearest to y⃗\vec y. Then

rT(x⃗)=1K∑y⃗t∈NT(x⃗)cos⁡(x⃗,y⃗t),rS(y⃗)=1K∑x⃗s∈NS(y⃗)cos⁡(x⃗s,y⃗).r_T(\vec x) = \frac{1}{K}\sum_{\vec y_t \in N_T(\vec x)} \cos(\vec x, \vec y_t), \qquad r_S(\vec y) = \frac{1}{K}\sum_{\vec x_s \in N_S(\vec y)} \cos(\vec x_s, \vec y).
  1. CSLS-RETRIEVE(sources x_1..x_n, targets y_1..y_m, K)
  2. 1for each source s do r_T(s) ← mean of the K largest cos(x_s, y_t) over the targets t
  3. 2for each target t do r_S(t) ← mean of the K largest cos(x_s, y_t) over the sources s
  4. 3for each source s do
  5. 4answer(s) ← argmax over t of 2 cos(x_s, y_t) - r_T(s) - r_S(t)
  6. 5return answer

Lines L1 and L2 compute the two mean similarities, with the cosines of the same n×mn \times m matrix read by rows and by columns. Line L4 ranks the targets of one source. For a fixed source, rT(s)r_T(s) is the same for every target, so the ranking of line L4 is the ranking by 2cos⁡(x⃗s,y⃗t)−rS(t)2\cos(\vec x_s, \vec y_t) - r_S(t). The term rTr_T matters when scores of different sources are compared, for example to keep the most confident pairs or to test whether two words are mutual nearest neighbours.

With brute force, lines L1 to L4 cost O(nmd)O(nmd) for the cosines and O(nmlog⁡K)O(nm \log K) for the heaps of the KK best values, and the heaps take O((n+m)K)O((n + m)K) space. In practice the neighbourhoods are found with a fast nearest neighbour index, often on the most frequent words only.

Example. Take three sources and three targets with the cosines below, and K=2K = 2.

t1t_1 t2t_2 t3t_3 rTr_T
s1s_1 0.60 0.20 0.65 0.625
s2s_2 0.10 0.55 0.60 0.575
s3s_3 0.20 0.15 0.70 0.450
rSr_S 0.400 0.375 0.675

Every row has its largest cosine at t3t_3, so plain nearest neighbour retrieval sends all three sources to the hub t3t_3. Its rSr_S is high, (0.70+0.65)/2=0.675(0.70 + 0.65)/2 = 0.675. The CSLS scores are

t1t_1 t2t_2 t3t_3 answer
s1s_1 0.175 -0.600 0.000 t1t_1
s2s_2 -0.775 0.150 -0.050 t2t_2
s3s_3 -0.450 -0.525 0.275 t3t_3

For example, CSLS(s1,t1)=2(0.60)−0.625−0.400=0.175\mathrm{CSLS}(s_1, t_1) = 2(0.60) - 0.625 - 0.400 = 0.175. Each target is now chosen once, and each pair is a mutual best match under CSLS.

Classic examples

  • Word translation. After a cross-lingual embedding mapping by Procrustes, each source word is translated to the target word with the highest CSLS score. This is the use in Conneau et al., where it beat plain cosine and the inverted softmax in most language pairs of their Table 1.
  • Dictionary induction for refinement. The Procrustes refinement of Conneau et al. builds each new seed dictionary from the pairs that are mutual nearest neighbours under CSLS, so that the next map is fitted on more reliable pairs.
  • Analogy and similarity search. The same correction applies to any k-nearest neighbor search in a high-dimensional embedding space where hubness appears, such as the retrieval step of a word analogy query.
  • Cross-lingual sentence retrieval. With multilingual sentence embeddings, the same kind of score, the cosine corrected by the mean similarity to the nearest neighbours, is used to find sentences that are translations of each other.

Variations

  • The size of the neighbourhood. Conneau et al. report that the results are about the same for K=5K = 5, 10 and 50, and use K=10K = 10 in all their tests.
  • Inverted softmax. Smith et al. (2017) normalize the similarities of each target over all sources with a softmax at a temperature, which also lowers the scores of hubs. The temperature must be tuned, which is hard without a validation dictionary.
  • Reverse ranks. Dinu, Lazaridou and Baroni (2015) rank the candidates by how highly the query ranks among the neighbours of each candidate, instead of by the similarity itself.
  • Ratio form. Dividing the cosine by the mean neighbourhood similarity, instead of subtracting it, gives a margin of a related form.
  • One domain. For retrieval in one set, both means are taken in that set. Conneau et al. note that this kind of correction resembles the preprocessing of some spectral clustering methods.

Pitfalls

  • Neighbourhoods on the wrong side. rT(x⃗)r_T(\vec x) uses the target neighbours of a source vector and rS(y⃗)r_S(\vec y) the source neighbours of a target vector. Computing rS(y⃗)r_S(\vec y) from the other target vectors measures density in the wrong set and does not correct cross-domain hubs.
  • Unmapped sources. rSr_S must use the mapped source vectors Wx⃗sW\vec x_s. Using the source vectors before the map gives meaningless cosines.
  • Reading the score as a cosine. CSLS values are not in [−1,1][-1, 1] and can be negative for the best match. Use them to rank, not as a similarity with a fixed threshold.
  • The cost of rSr_S. Line L2 needs the KK nearest sources of every target, which is a search over the whole source vocabulary for each target. Restrict both vocabularies to the frequent words, or use an index.
  • Different sets at test time. rSr_S and rTr_T depend on which words are in the two sets. Adding or removing words changes the scores of all pairs, so compare results only with the same vocabularies.

Exercises

  1. Show that for a fixed source ss, ranking the targets by CSLS gives the same order as ranking them by 2cos⁡(x⃗s,y⃗t)−rS(t)2\cos(\vec x_s, \vec y_t) - r_S(t).
  2. In the example, change the cosine of s1s_1 and t3t_3 from 0.65 to 0.75. Recompute rT(s1)r_T(s_1), rS(t3)r_S(t_3) and the CSLS answers.
  3. Show that if every target has the same rSr_S, CSLS retrieval returns the same answers as plain nearest neighbour retrieval.

References

  1. Conneau, Lample, Ranzato, Denoyer and Jégou 2018, Section 2.3 (equations 5 and 6, the definition of r_T, r_S and CSLS), Section 3.4 (mutual nearest neighbours under CSLS for the dictionary), Section 4 and Table 1 (K = 10, and Procrustes with NN, ISF and CSLS retrieval)
  2. Radovanović, Nanopoulos and Ivanović 2010, the hubness phenomenon, some points are nearest neighbours of many points in high dimension
  3. Dinu, Lazaridou and Baroni 2015, the hubness of mapped vectors and the retrieval rule based on reverse ranks
  4. Smith, Turban, Hamblin and Hammerla 2017, the inverted softmax, which normalizes the similarity of each target word over the source words

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • five exercises