Multi-Head Attention › The Complete Formula
Language AI HandbookAll chapters Multi-Head Attention › The Complete FormulaOne section · Sections in this chapter4 / 14 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Now that we've walked through each step conceptually, let's see how they combine into a complete formulation. Understanding this picture helps clarify how multi-head attention works: by running multiple specialized attention operations in parallel and learning to combine their insights.
The outer structure of multi-head attention takes the outputs from all heads and merges them:
This formula reads from inside out: first compute each head, then concatenate their outputs, then apply the output projection. But what exactly is each head doing? Each head applies its own learned projections before computing attention:
where:
- : the QKV inputs (in self-attention, all three equal the input )
- : the per-head projection matrices for head , each transforming the full -dimensional input into a smaller -dimensional subspace
- : the scaled dot-product attention function from the previous chapter
- : the output projection that learns how to combine all heads' contributions
The design is flexible. In self-attention, , so the same input sequence projects to all three roles. But the same multi-head attention mechanism also supports cross-attention in encoder-decoder models, where queries come from the decoder and keys/values come from the encoder. The mathematical structure remains identical; only the inputs differ.
Why This Works: The Subspace Intuition
Why should projecting to lower-dimensional subspaces and computing attention there be useful? Think of it this way: the full -dimensional embedding space contains many different types of information about each token. Its syntactic role, semantic meaning, position in the sentence, relationship to neighboring words. A single attention operation in the full space must somehow balance all of these considerations when deciding what to attend to.
By projecting to a lower-dimensional subspace, each head can focus on a particular "slice" of the information. One head's projection matrices might learn to emphasize dimensions that capture syntactic structure, causing that head to attend based on grammatical relationships. Another head's projections might emphasize semantic dimensions, causing it to attend based on meaning similarity. The output projection then learns to weigh and combine these different perspectives.
This is analogous to how ensemble methods work in machine learning: multiple weak learners, each capturing part of the pattern, combine to form a stronger predictor than any single model could achieve.
There is a subtle but important distinction between multi-head attention and a simple ensemble of independent attention modules. In an ensemble, each member operates on the same full representation and the combination is usually an average or a vote. In multi-head attention, the combination is a learned linear transformation through . This output projection is not a passive merger; it actively learns how to mix head contributions in a context-dependent way. Heads that are more relevant for a given input can have their contributions amplified, while heads that detected spurious patterns can be suppressed. The projection also allows interactions between head outputs, so the final representation can encode relationships that no single head captured on its own.
In practice, this means you should think of the output projection as a learned integration step rather than a simple concatenation. The concatenation preserves all head outputs separately so the projection can distinguish between them. If you replaced concatenation with summation, you would lose the ability for the projection to treat different heads differently, and you would also lose the dimensional expansion that gives the projection enough freedom to mix contributions flexibly.
Parameter Count Analysis
A natural question arises: if we're running separate attention operations, doesn't that multiply our parameter count by ? No. The dimension splitting ensures that multi-head attention uses approximately the same number of parameters as single-head attention with the full dimension.
Let's trace through the calculation:
-
Per head: Each head has three projection matrices (, , ), each of size . That's parameters per head.
-
All heads: With heads, we multiply by : total QKV parameters are . Here's the key insight: since , we have . This simplifies our expression to .
-
Output projection: The matrix has size . With , the input dimension is , giving us parameters.
-
Grand total: parameters.
This is exactly what we'd have with single-head attention using the full dimension, plus one additional output projection. The key benefit of multi-head attention is that we get different attention patterns, each potentially specializing in different relationships, for essentially the same parameter budget as a single larger head.