A13 · Lesson 15 · Automatic differentiation
Exact derivatives of any program, without a step size
Lesson 14's finite differences trade truncation against rounding and keep only about half the digits. Writing out derivatives by hand works for a formula but not for a 2000-line device model. Newton's method in a circuit simulator needs the Jacobian of that model at every step, and training a neural network needs the gradient of a loss with millions of inputs. Automatic differentiation gets these derivatives exactly, to rounding error, by running the program itself on numbers that carry their derivatives along. It's how simulators, optimizers and every machine-learning framework compute gradients.
- The curve is the error of a central difference for the derivative of at , against the step , on log axes. The flat dashed line is automatic differentiation's error: rounding only, about , with no step to choose.
- Even the best step leaves the difference formula around .
Predict
A number obeys ordinary arithmetic with one extra rule, . Square . What do you get?
: and the last term vanishes. The part, 4, is the derivative of at 2. That's not a coincidence: feeding into any computation returns .
Name it
Dual numbers
A dual number has . Arithmetic follows:
and an elementary function applies its derivative to the part: , so .
The product line is the product rule, the quotient line the quotient rule, and the function line the chain rule. Run any program on and every intermediate value carries its own derivative; the result is , with no step size and no truncation error.
Forward and reverse mode
- Forward mode (dual numbers) gives the derivative with respect to one input per run. For , runs give the full Jacobian, each costing a small multiple of one evaluation of .
- Reverse mode records the computation, then sweeps backward through it, carrying the derivative of the output with respect to each intermediate value (its adjoint). One backward sweep gives the gradient of one output with respect to every input. For a loss with a million parameters that's the difference between a million runs and one: it's backpropagation.
How it relates to the complex step
Lesson 14's complex step, , works for the same reason: with a tiny behaves almost like . Dual numbers make the "almost" exact and work for any code, including code that isn't analytic.
Worked example
Find the value and derivative of at with dual numbers.
Show the solution
- Seed the input. Start with .
- Square it.
- Take the sine.
- Multiply.
- Read off the result. So and , matching at 2.
- What the computer did. It did exactly the arithmetic of the product rule, one operation at a time, without ever forming the formula.
Answer and .
Worked example
A diode ( A, = 25.85 mV) and resistor = 1 kΩ are in series across 5 V. Newton's method on the diode voltage needs for . How does forward-mode differentiation supply it?
Show the solution
- Evaluate on . The exponential gives
- The resistor term.
- Add the parts. The sum's part is exactly .
- The Newton step. It is then the value part over the part.
- Why simulators use it. A simulator does this for every device model, so a new model needs only its current equation, never a hand-derived conductance.
Answer evaluating on carries in the part, and Newton's step is the value part over the part.
Worked example
For at , find both partial derivatives by reverse mode.
Show the solution
- Forward sweep.
- Backward sweep. Start with , so ; then
- Compare with forward mode. One backward pass gave both partials; forward mode would have needed two runs, one seeded on and one on .
Answer and , both from one backward pass.
Check
What is the part of ?
Check
Feeding with = 1 into , what is the part of the result?
Check
Write a class Dual(a, b) with +, -, *, / (each also with a plain number on either side), and functions dsin, dexp that act on duals. Then set val, der to the value and derivative of at x0 = 1, by evaluating f(Dual(x0, 1)).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Solve for the diode voltage with Newton's method, getting by evaluating on a dual number instead of by hand ( A, = 0.02585 V, Vs = 5 V, R = 220 Ω). Start from and limit each step to . Set v to the result and I to the current.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A function maps 100 inputs to 2 output(s). How many forward-mode passes does its full Jacobian need?
Check
Run reverse mode on at = 1, = 3, with intermediates and .
(a)
What is , collecting the adjoints that flow into from and from ?
(b)
What is ?
Check
Show that dual-number division gives the quotient rule. Multiply top and bottom of by .
Step 1
What does the denominator become?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
What is the part of the quotient?
Check
Why does automatic differentiation have no truncation error, unlike a finite difference?
Explain why
Training a network with a million weights needs the gradient of one loss with respect to all of them. Why is reverse mode the right choice, and what does it cost?
Forward mode gives the derivative with respect to one input per run, so a million inputs would take a million runs. Reverse mode runs the computation once forward, storing the intermediate values, then sweeps backward once, passing each intermediate's sensitivity of the single output to everything that fed it; at the end every input has its partial derivative. The cost is a small multiple of one evaluation of the loss, whatever the number of weights, plus the memory to store the forward pass. For one output and many inputs that's an enormous saving, which is why backpropagation is reverse mode.
Practice
Keep practicing
8 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.