Core AI Intuitions · lesson 01/4
Similarity With Dot Product
When a model says two sentences are similar, it is usually taking a dot product of their embeddings and comparing the result to a threshold. The dot product is the smallest operation in deep learning that carries a notion of agreement, and attention scores, retrieval rankings, and recommendation scores are all built on it.
The idea
The dot product of two vectors of the same length multiplies matching coordinates and sums the results:
Read that second form as two separate signals:
- Direction — is positive when the vectors point the same way, zero when they are perpendicular, and negative when they oppose each other.
- Magnitude — the lengths and scale the score, so a long vector can outscore a better-aligned short one.
Cosine similarity divides the magnitudes out: , which ranges from to . For unit vectors the dot product is the cosine, so normalizing first turns a magnitude-sensitive score into a pure direction score.
Unlike Euclidean distance, the dot product is not a metric: it has no triangle inequality, and it is maximized rather than minimized for identical vectors. The two are related by .
Worked example
Take and :
- , so — nearly aligned.
Now make them orthogonal: , gives . And scale : , gives a dot product of but a cosine of . Same direction, twice the length, a much larger raw score.
In code
import torch
import torch.nn.functional as F
a = torch.tensor([1.0, 2.0, 3.0])
b = torch.tensor([2.0, 4.0, 6.0]) # same direction, twice as long
print(torch.dot(a, b)) # tensor(28.)
print(F.cosine_similarity(a, b, dim=0)) # tensor(1.0000)
print(F.normalize(a, dim=0) @ F.normalize(b, dim=0)) # tensor(1.0000)The raw dot product is 28, but once both vectors are scaled to unit length the score collapses to 1.0 because the directions are identical.
Check yourself
- Two nonzero vectors have a dot product of zero. What is the angle between them, and does that conclusion depend on their lengths?
- Why can the dot product rank embeddings badly when vector lengths vary, and what does cosine similarity change?
- For unit-length vectors, why is minimizing Euclidean distance the same as maximizing the dot product?
Key takeaways
- The dot product measures directional agreement, weighted by both vector magnitudes.
- Cosine similarity removes magnitude; for unit vectors it equals the dot product.
- Attention weights and similarity search are rankings over dot products of embeddings.