Skip to content
AI Engineer Guide.
juniorReportedcurated

Cosine similarity versus dot product: why normalise, and what breaks if you don't?

30-second answer

Likely follow-ups

Open a question to rehearse the answer.

What happens if you normalise at query time but not at ingest — how does the ranking change?
  • Query normalisation fixes the query length but leaves document norms in the inner product, so the score becomes cosine multiplied by each document's norm and long chunks are still promoted.
  • The ranking is not cosine with a constant offset; it is cosine weighted by document length, so the distortion varies per candidate and cannot be corrected by rescaling scores after search.
  • The symptom is a top-k that drifts toward high-norm, usually longer, documents, and the drift grows with the spread of document norms in the corpus; confirm it by comparing Python cosine against the index's top ten.
On unit vectors, how do cosine, dot product and Euclidean distance rank, and where does that equivalence break?
  • For unit vectors cosine and dot product are the same number, and squared Euclidean distance equals 2 minus 2 cos, so ranking by L2 distance produces the same order.
  • The equivalence breaks as soon as magnitudes differ: dot product scales with both lengths and L2 distance mixes angle with length, so the three orders diverge.
  • Translate a distance threshold into a similarity threshold with the formula rather than guessing: on unit vectors ||q - d||^2 = 2 - 2cos, so a cosine cutoff of 0.8 is squared-L2 0.4, and the comparison inverts because L2 is a distance and cosine is a similarity.
When would you deliberately keep magnitude in the score, and what would you have to recalibrate?
  • Keep magnitude when the encoder was trained with a dot-product objective and the norm was shown to correlate with answer quality on your own labelled pairs.
  • Recalibrate every cutoff, deduplication radius and clustering threshold, because raw dot scores are unbounded and not comparable across queries.
  • Monitor the norm distribution and its correlation with judgements; a long tail means the ranking is partly a popularity ranking.

Red flags

  • says cosine == dot without caveat
  • ignores ingest/query parity

Concepts this tests

Tags

Source: ishwarj.com

More in Embeddings & retrieval