Multi-Head Attention
Several smaller attentions run in parallel so the model can learn different kinds of relationships at once
Several smaller attentions run in parallel so the model can learn different kinds of relationships at once
One attention head produces one softmax-weighted view of how tokens relate to each other. Multi-head attention runs several such heads in parallel, each with its own learned projections, and merges their outputs — so a single layer can capture several different kinds of relationships simultaneously instead of being forced into one compromise view.
Multi-head attention is used in every Transformer encoder and decoder layer — BERT, GPT, T5, and essentially every modern Transformer-based generative model. It's a key reason Transformers can represent syntax, coreference, and positional patterns all in the same layer, rather than one attention mechanism trying to average all of that into a single distribution.
A single attention head produces exactly one set of attention weights per token — one weighted average per token, full stop. That's a single "compromise" view of relevance. But language has many kinds of relationships at once: which word is the subject of which verb, which pronoun refers to which earlier noun, which words are simply nearby in position. Ek hi head se yeh sab ek saath pakadna mushkil hai — isliye alag-alag heads ko alag-alag projections mein chalaya jaata hai, so each can specialize in a different kind of relationship, and the results are combined afterwards.
Q, K, V are each linearly projected separately for every head i,
using that head's own learned matrices W_Q^i, W_K^i, W_V^i, typically down to a smaller
dimension d_k = d_model / h.Q, K, V.h heads' output vectors are concatenated back together along the feature
dimension.W_O
that mixes information across heads back into a single d_model-sized vector.head_i = Attention(QW_Q^i, KW_K^i, VW_V^i)
MultiHead(Q,K,V) = Concat(head_1, ..., head_h) W_O
h — number of attention heads.W_Q^i, W_K^i, W_V^i — per-head learned projection matrices (distinct for every head i).Attention(...) — the usual scaled dot-product attention, softmax(QK^T/√d_k)V, applied inside
each head on that head's own projected Q, K, V.Concat(head_1,...,head_h) — stacking all heads' outputs side by side into one vector per token.W_O — a learned output projection that maps the concatenated vector back to d_model dimensions
and lets information from different heads mix.Because each head works in a reduced dimension d_k = d_model / h rather than the full d_model,
the total compute across all h heads stays comparable to a single full-dimension head — multi-head
attention buys breadth of relationship-types, not a multiplied compute cost.
Take the sentence: "The dog chased the ball because it was fast." This is an illustrative, not a real measured example, of what different heads could specialize in after training:
In practice, heads aren't hand-assigned these roles; such specialization, if it happens, emerges from training, and different heads in a real trained model often split responsibilities in less clean-cut ways than this illustration.
It also helps to think about why a single head genuinely can't do this job alone: a head produces
exactly one softmax distribution per token, i.e. one number per (token, other-token) pair. If
"which word is my subject" and "which word is nearby" both needed to be expressed through that same
single distribution, the head would have to blend both signals into one compromise set of weights —
losing resolution on both. Separate heads, each with their own W_Q^i, W_K^i, W_V^i, give the model
separate "slots" to put each kind of signal into, without one drowning out the other.
d_k = d_model/h); it does
not run h full-size attentions and multiply the compute by h.Concat(...)W_O step is required — without the output projection, the heads' outputs
are just disconnected slices sitting next to each other, with no mixing across heads.d_model must be divisible by h so that d_k = d_model / h is a whole number.h while keeping d_model fixed shrinks each head's dimension
d_k — more heads is not automatically "more capacity," it's "more, narrower views."Multi-head attention = run several smaller attentions in parallel on different learned projections,
then concatenate and mix them with W_O — breadth of relationship-types in one layer instead of one
attention trying to do everything.