The bill for a gradient
The topic From linear models to networks built a network as a stack of layers, and with . It left one question open, and it comes with a bill. The small digit classifier 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 both ways, rerun the network and divide:
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 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.