A1315 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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 x2sin⁡x at x=1, against the step h, on log axes. The flat dashed line is automatic differentiation's error: rounding only, about 10−16, with no step to choose.
  • Even the best step leaves the difference formula around 10−11.

Predict

A number a+bε obeys ordinary arithmetic with one extra rule, ε2=0. Square 2+ε. What do you get?

Name it

Dual numbers

A dual number a+bε has ε2=0. Arithmetic follows:

(a+bε)±(c+dε)=(a±c)+(b±d)ε
(a+bε)(c+dε)=ac+(ad+bc)ε
a+bεc+dε=ac+bc−adc2ε

and an elementary function applies its derivative to the ε part: g(a+bε)=g(a)+g′(a)bε, so sin⁡(a+bε)=sin⁡a+bcos⁡aε.

The product line is the product rule, the quotient line the quotient rule, and the function line the chain rule. Run any program on x+1ε and every intermediate value carries its own derivative; the result is f(x)+f′(x)ε, 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 f:ℝn→ℝm, n runs give the full Jacobian, each costing a small multiple of one evaluation of f.
  • 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, f′(x)≈Imf(x+ih)/h, works for the same reason: i with a tiny h 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 f(x)=x2sin⁡x at x=2 with dual numbers.

Show the solution
  1. Seed the input. Start with x=2+ε.
  2. Square it. x2=4+4ε
  3. Take the sine. sin⁡x=sin⁡2+cos⁡2ε=0.9093−0.4161ε
  4. Multiply. f=4(0.9093)+(4(−0.4161)+4(0.9093))ε=3.6372+1.9726ε
  5. Read off the result. So f(2)=3.6372 and f′(2)=1.9726, matching 2xsin⁡x+x2cos⁡x at 2.
  6. What the computer did. It did exactly the arithmetic of the product rule, one operation at a time, without ever forming the formula.

Answer f(2)=3.6372 and f′(2)=1.9726.

Worked example

A diode (Is=10−14 A, VT = 25.85 mV) and resistor R = 1 kΩ are in series across 5 V. Newton's method on the diode voltage needs f′(v) for f(v)=Is(ev/VT−1)−(5−v)/R. How does forward-mode differentiation supply it?

Show the solution
  1. Evaluate on v+ε. The exponential gives ev/VT+1VTev/VTε
  2. The resistor term. −5−vR−−1Rε
  3. Add the ε parts. The sum's ε part is IsVTev/VT+1R exactly f′(v).
  4. The Newton step. It is then the value part over the ε part.
  5. 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 f on v+ε carries f′(v)=IsVTev/VT+1R in the ε part, and Newton's step is the value part over the ε part.

Worked example

For f(x,y)=xy+sin⁡x at (1,2), find both partial derivatives by reverse mode.

Show the solution
  1. Forward sweep. a=xy=2b=sin⁡x=0.8415f=a+b=2.8415
  2. Backward sweep. Start with f¯=1, so a¯=b¯=1; then x¯=a¯y+b¯cos⁡x=2+0.5403=2.5403y¯=a¯x=1
  3. Compare with forward mode. One backward pass gave both partials; forward mode would have needed two runs, one seeded on x and one on y.

Answer ∂f/∂x=2.5403 and ∂f/∂y=1, both from one backward pass.

Check

What is the ε part of (2+1ε)(5+7ε)?

Check

Feeding x+ε with x = 1 into f(x)=esin⁡x, 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 f(x)=x2sin⁡x1+e−x 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 Is(ev/VT−1)=(Vs−v)/R for the diode voltage with Newton's method, getting f′(v) by evaluating f on a dual number instead of by hand (Is=10−14 A, VT = 0.02585 V, Vs = 5 V, R = 220 Ω). Start from v=0.6 and limit each step to 2VT. 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 f=xy+sin⁡x at x = 1, y = 3, with intermediates a=xy and b=sin⁡x.

(a)

What is ∂f/∂x, collecting the adjoints that flow into x from a and from b?

(b)

What is ∂f/∂y?

Check

Show that dual-number division gives the quotient rule. Multiply top and bottom of a+bεc+dε by c−dε.

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°.

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?

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.