Similarity With Dot Product
The one operation behind embeddings, search, and attention.
๐ก The intuition
Two people rate pizza, hiking, and jazz. Multiply their ratings category by category and add it all up. Shared loves make the total big; opposite tastes make it small. That single number is the dot product โ a score for how aligned two vectors are.
Query concept
king = [0.9, 0.8, 0.1]
Most similar to โkingโ (by cosine)
Step by step: king ยท queen
dot = 0.9ร0.8 + 0.8ร0.9 + 0.1ร0.15 = 1.455
|king| = 1.208 , |queen| = 1.213
cosine = dot / (|king|ยท|queen|) = 0.992
Cosine near 1 = same direction (very similar); near 0 = unrelated; toward โ1 = opposite.
A tiny worked example
A = [1, 2, 3], B = [2, 0, 4]
dot = (1ร2) + (2ร0) + (3ร4) = 2 + 0 + 12 = 14
|A| = โ14 โ 3.742, |B| = โ20 โ 4.472
cosine = 14 / (3.742 ร 4.472) โ 0.837 โ close to 1 โ similar direction
Free lesson complete
Continue with the complete Core AI Intuitions path.
Pro unlocks the surrounding lessons, runnable practice, and the full build-ready sequence.
Why it matters in AI
- โธSemantic search ranks documents by cosine similarity to your query.
- โธAttention scores are query ยท key โ how much one token should attend to another.
- โธDeduplication and clustering use cosine โ 1 to find near-identical items.
- โธRecommendations match a user vector against item vectors with a dot product.
Things people get wrong
โ A bigger dot product always means more similar.
โ Only if magnitudes are comparable. Cosine normalizes out length so direction is what counts.
โ Cosine and dot product are the same thing.
โ Cosine is the dot product of two length-1 vectors. They only match after normalizing.
โ Similarity can't be negative.
โ Cosine ranges from โ1 to +1. 0 means unrelated; negative means opposing direction.
Related reading ยท Knowledge Lab