Log inStart free
LESSON 9.05 · 6 STAGES · 2 CONTROLS · 3 CHECKSPRO · PREVIEW

Multi-head attention

Watch one attention layer split into heads that each look at different tokens.


Pro lesson. Stage 1 is free to watch. Pro opens all 6 stages, 2 controls and 3 checks, in this lesson and every other.

Go Pro · $8.25/mo
EXPLAINER · 9.05 · ATTENTION BAYX 6 × 4 · 48 WEIGHTS MAKE Q, K, V
01
Stage 1 of 6 · Three projections
Each token vector x holds 4 numbers. Three weight matrices turn it into a query, a key and a value.
Explain it simply

Each row on the left is one word, stored as 4 numbers. Three small grids of weights turn each row into three new rows. The query says what a word looks for. The key says what a word offers, and the value is what it passes on.

Six token rows of 4 numbers. Three 4 by 4 weight matrices make the Q, K and V blocks of 6 rows by 4 columns. The query token is "a".

x, Q, K, Vweight matricesattention, outputhead
xQKVWQWKWVthecatsatonamatThree projectionsq("a") = x("a") · W_Q = [0.88, 1.84, −0.57, −1.75]xQKVWQWKWVthecatsatonamatThree projectionsq("a") = x("a") · W_Q = [0.88, 1.84, −0.57, −1.75]
Toy: 6 tokens · width 4 · 1, 2 or 4 heads · random weights, not trained
STAGE 1 / 6
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
Q=X WQ,K=X WK,V=X WV\sy{q}{data}{Q} = \sy{x}{data}{X}\,\sy{wq}{param}{W_Q},\quad \sy{k}{data}{K} = \sy{x}{data}{X}\,\sy{wk}{param}{W_K},\quad \sy{v}{data}{V} = \sy{x}{data}{X}\,\sy{wv}{param}{W_V}
qa=[0.88, 1.84, −0.57, −1.75]q_{\text{a}} = [0.88,\ 1.84,\ -0.57,\ -1.75]

Each weight matrix maps a token vector to 4 new numbers.

Common mix-ups

MYTHMore heads add more weights and more work.

The fix, and 1 more mix-up with the diagram to show them, come with Pro.

Predict, then reveal
3 CHECKS · PRO
Q01With 4 heads, how many numbers does each head's query hold?

Make your guess, then test it in the diagram. All 3 checks come with Pro.

PRO LESSON · YOU WATCHED STAGE 1 OF 6
Unlock the full explainer
  • 5 more stages and 2 live controls
  • 2 mix-up cards and 3 predict-then-reveal checks
  • Watch it as a narrated video, in three voices
  • Every Pro lesson and capstone · 656 in total

$99 a year, or $15 a month. Cancel any time.