What one training step is
A language model learns by repeating one step many times. The forward pass runs the model on text and measures with a loss how badly it predicted each next token. The backward pass applies the chain rule from the loss back through every operation, giving the gradient of the loss with respect to every parameter. The optimiser then moves each parameter a little against its gradient.
This page records exactly one such step for GPT-2 (124M) on a batch of one sequence, To be, or not to be: 7 tokens, so 6 next-token targets. The buttons Forward x → loss, Backward ∂L/∂x ← loss and Update AdamW Δθ switch the diagram between the three passes. Dropout is off, so the step is deterministic.
The loss
At each position the final hidden state is scored against the whole vocabulary with the token embedding matrix (GPT-2 ties its output layer to its embedding), and the loss is the mean cross-entropy of the six correct next tokens :
Before the step the mean is 3.252 nats. The losses differ widely by position: after be the model gives the comma only 0.10 % (loss 6.869), while after or it already expects not (loss 1.343) and after the second to it is nearly certain of be (loss 0.039). The Probabilities column shows the full distribution at the chosen token, with the target's rank.
Backpropagation
The backward pass starts where the loss meets the logits. The gradient there has a simple form: the predicted probabilities minus a one-hot vector at the target, divided by the 6 targets:
The target is pulled up and every other token pushed down by its own probability, and the entries sum to zero. From there each operation passes the gradient to its inputs with its own rule. For a matrix product the input gradient goes back through the transposed weight and the weight gradient is a sum of outer products, . A residual connection adds the gradients of its two branches. A softmax row passes on , so adding the same constant to a whole row of scores gets no gradient. This is why the key projection's bias, which adds such a constant, receives exactly zero.
Each rule on the page, opened from its operation, matches float64 autograd at all 12 blocks to within , and finite differences confirm the gradients.
Where the gradient goes
The bottom strip compares the gradient norms of the parameter groups. Block 3's gradient has norm 10.52 and block 11's only 2.97; it mostly shrinks towards the top of the stack, with block 12 back up at 4.76. The largest by far belongs to , because the tied matrix is used twice: as the LM head it gets a gradient of norm 60.59 from all 50,257 logits at every position, and as the embedding table only 1.97, through the six rows the tokens looked up.
AdamW
Adam keeps two running averages per parameter, of the gradient and of its square, corrects their bias towards zero at the start, and divides one by the square root of the other. AdamW adds weight decay as a separate shrinking step rather than through the gradient:
The step uses , , , and . At the first step and , so the Adam step is : every coordinate whose gradient is much larger than moves by , whatever the gradient's size, and weight decay adds only . Adam keeps the sign and drops the magnitude. Every block's update therefore has the same norm, 0.133, although their gradients differ by more than three times. The token embedding is the exception: most of its rows receive only a tiny gradient from the LM head, and only 2.4 % of its coordinates move by a full .
What one step achieves
The second forward pass, with the updated weights, lowers the mean loss on the same sequence from 3.252 to 1.771. The comma after be rises from 0.10 % to 1.20 %, and not after or from 26.1 % to 91.9 %. The step was computed from this very batch, so this mostly measures how well it fits that batch. On the rest of the line, That is the question., which the step never saw, the mean loss falls only from 3.301 to 3.185. Real training averages the gradient over large batches and takes many thousands of steps, with a learning rate that changes over time.
Where it came from
Rumelhart, Hinton and Williams (1986) showed that backpropagating errors through a multi-layer network trains its hidden units to learn useful representations; automatic differentiation, surveyed by Baydin et al., is how frameworks apply those rules to any program. Kingma and Ba introduced Adam in 2014, and Loshchilov and Hutter showed that weight decay should be decoupled from Adam's adaptive step, which is AdamW. GPT-2 (Radford et al., 2019) was pre-trained with this next-token loss on web text. The Dense tab shows the forward pass of the same model in more detail.
Papers
- Learning representations by back-propagating errors Backpropagation: the chain rule run backwards through a network trains its hidden layers.
- Adam: A Method for Stochastic Optimization The Adam update with bias-corrected first and second moments, the step this page records.
- Decoupled Weight Decay Regularization AdamW: weight decay applied as its own shrinking step instead of through the gradient.
- Language Models are Unsupervised Multitask Learners GPT-2, the model this page trains for one step, pre-trained with the same next-token cross-entropy.
- Automatic differentiation in machine learning: a survey How reverse-mode automatic differentiation computes every gradient of a program in one backward pass.
- Layer Normalization The normalisation in every GPT-2 block, whose backward rule the page shows.