The same sentence as on the home page, “When Mary and John went to the store, John gave a drink to ___”, but this time we open up the math. Every formula is applied to GPT-2 small’s real numbers, so you can see what each symbol actually does.
You only need arithmetic to follow along. Each idea is built from the one before: dot products, then softmax, logs, derivatives, and finally how the model learns.
Scroll to play each step. ← → also step through.
Almost every step in a transformer is a dot product. Take two lists of numbers, multiply them position by position, and add the results:
When the two lists agree in sign at the same places, the products are positive and the total grows. When they disagree, the products cancel out. So a dot product measures how much two vectors point the same way.
These are real: the query at the last “to” and the key at “Mary” from attention head 9 in layer 9. The running total climbs to 49.4.
At the end, the model takes a dot product between its final vector and every word’s row in the token table. That gives one score per word, called a logit:
Doing all 50,257 dot products at once is a matrix multiplication, . The scores can be any number. Here they sit around −93, and Mary’s is the highest at −74.8. Only the differences matter.
To turn scores into probabilities we need numbers that are positive and add up to 1. Softmax does both:
makes everything positive, and it turns differences into ratios. A score 1 point higher means times as likely. Mary beats “them” by 1.66 points, so she’s times as likely: 67.7% against 12.8%.
Attention runs the same softmax over its query·key scores. But a dot product of 64 entries is a sum of 64 terms, so its spread grows like . Big gaps between scores make softmax all-or-nothing.
Raw, Mary’s score is 49.4 against 39.0 for ⟨start⟩: a gap of 10, which gives Mary 99.997%. Dividing by 8 shrinks the gap to 1.3, and the head keeps a blend: 67% Mary, 18% ⟨start⟩, 13% John. A blend lets the gradient reach every word, which is what learning needs.
The probability of a whole sentence is the product of each word’s probability given the words before it. For our sentence that’s 2.8 × 10⁻²⁴, too small to picture, and computers lose precision on numbers like that.
Logarithms turn multiplying into adding:
So instead of multiplying 14 probabilities we add 14 values of . They total 54.2. The surprising words cost the most: the first “Mary” (0.0003, cost 8.1) and “drink” (0.0005, cost 7.6).
Here are all 50,257 probabilities, sorted. On ordinary axes you see one spike for Mary and a flat line for everything else.
On a log scale each step on the axis multiplies by 10 instead of adding a fixed amount. Now the shape appears: the probabilities fall smoothly over 14 orders of magnitude, down to for the least likely word. Log scales are how you see quantities that span many sizes at once.
Training needs one number that says how wrong the model was. It uses the cross-entropy loss:
If the model is certain and right (), the loss is 0. Here , so the loss is 0.39. Had the answer been John, which it gave 2.3%, the loss would be 3.75. The curve shoots up near 0: being confident and wrong costs a lot.
How would the loss change if Mary’s score went up a little? Nudge the score by , see how the loss changes, and divide:
As shrinks, that line through two points turns into the tangent: the derivative. For softmax plus −log it comes out exactly as
Negative means raising Mary’s score lowers the loss.
The same rule gives the slope for every word’s score at once. Let be 1 for the right word and 0 for the rest:
So Mary’s slope is (push her up). Every other word’s slope is just its probability: “them” +0.128, “the” +0.048, “John” +0.023 (push them down). The more wrongly confident the model, the bigger the push.
The loss depends on , which depends on , which depends on . The chain rule says the slope through a chain is the product of the slopes of each link:
Backpropagation is just this, applied from the loss backward through every step. The result is 768 numbers: a pull toward Mary’s direction in the token table and away from “them”, “the” and “John”, each weighted by its .
The same pass keeps going: through the final norm, then each block’s feed-forward and attention layers, all the way to the token table. Each block passes on how much the loss cares about its output, and works out the slope for each of its own weights.
Because each block adds to the running vector, the slope flows straight back through those additions and doesn’t fade to nothing in 12 layers. Every one of the 124 million weights ends up with its own slope from this single sentence.
Now move every weight a small amount against its slope:
(eta) is the learning rate, here . The curve is real: it’s the loss on this sentence if we moved all 124 million weights along the slope by different amounts. One step of size takes the loss from 0.39 to 0.30 and P(Mary) from 67.7% to 74.3%.
Recompute the slopes, step again. After 10 real steps on this one sentence, the loss falls from 0.39 to 0.09 and P(Mary) rises to 91.5%, mostly taken from “them”.
Real training does exactly this, except each step averages the slopes over millions of words from many documents, so the model gets better at text in general instead of memorizing one sentence.
For the whole path through the model, step by step, watch Inside GPT-2: one sentence, every step. The written Transformers, ELI5 has small worked examples you can check by hand.
Model: GPT-2 small, full precision. The loss slice moves all weights along the gradient by multiples of η. The 10 steps are plain gradient descent on this one sentence. Numbers come from export_gpt2_math.py in the research folder.