Intuition Why Multiple Heads?
Imagine you're reading a sentence and trying to understand it. Your brain doesn't just look at words in one way—you simultaneously track grammar, meaning, context, and emotional tone. Multi-head attention does the same : it lets the model attend to different aspects of the input at once. One head might focus on syntactic relationships (subject-verb), another on semantic similarity, another on positional patterns. By running multiple attention mechanisms in parallel , we capture richer, more nuanced relationships than a single attention head ever could.
Single-head attention has limited representational capacity . A single set of Q , K , V Q, K, V Q , K , V projections can only learn one type of relationship pattern. But language (and most sequential data) has multiple simultaneous structure types:
Syntactic dependencies (subject agrees with verb)
Semantic relationships (synonym, antonyms)
Positional patterns (nearby words vs. distant context)
Co-reference (pronouns linking to earlier nouns)
Multi-head attention (MHA) learns multiple representation subspaces in parallel, each head specializing in different patterns.
Definition Multi-Head Attention
Multi-head attention consists of h h h parallel attention mechanisms (heads), each with its own learned projection matrices. The outputs are concatenated and linearly transformed:
MultiHead ( Q , K , V ) = Concat ( head 1 , … , head h ) W O \text{MultiHead}(Q, K, V) = \text{Concat}(\text{head}_1, \ldots, \text{head}_h) W^O MultiHead ( Q , K , V ) = Concat ( head 1 , … , head h ) W O
where each head is:
head i = Attention ( Q W i Q , K W i K , V W i V ) \text{head}_i = \text{Attention}(Q W_i^Q, K W_i^K, V W_i^V) head i = Attention ( Q W i Q , K W i K , V W i V )
Input dimension: d model d_{\text{model}} d model (e.g., 512)
Each head operates in dimension d k = d v = d model / h d_k = d_v = d_{\text{model}} / h d k = d v = d model / h (e.g., 64 for 8 heads)
Projection matrices: W i Q , W i K ∈ R d model × d k W_i^Q, W_i^K \in \mathbb{R}^{d_{\text{model}} \times d_k} W i Q , W i K ∈ R d model × d k , W i V ∈ R d model × d v W_i^V \in \mathbb{R}^{d_{\text{model}} \times d_v} W i V ∈ R d model × d v
Output projection: W O ∈ R h d v × d model W^O \in \mathbb{R}^{h d_v \times d_{\text{model}}} W O ∈ R h d v × d model
Goal : Run h h h attention heads in parallel without increasing computational cost.
If we have d model = 512 d_{\text{model}} = 512 d model = 512 and want h = 8 h = 8 h = 8 heads:
Naive approach : Each head uses full512-dim → total cost 8 × cost ( 512 ) 8 \times \text{cost}(512) 8 × cost ( 512 )
Smart approach : Each head uses d k = 512 / 8 = 64 d_k = 512/8 = 64 d k = 512/8 = 64 → total cost ≈ cost ( 512 ) \approx \text{cost}(512) ≈ cost ( 512 )
Why this works : Attention cost is O ( n 2 d ) O(n^2 d) O ( n 2 d ) where n n n = sequence length, d d d = dimension.
8 heads of 64-dim each: 8 × O ( n 2 × 64 ) = O ( n 2 × 512 ) 8 \times O(n^2 \times 64) = O(n^2 \times 512) 8 × O ( n 2 × 64 ) = O ( n 2 × 512 )
Same total FLOPs as one512-dim head, but we get8 different views!
For head i i i , we project the shared Q , K , V Q, K, V Q , K , V (shape [ n , d model ] [n, d_{\text{model}}] [ n , d model ] ) down to [ n , d k ] [n, d_k] [ n , d k ] :
Q i = Q W i Q , K i = K W i K , V i = V W i V Q_i = Q W_i^Q, \quad K_i = K W_i^K, \quad V_i = V W_i^V Q i = Q W i Q , K i = K W i K , V i = V W i V
where W i Q , W i K , W i V W_i^Q, W_i^K, W_i^V W i Q , W i K , W i V are learned , different for each head.
WHY different projections?
If all heads used the same W Q , W K , W V W^Q, W^K, W^V W Q , W K , W V , they'd compute identical attention patterns—no benefit!
Different projections let each head learn to extract different features from the same input.
Intuition : Think of each W i Q W_i^Q W i Q as asking different question:
Head 1 might project to capture "What words are grammatically related?"
Head 2 might project to capture "What words are semantically similar?"
Head 3 might project to capture "What words are nearby in position?"
Each head independently computes:
head i = softmax ( Q i K i T d k ) V i \text{head}_i = \text{softmax}\left(\frac{Q_i K_i^T}{\sqrt{d_k}}\right) V_i head i = softmax ( d k Q i K i T ) V i
This gives an output of shape [ n , d v ] [n, d_v] [ n , d v ] for each head.
WHY the same scaled dot-product formula?
It works! The magic is in the different learned projections W i Q , K , V W_i^{Q,K,V} W i Q , K , V , not in the attention mechanism itself.
Using the same mechanism means each head is interpretable in the same way.
Stack the h h h head outputs side-by-side:
Concat ( head 1 , … , head h ) = [ head 1 ∣ head 2 ∣ ⋯ ∣ head h ] \text{Concat}(\text{head}_1, \ldots, \text{head}_h) = [\text{head}_1 \mid \text{head}_2 \mid \cdots \mid \text{head}_h] Concat ( head 1 , … , head h ) = [ head 1 ∣ head 2 ∣ ⋯ ∣ head h ]
This produces shape [ n , h ⋅ d v ] = [ n , d model ] [n, h \cdot d_v] = [n, d_{\text{model}}] [ n , h ⋅ d v ] = [ n , d model ] (since h ⋅ d v = d model h \cdot d_v = d_{\text{model}} h ⋅ d v = d model ).
WHY concatenate instead of sum/average?
We want to preserve all information from every head.
Suming would merge them, losing the distinct patterns each head learned.
Concatenation keeps them separate, but we need one more step to blend them...
MultiHead ( Q , K , V ) = Concat ( head 1 , … , head h ) W O \text{MultiHead}(Q, K, V) = \text{Concat}(\text{head}_1, \ldots, \text{head}_h) W^O MultiHead ( Q , K , V ) = Concat ( head 1 , … , head h ) W O
where W O ∈ R d model × d model W^O \in \mathbb{R}^{d_{\text{model}} \times d_{\text{model}}} W O ∈ R d model × d model is learned.
WHY this final projection?
The concatenated heads are still in separate "channels"—W O W^O W O lets the model mix information across heads .
It learns how to combine different types of attention (syntactic + semantic + positional).
Returns output to full d model d_{\text{model}} d model dimension, ready for the next layer.
Worked example 8-Head Attention with
d model = 512 d_{\text{model}} = 512 d model = 512
Setup :
Sequence length n = 10 n = 10 n = 10 tokens
Model dimension d model = 512 d_{\text{model}} = 512 d model = 512
Number of heads h = 8 h = 8 h = 8
Per-head dimension d k = d v = 512 / 8 = 64 d_k = d_v = 512/8 = 64 d k = d v = 512/8 = 64
Input : Q , K , V ∈ R 10 × 512 Q, K, V \in \mathbb{R}^{10 \times 512} Q , K , V ∈ R 10 × 512 (from previous layer or embedings)
Step 1 — Project to Heads :
For each head i = 1 , … , 8 i = 1, \ldots, 8 i = 1 , … , 8 :
Q i = Q W i Q Q_i = Q W_i^Q Q i = Q W i Q where W i Q ∈ R 512 × 64 W_i^Q \in \mathbb{R}^{512 \times 64} W i Q ∈ R 512 × 64 → Q i ∈ R 10 × 64 Q_i \in \mathbb{R}^{10 \times 64} Q i ∈ R 10 × 64
K i = K W i K K_i = K W_i^K K i = K W i K → K i ∈ R 10 × 64 K_i \in \mathbb{R}^{10 \times 64} K i ∈ R 10 × 64
V i = V W i V V_i = V W_i^V V i = V W i V → V i ∈ R 10 × 64 V_i \in \mathbb{R}^{10 \times 64} V i ∈ R 10 × 64
Why this step? Each head gets its own "view" of the data in a lower-dimensional subspace.
Step 2 — Attention Per Head :
head i = softmax ( Q i K i T 64 ) V i \text{head}_i = \text{softmax}\left(\frac{Q_i K_i^T}{\sqrt{64}}\right) V_i head i = softmax ( 64 Q i K i T ) V i
Scores matrix: Q i K i T ∈ R 10 × 10 Q_i K_i^T \in \mathbb{R}^{10 \times 10} Q i K i T ∈ R 10 × 10 (how much each token attends to each other token)
Scale by 64 = 8 \sqrt{64} = 8 64 = 8 to stabilize gradients
Softmax over each row (attention weights sum to 1)
Multiply by V i V_i V i → head i ∈ R 10 × 64 \text{head}_i \in \mathbb{R}^{10 \times 64} head i ∈ R 10 × 64
Why this step? Compute context-aware representation for this head's subspace.
Step 3 — Concatenate :
Concat ( head 1 , … , head 8 ) ∈ R 10 × 512 \text{Concat}(\text{head}_1, \ldots, \text{head}_8) \in \mathbb{R}^{10 \times 512} Concat ( head 1 , … , head 8 ) ∈ R 10 × 512
Stack the8 outputs horizontally: [ 64 + 64 + ⋯ + 64 ] = 512 [64+ 64 + \cdots + 64] = 512 [ 64 + 64 + ⋯ + 64 ] = 512 dims.
Why this step? Preserve distinct information from each head.
Step 4 — Output Projection :
Output = Concat ( … ) W O \text{Output} = \text{Concat}(\ldots) W^O Output = Concat ( … ) W O
where W O ∈ R 512 × 512 W^O \in \mathbb{R}^{512 \times 512} W O ∈ R 512 × 512 → Output ∈ R 10 × 512 \text{Output} \in \mathbb{R}^{10 \times 512} Output ∈ R 10 × 512
Why this step? Allow heads to interact and blend their specialized knowledge.
Worked example Concrete Numbers Walkthrough
Let's compute one entry in head1's output for token position 3.
Input : Token 3's query vector q 3 , 1 ∈ R 64 q_{3,1} \in \mathbb{R}^{64} q 3 , 1 ∈ R 64 (after projection by W 1 Q W_1^Q W 1 Q )
Step 1 — Compute Scores :
For each position j = 1 , … , 10 j = 1, \ldots, 10 j = 1 , … , 10 :
score 3 , j = q 3 , 1 T k j , 1 64 = q 3 , 1 T k j , 1 8 \text{score}_{3,j} = \frac{q_{3,1}^T k_{j,1}}{\sqrt{64}} = \frac{q_{3,1}^T k_{j,1}}{8} score 3 , j = 64 q 3 , 1 T k j , 1 = 8 q 3 , 1 T k j , 1
Example values:
score 3 , 1 = 32 / 8 = 4.0 \text{score}_{3,1} = 32/8 = 4.0 score 3 , 1 = 32/8 = 4.0
score 3 , 2 = 16 / 8 = 2.0 \text{score}_{3,2} = 16/8 = 2.0 score 3 , 2 = 16/8 = 2.0
score 3 , 3 = 40 / 8 = 5.0 \text{score}_{3,3} = 40/8 = 5.0 score 3 , 3 = 40/8 = 5.0 (attending to itself)
... (7 more)
Why divide by 8? Keeps dot products from exploding in magnitude.
Step 2 — Softmax :
weight 3 , j = e score 3 , j ∑ j ′ e score 3 , j ′ \text{weight}_{3,j} = \frac{e^{\text{score}_{3,j}}}{\sum_{j'} e^{\text{score}_{3,j'}}} weight 3 , j = ∑ j ′ e score 3 , j ′ e score 3 , j
Example (simplified):
weight 3 , 1 = 0.12 \text{weight}_{3,1} = 0.12 weight 3 , 1 = 0.12
weight 3 , 2 = 0.03 \text{weight}_{3,2} = 0.03 weight 3 , 2 = 0.03
weight 3 , 3 = 0.45 \text{weight}_{3,3} = 0.45 weight 3 , 3 = 0.45 (highest attention to self)
... (sum to 1.0)
Why softmax? Converts scores to probabilities (how much to pay attention to each token).
Step 3 — Weighted Sum of Values :
head 1 [ 3 ] = ∑ j = 1 10 weight 3 , j ⋅ v j , 1 \text{head}_1[3] = \sum_{j=1}^{10} \text{weight}_{3,j} \cdot v_{j,1} head 1 [ 3 ] = ∑ j = 1 10 weight 3 , j ⋅ v j , 1
This produces a 64-dimensional vector that mixes information from all positions, weighted by attention.
Why this step? The output is a context-aware representation: token 3 has "gathered" relevant info from other positions based on what head 1 thinks is important.
Common mistake Mistake 1: "More heads always means better performance"
Why it feels right : More heads = more diversity = richer representations!
Why it's wrong :
Each head gets dimension d k = d model / h d_k = d_{\text{model}} / h d k = d model / h . With too many heads, each head has very few dimensions (d k → d_k \to d k → small) and loses expressiveness.
Empirically, 8-16 heads work well for typical model sizes. Beyond that, returns diminish.
Example: d model = 512 d_{\text{model}} = 512 d model = 512 , h = 64 h = 64 h = 64 → each head only has d k = 8 d_k = 8 d k = 8 dimensions. Too little capacity to learn meaningful patterns.
The fix : Balance head count with per-head dimension. Standard practice: h ∈ { 8 , 12 , 16 } h \in \{8, 12, 16\} h ∈ { 8 , 12 , 16 } for d model ∈ { 512 , 768 , 1024 } d_{\text{model}} \in \{512, 768, 1024\} d model ∈ { 512 , 768 , 1024 } .
Common mistake Mistake 2: "Heads learn predetermined roles (syntax head, semantics head, etc.)"
Why it feels right : Intuitive explanations often say "head 1 learns syntax, head 2 learns semantics."
Why it's wrong :
Heads are not pre-assigned roles . They learn through backpropagation based on task loss.
Interpretability studies show heads learn varied, sometimes overlapping patterns—not clean categories.
Different tasks lead to different specializations (translation vs. summarization).
The fix : Think of heads as learnable feature extractors that discover useful attention patterns during training. Their roles emerge from data, not design.
Common mistake Mistake 3: "Concatenation is just a detail—suming heads would work too"
Why it feels right : Both combine information from multiple heads, so what's the difference?
Why it's wrong :
Suming loses information : If head 1 attends strongly to position 5and head 2 attends strongly to position 8, suming their outputs mixes these two distinct patterns.
Concatenation preserves distinctness : The final W O W^O W O projection can learn to weight/combine heads as needed, but the raw information from each head is still accessible.
Ablation studies show concatenation + W O W^O W O outperforms summing.
The fix : Concatenation + linear projection gives the model flexibility to both preserve and mix head outputs as needed.
Let's count parameters for multi-head attention:
Per head :
W i Q W_i^Q W i Q : d model × d k d_{\text{model}} \times d_k d model × d k
W i K W_i^K W i K : d model × d k d_{\text{model}} \times d_k d model × d k
W i V W_i^V W i V : d model × d v d_{\text{model}} \times d_v d model × d v
Total per head: 3 d model ⋅ d k 3 d_{\text{model}} \cdot d_k 3 d model ⋅ d k
All heads : h ⋅ 3 d model ⋅ d k = 3 d model ⋅ ( h ⋅ d k ) = 3 d model 2 h \cdot 3 d_{\text{model}} \cdot d_k = 3 d_{\text{model}} \cdot (h \cdot d_k) = 3 d_{\text{model}}^2 h ⋅ 3 d model ⋅ d k = 3 d model ⋅ ( h ⋅ d k ) = 3 d model 2 (since h ⋅ d k = d model h \cdot d_k = d_{\text{model}} h ⋅ d k = d model )
Output projection : W O W^O W O has d model × d model = d model 2 d_{\text{model}} \times d_{\text{model}} = d_{\text{model}}^2 d model × d model = d model 2 parameters
Total : 3 d model 2 + d model 2 = = 4 d model 2 = = 3 d_{\text{model}}^2 + d_{\text{model}}^2 ==4 d_{\text{model}}^2== 3 d model 2 + d model 2 == 4 d model 2 == parameters
Per head :
Computing Q i , K i , V i Q_i, K_i, V_i Q i , K i , V i : O ( n ⋅ d model ⋅ d k ) O(n \cdot d_{\text{model}} \cdot d_k) O ( n ⋅ d model ⋅ d k ) each, total 3 n ⋅ d model ⋅ d k 3 n \cdot d_{\text{model}} \cdot d_k 3 n ⋅ d model ⋅ d k
Attention scores Q i K i T Q_i K_i^T Q i K i T : O ( n 2 d k ) O(n^2 d_k) O ( n 2 d k )
Softmax: O ( n 2 ) O(n^2) O ( n 2 )
Weighted sum with V i V_i V i : O ( n 2 d v ) O(n^2 d_v) O ( n 2 d v )
All heads : h h h times the per-head cost
Dominant term : Attention scores O ( h ⋅ n 2 d k ) = O ( n 2 d model ) O(h \cdot n^2 d_k) = O(n^2 d_{\text{model}}) O ( h ⋅ n 2 d k ) = O ( n 2 d model ) (since h ⋅ d k = d model h \cdot d_k = d_{\text{model}} h ⋅ d k = d model )
Output projection : O ( n ⋅ d model 2 ) O(n \cdot d_{\text{model}}^2) O ( n ⋅ d model 2 )
For long sequences where n ≫ d model n \gg d_{\text{model}} n ≫ d model , the O ( n 2 d model ) O(n^2 d_{\text{model}}) O ( n 2 d model ) attention term dominates. This is why Transformers struggle with very long sequences (length 10,000+)—quadratic in sequence length!
Recall Explain to a 12-Year-Old
Imagine you're trying to understand a friend's long story. You can't focus on everything at once, so your brain does something smart: it pays attention to different parts in different ways at the same time.
One part of your brain listens for who did what (like "Alice threw the ball").
Another part listens for emotions (like "she was excited").
Another part tracks when things happened (like "first... then... finally...").
Multi-head attention works the same way! The computer has multiple "attention heads" (like different parts of your brain), and each one focuses on different patterns in the text:
Head 1 might connect words that go together grammatically ("the cat" → "sat").
Head 2 might connect words that mean similar things ("happy" ← "joyful").
Head 3 might connect words that are nearby.
Each head is like a friend with a special talent—one is great at grammar, one is great at understanding feelings, one is great at remembering order. By having all these friends work together (that's the "multi" part), the computer understands the story much better than if it only had one friend helping!
The "concatenate and project" step at the end is like having all your friends sit down and share what they noticed, so you get the full picture.
Mnemonic Remember MHA Structure
"Quite Kindly, Volunteers Carry Heavy Objects"
Q uite → Q ueries
K indly → K eys
V olunters → V alues
C ary → C oncatenate heads
H eavy → H eads (multiple)
O bjects → O utput projection (W O W^O W O )
Flow: Project Q , K , V Q, K, V Q , K , V → Compute attention in each H ead → C oncatenate → O utput transform
Self-Attention : MHA is typically used in self-attention (Q, K, V from same source)
Cross-Attention : MHA can also be used for encoder-decoder attention (Q from decoder, K,V from encoder)
Transformer Encoder Layer : Uses MHA followed by feed-forward network
Positional Encoding : Provides position information that attention heads can leverage
Attention Visualization : Techniques to interpret what each head has learned
Query-Key-Value Intuition : Foundation for understanding the projections
Linear Projections in Neural Networks : What the W Q , W K , W V W O W^Q, W^K, W^V W^O W Q , W K , W V W O matrices do
#flashcards/ai-ml
What is the primary advantage of multi-head attention over single-head attention? :: Multi-head attention learns multiple representation subspaces in parallel, allowing the model to capture different types of relationships (syntactic, semantic, positional) simultaneously. A single head has limited representational capacity.
In multi-head attention with h h h heads and model dimension d model d_{\text{model}} d model , what is the dimension of each head? Each head operates in dimension
d k = d v = d model / h d_k = d_v = d_{\text{model}} / h d k = d v = d model / h . This keeps total computation constant while enabling parallel specialization.
Why do we use different projection matrices W i Q , W i K , W i V W_i^Q, W_i^K, W_i^V W i Q , W i K , W i V for each head? Different projections allow each head to learn different features from the same input. If all heads used identical projections, they would compute identical attention patterns, providing no benefit.
What is the formula for multi-head attention? MultiHead ( Q , K , V ) = Concat ( head 1 , … , head h ) W O \text{MultiHead}(Q, K, V) = \text{Concat}(\text{head}_1, \ldots, \text{head}_h) W^O MultiHead ( Q , K , V ) = Concat ( head 1 , … , head h ) W O where
head i = Attention ( Q W i Q , K W i K , V W i V ) \text{head}_i = \text{Attention}(Q W_i^Q, K W_i^K, V W_i^V) head i = Attention ( Q W i Q , K W i K , V W i V )
Why do we concatenate head outputs instead of suming them? Concatenation preserves distinct information from each head. Summing would mix the patterns, losing the specialized information each head learned. The final
W O W^O W O projection can then learn how to optimally combine the heads.
What is the total parameter count for multi-head attention with model dimension d model d_{\text{model}} d model ? 4 d model 2 4 d_{\text{model}}^2 4 d model 2 parameters:
3 d model 2 3 d_{\text{model}}^2 3 d model 2 for all the
W Q , W K , W V W^Q, W^K, W^V W Q , W K , W V projections across heads, plus
d model 2 d_{\text{model}}^2 d model 2 for the output projection
W O W^O W O .
What is the computational complexity of multi-head attention? The dominant term is
O ( n 2 d model ) O(n^2 d_{\text{model}}) O ( n 2 d model ) where
n n n is sequence length, coming from computing attention scores across all positions. This is quadratic in sequence length.
Why does the final W O W^O W O projection exist in multi-head attention? W O W^O W O allows the model to mix information across heads. After concatenation, the heads are still in separate "channels"—
W O W^O W O learns how to combine different types of attention (syntactic + semantic + positional) optimally for the task.
One relationship type only
Own representation subspace
Syntax semantics position coreference
d_k equals d_model over h
Richer output representation
Intuition Hinglish mein samjho
Multi-head attention ek bohot hi powerful idea hai jisse Transformer models itne effective bante hain. Socho tumhe ek paragraph padhna hai aur uska meaning samajhna hai. Tumara dimag sirf ek tareke se nahi soch