Backpropagation

Level AdvancedDifficulty ★★★★★Application⌖ Open in the map

What is it?

The algorithm that computes ∂L/∂W\partial L/\partial W and ∂L/∂b\partial L/\partial b for every layer of a network: one forward pass storing intermediate values, then one backward pass applying the chain rule from the loss down to the inputs. Cost: about twice the forward pass, whatever the number of parameters.

Why does it exist?

Gradient descent needs the gradient with respect to every weight. Computing each partial derivative separately (or by finite differences) costs one full network evaluation per weight: impossible with 10910^9 weights. Backprop shares the work: the derivative of the loss with respect to a layer's output is computed once and reused for all the weights feeding into it.

Intuition

Blame assignment. The loss says "the output was too high by this much". Each layer passes the blame back to its inputs in proportion to how much they influenced it (the local derivatives), and each weight receives (blame arriving at its neuron) × (the input it multiplied). That product is ∂L/∂w\partial L/\partial w.

Where does it come from? Follow the prerequisites: backpropagation ← chain rule ← derivatives; ← partial derivatives ← gradient ← functions of several variables; ← gradient descent ← optimization; ← neural networks.

Formal definition

For y^=f(z)\hat y = f(z), z=Wx+bz = Wx + b and loss L(y^,y)L(\hat y, y), with δ=∂L∂z=∂L∂y^⊙f′(z)\delta = \frac{\partial L}{\partial z} = \frac{\partial L}{\partial\hat y}\odot f'(z):

∂L∂W=δ x𝖳,∂L∂b=δ,∂L∂x=W𝖳δ.\frac{\partial L}{\partial W} = \delta\,x^{\mathsf T}, \qquad \frac{\partial L}{\partial b} = \delta, \qquad \frac{\partial L}{\partial x} = W^{\mathsf T}\delta.

For a deep network the last formula hands δ\delta to the previous layer: δ(ℓ)=(W(ℓ+1)𝖳δ(ℓ+1))⊙σ′(z(ℓ))\delta^{(\ell)} = \big(W^{(\ell+1)\mathsf T}\delta^{(\ell+1)}\big)\odot\sigma'(z^{(\ell)}).

Formulas

∂L∂W=∂L∂y^⋅∂y^∂z⋅∂z∂W,y^=f(Wx+b)\frac{\partial L}{\partial W} = \frac{\partial L}{\partial\hat y}\cdot\frac{\partial\hat y}{\partial z}\cdot\frac{\partial z}{\partial W}, \qquad \hat y = f(Wx + b)
∂L∂W=δ x𝖳,∂L∂b=δ\frac{\partial L}{\partial W} = \delta\,x^{\mathsf T}, \qquad \frac{\partial L}{\partial b} = \delta
δ(ℓ)=(W(ℓ+1)𝖳 δ(ℓ+1))⊙σ′(z(ℓ))\delta^{(\ell)} = \Big(W^{(\ell+1)\mathsf T}\,\delta^{(\ell+1)}\Big)\odot\sigma'\big(z^{(\ell)}\big)

How is it computed?

forward:  a0 = x;  for ℓ = 1..L:  zℓ = Wℓ aℓ₋₁ + bℓ;  aℓ = σ(zℓ)     # keep every zℓ, aℓ
loss:     L = ℓ(aL, y);  δ = ∂ℓ/∂aL ⊙ σ'(zL)
backward: for ℓ = L..1:
              ∂L/∂Wℓ = δ aℓ₋₁ᵀ ;  ∂L/∂bℓ = δ
              δ = (Wℓᵀ δ) ⊙ σ'(zℓ₋₁)
update:   Wℓ ← Wℓ − η ∂L/∂Wℓ ;  bℓ ← bℓ − η ∂L/∂bℓ

Example

One neuron, y^=σ(wx+b)\hat y = \sigma(wx + b), squared loss L=12(y^−y)2L = \frac12(\hat y - y)^2, with x=1.5x = 1.5, y=1y = 1, w=0.8w = 0.8, b=−0.2b = -0.2. Forward: z=1.0z = 1.0, y^=σ(1)≈0.731\hat y = \sigma(1) \approx 0.731, L≈0.036L \approx 0.036. Backward: ∂L/∂y^=−0.269\partial L/\partial\hat y = -0.269, σ′(z)=0.197\sigma'(z) = 0.197, δ≈−0.053\delta \approx -0.053, so ∂L/∂w=δx≈−0.080\partial L/\partial w = \delta x \approx -0.080, ∂L/∂b≈−0.053\partial L/\partial b \approx -0.053. Both negative: increase ww and bb. Reproduce it step by step in the demo.

Interactive visualization

x
w
b
→
z = wx + b
→
ŷ = f(z)
→
L = ½(ŷ − y)²∂L/∂L = 1

forward value ·gradient ∂L/∂· flowing backwards

The chain rule, evaluated backwards: ∂L/∂w = (∂L/∂ŷ)·(∂ŷ/∂z)·(∂z/∂w). For a layer ŷ = f(Wx + b) the same computation gives ∂L/∂W = δ xᵀ and ∂L/∂b = δ with δ = (ŷ − y) ⊙ f′(z). Try ReLU with a negative z: the gradient is zero and the neuron cannot learn ("dying ReLU").

Why does it matter?

Popularized by Rumelhart, Hinton and Williams (1986), with roots in Linnainmaa's reverse-mode AD (1970) and control theory. It made multilayer networks trainable and, scaled up on GPUs, made deep learning possible. Every loss.backward() call runs it.

The mathematics behind it

  • Chain rule★★★★★fundamental

    Backprop is the chain rule evaluated from the loss backwards, reusing every intermediate product.

  • Partial derivatives★★★★★fundamental

    Backprop computes ∂L/∂w\partial L/\partial w for every weight ww of the network.

  • Multivariable chain rule★★★★★fundamental

    Backprop = the multivariable chain rule evaluated in reverse order on the network's graph.

Where is it used?

Computing topics reachable from here, through the chain of ideas that leads to them:

What depends on it

Exercises

1AI

For y^=σ(wx+b)\hat y = \sigma(wx + b) and L=12(y^−y)2L = \frac12(\hat y - y)^2, derive ∂L/∂w\partial L/\partial w and ∂L/∂b\partial L/\partial b.

Solution

∂L/∂w=(y^−y) y^(1−y^) x\partial L/\partial w = (\hat y - y)\,\hat y(1 - \hat y)\,x and ∂L/∂b=(y^−y) y^(1−y^)\partial L/\partial b = (\hat y - y)\,\hat y(1 - \hat y).

2Computing

Why does backprop need to store the forward activations, and how does gradient checkpointing trade memory for compute?

Solution

The local derivatives (σ′(z(ℓ))\sigma'(z^{(\ell)}), a(ℓ−1)a^{(\ell-1)}) depend on forward values. Checkpointing stores only some layers' activations and recomputes the others during the backward pass: memory drops (to O(L)O(\sqrt L) with optimal placement) at the cost of roughly one extra forward pass.

↑ ↓ to navigate · ↵ · Esc