Embedding Geometry and Similarity
Cosine, dot product and Euclidean distance and the exact relations between them; why high-dimensional near-orthogonality is what makes retrieval possible; and the maximum-of-N problem that limits it.
An embedding model is a map from text (or images, or anything) to vectors. Retrieval ranks documents by a similarity between a query embedding and a document embedding. This page is about the geometry that ranking lives in.
Three similarities and how they relate
For with ,
Hence ranking by largest dot product, largest cosine, or smallest Euclidean distance gives the identical ordering on the unit sphere.
For general ,
So dot-product ranking of documents against a fixed query orders by , which a document can increase by growing its norm at fixed direction. Euclidean ranking penalises large instead. The three criteria therefore give three different orderings.
Near-orthogonality
Let be independent and uniform on , . Then has density
with , , and
Key idea
Two independent directions in have cosine within about of zero. So a "background" similarity of is what noise looks like, and a genuine match at stands out by a factor of eight. This is what makes dense retrieval possible at all: high dimension supplies an enormous number of nearly-orthogonal directions, so distinct meanings can be assigned distinct directions without crowding.
Quantitatively, by the tail bound plus a union bound, one can place directions with all pairwise cosines below as long as , i.e. — exponentially many in the dimension. At and that is strictly -separated directions by this crude bound, and far more with a sharper packing argument; the exact statement is the packing number of the sphere.
The maximum-of-N problem
Retrieval is not a pairwise question. The relevant document must beat all distractors, so the quantity that matters is the maximum of similarities.
Let be independent uniform directions and a fixed unit vector. Then
and with probability at least ,
Suppose the relevant document satisfies and the distractors are independent uniform directions. Then top-1 retrieval succeeds with probability at least provided
Key idea
Put numbers in. At and :
| Corpus size | Required alignment |
|---|---|
The threshold grows like — slowly, but it grows, and there is no corpus size at which it stops. Tripling the required alignment is not possible by scaling the index; it requires a better embedding model or a re-ranking stage. Conversely, raising helps as , which is the mathematical case for larger embedding dimensions.
Empirical observation — a regularity seen in experiments. No theorem on this page establishes it in general.
Learned embeddings do not occupy the sphere uniformly. Ethayarajh measured average cosine similarities between random unrelated sentences in contextual models and found values of – in some layers — far above the the isotropic model predicts. The embeddings lie in a narrow cone.
What this does not say
Every theorem on this page assumes the distractors are independent uniform directions. Anisotropy breaks that assumption in the direction that makes retrieval harder:
- the baseline similarity is inflated, so the contrast between a match and a non-match shrinks;
- the distractors are correlated, so the maximum-of- bound — which is driven by the effective number of independent directions — is not the right calculation, and the effective can behave like a much larger number of truly independent draws in the cone's few informative directions;
- near-duplicate documents, which real corpora are full of, are the worst case: they are highly correlated with the true match and the maximum-of- analysis says nothing about them.
The standard remedies are all attempts to restore approximate isotropy: mean-centring, whitening (BERT-flow and successors), and contrastive training with in-batch negatives, which explicitly penalises high similarity between unrelated pairs and is why models like Sentence-BERT and DPR behave far better than raw pretrained encoders.
Read this page's thresholds as a best case: the alignment actually required on real corpora is higher, and how much higher is an empirical question about the specific embedding model, not something the geometry determines.
Problems
Three similarities agree on the unit sphereProof · Foundation · 15 min The density of the cosine between random directionsProof · Advanced · 1 hour The maximum of N distractor similaritiesProof · Advanced · 30 min A sufficient condition for correct retrievalProof · Intermediate · 30 min Measure anisotropy in an embedding spaceCoding · Intermediate · 1 hourReferences
- PaperVladimir Karpukhin and collaborators (2020). Dense Passage Retrieval for Open-Domain Question Answering
- PaperNils Reimers and Iryna Gurevych (2019). Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks
- PaperKawin Ethayarajh (2019). How Contextual are Contextualized Word Representations?
- PaperBohan Li and collaborators (2020). On the Sentence Embeddings from Pre-trained Language Models
- BookRoman Vershynin (2018). High-Dimensional Probability: An Introduction with Applications in Data Science
These works are cited as sources to read, not reproduced. All exposition, examples and problems on this site are written independently; where an argument is standard, the citation points to a canonical treatment.
Results stated on this page
- PropositionCosine, dot product and Euclidean distance agree on normalised vectorsproof includedOn the unit sphere the three ranking criteria induce the same ordering; off it they do not.
- PropositionWithout normalisation the norm dominates the rankingproof includedDot-product retrieval prefers high-norm documents independently of direction, which is a documented failure mode.
- TheoremDistribution of the cosine between random directionsproof includedThe cosine between two independent uniform directions has an explicit density concentrating at zero at rate one over the square root of the dimension.
- TheoremThe maximum of N distractor similaritiesproof includedThe best of N random distractor scores grows like the square root of two log N over d, so the alignment a retriever needs grows with the corpus.
- CorollaryA sufficient condition for correct top-1 retrievalproof includedThe query-document alignment must exceed the maximum distractor similarity, giving an explicit threshold in terms of corpus size and dimension.
- Empirical observationReal embeddings are anisotropicLearned embeddings occupy a narrow cone rather than the sphere uniformly, which inflates baseline similarities and invalidates the isotropic model.
Experiments
Problems (5)
All problems- The density of the cosine between random directionsProofAdvanced⏱ 1 hour
- The maximum of N distractor similaritiesProofAdvanced⏱ 30 min
- A sufficient condition for correct retrievalProofIntermediate⏱ 30 min
- Three similarities agree on the unit sphereProofFoundation⏱ 15 min
- Measure anisotropy in an embedding spaceCodingIntermediate⏱ 1 hour
Papers that use this mathematics
Dense Passage Retrieval for Open-Domain Question Answering
Vladimir Karpukhin, Barlas Oğuz, Sewon Min, Patrick Lewis, Ledell Wu, Sergey Edunov, Danqi Chen, Wen-tau Yih · 2020
Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks
Patrick Lewis, Ethan Perez, Aleksandra Piktus, Fabio Petroni, Vladimir Karpukhin, Naman Goyal, Heinrich Küttler, Mike Lewis, Wen-tau Yih, Tim Rocktäschel, Sebastian Riedel, Douwe Kiela · 2020
Distributed Representations of Words and Phrases and their Compositionality
Tomas Mikolov, Ilya Sutskever, Kai Chen, Greg Corrado, Jeffrey Dean · 2013