Skip to content
mlmentorship

Embedding spaces and similarity metrics

Choose dot product, cosine, or distance from the retrieval objective, then verify normalization and approximate indexing.

Published · 6 min read ·Role-specific ·Intermediate
Reading depth

Summary

An embedding maps an object to a vector; its training objective determines which vector relationships should predict relevance. Choose whether length, direction, or absolute distance should affect retrieval before choosing the index. Normalization changes the ranking objective unless the model and task already make length irrelevant.

Common similarity metrics

For two vectors :

MetricFormulaWhen to use
Dot productWhen magnitudes are meaningful (e.g., learned matrix-factorization scores). Indexable with MIPS algorithms.
CosineDefault for embeddings where direction encodes meaning and magnitude is a noise/popularity confound.
Euclidean / L2When distances have physical meaning (image patches in pixel space, geographic coordinates).
Negative Euclidean²Equivalent to dot product on L2-normalized vectors plus a constant.

Cosine requires nonzero vectors and explicitly discards positive scale. Some models train with normalized similarities; others use unnormalized scores. Follow the model’s documented scoring contract instead of assuming all embedding models share one metric.

L2 normalization

For nonzero , define and . Then

Thus normalized dot product, cosine, and normalized squared distance induce the same exact ranking, including ties. The identity follows by expanding the squared difference and substituting both unit norms.

Take query and candidates , . Raw dot product favors A: . Cosine favors B: approximately . Their normalized chord distances are approximately .

Metric geometry

Normalization removes the length advantage that made A win the raw dot product

Length wins the raw dot product; direction wins after normalizationFor q=(1,0), A=(10,10) beats B=(1,0) by raw dot product. Cosine reverses the order because B points exactly along q.Remove length. Keep direction.A: raw dot 10; cosine 0.7071B: raw dot 1; cosine 1The diagonal loses its lengthadvantage.
Derivation

Before: magnitude can win

Raw dot product rewards length as well as alignment.

Raw query and candidate embedding vectors From a shared origin, unit query q points right. Candidate A is a longer solid ray ending in a circle at coordinates 1.8, 1.6. Candidate B is a shorter dashed ray ending in a diamond at 0.9, 0.2. Although B has the smaller angle to q, raw dot product ranks A first because A is longer. 0 q = (1, 0) A = (1.8, 1.6) B = (0.9, 0.2) raw dot: A 1.80 > B 0.90

After: direction decides

Normalization preserves each angle and sets every length to 1.

The query and candidates normalized onto a unit circle The same three vector directions now end on one circle. Candidate B's diamond endpoint is closer to query q than candidate A's circle endpoint. Dot product equals cosine, so both rank B first; Euclidean chord distance is also smaller for B. q unit A unit B all endpoints: length = 1 dot = cos: B 0.976 > A 0.747 L2 chord: B 0.218 < A 0.711
Read it this way: raw dot product favors the longer A. Unit normalization preserves angles but discards length. Cosine, normalized dot product, and normalized chord distance then agree on B.
Check: Why does B win both normalized dot and nearest L2 even though A wins the raw dot product?
Read it this way: For q=(1,0), A=(10,10) beats B=(1,0) by raw dot product. Cosine reverses the order because B points exactly along q.
After normalization, can increasing only A’s original length improve its cosine?

Normalize stored candidates once when cosine is the intended objective. Normalize each nonzero query or divide scores by its norm when actual cosine values are needed. Declare a rejection or fallback policy for zero vectors; adding an arbitrary epsilon changes the score definition.

Change the Requirement

Retain Meaningful Magnitude

New task: Serve a model trained to encode relevance through both vector length and direction.

Picture and change: Use the raw dot product rather than projecting onto the unit sphere.

Check: The same fixture now ranks A above B, matching its unnormalized objective.

Use when: Training and evaluation support that scoring contract. Length may be useful signal or an unwanted association; geometry alone cannot determine which.

Trade Exact Search for Candidate Speed

New task: Retrieve from an approximate index under a latency budget.

Picture and change: Candidate generation may omit the exact nearest item; reranking cannot recover an absent candidate.

Check: If exact top-one is B but the index returns only A, top-one recall is zero despite correct reranking.

Use when: Measured latency and recall justify approximation under the same metric, filters, and data version.

Geometry of learned embeddings

Clusters, linear analogies, anisotropy, and distance concentration depend on the model and input distribution. Whitening or centering changes geometry; validate relevance afterward rather than assuming improvement. Compare dimensions through measured retrieval quality, memory, and latency, not a universal preferred range.

Indexing for fast retrieval

For items and queries:

MethodDecision to measure
Exact scan arithmetic; establishes the same-metric reference ranking
HNSWGraph-search breadth, build cost, recall, and memory
IVF with product quantizationNumber of searched partitions and quantization error
ScaNNCandidate-search and quantization settings under the workload

Approximate latency and recall are empirical workload properties, not universal logarithmic guarantees or fixed percentages.

Common pitfalls

  • Mixed objectives. Normalizing only some candidates invalidates the unit-vector equivalence.
  • Mixed coordinate systems. Independently trained encoders need a validated shared-space or alignment contract.
  • Unproved bias claims. Removing length changes scores; it does not establish fairer or better retrieval.

Reconstruct

Can exact reranking fix a missing relevant candidate?

No. Reranking only reorders supplied candidates. Measure candidate coverage separately from ranking quality, including filtered and tail-query populations.