Chapter 3 of 5

Attention

Every token gets to ask the others a question, and the answers it gets back become its new vector. That is attention. The formula for it fits on one line and looks like nothing at all, so this chapter takes it apart into the four small steps it actually is, and lets you move the numbers.

About 12 minutesNotebook Part 3

The idea in plain words

Think of a soft lookup. Each token produces a query, saying what it is looking for. Each token also produces a key, saying what it offers, and a value, which is what it actually hands over. Compare a query against every key and you get a score per token. Turn those scores into proportions. Then mix the values in those proportions. That mixture is the token's new vector, and it now carries information from the tokens it found relevant.

The one line version is softmax of Q times K transposed over the square root of d_k, times V. Four steps: compare, scale, normalise, mix. The first explorer lets you do them one at a time on real matrices, and change any number you like.

Two things in that line are easy to skip past and worth stopping on. The square root of d_k is not decoration: without it the scores spread out as the vectors get longer, and softmax stops spreading attention at all. And nothing in the line mentions position, which turns out to matter a great deal.

Attention step by step

Do the formula one step at a time, on matrices you can edit.

Run this yourself: notebook Part 3

Loading the notebook's matrices.

What to notice

  • Change one entry of Q and watch which weights move. A query only affects its own row, because softmax runs along the row.
  • Step 2 divides by the square root of d_k. Compare the spread of the numbers before and after, then look at how even the weights in step 3 are. That relationship is the whole reason the scaling is in the formula.
  • Switch on the mask. The first row can only see itself, so its weight is exactly 1, and every row keeps a budget of 1 however much is hidden.
  • The line under the steps says whether this browser's arithmetic still agrees with the notebook's. It should, to five decimal places, until you edit something.
Go deeper: the equations, the shapes and the code

Shapes, for anyone keeping track. With a sequence of n tokens and a head width of d_k, Q and K are both n by d_k, so Q times K transposed is n by n: one score for every ordered pair of tokens. V is n by d_v, and the output is n by d_v. In the notebook's case n is 5, d_k is 8 and d_v is 8, so the weight matrix is 5 by 5 and the output is 5 by 8.

The masked version sets the hidden scores to minus infinity before the softmax rather than zeroing the weights afterwards, which matters: a softmax of minus infinity is exactly 0, and the remaining weights still sum to 1. Zeroing afterwards would leave the row summing to less than 1. The notebook uses minus 1e9 instead of minus infinity, which gives the same answer to far more places than anything here displays.

One honest limit of the numbers on this page: the data is sent rounded to six decimal places, so recomputing from it agrees with the notebook to about five, not to machine precision. The worst difference measured across every value in this case is 1.1e-5 on a score of about 70.

Source: numpy. Generated 2026-12-05 by pipeline/export_fixtures.py.8.4 KB, checksum a79394296341.

Scaling and softmax

Find out what the square root of d_k is doing by taking it away.

Run this yourself: notebook Part 3

64 , which is what BERT base uses

Scaling

500 draws of 6 keys

Largest weight, on average
0.424

An even spread over 6 keys would be 0.167. Still close to an even spread, which is what the scaling is for.

Spread of the scores
1.03

Standard deviation of the numbers going into the softmax. Dividing by 8.00 holds this near 1 whatever d_k is, which is the whole trick.

One trial, as softmax sees it

  • key 1 1.88 0.726
  • key 2 -0.76 0.052
  • key 3 -0.44 0.071
  • key 4 -0.21 0.090
  • key 5 -1.71 0.020
  • key 6 -0.98 0.042

Scores on the left, the weight each one gets on the right. Switch the scaling off and watch one bar swallow the rest.

Provenance unavailable: manifest.json did not load. The live figures are simulated in your browser, seeded so a reload repeats them, on a different random stream from the notebook's.

What to notice

  • Leave the scaling on and drag d_k from 4 to 2048. The largest weight barely moves. Switch the scaling off and drag it again: by d_k 512 one key is taking almost everything.
  • Watch the spread of the scores, not just the weights. Scaled, it stays near 1 whatever d_k is. Unscaled, it grows like the square root of d_k, and that is the entire cause.
  • A saturated row is a row that has stopped choosing. Every other key gets a weight near zero, so no information from them reaches the output, and the gradient through them is near zero as well.
  • The notebook's measured table is beside the live figures. They come from different random draws, so they land close rather than identical, and the panel says how close.
Go deeper: the equations, the shapes and the code

Why the square root, rather than d_k itself. Take a query and a key whose entries are independent with mean 0 and variance 1. Their dot product is a sum of d_k such products, so it has mean 0 and variance d_k, and therefore a standard deviation of the square root of d_k. Dividing by that square root returns the scores to a standard deviation of 1, whatever the width. Dividing by d_k instead would shrink them towards 0 as the model got wider, and softmax would flatten to an even spread that carries no information either.

This is the argument the 2017 paper makes in a footnote, and it is worth knowing that it assumes independence and unit variance. Real queries and keys are the output of learned projections and are neither, so the square root of d_k is the right order of magnitude rather than an exact correction. It works well enough that nothing has replaced it.

The simulation here uses its own generator, because reproducing NumPy's bit stream in a browser would mean shipping an implementation of it. Two independent samples of 500 trials estimate the same quantity, so the live figure and the notebook's agree to about 0.02. The unit tests assert that at every d_k the notebook measured.

Source: numpy. Generated 2026-12-05 by pipeline/export_fixtures.py.8.4 KB, checksum a79394296341.

Go deeper

Attention arrived as a fix for a bottleneck. An encoder decoder model compressed a whole input sentence into one vector and then generated from it, which worked until sentences got long. Bahdanau, Cho and Bengio in 2014 let the decoder look back at every input position instead, weighting them by relevance at each step. Vaswani and colleagues in 2017 then threw away the recurrence and kept only the attention, which is the architecture everything here is built on.

The word attention invites a story about the model understanding what it is looking at, and the stories are often wrong. A head that puts a high weight from one token onto another has told you where information flowed, not why, and not what the model concluded. Keep that in mind on the next chapter, where the heads are real.

Three questions before you move on

Nothing is scored or saved. Pick an answer and the reason appears either way.

  1. Why divide the scores by the square root of d_k before the softmax?

  2. A row of the attention matrix sums to 1. What does a row mean?

  3. Attention with no positional information is given a shuffled sentence. What comes out?