Log inStart free
LESSON 9.C1 · 6 STAGES · 3 CONTROLS · 3 CHECKSPRO · PREVIEW

Scaled dot-product attention end to end

Follow one token through scores, scale, mask, softmax and heads in one layer.


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

Go Pro · $8.25/mo
EXPLAINER · 9.C1 · ATTENTION BAYX 6 × 4 · 48 WEIGHTS MAKE Q, K, V
01
Stage 1 of 6 · Three projections
Three weight matrices turn each token into a query, a key and a value. The 4 columns split into 2 heads of 2.
Explain it simply

Each row on the left is one word, stored as 4 numbers. Three grids of weights turn each row into a query, a key and a value, as in 9.01. Each grey band is one head with its own share of the columns, as in 9.05.

X has 6 rows of 4 numbers. W Q, W K and W V turn it into Q, K and V. They split into 2 heads of 2 columns. The query of "sat" is 1.92, 0.39, −1.37, −0.83.

Weights are random, not trained.
x, Q, K, Vweight matrices, max mscores, weights, outputnegative scoresmaskedhead
head 1head 2xQKVWQWKWVthecatsatonamatThree projections9.01 scores9.03 mask9.02 softmax9.05 headsq("sat") = x("sat") · W_Q = [1.92, 0.39, −1.37, −0.83]head 1head 2xQKVWQWKWVthecatsatonamatThree projections9.01 scores9.03 mask9.02 softmax9.05 headsq("sat") = x("sat") · W_Q = [1.92, 0.39, −1.37, −0.83]
Toy: 9.05's 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=XWQ,K=XWK,V=XWV\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}
qsat=[1.92, 0.39, −1.37, −0.83]q_{\text{sat}} = [1.92,\ 0.39,\ -1.37,\ -0.83]

Each token vector times three weight matrices gives its query, key and value.

Common mix-ups

MYTHMore heads let each token read more tokens.

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

Predict, then reveal
3 CHECKS · PRO
Q01Does the causal mask change the output row of "mat", the last token?

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 3 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.