Visual walkthrough — Reading and reproducing ML papers
We build every idea from scratch. If you have never seen a dot product, a variance, or a softmax — good. You will meet each one at the exact moment we need it, and never before.
Step 1 — What is a "query" and a "key"? Two arrows.
WHAT. In the Transformer, every word is turned into a list of numbers — an arrow (a vector) in space. When we ask "how much should word A pay attention to word B?" we compare two arrows:
- (the query) = "what am I looking for?"
- (the key) = "what do I offer?"
Each arrow has numbers in it. That letter just means "how many numbers are in each arrow" — its dimension. If , an arrow is a pair like and we can literally draw it.
WHY these two, not one? Attention is a matching operation: a question meeting an answer. You cannot match one thing against nothing — you need two arrows to compare.
PICTURE. Two arrows from the same origin. The only thing that matters for matching is: do they point the same way?
Step 2 — The dot product: turning two arrows into one number
WHAT. To measure "do these arrows agree?" we multiply them component by component and add up the results. This single number is the dot product, written (the little just means we line them up to multiply — ignore it as machinery for now):
Reading the equation left to right: take the first number of times the first number of , then the second times the second, and so on for all pairs, then add every product into one total. The big symbol is just shorthand for "add all of these up, with counting from to ."
WHY the dot product and not, say, the distance between arrows? Because the dot product is large and positive when arrows point the same way, near zero when they are perpendicular, and negative when they point apart. That is exactly the "how much do they agree?" score attention wants. Distance would only tell us how far apart the tips are, which mixes up length with direction.
PICTURE. Same two arrows, now shown as they swing from aligned → perpendicular → opposed, with the resulting number dropping from big-positive to zero to negative.
Recall
Why does a dot product answer "do these agree?" and not "how far apart are they?" ::: Because it grows with alignment (direction), peaking when arrows point the same way and going negative when opposed — length matters less than direction here.
Step 3 — The score is a sum of random pieces
WHAT. When the network starts training, the numbers inside and are basically random. A standard way to model "random with typical size 1, centred on 0" is to say each entry is drawn from — read as "a bell-shaped spread of values, centred at , whose typical wobble is ." So each product is a small random number, and our score is the sum of of them:
WHY care that it is a sum? Here is the crux of the whole page: when you add up many independent random pieces, the total does not stay small — it spreads out more and more as you add more pieces. More pieces wilder total. And (the number of pieces) is often or more. So a big makes the score swing to huge magnitudes for no meaningful reason — purely from stacking randomness.
PICTURE. Three histograms of the score for , , : same shape, but the bell gets visibly wider as grows.
Step 4 — Measuring the swing: variance, one tool for one job
WHAT. We need a number for "how wide is the spread of the score?" That number is the variance — the average of the squared distance from the centre. We write . Two facts about variance are all we need, and both are pictures:
-
Independent things add their variances. If you stack independent random pieces, the total variance is the sum of their variances. Each term on the right is the spread of one product; adding them gives the spread of the whole sum.
-
One product has variance . Since and are each independent (spread , centre ), their product has variance .
Put them together: Reading it: each of the products contributes a spread of ; add ones; you get .
WHY variance and not just "the biggest value"? Because variance adds cleanly over independent pieces (fact 1). The maximum, the range, the average magnitude — none of them add so simply. Variance is the one measure of spread that turns "sum of pieces" into "sum of numbers", which is exactly the algebra we need.
PICTURE. A bar chart: little bars each of height (one per product), stacking into a total tower of height — the variance of the score literally is the count of pieces.
Recall
Why is the variance of the score exactly ? ::: It is a sum of independent products, each of variance ; independent variances add, so .
Step 5 — What softmax does, and why big scores break it
WHAT. The scores feed into softmax, the function that turns a list of raw scores into a list of probabilities (positive numbers that sum to ). For scores : The (exponential) blows small differences up: because grows so fast, a score even a little bigger than the rest ends up with almost all the probability.
WHY does big variance hurt? If the scores swing to huge (Step 4), then one dwarfs every other, so softmax outputs something like — a spike. A spike means the model has committed 100% to one word before it has learned anything. Worse, at a spike the softmax is flat (its slope ), so the gradient — the signal that tells the network how to improve — vanishes. Learning stalls. This is the "saturation" the parent note warned about.
PICTURE. Two softmax outputs on the same raw pattern of scores: gentle scores → soft, spread-out weights (healthy); scores multiplied up by → a single spike (dead gradient).
Step 6 — The fix falls out: divide by
WHAT. We know the score has variance (Step 4). We want variance back to (a gentle, un-saturated spread, Step 5). There is a rule for scaling: dividing a random quantity by a number divides its variance by . Set this equal to and solve for :
That is the whole reason the paper writes : it is the unique number that pulls the score's spread back down to , whatever is.
WHY and not ? A common wrong guess. Dividing by would give variance — now the scores are too flat, softmax gives everything equal weight, and attention learns nothing. We want variance exactly , and only achieves that.
PICTURE. The wide histogram from Step 3, and next to it the same scores after dividing by — snapped back to the tidy unit-width bell.
Recall
Why rather than ? ::: Dividing by divides variance by ; to turn into we need , i.e. . Dividing by over-shrinks to .
Step 7 — The degenerate cases (never leave a gap)
Good reproduction (per the parent's checklist) means checking the corners. Each is a quick sanity picture:
- . One number in each arrow, score variance already, and : the scaling divides by and does nothing. Correct — there is no stacked randomness to tame.
- Scores all equal (perfectly tied words). Softmax gives a flat : attention spreads evenly. The never changes this — scaling a constant leaves it constant. No blow-up possible.
- One score . Softmax a one-hot spike; gradient . This is the failure mode; scaling reduces how often we land here but cannot rescue an already-saturated score. That is why the parent's mask uses deliberately — a chosen extreme, not an accidental one.
- constant across words. Output equals that constant no matter the weights: attention can only mix the values it is given.
The one-picture summary
Everything on this page in a single diagram: two random arrows → dot product → wide score → divide by → tame score → soft, learnable attention.
Recall Feynman retelling — say it to a friend
Every word becomes an arrow. To ask "how much should this word care about that one," we slide the two arrows together and add up their matching parts — that is the dot product, one number that is big when they point the same way. But each arrow holds numbers, and adding random little products makes the total swing wildly — the more numbers, the wilder, and the size of that swing (its variance) works out to exactly . A wild score is poison to softmax: one huge score grabs all the attention and the learning signal dies. So we shrink the score by dividing by — because dividing by a number cuts the swing by that number squared, and squared is , which cancels the swing perfectly back to size . Not (too much) — , exactly. That one square root is the whole trick, and now you could rederive it on a napkin.
See also: Attention Mechanisms for where this line lives in the full model, Statistical Significance Testing for judging whether a claimed gain is real, Experiment Tracking and Versioning and Hyperparameter Tuning for the reproduction run, Model Debugging for the overfit-10-samples sanity check, and Transfer Learning / Paper Writing and Publication for what comes after you have reproduced it.