Part XI — Sequence Models: RNNs, LSTMs, Attention & The Transformer Block · Chapter 9

Self-attention & multi-head attention

Hook

Chapter 10 had one query looking at a fixed set of tokens from the outside. What if every token in a sequence looked at every other token — including itself — all at the same time?

Intuition
0.80.00.20.00.80.20.30.30.3
self-attention (rows = query, cols = key: the, cat, sat)

Row "sat" is exactly uniform — it sits equally close to both "the" and "cat", so it has no reason to favor either.

This is self-attention: the query, the keys, and the values are all the same sequence. Every row is one token acting as a query; every column is a token it can attend to, itself included. "The" mostly attends to itself; "sat," sitting exactly between "the" and "cat," has no reason to prefer either.

Formalize

A single attention computation is one view of the sequence. Multi-head attention runs several of these views in parallel, each looking at the tokens through a different projection, then combines what they found:

headi=Attention(QWiQ,KWiK,VWiV),output=concat(head1,,headh)\text{head}_i = \text{Attention}(Q W_i^Q,\, K W_i^K,\, V W_i^V), \qquad \text{output} = \text{concat}(\text{head}_1, \dots, \text{head}_h)
  • Q,K,VQ, K, V — the query, key, and value matrices, here all built from the same input sequence since this is self-attention.
  • WiQ,WiK,WiVW_i^Q, W_i^K, W_i^V — head ii's own learned projection matrices, giving that head its own view of the sequence.
  • headi\text{head}_i — the output of attention computed inside head ii, using its own projections.
  • hh — the number of attention heads running in parallel.
  • output — the combined result of all heads, concatenated together.
  1. Each head learns its own view

    Each head's WQ,WK,WVW^Q, W^K, W^V are learned separately, so each head is free to pick up on a completely different signal in the same tokens — one might track syntax, another meaning, another position.

  2. No single head does all the work

    Because the heads specialize independently, the model isn't forced to squeeze every kind of relationship through one shared projection.

Play
0.90.00.10.30.30.30.70.10.2
Head A (x-axis)
0.30.30.30.00.90.10.10.70.2
Head B (y-axis)

query = "sat" — Head A row: [0.67, 0.09, 0.24], Head B row: [0.09, 0.67, 0.24] — disagreement = 0.813

Two toy heads, one projecting each token onto just its horizontal axis, the other onto just its vertical axis. Click through the query tokens. For "the" and "cat," one head goes flat (uniform) while the other has a clear opinion — that head simply has nothing to say, not a real disagreement. Watch "sat," though: both heads have a strong, opposite opinion about where to look.

Worked example
  1. Project the query

    As a query, "sat" =(1,1)=(1,1) projects to 11 on both axes.

  2. Head A (horizontal)

    Multiplying the query's projection (11) by each token's own horizontal projection:

    • "the" =2=2: score 1×2=21\times2=2
    • "cat" =0=0: score 1×0=01\times0=0
    • "sat" =1=1: score 1×1=11\times1=1

    Softmax of (2,0,1)(2,0,1) gives roughly (0.665,0.090,0.245)(0.665, 0.090, 0.245) — favoring "the."

  3. Head B (vertical)

    The exact same query projection (11), now against each token's vertical projection:

    • "the" =0=0: score 1×0=01\times0=0
    • "cat" =2=2: score 1×2=21\times2=2
    • "sat" =1=1: score 1×1=11\times1=1

    Softmax of (0,2,1)(0,2,1) gives roughly (0.090,0.665,0.245)(0.090, 0.665, 0.245) — favoring "cat," the mirror image.

  4. Concatenate

    Combining both heads' outputs keeps both signals side by side, instead of forcing one projection to average them away.

Checkpoint

Click the token where Head A and Head B disagree the most about where to look.

0.90.00.10.30.30.30.70.10.2
Head A (x-axis)
0.30.30.30.00.90.10.10.70.2
Head B (y-axis)
Click a token to try it
Summary
output=concat(head1,,headh)\text{output} = \text{concat}(\text{head}_1, \dots, \text{head}_h)

Self-attention lets every position see every other position in one step, with no recurrence and no fixed distance penalty. Multi-head attention lets that happen from several angles at once, so a token whose relationships are genuinely multi-faceted — like "sat," pulled toward both its subject and its location — doesn't have to pick just one. The next chapter assembles this, a feedforward layer, and a residual connection into the actual repeating unit a Transformer stacks: the Transformer block.