Lecture 3 of 1420 minInteractiveFree lecture

Backpropagation: how networks learn

Training needs the gradient of the loss with respect to every weight. Backpropagation gets all of them in one backward sweep using the chain rule. Follow it through a small computation graph, check it numerically, and see why gradients can vanish or explode in deep networks.

In this lecture you will

  • Describe a network as a computation graph of simple operations
  • Apply the chain rule to compute a gradient step by step
  • Explain why backpropagation computes every gradient in a single backward pass
  • Recognize vanishing and exploding gradients and the fixes for them

Gradient descent, from Machine Learning Foundations, needs one thing: the gradient of the loss with respect to every parameter. For two parameters we could work that out by hand. A modern network has millions or billions, all tangled together through layer after layer. Backpropagation is the algorithm that computes all of those gradients efficiently — and it is nothing more than the chain rule from calculus, applied systematically.

Networks are computation graphs

Any network, however large, is built from simple operations — multiply, add, apply an activation, compute a loss. Draw each operation as a node and each value as an edge, and you get a computation graph. We will use the smallest interesting one: a single neuron with a sigmoid, trained on one example.

  • Forward: z=w⋅x+bz = w \cdot x + b, then a=σ(z)a = \sigma(z), then the loss L=(a−y)2L = (a - y)^2.
  • We want ∂L/∂w\partial L/\partial w and ∂L/∂b\partial L/\partial b: how much the loss changes when we nudge each parameter.

The chain rule, one node at a time

The chain rule says that if a change in ww affects zz, which affects aa, which affects LL, then the total effect is the product of the individual effects:

∂L∂w=∂L∂a⋅∂a∂z⋅∂z∂w\frac{\partial L}{\partial w} = \frac{\partial L}{\partial a} \cdot \frac{\partial a}{\partial z} \cdot \frac{\partial z}{\partial w}

Each factor is a local slope that only depends on one operation. Backpropagation computes them from the loss backwards, passing the running product from node to node.

AnimationForward pass, then backward pass
Loading animation…
  1. The computation graphThe input x = 2 and the parameters w = 0.5 and b = −0.3 flow through a multiply, an add, a sigmoid and a squared-error loss against the target y = 1.
  2. Forward pass: compute every valueValues flow left to right: w·x = 1, z = 0.7, and the sigmoid gives a prediction of a = 0.668. Every intermediate value is kept for later.
  3. Measure the lossThe prediction 0.668 is below the target 1, so the loss is (0.668 − 1)² = 0.110. Now we need to know how to change w and b to reduce it.
  4. Start the backward pass at the lossThe loss changes by 2(a − y) = −0.664 for each unit change in a. The minus sign says: increasing a would lower the loss.
  5. Multiply by the local slopeThe sigmoid’s slope at z = 0.7 is a(1 − a) = 0.222. Multiplying gives ∂L/∂z = −0.147: how the loss responds to the neuron’s weighted sum.
  6. Reach the parameters and updateThe add node passes the gradient to both inputs; the multiply node scales it by x. So ∂L/∂w = −0.294 and ∂L/∂b = −0.147. One gradient step lowers the loss from 0.110 to 0.065.

Notice two things. First, the backward pass reused values from the forward pass — the sigmoid output aa, the input xx. Second, the gradient for bb came almost for free once we had ∂L/∂z\partial L/\partial z. In a big network, thousands of weights share the same intermediate gradients, which is why backpropagation computes all of them for roughly the cost of one extra forward pass.

Try it yourself

The calculator below runs the same neuron. Change the input, target and parameters, then take gradient steps and watch the loss fall. The numerical check at the bottom nudges ww by a tiny amount and measures the change in the loss directly — it always agrees with backpropagation.

InteractiveThe chain rule, live
Loading visual…

Try a learning rate of 5: the steps become large and the loss can bounce. Then set the weight to −3: the sigmoid saturates, its slope ∂a/∂z becomes tiny, and learning crawls even though the loss is large.

Vanishing and exploding gradients

In a deep network, the gradient reaching the first layer is a product of many factors — one slope and one weight matrix per layer. Products of many numbers behave dramatically: if the factors are a little below 1, the product shrinks towards zero; a little above 1, and it grows without bound.

InteractiveGradient size at every layer
Loading visual…

Each bar is the average size of the gradient signal that reaches one layer of a random network, on a log scale: every grid line is a factor of 100.

  • With sigmoid activations, each layer multiplies the gradient by at most 0.25. Add layers and watch the early bars collapse — the first layers stop learning.
  • With ReLU and standard (He) initialization, the bars stay level even at 40 layers.
  • Make the initial weights too large and the gradients explode instead; too small and they vanish, even with ReLU.
Vanishing gradients
Gradients shrink as they travel back through many layers, so early layers learn extremely slowly.
Exploding gradients
Gradients grow layer by layer, causing huge, unstable updates and loss values of NaN.
Weight initialization
How weights are set before training. Schemes such as Xavier and He scale random weights by the layer width so signals keep a stable size.
Gradient clipping
Capping the size of the gradient before each update — a standard safeguard against explosions, especially in recurrent networks and transformers.
Residual connection
A shortcut that adds a layer’s input to its output, giving gradients a direct path backwards. It is why networks with hundreds of layers can be trained.

What the framework does for you

In practice you never write backpropagation by hand. Frameworks record the computation graph as your code runs and apply the chain rule automatically — this is called automatic differentiation:

import torch

w = torch.tensor(0.5, requires_grad=True)
b = torch.tensor(-0.3, requires_grad=True)
x, y = torch.tensor(2.0), torch.tensor(1.0)

a = torch.sigmoid(w * x + b)   # forward pass (the graph is recorded)
loss = (a - y) ** 2
loss.backward()                # backward pass: fills w.grad and b.grad

print(w.grad, b.grad)          # tensor(-0.2943) tensor(-0.1471)

Backpropagation tells us which direction to move each weight. How far to move — and how to avoid zig-zagging through narrow valleys — is the job of the optimizer, the subject of the next lecture.

Key takeaways

  • A forward pass computes values from inputs to loss; the backward pass sends gradients from the loss back to every parameter.
  • At each operation, the incoming gradient is multiplied by that operation’s local slope — the chain rule.
  • Backpropagation reuses intermediate results, so one backward pass costs about as much as one forward pass, whatever the number of parameters.
  • Gradients that shrink or grow at every layer vanish or explode in deep networks; ReLU, careful initialization, normalization and residual connections keep them healthy.

Check your understanding

4 questions · answers explained

Q1In backpropagation, what is multiplied together to get the gradient of the loss with respect to a weight deep in the network?

That is the chain rule. Each operation contributes its local slope, and multiplying them along the path gives the total effect of the weight on the loss.

Q2Why is backpropagation much faster than nudging each weight separately and re-measuring the loss?

Nudging each of a million weights would need a million forward passes. Backpropagation gets all million gradients for roughly the cost of one extra pass, which is what makes training large networks possible.

Q3A 30-layer network with sigmoid activations trains well in its last layers but its first layers barely change. What is the likely cause?

Multiplying by a small slope at every layer makes the gradient shrink exponentially with depth, so early layers receive almost no learning signal. Switching to ReLU and using proper initialization are standard fixes.

Q4The gradient ∂L/∂w for a weight is negative. What will a gradient-descent step do to w?

The update is w ← w − η·∂L/∂w. Subtracting a negative number increases w, which is the direction that lowers the loss.