Walk me through backpropagation
Written by the mlprep team · Updated
Walk me through backpropagation. How does a neural network actually learn from a mistake?
Backpropagation is just the chain rule of calculus applied systematically to a computational graph. The goal is to compute ∂L/∂w for every weight w in the network, where L is the loss. With those gradients, gradient descent updates each weight in the direction that reduces the loss.
The forward pass#
Run the input through the network layer by layer, computing activations at each step. Cache the intermediate values — you'll need them during the backward pass.
The backward pass#
Start at the loss. The loss is a function of the final layer's output, which is a function of the previous layer's output, and so on back to the weights. The chain rule says:
∂L/∂w = ∂L/∂a_out · ∂a_out/∂z · ∂z/∂w
Where z = wᵀx + b is the pre-activation and a_out = σ(z) is the activation. Each term is easy to compute locally:
- ∂L/∂a_out comes from the layer above (it's propagated backward)
- ∂a_out/∂z is the derivative of the activation function (sigmoid', ReLU', etc.)
- ∂z/∂w = x (the input to that layer, cached from the forward pass)
In practice: the delta rule#
Define δ_l = ∂L/∂z_l (the gradient with respect to pre-activations at layer l). Then:
δ_l = (W_{l+1}ᵀ δ_{l+1}) ⊙ σ'(z_l)
The gradient flows backward through the transposed weight matrix and gets element-wise multiplied by the activation derivative. This is why it's called backpropagation — you're propagating the error signal backward through the network.
Why it's efficient#
Naive computation of each gradient independently would be O(n²) in the number of weights. Backprop is O(n) — each intermediate gradient is computed once and reused. The caching of forward-pass activations is what makes this possible. This is why deep learning frameworks build computational graphs: they track the operations in the forward pass so they can automatically reverse them.
Interview red flags#
- Describing backprop as "the network adjusts its weights" without the chain rule or cached forward activations.
- Mixing up backpropagation (computing gradients) with gradient descent (using them to update weights).
- Being unable to explain vanishing gradients mechanically: small derivatives multiplied across many layers.
- Not knowing why ReLU and residual connections help gradient flow in deep networks.
You mentioned the activation derivative — what happens to gradients in very deep networks, and how do residual connections address it?
Vanishing gradients#
In a deep network, the backward pass multiplies the gradient by the activation derivative at every layer. For sigmoid and tanh, the maximum derivative is 0.25 and 1.0 respectively — and near saturation (large activations), they're close to zero. Chain 50 layers with sigmoid activations, and the gradient reaching layer 1 can be 0.25⁵⁰ ≈ 10⁻³⁰. The early layers learn nothing.
ReLU (f(x) = max(0, x)) has derivative 1 for x > 0 and 0 for x < 0. It doesn't saturate for positive inputs, which largely solved vanishing gradients for feedforward networks. The problem it introduced: dead neurons. If a neuron's input is always negative during training, its gradient is always 0 and it never updates. Leaky ReLU and GELU address this.
Exploding gradients#
The opposite problem: if weights are large, gradients can grow exponentially through layers. Addressed by gradient clipping (cap ‖∇‖ at a threshold before the update) and careful weight initialization (Xavier/He init keeps variance roughly constant across layers).
Residual connections#
A residual block computes F(x) + x instead of F(x). The addition means the gradient has two paths backward: through F(x) and directly through x. The direct path has gradient 1 — no activation derivative, no weight matrix multiplication. No matter how many residual blocks you stack, there's always a gradient highway from the output to the input.
Formally, if the loss gradient at block output is δ:
∂L/∂x_in = δ · (∂F/∂x + I)
The identity term I means the gradient never vanishes through this path, regardless of what F(x) does. This is why ResNets can be 152+ layers deep and still train stably.
The key takeaway unlocks after you reveal an expert answer.
Key Takeaway
Backprop applies the chain rule backward through the network, computing ∂L/∂w at each layer using cached forward-pass activations. Vanishing gradients occur when activation derivatives are small and multiply across many layers — ReLU reduced this by having derivative 1 for positive inputs. Residual connections add a direct identity gradient path that bypasses the activation derivatives entirely, making very deep networks trainable.
Follow-up Questions to Test Yourself
Interviewers often pivot here once you cover the core concept.
- How does batch normalization interact with backpropagation, and why does it help with training stability?
- What's the difference between gradient checkpointing and standard backprop, and when would you use it?
- How would you debug a network where training loss isn't decreasing at all from epoch 1?
How confident do you feel explaining this?
Track your understanding or flag it to revisit before your next loop.
Related Questions