Modules/Gen AI/ Similarity Metric Lab

Dot Product vs Cosine Similarity

Two ways to score "how similar", and they disagree the moment length stops being constant. Which one your embedding model was trained for is not optional trivia.

Stretch Doc B

1.0×

same direction as before, just longer — like a verbose, repetitive document embedding

Rank By

Query: "lightweight training method"

The Space, 2D For Clarity

Both Scores

 

Dot Product vs Cosine: A Practical Guide

The same two vectors, two different questions asked of them.

Quick Context

Cosine similarity asks "what angle apart are these two vectors" and ignores length entirely. Dot product asks "how much do these two vectors agree, weighted by how long they both are" — length is part of the answer, not discarded. Most embedding search defaults to cosine, but a growing number of models (some recommendation embeddings, some matryoshka and MIPS-optimised models) are trained so that dot product is the correct similarity, and using cosine on them silently under-uses the model.

The formulas

dot(a,b) = Σ aᵢbᵢ     cos(a,b) = dot(a,b) / (‖a‖ ‖b‖)

Cosine is the dot product after dividing out both vectors' lengths — which is exactly why stretching one vector along its own direction changes its dot product with anything but never changes its cosine with anything.

Interactive Exploration Guide

  1. Start at 1x with cosine ranking. Doc A, the concise on-topic passage, ranks first.
  2. Stretch Doc B to 5x. Under cosine, nothing about the ranking moves — Doc B's direction, and therefore its angle to the query, never changed.
  3. Switch to dot product and stretch again. Somewhere around 3-4x, Doc B overtakes Doc A — purely because it got longer, with its actual topical relevance unchanged.
  4. Read both scores side by side at 5x. Cosine still ranks Doc A first; dot product now ranks Doc B first. Same embeddings, same query, opposite answer.

Which one is "right"?

Neither, in the abstract — it depends on what the embedding model was trained to make meaningful. If length in your model's embeddings correlates with noise (document verbosity, padding, repetition), cosine is safer. If length was trained to carry real signal (popularity, confidence, specificity), dot product is the metric the model actually optimised for, and cosine throws that signal away. Check your model's documentation; do not assume.

Key Takeaway

Cosine similarity divides out vector length and only ever measures direction; dot product does not, so it rewards longer vectors regardless of whether that length means anything. The two metrics agree only when every vector has the same length — true if you normalise your embeddings to unit length, false otherwise. Know which one your embedding model was trained against, because using the wrong one is a silent, hard-to-debug retrieval quality bug.

Predict, then reveal

About to run: Start at 1x with cosine ranking. Before it does — what happens to the readout?

Committing to an answer first is the point — the reveal runs the experiment on the visualisation above and reads the real value back, so nothing here is scripted.

Recall check

0 of 3

Say the answer out loud before you reveal it — recalling it is what makes it stick, and rereading it is not.

  1. Without scrolling back — what is the one-line takeaway from this module?

  2. What does this module say about “Stretch Doc B”?

  3. What does this module say about “Quick Context”?

Cheat sheet

Dot Product vs Cosine Similarity for Retrieval

Two ways to score "how similar", and they disagree the moment length stops being constant. Which one your embedding model was trained for is not optional trivia.

GEN AI · vizlearn.in/gen_ai/dot_product_vs_cosine_similarity.html