34 Calculus and Automatic Differentiation
Throughout this book, training a neural network follows the same rhythm: evaluate a loss, ask the framework for its gradient, and nudge every weight a small step downhill. We have relied on that loop without pausing to ask two natural questions about how it works. First, suppose a training run suddenly diverges after we double the learning rate. The gradient itself is still correct, yet the step has carried us past the small neighborhood where a local slope reliably predicts the loss. How far can that linear approximation be trusted? Second, notice how remarkably cheap the gradient is. Estimating every slope by perturbing a hundred million weights one at a time would require a hundred million forward passes, yet a single backward call returns all of them for roughly the cost of a few forward passes. How is that possible?
Both questions belong to calculus, a subject born in the seventeenth century when Newton and Leibniz asked how to measure continuous change and how to add up infinitely many tiny pieces. Their two tools, the derivative and the integral, turn out to be inverses of each other. In deep learning, the derivative supplies the best linear model of a loss surface near our current weights, and aside from the loss value itself, it is the only information an optimizer ever sees. The integral enters as soon as data or weights are random: a probability or an expected loss is an area under a curve, and methods such as policy gradients (Section 14.7) must differentiate that integral with respect to the very distribution they sample from.
Earlier in the book, we stated the basic rules of differentiation (Section 1.4) and treated automatic differentiation as a ready-made tool (Section 1.5). The main chapters require nothing more, so this appendix chapter is optional reading for readers who want to see why the machinery works and where its guarantees come from. Three results do most of the heavy lifting: the descent lemma, which converts a bound on curvature into the safe step-size rule quoted in Section 9.1; the chain rule written as a product of Jacobians, which becomes far cheaper when multiplied from the loss backward to give backpropagation (Section 4.3); and the two complementary estimators for the gradient of an expectation, which trade generality against variance.
The chapter unfolds in three parts. We first build linear and quadratic approximations of a function, in one variable (Section 34.1) and then in many (Section 34.2), so we can bound safe step sizes and tell a minimum from a saddle point. Next, we follow the chain rule from a sum over paths in a computation graph (Section 34.3) to products of layer Jacobians (Section 34.4), and finally to the algorithms that multiply them automatically (Section 34.5). The closing part introduces integration (Section 34.6) and brings sampling and differentiation together to estimate gradients of expectations (Section 34.7).
If you are already comfortable with introductory calculus, feel free to skim the warm-up pages that open several sections. We omit vector calculus beyond the gradient (such as divergence and Stokes’ theorem) as well as measure theory; for differential equations, see Chapter 38.