Automatic Differentiation
A computer can get exact derivatives of any program by applying the chain rule to each tiny operation, sweeping either forward from one input or backward from one output. One forward sweep gives the derivative with respect to one input; one backward sweep gives the derivatives with respect to all inputs at once, which is why training uses the backward sweep.
What the symbols mean
- Infinitesimal part
A formal symbol whose square counts as 0; its coefficient carries the derivative.
What the symbols mean
- Adjoint of u
∂L/∂u: how the final output reacts to the intermediate value u.
- Local derivative
Of one operation v with respect to its input u.
What the symbols mean
- Step size
A small nudge; about 10⁻⁵ works well in double precision.
Explanation
Automatic differentiation is simply a way for a computer to get exact derivatives of any program by applying the chain rule to each tiny operation as the program runs. It's "automatic" because you write only the forward calculation and the derivatives come along for free. It's neither of the two methods you might guess: it's not symbolic differentiation (no giant derivative formula ever gets written out), and it's not finite differences (no nudging, no approximation). There are two ways to sweep the chain rule through the computation graph. Forward mode carries a derivative alongside every value, from the inputs toward the output. Reverse mode, which is backpropagation, runs the program first and then carries derivatives backward from the output. They give exactly the same numbers. What differs is the cost, and for training networks the difference is millions to one.
Forward mode: carry a derivative with every value. Choose one input, say . Next to every intermediate value , keep a second number (read "v dot"). Seed it with and for every other input, then update both numbers at each operation: for , ; for , . When the program finishes, the output's dot is .
There's a neat algebraic way to see this. Invent a symbol with and compute with dual numbers . Multiplying, : the product rule appears by itself in the part. In general, the Taylor series stops after two terms because . That's linear approximation made exact. Run the program on and the derivative rides along in the part.
The catch: one forward sweep answers "how does everything depend on one input?" To get the derivative with respect to inputs, you need sweeps.
Reverse mode: carry an adjoint back from the output. Run the program once, recording every operation and value (a list called the tape). Then give every value an adjoint (read "u bar"), seed , and walk the tape backward. Each value's adjoint is the sum, over everything that used it, of that user's adjoint times the local derivative: the second formula, which is exactly the "multiply along, add over paths" rule from Computation Graphs. One reverse sweep answers "how does one output depend on every input?"
Why reverse mode wins for training. For a function with inputs and outputs, the full set of derivatives costs about forward-mode sweeps or about reverse-mode sweeps, each a small multiple of running the program once. Training has one output, the loss, and millions of inputs, the weights. Forward mode would need millions of sweeps; reverse mode needs one (about three forward passes of work, as the last two lessons counted). The price is memory: reverse mode must keep every intermediate value from the forward pass until the backward pass uses it. That's why training a network needs far more memory than just running it, and why big models trade a little compute for memory with checkpointing (store only some layers' values, and recompute the rest during the backward pass). Forward mode is the better choice when there are few inputs and many outputs, like how every output of a simulation responds to a single parameter.
Checking gradients with finite differences. The derivative's definition gives a way to check any gradient code: nudge and measure. The one-sided version has error about . The central version is much better. Expand both sides with Taylor series,
Subtracting cancels the even powers, leaving , and dividing by gives . The error shrinks like . But can't be tiny, either: a computer keeps about 16 significant digits, and subtracting two nearly equal numbers and loses digits, adding an error of roughly . The sweet spot for central differences is around . Compare the two answers with the relative error : about or smaller means your backprop is right, and anything near means a bug. Check a few randomly chosen weights; checking all of them would cost one pass per weight, which is the very thing backprop exists to avoid.
A common mistake. It's tempting to think automatic differentiation is a fast version of finite differences, or some other approximation, because the computer never sees a formula for the derivative. It isn't. Every local derivative it uses is exact (the derivative of really is ), so the result is exact up to ordinary floating-point rounding, as accurate as typing in a hand-derived formula. The finite difference is the approximate one, which is exactly why it's used as the checker and not the method.
See it
Worked examples
Use forward mode to find both partial derivatives of at .
- 1
Break into operations: , , and .
Use reverse mode on the same at .
- 1
Run the forward pass and store everything on the tape: , , , , .
Check the derivative of at with , using both the one-sided and the central difference. How big is each error?
- 1
The true derivative is .
Practice
Type your answer and check it. Solve them all to complete the lesson.
Use dual numbers to find for : compute with and read off the coefficient.
A model has 5 million weights, and one run of the network on a batch takes seconds. How many hours would forward-mode differentiation need to get the full gradient of the loss, at one sweep per weight, each costing about one run? Give 3 significant figures.
For which function is forward-mode differentiation the better choice?
Estimate the derivative of at with the central difference and . Give 4 decimal places.
Your backprop code says a weight's gradient is , but a central-difference check gives . Compute the relative error to 3 decimal places.
(Derivative definition review.) Estimate the derivative of at with the one-sided difference and .
Use one reverse-mode sweep to find for at . Use , and .
Reverse mode has to keep every layer's activations from the forward pass until the backward pass needs them. A 50-layer network produces 1,000,000 activations per layer for one input, each stored in 4 bytes. How many megabytes ( bytes) must be kept for one input?
The hardest one here. Get it, and you've got this lesson.
A central-difference gradient check on at has total error about : the first term is the Taylor error , and the second is rounding, which grows as shrinks. Find the step that minimizes . Give it in millionths ( in units of ), to 3 significant figures.
Where it's used
Every deep learning library is built on reverse-mode automatic differentiation: you write the forward pass, it records the tape, and one call runs the backward sweep for all the weights at once. The same idea is decades older in science and engineering, where it's called the adjoint method. Aircraft designers use it to get the sensitivity of drag to thousands of wing-shape parameters from one extra solve instead of thousands, and weather forecasters use it to adjust millions of starting values of a forecast model to match observations. One output, many inputs: reverse mode wins every time.