Multi-Head Attention

Comic titled 'Same Sentence, Different Eyes' showing four robots with different goggles (Grammar, Meaning, Position, Description) each seeing different connection lines in the sentence 'The tired cat sat on the warm mat.' Their notes feed into a synthesis funnel producing a 'Combined Attention Report'
What You’ll Learn

You’ll learn why one attention head can capture only one relationship pattern at a time, and how multiple heads give the model several perspectives in parallel. You’ll also see how the model dimension is split across heads, what trained heads often specialize in, and how the output projection mixes their views back together.

The Problem: One Perspective Isn’t Enough

Self-attention lets every token look at every other token. But a single attention head computes one set of attention weights, a single perspective on the relationships.

Language is multi-faceted. Consider “The tired cat sat on the warm mat.” A reader simultaneously tracks grammar (“cat” is the subject of “sat”), meaning (“tired” describes “cat”), and local structure (“on” connects “sat” to “mat”). Can one attention head capture all of these at once?

Clear the 3 levels below to see why one head has to compromise:

Level 1 · Track the Subject & Verb In Grammar mode, look along the “cat” row and click the cell where “cat” pays 85% of its attention.
Key (attends to) →
← Query
The
tired
cat
sat
on
the
warm
mat
The
tired
cat
sat
on
the
warm
mat

Think of it like a team of analysts reading the same document. One analyst tracks grammar, another tracks meaning, a third tracks nearby words. Each writes a summary, and a manager combines all summaries into a final report. Where this analogy breaks: unlike real analysts, all heads operate on the same token representations, and their outputs are mathematically concatenated and mixed, not summarized by a human.

Fill in the Blanks
A single attention head can only capture type(s) of relationship pattern at a time.
Fill in every blank, then check your answer.

See Multiple Heads Working Together

What if we ran several attention heads in parallel, each free to specialize? Click on individual heads to see what each one captures:

Multi-Head Attention Visualizer

4 heads
All Heads (showing patterns)
H1
H2
H3
H4
Each head produces its own output vectors. These are concatenated and multiplied by an output projection matrix, producing one context-aware vector per token. That vector then feeds into a feed-forward network, and the whole block repeats. Click a head above to see its attention pattern.

What to look for: click each head and observe its attention matrix. Do you notice different patterns? One head might focus on nearby tokens, another on specific word relationships, another on broad context. That’s the whole point: each head independently discovers a different pattern.


How It Works: The Dimension Split

We don’t create separate attention mechanisms for each head. Instead, we split the model dimension across heads. If your model dimension is 512 and you have 8 heads, each head works in a 64-dimensional space (512 / 8 = 64). Step through the animation to see the full pipeline:

Step 0/4: Embedding
 0.2 d1
-0.5 d2
 0.8 d3
 0.1 d4
-0.3 d5
 0.7 d6
-0.1 d7
 0.4 d8
d_model = 8
One token's 8-dimensional embedding (real models use 512 or more).

What to look for: watch how the full embedding vector gets sliced into smaller chunks, one per head. Each head runs standard attention on its own slice, then the results are concatenated back to the original dimension. The total amount of data never changes, it’s just reorganized.

Why not just average the heads? Because the output projection WOW^O learns how to blend perspectives. Maybe head 3’s syntactic information should interact with head 7’s semantic information in a specific way. Averaging would lose those interactions.

ConfigModel dimHeadsPer-head dimTotal params
Original Transformer512864~1M
GPT-2 Small7681264~2.4M
GPT-31228896128~600M

The key insight: more heads doesn’t mean more parameters. It means the same capacity is divided into more specialized perspectives.

Fill in the Blanks
With d_model = 512 and 8 attention heads, each head has d_k = dimensions.
Fill in every blank, then check your answer.

Parameter Count

Multi-head attention has four weight matrices (Q, K, V projections + output projection), each of shape (d_model × d_model). For d_model = 512, that’s 4 × 512² ≈ 1 million parameters.

The number of heads doesn’t change the total parameters. It only changes how they’re organized internally.

Quick Check
If we increase the number of heads from 8 to 16 (keeping d_model = 512), what happens to the total parameter count?
Your turn
Choose an answer to check your understanding.

What Do Different Heads Learn?

Research on real transformer models reveals remarkable specialization:

Head PatternWhat It LearnsExample
PositionalAttend to adjacent/fixed-offset positionsToken at pos i always attends to pos i-1
SyntacticSubject-verb agreement”The dogs … are barking”
SemanticWord meaning relationships”bank” attends to “river” or “money”
DelimiterAttend to punctuation/special tokensFocus on periods, commas, [SEP]
Rare tokenAttend to infrequent wordsProper nouns get extra attention

Some heads can even be pruned (removed) without hurting performance because they learn redundant or less useful patterns.



Quick Summary

ConceptSingle HeadMulti-Head
Perspectives1h (typically 8-96)
Per-head dimd_modeld_model / h
Total params4 × d_model²4 × d_model² (same!)
What it capturesOne relationship typeMany relationship types
Learning Checklist
  • I can explain why a single attention head is insufficient for capturing multiple relationship types simultaneously
  • I understand how the model dimension is split across heads without increasing total parameters
  • I can describe what the output projection WOW^O does and why it’s needed after concatenation
  • I can give at least three examples of patterns that different trained heads learn (positional, syntactic, semantic, etc.)

Next: Positional Encoding

Self-attention treats all positions equally. It’s permutation-invariant: shuffle the tokens and the attention scores don’t change. We need to add position information. That’s Positional Encoding →