The problem: billions of slopes
In the gradient descent lesson, each step needed the slope of the loss for every weight. The obvious way to find a weight’s slope is to nudge it a little, run the model again, and see how the loss changes. For a model with 8 billion weights, that is 8 billion runs of the model for one single step. Training would never finish.
Backpropagation (backprop for short) gets every slope from just two passes. The forward pass runs the model normally and remembers the numbers it computed along the way. The backward pass then goes through the layers in reverse, from the loss back to the inputs, handing slopes from each layer to the one before. It was made popular by a four-page Nature paper in 1986, and every model in this course was trained with it.
The chain rule: effects multiply
A network is a chain of simple steps: multiply by a weight, add, bend, multiply again, compute the loss. The chain rule from calculus says that the effect of an early number on the final loss is the product of the effects of each step along the way.
∂loss/∂w = ∂loss/∂y × ∂y/∂wRead “∂loss/∂w” as “how much the loss changes when w is nudged”. Papers use the curly ∂ for slopes when there are many variables.
A worked example
Here is a network with two weights, small enough to do on paper. One input x goes into a neuron with weight w₁ and a ReLU, then into an output with weight w₂. We want the output to be 10.
| Step | Formula | Value |
|---|---|---|
| Input | x | 1 |
| Hidden neuron | h = ReLU(w₁ × x) = ReLU(2 × 1) | 2 |
| Output | y = w₂ × h = 3 × 2 | 6 |
| Loss | (y − 10)² = (6 − 10)² | 16 |
| Slope | Chain rule | Value |
|---|---|---|
| Loss by output | ∂L/∂y = 2 × (y − 10) | −8 |
| Loss by w₂ | ∂L/∂w₂ = ∂L/∂y × h = −8 × 2 | −16 |
| Loss by hidden value | ∂L/∂h = ∂L/∂y × w₂ = −8 × 3 | −24 |
| Loss by w₁ | ∂L/∂w₁ = ∂L/∂h × x = −24 × 1 | −24 |
ReLU passes the slope through unchanged here, because its input (2) is positive. For a negative input it would pass back zero.
Both slopes are negative, so gradient descent raises both weights. With a learning rate of 0.01: w₂ becomes 3 + 0.16 = 3.16 and w₁ becomes 2 + 0.24 = 2.24. Run the forward pass again: h = 2.24, y = 7.08, and the loss falls from 16 to 8.5 in a single step.
Look at the third row. The slope for h was computed once and then reused for everything below it. In a real network each layer receives one such slope from the layer above, uses it for its own weights, and passes a new slope down. No work is repeated. That is why the backward pass is cheap.
Quick check
What training really costs
The backward pass does about twice the work of the forward pass, because for each matrix it needs two slopes: one for the weights, and one to pass on to the layer below. That gives a famous rule of thumb.
running a model ≈ 2 × parameters operations per tokenThe forward pass: a multiply and an add for each weight.
training ≈ 6 × parameters × tokens operationsForward (2) plus backward (4), for every training token.
6 × 37 billion × 14.8 trillion ≈ 3.3 × 10²⁴DeepSeek-V3: 37 billion parameters used per token, 14.8 trillion training tokens.
Training also needs far more memory than running. For the backward pass, the forward pass has to keep its in-between values (called activations). And besides the weights, training stores a slope for every weight and the optimizer’s own numbers: AdamW keeps two extra values per weight. With the usual mix of precisions, this comes to about 16 bytes per parameter before any activations.
| Task | Roughly |
|---|---|
| Running it, BF16 weights | 16 GB |
| Running it, squeezed to about 4.5 bits | 4.5 GB |
| Fully training it with AdamW (16 bytes per parameter) | 128 GB, plus activations: several data-centre GPUs |
This gap is why most people do not fully retrain models, but use cheaper methods such as LoRA, which updates a small number of extra weights. That has its own lesson in part 5.
Keeping the slopes alive
The chain rule multiplies slopes along the path. Over many layers, multiplying many numbers smaller than 1 drives the result towards zero, and the early layers stop learning. This is the vanishing gradient problem. Numbers larger than 1 cause the opposite, exploding gradients. For decades this made deep networks very hard to train.
Two simple fixes made depth possible, and both are in every transformer:
- Residual connections (from ResNet, 2015). Instead of replacing its input, each block adds its result to it: output = x + block(x). The slope then always has a direct path back through the “+ x”, with nothing to shrink it. This is the residual stream from the vectors lesson. ResNet trained image networks 152 layers deep this way.
- Normalisation. Before each block, the vector is rescaled to a standard size, so numbers neither shrink nor blow up from layer to layer. Most open models use RMSNorm, a simpler version of the older LayerNorm.
You never write it yourself
Nobody works out these slopes by hand for real models. Frameworks such as PyTorch record every operation in the forward pass and run the backward pass automatically. This is called autograd (automatic gradients). Here is the one-weight version of the example above:
import torch
w = torch.tensor(3.0, requires_grad=True)
loss = (w * 2.0 - 10.0) ** 2
loss.backward()
print(w.grad) # tensor(-16.)
requires_grad=Truetells PyTorch to track this number.loss.backward()runs the backward pass through every recorded step.w.gradnow holds the slope: (6 − 10) × 2 × 2 = −16, the same as the second row of the table.
Forward pass or backward pass?
-
Computing the model’s output from the input
Answer: Forward pass
-
Working out ∂loss/∂w for every weight
Answer: Backward pass
-
Computing the loss
Answer: Forward pass
-
Handing a slope from layer 32 to layer 31
Answer: Backward pass
-
Saving activations to use later
Answer: Forward pass
-
The only pass needed when you chat with a model
Answer: Forward pass
Remember
- Backpropagation finds the slope of the loss for every weight with one forward pass and one backward pass.
- It is the chain rule: effects multiply along the path, and each layer passes one slope to the layer before, so nothing is repeated.
- Running costs about 2 operations per weight per token; training about 6 × parameters × tokens.
- Training needs about 16 bytes per parameter plus activations, far more than running a model.
- Residual connections and normalisation stop gradients vanishing or exploding. PyTorch’s autograd does it all for you.
Words to know
- Backpropagation
- The method that computes every weight’s slope by going backwards through the network once.
- Forward pass
- Running the network from input to output (and to the loss).
- Backward pass
- Going from the loss back through the layers, computing slopes with the chain rule.
- Chain rule
- The effect of a change through several steps is the product of each step’s effect.
- Activations
- The in-between values a network computes, kept from the forward pass for the backward pass.
- Vanishing gradient
- Slopes shrinking to almost nothing on their way back through many layers, so early layers stop learning.
- Residual connection
- Adding a block’s output to its input (x + block(x)), giving slopes a direct path back.
- Normalisation
- Rescaling each vector to a standard size between blocks (LayerNorm, RMSNorm).
- Autograd
- Software that records the forward pass and computes all slopes automatically.
Go to the source
This lesson is built on these papers and documents. Paper links open arxiv.org, a separate site.
-
Learning representations by back-propagating errors
The four-page paper that made backpropagation famous. Now you can follow it.
-
Calculus on Computational Graphs: Backpropagation (C. Olah)
A short, beautifully drawn explanation of why the backward direction is the efficient one.
-
micrograd (A. Karpathy)
A tiny autograd engine you can read in one sitting.
-
Deep Residual Learning for Image Recognition (ResNet)
The residual connection, the idea that made very deep networks trainable.
-
ZeRO: Memory Optimizations Toward Training Trillion Parameter Models
Where the “16 bytes per parameter” count comes from, and how big training runs split that memory across GPUs.
Check yourself
5 questions. Tap the answer you think is right.
0 of 5 answered
Lesson complete
Answer every question above to complete this lesson.
Your progress is saved on this device only.
Next lessonBefore transformers: the networks that led here