Forward & Backward Propagation
Compute predictions forward and gradients backward through the computation graph.
In plain words
First the network makes a guess and checks how wrong it was. Then it works backwards, step by step, figuring out how much each setting should change to be less wrong.
Think of it like…
Tracing blame through a relay of workers. The forward pass is the product moving along the line to a final defect score; the backward pass walks back along the line asking each worker how much of the defect was theirs, using each worker's own sensitivity to pass the blame upstream.
Where you’ve seen it
A one-neuron-per-layer network is small enough to compute by hand. Input x = 2, hidden layer z = w₁x + b₁ with w₁ = 0.5 and b₁ = 0.5, so z = 1.5 and h = ReLU(z) = 1.5; output ŷ = w₂h + b₂ with w₂ = 2 and b₂ = −1 gives ŷ = 2.0. With target y = 1 and loss L = ½(ŷ − y)² = 0.5, backprop runs in reverse: ∂L/∂ŷ = ŷ − y = 1; ∂L/∂w₂ = 1 × h = 1.5; ∂L/∂b₂ = 1; ∂L/∂h = 1 × w₂ = 2; because z > 0 the ReLU derivative is 1, so ∂L/∂z = 2; ∂L/∂w₁ = 2 × x = 4 and ∂L/∂b₁ = 2. The team checks these against a numerical gradient: nudging w₂ by ±0.001 changes the loss from 0.4985 to 0.5015, and (0.5015 − 0.4985) / 0.002 = 1.5, matching. A gradient step with learning rate 0.05 gives w₂ = 1.925, b₂ = −1.05, w₁ = 0.3 and b₁ = 0.4, and the new forward pass gives ŷ = 0.875 and a loss of 0.0078. A learning rate of 0.1 overshoots: ŷ becomes −0.175 and the loss rises to 0.69, showing that backprop gives the direction and the learning rate controls the step.
Words you’ll hear
- Chain rule
- The derivative of a composition is the product of the derivatives of its parts.
- Computation graph
- The recorded sequence of operations that autodiff walks backwards.
- Gradient
- The vector of partial derivatives of the loss with respect to the parameters.
- Activation cache
- Forward-pass values kept in memory for use in the backward pass.
Visual diagram
Theory & deep dive
The chain rule, step by step
For y = f(g(x)) the derivative is f′(g(x)) · g′(x). In a network, the loss is a long composition, so backprop works from the output: first the local gradient of the loss with respect to the prediction (ŷ − y for squared error), then, at each layer, the gradient with respect to the layer's input is the incoming gradient multiplied by the layer's local derivative, and the gradient with respect to its weights is the incoming gradient multiplied by the layer's input. In the worked example, the output layer gives ∂L/∂w₂ = (ŷ − y) × h = 1 × 1.5 and passes ∂L/∂h = (ŷ − y) × w₂ = 2 back. The ReLU contributes a factor of 1 (z is positive), so ∂L/∂z = 2, and then ∂L/∂w₁ = 2 × x = 4 and ∂L/∂b₁ = 2.
For vectors the same logic uses matrices: for a layer z = Wx + b, ∂L/∂W = (∂L/∂z) xᵀ, ∂L/∂b = ∂L/∂z and ∂L/∂x = Wᵀ (∂L/∂z). Each gradient has the same shape as its parameter, which is a useful sanity check when debugging shapes. If a ReLU unit's z is negative its derivative is 0, so no gradient flows through it for that example; with sigmoid activations each layer multiplies the gradient by at most 0.25, which is the vanishing-gradient mechanism.
Loss at w₂ = 2.001: ŷ = 2.0015, L = 0.5015. At w₂ = 1.999: ŷ = 1.9985, L = 0.4985. Central difference (0.5015 − 0.4985) / (2 × 0.001) = 1.5, equal to the analytic ∂L/∂w₂ = 1.5.
Memory, cost and what can go wrong
Training memory is dominated by the cached activations (they scale with batch size and depth), plus parameters, gradients and optimiser state, which is why inference fits on hardware where training does not. The backward pass costs roughly twice the forward pass in floating-point operations, so a training step is about three times the cost of inference. Techniques such as gradient checkpointing trade compute for memory by discarding some activations and recomputing them, and mixed precision reduces the footprint of the activations.
Typical failures: vanishing gradients (products of factors below 1 in deep sigmoid or tanh stacks; fix with ReLU-family activations, residual connections, normalisation and careful initialisation), exploding gradients (factors above 1; fix with gradient clipping and lower learning rates), dead ReLUs, in-place operations that destroy values autograd needs, and forgetting to zero gradients, because PyTorch accumulates them: calling backward() twice without zero_grad() doubles the gradient. Check a new layer with a finite-difference test, and verify that a tiny batch can be overfit to near-zero loss before scaling up.
Gradients: ∂L/∂w₂ = 1.5, ∂L/∂b₂ = 1, ∂L/∂w₁ = 4, ∂L/∂b₁ = 2. With lr = 0.05: w₂ = 1.925, b₂ = −1.05, w₁ = 0.3, b₁ = 0.4, so z = 1.0, ŷ = 1.925 × 1.0 − 1.05 = 0.875 and L = 0.0078. With lr = 0.1: w₁ = 0.1, b₁ = 0.3, w₂ = 1.85, b₂ = −1.1, so ŷ = −0.175 and L = 0.69, worse than before.
How it works, step by step
Step 1 of 41. Forward pass
Compute each layer's pre-activation and activation, store them and finish with the loss.
- Cache
- activations
When to use it & trade-offs
What it is
Forward propagation pushes an input through the network layer by layer to produce a prediction and a loss. Backward propagation (backprop) then applies the chain rule in reverse, from the loss back to each parameter, to compute the gradient ∂L/∂w for every weight. The forward pass stores intermediate values (pre-activations and activations) because the backward pass needs them: the gradient of a layer's weights depends on the activations that fed into it, and the gradient passed to an earlier layer depends on the local derivative of the activation. An optimiser then uses these gradients to update the weights. Modern frameworks automate this with automatic differentiation, building a computation graph during the forward pass and walking it backwards.
Why it matters: Backprop is what makes training networks with millions of parameters feasible: one backward pass yields every gradient at roughly the cost of two forward passes, instead of perturbing each weight separately. Understanding it explains vanishing and exploding gradients, why training needs far more memory than inference, why loss.backward() must be paired with zero_grad(), and how to debug models by checking gradients numerically. It is also the standard whiteboard question for deep-learning interviews.
When to reach for it
- Training any neural network by gradient descent
- Debugging custom layers with a finite-difference test
- Reasoning about vanishing and exploding gradients
When to avoid or be careful
- Finite differences as the training method for large models
- In-place edits of tensors that autograd still needs
- Calling backward repeatedly without clearing gradients
Ways to get gradients
Strengths: All gradients in about 2× the forward cost.
Watch out: Stores activations; memory heavy.
Strengths: Simple and independent of the graph.
Watch out: One extra forward pass per parameter; approximate.
Strengths: Cuts activation memory sharply.
Watch out: Extra recomputation time.
Interview signals & numbers to know
When this topic comes up
- “Walk me through backprop for a two-layer network”
- Why training uses more memory than inference
- Vanishing and exploding gradients
- Why zero_grad is needed
- Gradient checking
Benchmarks & rules of thumb
- ≈ 2× forward
- typical cost of the backward pass
- 4 · 2 · 1.5 · 1
- gradients for w₁, b₁, w₂, b₂ in the worked example
- 0.5 → 0.0078
- loss after one step at lr 0.05
Common mistakes & follow-ups
Common interview mistakes
- Forgetting to reset gradients, which silently accumulate.
- Using a learning rate so large that the loss rises after the step.
- Detaching or modifying tensors the backward pass still needs.
Likely interviewer follow-ups
- How would you verify a hand-written gradient?
- Why does depth with sigmoids make gradients vanish?
- How does gradient checkpointing save memory?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Forward & Backward Propagation with data science interview Q&A and hands-on practice.