Search

Search papers and lessons

Backpropagation

Level 3 · 16 min read

Backpropagation computes the gradient of a network's loss with respect to every weight by applying the chain rule backwards, one layer at a time, reusing the work already done.

On this page

  • Colour = topic mix
  • Calculus
  • Linear algebra
  • Neural network basics
  • Probability
  • Losses & regularization
  • Optimizers
  • Sign in to see your progress

TL;DR

Training a neural network means nudging every weight in the direction that lowers the loss. To know which way to nudge, you need the derivative of the loss with respect to each weight. Backpropagation is the procedure that computes all of those derivatives in one backward sweep through the network, by applying the chain rule from the output back to the input and reusing each intermediate result.

Where you'll see it: in the training loop of every neural network, from a small classifier on a laptop to large language models. Every framework's loss.backward() call is this algorithm.

Intuition

Blame, passed backwards

Picture a factory line with three stations. The final product is wrong by 2 cm. Station three could have caused some of that error, station two could have, and station one could have. To fix the product, each station needs to know how much its own setting contributed. The useful way to work this out is backwards: the last station looks at the error and says "if I had moved my setting by a little, the output would have moved by this much." Then it passes that sensitivity to the station before it, which multiplies it by its own sensitivity and passes the product on again.

That is backpropagation. A neural network is a chain of simple steps: multiply by a weight matrix, add a bias, apply a bend. The loss is at the end. Each step has a local sensitivity, meaning how much its output changes when its input changes. Starting from the loss and walking backwards, you multiply these local sensitivities together, and at every step you also read off how much each weight at that layer contributed.

Why not just measure each weight?

You could find the gradient of each weight numerically. Nudge one weight a tiny amount, rerun the network, see how the loss moved. That works, but a network with a million weights needs a million forward passes to get one gradient, and training needs a gradient on every step. Backpropagation gets all of them for a cost of only a small multiple of one forward pass. Its trick is to reuse the intermediate values from the forward pass and the chain of sensitivities from the backward pass, instead of recomputing the network for each weight.

The reason it is so efficient is that the loss is one number and the parameters are many. Working backwards from one number to all the parameters costs only a small multiple of one forward pass. Working forwards would need a separate pass for each parameter.

−0.500.511.50510w₁loss L
  • loss as a function of w₁
A toy network's loss as one weight changes, with everything else fixed. At w₁ = 0.5 the curve is rising with slope 6, so gradient descent moves w₁ to the left.

The math

The network and its loss

Use the notation from Feedforward Neural Network. The input is x\mathbf{x}. Each layer ℓ=1,…,L\ell = 1, \dots, L computes a pre-activation and an activation:

z(ℓ)=W(ℓ)h(ℓ−1)+b(ℓ),h(ℓ)=σ ⁣(z(ℓ)),h(0)=x\mathbf{z}^{(\ell)} = W^{(\ell)} \mathbf{h}^{(\ell-1)} + \mathbf{b}^{(\ell)}, \qquad \mathbf{h}^{(\ell)} = \sigma\!\left(\mathbf{z}^{(\ell)}\right), \qquad \mathbf{h}^{(0)} = \mathbf{x}

Here W(ℓ)W^{(\ell)} is a weight matrix, b(ℓ)\mathbf{b}^{(\ell)} is a bias vector, and σ\sigma is an Activation Function applied entry by entry. The output is y^=z(L)\hat{\mathbf{y}} = \mathbf{z}^{(L)} (no activation on the last layer). The loss L(y^,y)\mathcal{L}(\hat{\mathbf{y}}, \mathbf{y}) is a single number that measures how wrong the prediction is. Training needs the gradient ∇W(ℓ)L\nabla_{W^{(\ell)}} \mathcal{L} and ∇b(ℓ)L\nabla_{\mathbf{b}^{(\ell)}} \mathcal{L} for every layer.

The error signal

Define the error signal at layer ℓ\ell as the sensitivity of the loss to that layer's pre-activation:

δ(ℓ)=∂L∂z(ℓ)\boldsymbol{\delta}^{(\ell)} = \frac{\partial \mathcal{L}}{\partial \mathbf{z}^{(\ell)}}

Each entry of δ(ℓ)\boldsymbol{\delta}^{(\ell)} answers the question "if this neuron's pre-activation moved a little, how much would the loss move?" Once you have δ(ℓ)\boldsymbol{\delta}^{(\ell)} for a layer, the weight gradients for that layer follow directly.

The four equations

Backpropagation is four equations. The first sets up the start of the backward pass, the second moves the error signal back one layer, and the last two read off the parameter gradients.

δ(L)=∂L∂y^δ(ℓ)=(W(ℓ+1))⊤δ(ℓ+1)⊙σ′ ⁣(z(ℓ))∂L∂W(ℓ)=δ(ℓ)(h(ℓ−1))⊤∂L∂b(ℓ)=δ(ℓ)\begin{aligned} \boldsymbol{\delta}^{(L)} &= \frac{\partial \mathcal{L}}{\partial \hat{\mathbf{y}}} \\[4pt] \boldsymbol{\delta}^{(\ell)} &= \left(W^{(\ell+1)}\right)^{\top} \boldsymbol{\delta}^{(\ell+1)} \odot \sigma'\!\left(\mathbf{z}^{(\ell)}\right) \\[4pt] \frac{\partial \mathcal{L}}{\partial W^{(\ell)}} &= \boldsymbol{\delta}^{(\ell)} \left(\mathbf{h}^{(\ell-1)}\right)^{\top} \\[4pt] \frac{\partial \mathcal{L}}{\partial \mathbf{b}^{(\ell)}} &= \boldsymbol{\delta}^{(\ell)} \end{aligned}

Here ⊙\odot is the entry-by-entry product, ⊤\top is transpose, and σ′\sigma' is the derivative of the activation. The second equation is the chain rule written for vectors. The loss depends on z(ℓ)\mathbf{z}^{(\ell)} only through z(ℓ+1)\mathbf{z}^{(\ell+1)}, and z(ℓ+1)\mathbf{z}^{(\ell+1)} depends on h(ℓ)\mathbf{h}^{(\ell)} through the matrix W(ℓ+1)W^{(\ell+1)}. Multiplying by (W(ℓ+1))⊤(W^{(\ell+1)})^{\top} carries the signal back through that matrix, and the ⊙σ′\odot \sigma' term carries it back through the bend.

The third equation says that the gradient for a weight is the product of the error at its output neuron and the activation at its input neuron. It is an outer product, so each weight gets its own entry. The fourth says the bias gradient is just the error signal.

Gradients through a matrix product

For a layer z=Wh\mathbf{z} = W\mathbf{h}, the gradient of a scalar loss with respect to h\mathbf{h} is W⊤∂L/∂zW^{\top} \partial\mathcal{L}/\partial \mathbf{z}, and the gradient with respect to WW is (∂L/∂z)h⊤(\partial\mathcal{L}/\partial \mathbf{z}) \mathbf{h}^{\top}. These two expressions are the building blocks of the four equations above. The Jacobian Matrix of z\mathbf{z} with respect to h\mathbf{h} is WW, and the backward pass multiplies by its transpose.

Algorithm

Algorithm
Backpropagation for one training example
  1. Forward pass. Compute z(ℓ)\mathbf{z}^{(\ell)} and h(ℓ)\mathbf{h}^{(\ell)} for ℓ=1,…,L\ell = 1, \dots, L, and store them. Compute the loss.
  2. Output error. Compute δ(L)\boldsymbol{\delta}^{(L)} from the loss.
  3. Backward pass. For ℓ=L−1,…,1\ell = L-1, \dots, 1, compute δ(ℓ)\boldsymbol{\delta}^{(\ell)} from δ(ℓ+1)\boldsymbol{\delta}^{(\ell+1)}, using the stored z(ℓ)\mathbf{z}^{(\ell)} for σ′\sigma'.
  4. Read off gradients. For each layer, compute ∂L/∂W(ℓ)\partial\mathcal{L}/\partial W^{(\ell)} and ∂L/∂b(ℓ)\partial\mathcal{L}/\partial \mathbf{b}^{(\ell)}.

Step 1 must store the intermediate values. Step 3 needs them, so the memory cost of backpropagation is the cost of keeping every layer's activations until the backward pass is done.

Worked examples

Example
A scalar network, by hand

Take a network with one input, one hidden neuron with a ReLU activation, and one output with no activation. The loss is squared error. The numbers are:

  • input x=2x = 2, target y=1y = 1
  • hidden weight w1=0.5w_1 = 0.5, hidden bias b1=0b_1 = 0
  • output weight w2=−1w_2 = -1, output bias b2=0.5b_2 = 0.5

Forward pass.

  1. z1=w1x+b1=0.5⋅2+0=1z_1 = w_1 x + b_1 = 0.5 \cdot 2 + 0 = 1
  2. h1=ReLU(z1)=max⁡(1,0)=1h_1 = \text{ReLU}(z_1) = \max(1, 0) = 1
  3. y^=w2h1+b2=−1⋅1+0.5=−0.5\hat{y} = w_2 h_1 + b_2 = -1 \cdot 1 + 0.5 = -0.5
  4. L=(y^−y)2=(−0.5−1)2=2.25\mathcal{L} = (\hat{y} - y)^2 = (-0.5 - 1)^2 = 2.25

Backward pass. The chain rule is applied from the output back to the input.

  1. ∂L/∂y^=2(y^−y)=2(−1.5)=−3\partial\mathcal{L}/\partial \hat{y} = 2(\hat{y} - y) = 2(-1.5) = -3
  2. Output layer: ∂L/∂w2=(∂L/∂y^)⋅h1=−3⋅1=−3\partial\mathcal{L}/\partial w_2 = (\partial\mathcal{L}/\partial \hat{y}) \cdot h_1 = -3 \cdot 1 = -3, and ∂L/∂b2=−3⋅1=−3\partial\mathcal{L}/\partial b_2 = -3 \cdot 1 = -3
  3. Carry the signal back: ∂L/∂h1=(∂L/∂y^)⋅w2=−3⋅(−1)=3\partial\mathcal{L}/\partial h_1 = (\partial\mathcal{L}/\partial \hat{y}) \cdot w_2 = -3 \cdot (-1) = 3
  4. Through the ReLU: since z1=1>0z_1 = 1 \gt 0, ReLU′(z1)=1\text{ReLU}'(z_1) = 1, so ∂L/∂z1=3\partial\mathcal{L}/\partial z_1 = 3
  5. Hidden layer: ∂L/∂w1=3⋅x=3⋅2=6\partial\mathcal{L}/\partial w_1 = 3 \cdot x = 3 \cdot 2 = 6, and ∂L/∂b1=3⋅1=3\partial\mathcal{L}/\partial b_1 = 3 \cdot 1 = 3

Check. Nudge w1w_1 to 0.5+ε0.5 + \varepsilon. Then h1=1+2εh_1 = 1 + 2\varepsilon, y^=−0.5−2ε\hat{y} = -0.5 - 2\varepsilon, and L=(−1.5−2ε)2≈2.25+6ε\mathcal{L} = (-1.5 - 2\varepsilon)^2 \approx 2.25 + 6\varepsilon. The slope is 6, which matches step 9. This is the same curve shown in the plot above.

Take a step. With learning rate η=0.05\eta = 0.05, the update w←w−η ∂L/∂ww \leftarrow w - \eta \, \partial\mathcal{L}/\partial w gives w1=0.2w_1 = 0.2, b1=−0.15b_1 = -0.15, w2=−0.85w_2 = -0.85, b2=0.65b_2 = 0.65. Rerunning the forward pass: z1=0.25z_1 = 0.25, y^=0.4375\hat{y} = 0.4375, and L=(0.4375−1)2≈0.316\mathcal{L} = (0.4375 - 1)^2 \approx 0.316. The loss dropped from 2.25 to 0.316 in one step.

Interpretation: the gradient told us which direction reduces the error for every parameter at once, and a small step in that direction did reduce it. The step size of 0.05 was chosen to keep the change modest. A larger step could overshoot.

Example
A realistic network: what the backward pass costs

Consider a classifier for 28×2828 \times 28 images that has one hidden layer of 128 neurons and 10 outputs. Its input has 784784 values. The number of trainable parameters is

(784⋅128+128)+(128⋅10+10)=100,480+1,290=101,770(784 \cdot 128 + 128) + (128 \cdot 10 + 10) = 100{,}480 + 1{,}290 = 101{,}770

Forward pass cost. The two matrix-vector products cost 784⋅128+128⋅10=100,352+1,280=101,632784 \cdot 128 + 128 \cdot 10 = 100{,}352 + 1{,}280 = 101{,}632 multiply-adds.

Backward pass cost. Each layer needs one product to move δ\boldsymbol{\delta} back through W⊤W^{\top}, which costs as much as its forward product, and one outer product for the weight gradient, which costs the same again. So the backward pass costs roughly twice the forward pass, about 2⋅101,632≈203,0002 \cdot 101{,}632 \approx 203{,}000 operations.

Comparison. A forward-only method, which computes the derivative with respect to one parameter per pass, would need 101,770101{,}770 passes of about 101,632101{,}632 operations each. That is about 1.0×10101.0 \times 10^{10} operations. Backpropagation does the same job in about 2×1052 \times 10^5, roughly fifty thousand times less work.

Interpretation: the saving comes from the shape of the problem, one scalar loss and many parameters. It is why backpropagation scales to networks with billions of parameters, and why the number of parameters is not the bottleneck it would otherwise be.

Tradeoffs and failure modes

Backpropagation is the default because it is exact and efficient, but it has practical limits.

SituationWhat goes wrongBetter choice or fix
Very deep network with sigmoid activationsThe factor σ′≤0.25\sigma' \le 0.25 is multiplied at every layer, so gradients shrink toward zero (vanishing gradients). Early layers barely learn.Use Rectified Linear Unit (ReLU) or its variants, careful Xavier (Glorot) Initialization or He (Kaiming) Initialization, or residual connections.
Weights that grow large with depthRepeated multiplication by large matrices makes gradients explode.Gradient clipping, normalisation layers such as Layer Normalization, or smaller learning rates.
ReLU at exactly zeroThe derivative is undefined at z=0z = 0.In practice a convention is used, such as ReLU′(0)=0\text{ReLU}'(0) = 0. The effect is small.
Memory-limited trainingBackpropagation stores every activation for the backward pass, which can exceed GPU memory.Gradient checkpointing, which recomputes some activations during the backward pass.
Custom operationsAutomatic differentiation needs a derivative for every operation. A non-differentiable step breaks the chain.Replace the step with a differentiable approximation, or use a surrogate gradient.

Backpropagation computes the gradient of whatever the forward pass computes. It does not know whether that loss is the right one, and it cannot tell you whether a model will generalise. Those problems are handled by the choice of Loss Function, the data, and validation.

History

The core idea predates neural networks. In the early 1960s, control theorists such as Kelley and Bryson described how to compute the sensitivity of a multi-stage system's cost to its settings by working backwards through the stages. In 1970, Seppo Linnainmaa described the general method for computing derivatives of a program's output by running its operations in reverse. Its neural network form was described by Paul Werbos in his 1974 doctoral thesis, which applied it to training multilayer networks.

Interest grew in the mid-1980s. In 1986, David Rumelhart, Geoffrey Hinton and Ronald Williams published a paper in Nature showing that backpropagation could learn useful internal representations in multilayer networks. That paper is the usual reference, and it helped revive the field after a period when single-layer limitations had discouraged work on neural networks.

The method became practical for large networks later. Faster hardware, larger datasets, and automatic differentiation libraries made it routine. Modern frameworks compute the backward pass automatically from the forward definition, so the equations above are rarely written out by hand, but they are what those frameworks run.

Check yourself

QuestionRecall: what does the error signal δ(ℓ)\boldsymbol{\delta}^{(\ell)} measure?Show answer

It is the partial derivative of the loss with respect to the pre-activation z(ℓ)\mathbf{z}^{(\ell)} of layer ℓ\ell. Each entry says how much the loss changes if that neuron's pre-activation changes by a small amount. It is the quantity that the backward pass carries from layer to layer.

QuestionComputation: in the scalar example, what is ∂L/∂b2\partial\mathcal{L}/\partial b_2 if the target were y=0y = 0 instead of y=1y = 1, with all other values unchanged?Show answer

The forward pass is unchanged, so y^=−0.5\hat{y} = -0.5. Then ∂L/∂y^=2(y^−y)=2(−0.5−0)=−1\partial\mathcal{L}/\partial \hat{y} = 2(\hat{y} - y) = 2(-0.5 - 0) = -1. Since ∂y^/∂b2=1\partial\hat{y}/\partial b_2 = 1, the answer is −1-1.

QuestionWhy: why does backpropagation need the forward-pass activations, and what would happen if they were discarded?Show answer

The backward pass needs σ′(z(ℓ))\sigma'(\mathbf{z}^{(\ell)}) and h(ℓ−1)\mathbf{h}^{(\ell-1)} to compute the local sensitivities and the weight gradients. Those values depend on the input and the current weights, so they cannot be recomputed from the loss alone. Discarding them forces the network to be run again before the backward pass can proceed, which is the trade made by gradient checkpointing.

QuestionApplied: a 50-layer network with sigmoid activations trains very slowly, and the gradients for the first few layers are about 10−810^{-8} while the last layer's are about 10−110^{-1}. What is the likely cause, and what would you change?Show answer

The likely cause is vanishing gradients. Each sigmoid derivative is at most 0.25, so each backward step shrinks the signal, and after 50 layers the factor is tiny. A good response is to switch to ReLU-family activations, use He initialisation for the weights, and consider residual connections so the signal has a short path back to the early layers.

Sources