Why is cosine similarity preferred over Euclidean distance for text embeddings?
Same topic, related formats. Practice these next.
Same topic, related formats. Practice these next.
Text embedding norms vary with content length and density, so cosine measures direction (semantic meaning) while ignoring magnitude. Euclidean distance would conflate length with semantic difference.
Imagine arrows on a giant map, all starting from the centre and pointing outward. Each arrow stands for a sentence. We want to measure how alike two sentences are. One way: ask if the arrows point in the same direction, no matter how long they are. Another way: ask how close the tips of the arrows are. The first way only cares about direction. The second cares about direction and length together. For sentences, the direction is what carries meaning. The length usually just reflects boring things like how many words were in the sentence. If we measured by tip position, two paragraphs about the same topic but with different lengths would look unrelated. By only looking at direction, they line up the way a person would expect: same idea, same heading on the map.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
Open with direction vs magnitude as the conceptual axis. State why direction carries meaning and magnitude carries length-and-density noise. Note that modern APIs normalise, which makes cosine and Euclidean rank equivalently. Close with the contrastive-training argument tying the inference metric to the training objective.
Real products, models, and research that use this idea.
What an interviewer would ask next. Try answering before peeking at the approach.
Red flags and common mistakes that signal junior thinking. Click to expand.
Assuming cosine and Euclidean rank results identically. They agree only when all vectors are normalised to unit length. Modern embedding APIs often already L2-normalise, hiding the distinction in practice.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.