Multi-Head Attention › Worked Example: Tracing Through Two Heads

Michael BrenndoerferUpdated May 30, 20253 min read
Language AI HandbookAll chapters
Part 13 of 67Self-Attention
Chapter 94Multi-Head Attention › Worked Example: Tracing Through Two Heads
Reading section 5 of 14 · Worked Example: Tracing Through Two Heads · 3 minSection 5 of 14 · 3 min

Book contents

Section 5 of 14 · 3 min read

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 h=2h = 2 heads, giving each head dk=2d_k = 2 dimensions.

Our three token embeddings (rows of X\mathbf{X}) are:

X=[101001011100]\mathbf{X} = \begin{bmatrix} 1 & 0 & 1 & 0 \\ 0 & 1 & 0 & 1 \\ 1 & 1 & 0 & 0 \end{bmatrix}

where:

  • Row 1 ([1,0,1,0][1, 0, 1, 0]): the embedding for "cat"
  • Row 2 ([0,1,0,1][0, 1, 0, 1]): the embedding for "chases"
  • Row 3 ([1,1,0,0][1, 1, 0, 0]): the embedding for "mouse"

Head 1: Projecting Down

For head 1, suppose the learned projection matrices are:

W1Q=[10010000],W1K=[10010000],W1V=[00001001]\mathbf{W}^Q_1 = \begin{bmatrix} 1 & 0 \\ 0 & 1 \\ 0 & 0 \\ 0 & 0 \end{bmatrix}, \quad \mathbf{W}^K_1 = \begin{bmatrix} 1 & 0 \\ 0 & 1 \\ 0 & 0 \\ 0 & 0 \end{bmatrix}, \quad \mathbf{W}^V_1 = \begin{bmatrix} 0 & 0 \\ 0 & 0 \\ 1 & 0 \\ 0 & 1 \end{bmatrix}

These projections keep the first two embedding dimensions for queries and keys, and the last two dimensions for values. Computing Q1=XW1Q\mathbf{Q}_1 = \mathbf{X} \mathbf{W}^Q_1:

Q1=[100111],K1=[100111],V1=[100100]\mathbf{Q}_1 = \begin{bmatrix} 1 & 0 \\ 0 & 1 \\ 1 & 1 \end{bmatrix}, \quad \mathbf{K}_1 = \begin{bmatrix} 1 & 0 \\ 0 & 1 \\ 1 & 1 \end{bmatrix}, \quad \mathbf{V}_1 = \begin{bmatrix} 1 & 0 \\ 0 & 1 \\ 0 & 0 \end{bmatrix}

Computing Attention Scores

The raw attention scores for head 1 are Q1K1T/dk\mathbf{Q}_1 \mathbf{K}_1^T / \sqrt{d_k} with dk=2d_k = 2:

Q1K1T=[1⋅1+0⋅01⋅0+0⋅11⋅1+0⋅10⋅1+1⋅00⋅0+1⋅10⋅1+1⋅11⋅1+1⋅01⋅0+1⋅11⋅1+1⋅1]=[101011112]\mathbf{Q}_1 \mathbf{K}_1^T = \begin{bmatrix} 1\cdot1+0\cdot0 & 1\cdot0+0\cdot1 & 1\cdot1+0\cdot1 \\ 0\cdot1+1\cdot0 & 0\cdot0+1\cdot1 & 0\cdot1+1\cdot1 \\ 1\cdot1+1\cdot0 & 1\cdot0+1\cdot1 & 1\cdot1+1\cdot1 \end{bmatrix} = \begin{bmatrix} 1 & 0 & 1 \\ 0 & 1 & 1 \\ 1 & 1 & 2 \end{bmatrix}

Scaling by 2≈1.414\sqrt{2} \approx 1.414 gives:

Scores1=12[101011112]≈[0.7100.7100.710.710.710.711.41]\text{Scores}_1 = \frac{1}{\sqrt{2}} \begin{bmatrix} 1 & 0 & 1 \\ 0 & 1 & 1 \\ 1 & 1 & 2 \end{bmatrix} \approx \begin{bmatrix} 0.71 & 0 & 0.71 \\ 0 & 0.71 & 0.71 \\ 0.71 & 0.71 & 1.41 \end{bmatrix}

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 X\mathbf{X}. Even if head 2's projections emphasize different embedding dimensions, it still produces an n×dvn \times d_v output that captures a distinct relationship pattern. When you concatenate both head outputs and apply WO\mathbf{W}^O, 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.