Backpropagation

2 min read

Backpropagation computes the gradient of the loss with respect to every parameter in the network by applying the Chain Rule (Multivariable) on the computation graph.

Forward pass: compute output layer by layer, storing all intermediates.

Backward pass: propagate gradients from loss backward:

LWl=Lhl+1hl+1Wl\frac{\partial L}{\partial W_l} = \frac{\partial L}{\partial \mathbf{h}_{l+1}} \cdot \frac{\partial \mathbf{h}_{l+1}}{\partial W_l}
Backpropagation

Each node receives an upstream gradient and passes down a downstream gradient = upstream × local gradient, where the local gradient is the gradient of the node's output w.r.t. its input. Backprop visits nodes in reverse topological order, seeding the output gradient with L/L=1\partial L/\partial L = 1.

Gate intuitions:

  • ++ distributes the upstream gradient unchanged to each summand
  • max\max routes the upstream gradient to whichever input was largest
  • ×\times swaps the forward inputs as coefficients on the downstream gradient
  • gradients sum at outward branches: if yy feeds both aa and bb, then fy=faay+fbby\frac{\partial f}{\partial y} = \frac{\partial f}{\partial a}\frac{\partial a}{\partial y} + \frac{\partial f}{\partial b}\frac{\partial b}{\partial y}
Gradients

For a 2-layer MLP with h=σ(W1x)\mathbf{h} = \sigma(W_1\mathbf{x}), y^=W2h\hat{y} = W_2\mathbf{h}, L=12y^y2L = \frac{1}{2}\|\hat{y} - y\|^2:

  1. Ly^=y^y\frac{\partial L}{\partial \hat{y}} = \hat{y} - y
  2. LW2=Ly^h\frac{\partial L}{\partial W_2} = \frac{\partial L}{\partial \hat{y}} \cdot \mathbf{h}^\top
  3. Lh=W2Ly^\frac{\partial L}{\partial \mathbf{h}} = W_2^\top \frac{\partial L}{\partial \hat{y}}
  4. LW1=(Lhσ(W1x))x\frac{\partial L}{\partial W_1} = \left(\frac{\partial L}{\partial \mathbf{h}} \odot \sigma'(W_1\mathbf{x})\right) \cdot \mathbf{x}^\top

Vanishing gradients: if σ<1|\sigma'| < 1 consistently (sigmoid), gradients shrink exponentially with depth. Solutions:

Gradient checking: verify a hand-derived gradient against a numerical estimate using the symmetric difference f(x)f(x+h)f(xh)2hf'(x) \approx \frac{f(x+h) - f(x-h)}{2h}.

Activation / gradient checkpointing trades compute for memory: store only a subset of activations (the "checkpoints") and recompute the rest on the fly during the backward pass. For NN layers split into KK segments, memory drops from O(N)O(N) to O(K+N/K)O(K + N/K); the optimum K=NK = \sqrt{N} gives O(N)O(\sqrt{N}) memory at the cost of one extra forward pass (2N\sim 2N backward compute).

In PyTorch: loss.backward() traverses the computation graph and populates .grad for every parameter. optimizer.step() then applies SGD/Adam. Backprop must start from a scalar loss, since L/θ\partial L/\partial \theta is one number per parameter; with mean reduction, by linearity the batch gradient is exactly the mean of per-example gradients.

See also: The Artificial Neuron, Computation Graphs

Linked from