Chapter 10 had one query looking at a fixed set of tokens from the outside. What if every token in a sequence looked at every other token — including itself — all at the same time?
Row "sat" is exactly uniform — it sits equally close to both "the" and "cat", so it has no reason to favor either.
This is self-attention: the query, the keys, and the values are all the same sequence. Every row is one token acting as a query; every column is a token it can attend to, itself included. "The" mostly attends to itself; "sat," sitting exactly between "the" and "cat," has no reason to prefer either.
A single attention computation is one view of the sequence. Multi-head attention runs several of these views in parallel, each looking at the tokens through a different projection, then combines what they found:
- — the query, key, and value matrices, here all built from the same input sequence since this is self-attention.
- — head 's own learned projection matrices, giving that head its own view of the sequence.
- — the output of attention computed inside head , using its own projections.
- — the number of attention heads running in parallel.
- output — the combined result of all heads, concatenated together.
- Each head learns its own view
Each head's are learned separately, so each head is free to pick up on a completely different signal in the same tokens — one might track syntax, another meaning, another position.
- No single head does all the work
Because the heads specialize independently, the model isn't forced to squeeze every kind of relationship through one shared projection.
query = "sat" — Head A row: [0.67, 0.09, 0.24], Head B row: [0.09, 0.67, 0.24] — disagreement = 0.813
Two toy heads, one projecting each token onto just its horizontal axis, the other onto just its vertical axis. Click through the query tokens. For "the" and "cat," one head goes flat (uniform) while the other has a clear opinion — that head simply has nothing to say, not a real disagreement. Watch "sat," though: both heads have a strong, opposite opinion about where to look.
- Project the query
As a query, "sat" projects to on both axes.
- Head A (horizontal)
Multiplying the query's projection () by each token's own horizontal projection:
- "the" : score
- "cat" : score
- "sat" : score
Softmax of gives roughly — favoring "the."
- Head B (vertical)
The exact same query projection (), now against each token's vertical projection:
- "the" : score
- "cat" : score
- "sat" : score
Softmax of gives roughly — favoring "cat," the mirror image.
- Concatenate
Combining both heads' outputs keeps both signals side by side, instead of forcing one projection to average them away.
Click the token where Head A and Head B disagree the most about where to look.
Self-attention lets every position see every other position in one step, with no recurrence and no fixed distance penalty. Multi-head attention lets that happen from several angles at once, so a token whose relationships are genuinely multi-faceted — like "sat," pulled toward both its subject and its location — doesn't have to pick just one. The next chapter assembles this, a feedforward layer, and a residual connection into the actual repeating unit a Transformer stacks: the Transformer block.