Skip to content
mentoor

Topic 5 of 5 · 5 min read

Distance metrics and normalization

Goal: Choose the correct distance metric for a given embedding model and detect a mismatch from a set of odd similarity scores.

Use the distance metric your embedding model was trained with, and know whether its vectors are normalized, because for normalized vectors cosine, dot product and Euclidean distance give the same ranking.

Check yourself · 1 of 3

For vectors that all have length 1, which statement is true?

Choose one answer

Why it matters

A wrong metric does not fail loudly. Search returns results that look plausible and rank a little worse, or rank long, noisy chunks above good ones. You find out as "the answers are mediocre".

The three metrics

  • Cosine compares direction and ignores length. Two vectors pointing the same way score 1, regardless of magnitude.
  • Dot product (inner product) multiplies component by component and sums. It is affected by length, so a longer vector scores higher.
  • Euclidean (L2) is the straight-line distance between the points. Smaller is closer.

If every vector has length 1 (normalized), then dot product equals cosine, and Euclidean distance orders results the same way. OpenAI's embeddings documentation states its embeddings are normalized to length 1, so cosine and dot product give identical rankings, and dot product is slightly faster. Other models do not normalize, and some are trained for dot product on purpose.

How to choose and check

  1. Read the model card. It names the intended metric. Use that one.
  2. Check the norm. Embed 20 texts and compute their lengths. All near 1.0 means normalized.
  3. Match three places. The query operator (<=>, <#>, <->), the index operator class (vector_cosine_ops, vector_ip_ops, vector_l2_ops) and the model. A mismatch between the first two means the index is not used.
  4. Normalize yourself when you need a cheaper metric: divide each vector by its length before insert.

Before and after

# Before: a model that does not normalize, ranked by dot product
scores = chunks_matrix @ query_vec   # long chunks always win
import numpy as np
def unit(v): return v / np.linalg.norm(v)
scores = np.array([unit(c) for c in chunks]) @ unit(query_vec)   # cosine

Symptoms of a mismatch

  • Scores above 1 or far outside 0 to 1 from a "cosine" query: the code is returning raw dot products.
  • The same few long chunks appear for every query.
  • An off-by-one in meaning: distance 0.1 treated as similarity 0.1.
  • Switching models without re-checking the metric.

Examples

Diagnosing odd scores

A 'cosine' search returns scores of 14.2, 13.9 and 9.7. Cosine cannot exceed 1. The code is returning raw dot products of non-normalized vectors. Normalize on insert, or switch the query to true cosine.

Same ranking, cheaper operator

A model's vectors all have length 1.000. Cosine distance and negative inner product rank identically. Using vector_ip_ops and <#> is valid and slightly cheaper.

Key points

  • Use the metric the embedding model was trained for; its model card says which.
  • For vectors of length 1, cosine, dot product and Euclidean distance produce the same ranking.
  • Check normalization by embedding a sample and computing vector lengths.
  • The query operator, index operator class and model must all agree, or the index is skipped.
  • Raw dot products on non-normalized vectors favour long vectors and show up as scores outside 0 to 1.

Common mistakes

  • Assuming every model is normalized.
  • Mixing an L2 index with cosine queries.
  • Comparing scores across models as if they shared a scale.
  • Using cosine on vectors from one model and queries from another.

Check yourself · 2 of 3

A search labelled 'cosine' returns scores of 11.8, 10.2 and 7.9. What is the most likely problem?

Choose one answer

Check yourself · 3 of 3

A table has an index with vector_l2_ops, but queries use ORDER BY embedding <=> $1. What is the effect?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic