Skip to content
BIASED

Backpropagation

Block 1 of 8~2 min

The bill for a gradient

The topic From linear models to networks built a network as a stack of layers, zl=Wlhl−1+blz_l = W_l h_{l-1} + b_l and hl=φ(zl)h_l = \varphi(z_l) with h0=xh_0 = x. It left one question open, and it comes with a bill. The small digit classifier 64→128→64→1064 \to 128 \to 64 \to 10 from that topic already has 17,226 parameters, and gradient descent (see the topic Gradient descent) needs the derivative of the loss with respect to every one of them, at every step.

The baseline is the definition of the derivative. Nudge one parameter θi\theta_i both ways, rerun the network and divide:

∂L∂θi≈L(θ+εei)−L(θ−εei)2ε.\frac{\partial L}{\partial \theta_i} \approx \frac{L(\theta + \varepsilon e_i) - L(\theta - \varepsilon e_i)}{2\varepsilon}.

This costs two forward passes per parameter, and the answer is only approximate. A network with millions of weights would need millions of passes for a single step.

The calculus is already in place. The topic Gradients, Jacobians and Hessians showed that the chain rule is a product of Jacobians, and that multiplying them from the loss backwards gives the gradient of a small network with respect to its input in one sweep. The topic Matrix calculus found the gradient of a linear layer with respect to its weight matrix. Here these pieces become backpropagation: the gradient with respect to every weight of a network, for a whole batch, with a classification loss, for the price of about three forward passes. Then we let PyTorch do it and learn to check a gradient written by hand.

The key idea fits in one sentence. All partial derivatives of LL share the factors that lie between a parameter and the loss, so if we compute from the loss backwards, each shared factor is computed once and reused for everything below it.

Answers are saved on the server and tied to this browser until you sign in.