Multi-Head Attention
The transformer block assembled last lesson used single-head attention: one set of Q/K/V projections, capturing one kind of relationship. But a sentence has many kinds of relationships at once, which word is the subject, which pronoun refers to which noun, which word disambiguates another. A single head has to compromise across all of them.
The fix: run several attention computations in parallel, multiple heads, each with its own learned Q/K/V projections, so each head is free to specialize. Their outputs get concatenated and mixed back together with one more projection.
Two heads, same query, different weight vectors — click a token
Head A (dim 0)
Head B (dim 1)
Concatenate both heads' outputs, mix with an output projection W_O: [1.02, 0.38]
Head A and Head B here use different (fixed, toy) projections, and you can see them genuinely disagree about what a token should attend to, that's the entire point. A real model might have 8, 12, or dozens of heads, each nudged by training toward noticing something different, and it's the combination of all of them, mixed by the output projection, that gives a transformer block its expressive power.
Why do transformers use multiple attention heads instead of one larger one?
Three lessons of pieces, now put them all in one place. Pick any token below and step through (or hit Play) to watch its real, computed vector move through every stage of one complete transformer block.
The transformer block, assembled, click a stage or hit play
Pick a token above, then click any stage (or hit Play) to see its real computed vector and what that stage actually does.
The block output is still just a vector per token, a richer, context-aware one, but not yet a prediction. Turning it into an actual next-token probability, and training the whole stack to get better at that, is next.
Go deeper
Visualizes exactly the block you just assembled, stacked self-attention and feed-forward layers, inside a real, complete GPT-2 model.
Builds and trains a real GPT-style Transformer in code from the ground up, the natural next step after assembling one block by hand.