Multi-Head Attention › Worked Example: Tracing Through Two Heads
Language AI HandbookAll chapters Multi-Head Attention › Worked Example: Tracing Through Two HeadsOne section · Sections in this chapter5 / 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
Before turning to code, let's trace through a minimal example by hand to make the mechanics concrete. Suppose we have a sequence of three tokens: "cat chases mouse." We represent each token with a 4-dimensional embedding (a tiny model dimension chosen so the numbers stay readable), and we use heads, giving each head dimensions.
Our three token embeddings (rows of ) are:
where:
- Row 1 (): the embedding for "cat"
- Row 2 (): the embedding for "chases"
- Row 3 (): the embedding for "mouse"
Head 1: Projecting Down
For head 1, suppose the learned projection matrices are:
These projections keep the first two embedding dimensions for queries and keys, and the last two dimensions for values. Computing :
Computing Attention Scores
The raw attention scores for head 1 are with :
Scaling by gives:
After softmax (row-wise), row 3 ("mouse") ends up attending most strongly to itself because "mouse" has the largest self-similarity score. Row 1 ("cat") splits attention roughly equally between itself and "mouse." Row 2 ("chases") splits between "chases" and "mouse."
The Key Insight
Notice that head 2, which uses different projection matrices, will compute a completely different set of scores from the same input . Even if head 2's projections emphasize different embedding dimensions, it still produces an output that captures a distinct relationship pattern. When you concatenate both head outputs and apply , the final representation for each token can draw on both perspectives simultaneously.
In practice, trained models rarely use identity-like projection matrices. The projections are dense, full-rank matrices learned to maximize task performance. But the worked example illustrates the core mechanism: the same token sequence can be "seen" through multiple lenses, and those lenses are entirely determined by what the model learns to be useful.