Skip to content
mlmentorship

Embedding spaces and similarity metrics

How learned vector representations encode meaning, and why cosine similarity is the default metric for retrieval and recsys.

Published · 5 min read ·Role-specific ·Intermediate

Visual quick review

Visual first · depth when needed

See how L2 normalization removes vector magnitude from retrieval so dot product and cosine produce the same ranking, while Euclidean distance becomes a monotonic expression of the same angle.

Preparing the visual…

Summary

An embedding space is a learned vector space in which points represent objects (words, sentences, images, items, users) and geometric relationships. Distance, angle. Encode semantic relationships such as similarity or relevance.

Embeddings are the substrate for retrieval, recommendation, search, clustering, classification, and most LLM-adjacent products. A senior interview will check whether you can pick the right similarity metric, the right normalization, and the right index.

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 is the default for sentence embeddings (BERT-family), CLIP, two-tower retrieval, and most embedding APIs. Reason: training objectives (contrastive, triplet) typically L2-normalize, making magnitude meaningless.

L2 normalization

A common convention: project every embedding onto the unit sphere by dividing by its L2 norm before storing or comparing. Effects:

  • Dot product equals cosine similarity (no extra division at query time).
  • Vector index (FAISS, ScaNN, HNSW) can use Inner Product mode for cosine retrieval.
  • Magnitude (which often correlates with item popularity or training frequency) is removed as a confound.

Metric geometry

What changes when every embedding is projected onto the unit circle?

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: follow each ray from the origin. Before normalization, A's extra length overwhelms its worse angle, so dot product ranks A first. On the unit circle only direction remains: dot equals cosine, and the shorter L2 chord identifies the same winner, B.

If you store unnormalized embeddings and compare with cosine, you’re paying the normalization cost at every query.

Geometry of learned embeddings

Empirical regularities in well-trained embedding spaces:

  • Clusters form for semantically similar items.
  • Linear analogies (king − man + woman ≈ queen) hold in word2vec / GloVe; less reliably in modern contextual embeddings.
  • Anisotropy: contextual LM embeddings (BERT, GPT) often concentrate in a narrow cone; cosine on raw embeddings can be misleading. Whitening or mean-centering helps.
  • Curse of dimensionality: in high-d, all pairwise distances concentrate. Distinguishing top-1 from top-10 becomes noisier. Useful embedding dimensions are typically 64–1024 even when the model space is much larger.

Indexing for fast retrieval

For items and queries:

MethodBuildQueryRecall
Brute force.exact
HNSW (Malkov & Yashunin, 2018)tunable, ~95–99%
IVF + PQ (FAISS)tunable
ScaNN (Google)tunable

HNSW is the default for most production embedding stores (Pinecone, Weaviate, pgvector with hnsw index, Qdrant).

Common pitfalls

  • Mixing normalized and unnormalized vectors in the same index. Cosine and dot give different rankings.
  • Comparing across embedding models. Vectors from BERT and CLIP live in unrelated spaces; concatenating or comparing across them is meaningless without alignment.
  • Treating embedding dimension as quality. Higher-d embeddings are not strictly better; tradeoff is recall vs. storage and query latency.
  • Ignoring popularity bias. Magnitude correlates with frequency; if you don’t L2-normalize, popular items dominate top-k for everyone.