juniorReportedcurated
Cosine similarity versus dot product: why normalise, and what breaks if you don't?
30-second answer
- Cosine divides the dot product by both vector lengths, so it measures the angle between embeddings and discards magnitude; on L2-normalised vectors the denominator is one and cosine equals the dot product.
- Because the rankings are identical on unit vectors, the practical recipe is to normalise once at write time and query with an inner-product index, which is cheaper and friendlier to SIMD than computing two norms per request.
- Skip normalisation and the raw dot product rewards long vectors, so a long generic chunk can outrank a short precise one and the top of the list fills with documents that merely have large norms.
- The trade-off is that magnitude may carry real signal in some embedding spaces, but it is not a reliable confidence or length signal and you rarely want it in ranking.
- The failure that bites is convention drift: ingest unnormalised and query normalised, or change the index metric, and rankings shift silently without any error, so record normalisation in index metadata and verify it after deploy.
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
- Where do embeddings systematically fail as a retrieval mechanism, and which failures can you fix cheaply?
- You must choose an embedding model for a 900k-chunk support corpus. Walk me through the bake-off and the constraints that don't appear on a leaderboard.
- You have 25M chunks at 1,536 dimensions. Size the index and tell me how you make it fit — then what changes at 10x.