- Key idea
- a · b = a₁b₁ + a₂b₂ + … ; equals |a| |b| cos θ; sign tells same or opposite direction; cosine similarity; projection
- Real world
- A shopping bill. Quantities [2, 1, 3] (bread, milk, apples) dotted with prices [1.20, 0.90, 0.40] gives 2×1.20 + 1×0.90 + 3×0.40 = 4.50 pounds.
- In an LLM
- To decide where to look, each token’s query is dotted with every earlier token’s key. In “The cat sat on the”, the last “the” has the largest dot product with “cat”, so it pays most attention to “cat”.
Multiply and add
The of two vectors of the same length multiplies them position by position and adds everything up:
For and : . Two lists in, one number out. Both vectors must have the same length, or there is nothing to pair up.
The arithmetic is simple. What makes it useful is what the number means.
What the number measures
The same number can be computed from the arrows’ lengths and the angle between them:
is the length of the arrow (the norms lesson covers it). is 1 when the arrows point the same way, 0 at a right angle and −1 when they point in opposite directions. So:
- Positive: roughly the same direction (angle under 90°).
- Zero: at right angles. The vectors are called orthogonal: they share nothing.
- Negative: roughly opposite directions.
There is also a picture. Shine a light straight down onto the line through : casts a shadow on it, called its projection. The dot product is the shadow’s length times ’s length, negative if the shadow falls backwards.
Drag both arrows and try the three buttons:
You buy 2 loaves, 1 pint of milk and 3 apples: the quantity vector . The prices are pounds. The bill is their dot product, pounds. Every till in every shop computes dot products. And “how much does this basket match what is on offer” is the same calculation with discounts in place of prices.
Similarity: cosine
The dot product grows with the lengths of the vectors as well as with their alignment. To measure direction alone, divide by both lengths. This is the :
It always lies between −1 and 1. Here it is for 48 real GPT-2 word embeddings, each 768 numbers long. Nobody told GPT-2 that cats and dogs are both animals; the similarity comes from training.
The embeddings of “ cat” and “ dog” have cosine similarity 0.55: an angle of about 57° in 768 dimensions. “ cat” and “ France” have 0.25, about 76°. Countries are tightly grouped: “ France” and “ Germany” have 0.70. Search engines and retrieval systems use exactly this: turn a question and every document into vectors, and return the documents with the highest cosine.
Attention scores are dot products
Inside every attention head, each token makes a query vector (what it is looking for) and a key vector (what it contains). The score for “token i looks at token j” is the dot product of i’s query with j’s key. Here is the toy head from the Transformers course:
A real GPT-2 head does this with 64 numbers per query, for every pair of tokens. A 1,000-token text needs a million of these dot products per head, which is why they are organised as one matrix multiplication.
Why the two formulas agree
In 2D, write and . Then
In any dimension the same follows from the law of cosines applied to the triangle .
The projection of onto is the vector , and its signed length is .
Properties
- Symmetric: .
- Distributes: , and .
- .
In code
import numpy as np
a, b = np.array([3.0, 1.0]), np.array([2.0, 2.0])
a @ b # 8.0 (same as np.dot(a, b))
cos = a @ b / (np.linalg.norm(a) * np.linalg.norm(b)) # 0.894
import torch.nn.functional as F
F.cosine_similarity(x, y, dim=-1) # for torch tensorsRandom vectors in high dimensions
Two random directions in 768 dimensions are almost orthogonal: their cosine is typically within about of zero. That is why the GPT-2 values above, all at least 0.14, are not chance: the word embeddings share a common direction (they are “anisotropic”), and related words share more. Ethayarajh, 2019, measures the same effect in GPT-2’s hidden states.
In papers
- Dot-product attention: Vaswani et al., 2017, section 3.2; additive attention it replaced: Bahdanau et al., 2014.
- Cosine similarity for sentence embeddings: Reimers and Gurevych, 2019 (Sentence-BERT).