Deep Learning

Backpropagation

The engine of deep learning. Most candidates know the intuition; few can trace matrix dimensions correctly.

🟡 intermediate5 min readfundamentalsmust-know
Backpropagation computes gradients of scalar loss L with respect to all network weights W using the multivariable chain rule in reverse mode. Running backwards from output to input computes all ∂L/∂W in O(W) operations, whereas finite differences or forward-mode differentiation require O(W²) complexity. Key interview topics include computational graphs, local gradients, error signals (δ), and matrix shape matching.

Why reverse-mode automatic differentiation?

Consider a function L = f(x) with M inputs and N outputs.

In neural networks, weights M = 10⁶ to 10¹¹, while scalar loss N = 1. Backpropagation evaluates gradients wrt all parameters in a single reverse sweep taking O(W) computation.

The Chain Rule & Error Vector (δ)

For layer l:

Define error signal δ^(l) = ∂L / ∂z^(l).

Using the chain rule:

δ^(L) = ∂L / ∂a^(L) ⊙ σ'(z^(L))                  (Output layer error)

δ^(l) = ( (W^(l+1))ᵀ δ^(l+1) ) ⊙ σ'(z^(l))        (Backpropagated error signal)

Gradients with respect to parameters at layer l:

∂L / ∂W^(l) = δ^(l) (a^(l-1))ᵀ
∂L / ∂b^(l) = δ^(l)

Each layer takes incoming gradient δ^(l+1), computes local gradient σ'(z), passes δ^(l) to previous layer, and accumulates parameter gradients.

Matrix calculus dimension sanity check

In interviews, never guess matrix derivatives — use shape matching:

Given y = X W + b where:

Target shapes for gradients:

Say this out loud

"Backpropagation is reverse-mode automatic differentiation applied to computational graphs. Because neural nets map millions of weights to a single scalar loss, reverse mode computes all weight gradients in O(1) backward pass rather than O(W) forward sweeps. It uses the chain rule to recursively multiply local derivatives with upstream gradients, storing forward activations in memory to compute weight updates."

Follow-ups to expect

Check yourself

Question 1 of 3

Why is reverse-mode automatic differentiation (backpropagation) preferred over forward-mode AD for training deep neural networks with millions of parameters?

More in Deep Learning

See all →
Activation Functions4 minDropout4 minVanishing & Exploding Gradients4 min