Advanced Machine Learning

18: Algorithmic Differentiation

Schedule (you are here )

# date topic description
124-Aug-2026Introduction
226-Aug-2026Foundations of learningDrop/Add
331-Aug-2026PAC learnabilityHW1
402-Sep-2026Linear learning models
507-Sep-2026Labor Day HolidayHoliday
609-Sep-2026Principal Component AnalysisProject ideas
714-Sep-2026Curse of DimensionalityHW2, HW1 due
816-Sep-2026Bayesian Decision Theory
921-Sep-2026Parameter estimation: MLE
1023-Sep-2026Parameter estimation: MAP & NBfinalize teams
1128-Sep-2026Logistic RegressionHW3, HW2 due
1230-Sep-2026Kernel Density Estimation
1305-Oct-2026Support Vector Machines
1407-Oct-2026Matrix Factorization
1512-Oct-2026* MidtermExam, HW3 due
1614-Oct-2026k-means clustering
1719-Oct-2026Invited: Intro to Causal Learning I
# date topic description
1821-Oct-2026Invited: Intro to Causal Learning II
1926-Oct-2026* Mid-point projects checkpointHW4, P
2028-Oct-2026Expectation Maximization
2102-Nov-2026Stochastic Gradient Descent
2204-Nov-2026Automatic Differentiation
2309-Nov-2026Nonlinear embedding approachesHW5, HW4 due
2411-Nov-2026Model comparison & calibration
2516-Nov-2026Convolutional Neural Networks
2618-Nov-2026Word Embedding
2723-Nov-2026Thanksgiving BreakHoliday
2825-Nov-2026Thanksgiving BreakHoliday
2930-Nov-2026* Final ExamExam
3002-Dec-2026Project Final PresentationsHW5 due, P
3107-Dec-2026No classClasses End
3216-Dec-2026Project Reportsdue
3317-Dec-2026Grades due 5 p.m.PAWS

Outline for the lecture

  • Algorithmic differentiation
  • Forward mode AD
  • Backpropagation
  • Reverse mode AD

Algorithmic Differentiation (AD)

".. may be one of the best scientific computing techniques you’ve never heard of."
Alexey Radul
The very first computer science PhD dissertation introduced forward accumulation mode automatic differentiation.
Wengert (1964)
Robert Edwin Wengert. A simple automatic derivative evaluation program. Communications of the ACM 7(8):463–4, Aug 1964.
A procedure for automatic evaluation of total/partial derivatives of arbitrary algebraic functions is presented. The technique permits computation of numerical values of derivatives without developing analytical expressions for the derivatives. The key to the method is the decomposition of the given function, by introduction of intermediate variables, into a series of elementary functional steps. A library of elementary function subroutines is provided for the automatic evaluation and differentiation of these new variables. The final step in this process produces the desired function’s derivative. The main feature of this approach is its simplicity. It can be used as a quick-reaction tool where the derivation of analytical derivatives is laborious and also as a debugging tool for programs which contain derivatives.
Wengert dissertation
R. E. Bellman, H. Kagiwada, and R. E. Kalaba (1965) Wengert’s numerical method for partial derivatives, orbit determination and quasilinearization, Communications of the ACM 8(4):231–2, April 1965, doi:10.1145/363831.364886
In a recent article in the Communications of the ACM, R. Wengert suggested a technique for machine evaluation of the partial derivatives of a function given in analytical form. In solving nonlinear boundary-value problems using quasilinearization many partial derivatives must be formed analytically and then evaluated numerically. Wengert’s method appears very attractive from the programming viewpoint and permits the treatment of large systems of differential equations which might not otherwise be undertaken.
Automatic Differentiation (AD) mechanically calculates the derivatives (Leibnitz, 1664; Newton, 1704) of functions expressed as computer programs, at machine precision, and with complexity guarantees.

Automatic Differentiation

  • Derivative of $f: \RR^n \to \RR^m$ is $m\times n$ "Jacobian matrix" $\bm{J}$.
  • AD in the forward accumulation mode: $\bm{J}\vec{v}$ (Wengert, 1964)
  • AD in the reverse accumulation mode: $\bm{J}^T\vec{v}$ (Speelpenning, 1980)
  • About a zillion other modes and tricks
  • Vibrant field with regular workshops, conferences, updated community portal (http://autodiff.org)

What is AD?

  • Automatic Differentiation $\;\equiv\;$ Algorithmic Differentiation $\;\equiv\;$ Computational Differentiation
AD Type I
A calculus for efficiently calculating derivatives of functions specified by a set of equations.
AD Type II
A way of transforming a computer program implementing a numeric function to also efficiently calculate some derivatives.
AD Type III
A computer program which automatically transforms an input computer program specifying a numeric function into one that also efficiently calculates derivatives.

AD is not Numerical Differentiation

ADnotND
Numerical differentiation is a problematic approximation:
  • thou shalt not add small numbers to big numbers
  • thou shalt not subtract numbers which are approximately equal

What is wrong with Numerical Differentiation

round off and truncation errors

Why Finite Differences Fail in Machine Learning

Taylor expansion of $\frac{f(x+h)-f(x)}{h}$ in finite-precision floating point ($\varepsilon_{\text{mach}} \approx 10^{-16}$):
$\displaystyle \frac{\text{fl}(f(x+h)) - \text{fl}(f(x))}{h} \;=\; f'(x) \;+\; \underbrace{\frac{h}{2}f''(x) + O(h^2)}_{\substack{\text{Truncation Error } O(h)\\ \text{dominates when } h \text{ is large}}} \;+\; \underbrace{\frac{\pm 2\,\varepsilon_{\text{mach}}|f(x)|}{h}}_{\substack{\text{Cancellation Error } O(\varepsilon_{\text{mach}}/h)\\ \text{explodes as } h \to 0}}$
  • 1. Precision Trap: Even at the optimal step $h^* \approx \sqrt{\varepsilon_{\text{mach}}} \approx 10^{-8}$, half of the 16 float64 digits are lost!
  • 2. Complexity Wall: For $f: \RR^n \to \RR$, perturbing each coordinate $x_i + h$ takes $n + 1$ function evaluations ($10^9$ forward passes for a 1B-parameter model!).

Numerical Differentiation: Truncation vs. Cancellation

Slide step size h from 10⁻¹ down to 10⁻¹⁶ to see truncation error give way to catastrophic cancellation.
Exact AD f′(x₀): -- Fwd Err: -- Cen Err: --

AD is not Symbolic Differentiation

ADnotSymbolic
$$\begin{aligned} f(\vec{x}) = \prod_{i=1}^d x_i \implies \nabla f(\vec{x}) &= \left(\frac{\partial f}{\partial x_1}, \frac{\partial f}{\partial x_2}, \dots, \frac{\partial f}{\partial x_d}\right) = \Big(\underbrace{x_2 x_3 \cdots x_d}_{d-1 \text{ mults}}, \;\; \underbrace{x_1 x_3 \cdots x_d}_{d-1 \text{ mults}}, \;\; \dots, \;\; \underbrace{x_1 x_2 \cdots x_{d-1}}_{d-1 \text{ mults}}\Big) \end{aligned}$$

We want exact derivative at a point!

Derivatives

Forward Mode AD

AD is

ADis

AD is

AD

AD graph

$$ y = f(x_1, x_2) = \ln(x_1) + x_1x_2 - \sin(x_2) $$ graph

AD trace

computation graph trace

Forward Mode AD: Primal & Tangent Trace

computation graph
forward

AD: a Jacobian column in a pass

forward
For $f: \RR^n \to \RR^m$, seeding $\dot{\vec{x}} = \vec{e}_j$ computes column $j$ of $\bm{J}_f$ $\implies$ $n$ forward passes for the full Jacobian.

AD: directional derivative (JVP)

directional
Seeding $\dot{\vec{x}} = \vec{r}$ computes the Jacobian-Vector Product (JVP) $\bm{J}_f \vec{r} = \nabla_{\vec{x}} f(\vec{x}) \cdot \vec{r}$ in a single pass!

Dual numbers (1873)

$$\begin{aligned} v + \dot{v}\epsilon, &\qquad v, \dot{v} \in \RR, \quad \epsilon \ne 0, \quad \mathbf{\epsilon^2 = 0} \\ (v + \dot{v}\epsilon) + (u + \dot{u}\epsilon) &= (v + u) + (\dot{v} + \dot{u})\epsilon\\ (v + \dot{v}\epsilon)(u + \dot{u}\epsilon) &= (vu) + (v\dot{u} + u\dot{v})\epsilon + \cancel{\dot{v}\dot{u}\epsilon^2}\\ f(v+\dot{v}\epsilon) &= \underbrace{f(v)}_{\text{primal}} + \underbrace{f^{\prime}(v)\dot{v}}_{\text{tangent}}\epsilon \end{aligned}$$

Forward-Mode AD & Dual Numbers Graph Tracer

Step through the Wengert trace to watch dual numbers vᵢ + v̇ᵢε propagate forward.
Tangent Seed:

Backpropagation

The classical presentation

BP

Why Classical "Backprop" Is Confusing

No "errors" propagate backward! We are just evaluating the linearization (gradient) of a function at a point.
  • 1. Misleading metaphor: Nothing physical flows backward through neurons — it is simply the chain rule $\bm{J}^T \bar{\vec{y}}$ evaluated in reverse order.
  • 2. Scalar index soup: Element-wise chain rules ($\frac{\partial E}{\partial w_{jk}} = \delta_j z_k, \; \delta_j = h'(a_j)\sum_i w_{ij}\delta_i$) bury simple linear algebra under tedious scalar indices.
  • 3. The Reverse-Mode AD cure: Keep variables as matrices & vectors and differentiate at the matrix level!

Reverse Mode AD

In the 1970s, tools for automated generation of adjoint codes (aka reverse accumulation mode automatic differentiation, aka backpropagation) were developed.
Type I
Geniuses transforming mathematical systems (Gauss; Feynman (1939); Rozonoer and Pontryagin (1959))
Type II
Manual transformation of computational processes (Bryson (1962); Werbos (1974); Le Cun (1985); Rumelhart et al. (1986))
Type III
Computer programs transform other computer programs (Speelpenning (1980); LUSH; TAPENADE)
Type IV
First-class AD operators; closure (STALIN$\nabla$; R$^6$RS-AD; AUTOGRAD; DIFFSHARP)
Speelpenning
Bert Speelpenning
Speelpenning
$$y = \log(\sin(x^2))$$
  • Traces:
  • Primal ($z_i$)
  • Tangent Derivative ($\dot{z}_i = \frac{\partial z_i}{\partial x}$)
  • Cotangent / Adjoint ($\bar{z}_i = \frac{\partial y}{\partial z_i}$)
  • Direction:
  • $\rightarrow$ Forward
  • $\rightarrow$ Forward
  • $\leftarrow$ Reverse

Computation Graph

computational graph
  • Following precedence rules
  • binary/n-ary operators allowed $\rightarrow$ DAG (tree)
  • $y =$ root, $x =$ leaves

Intermediate Variables: $z_i$

$$\begin{aligned} x &\\ z_1 &= x^2\\ z_2 &= \sin(z_1)\\ z_3 &= \log(z_2)\\ y &= z_3 \end{aligned}$$

Adjoint: $\bar{z}_i$

How sensitive is the final scalar output $y$ to intermediate variable $z_i$? $\quad \bar{z}_i \;\def\; \frac{\partial y}{\partial z_i}$
$$\begin{aligned} \text{Seed at output: } \quad \bar{y} &= \frac{\partial y}{\partial y} = 1 \\[6pt] \text{Propagate backward: } \quad \bar{z}_i &= \frac{\partial y}{\partial z_i} = \sum_{j \in \text{children}(i)} \underbrace{\bar{z}_j}_{\frac{\partial y}{\partial z_j}} \frac{\partial z_j}{\partial z_i} \end{aligned}$$

Example

$$\begin{aligned} x &\\ z_1 &= x^2\\ z_2 &= \sin(z_1)\\ z_3 &= \log(z_2)\\ y &= z_3 \end{aligned}$$
  • $\bar{x} = \left(\left(\frac{\partial y}{\partial z_3}\frac{\partial z_3}{\partial z_2}\right) \frac{\partial z_2}{\partial z_1}\right)\frac{\partial z_1}{\partial x}$
  • $\bar{x} = \frac{\partial y}{\partial x} = \frac{\partial y}{\partial z_1}\frac{\partial z_1}{\partial x} = \bar{z}_1 2 x$
  • $\bar{z}_1 = \frac{\partial y}{\partial z_1} = \frac{\partial y}{\partial z_2}\frac{\partial z_2}{\partial z_1} = \bar{z}_2\mathrm{cos}(z_1)$
  • $\bar{z}_2 = \frac{\partial y}{\partial z_2} = \frac{\partial y}{\partial z_3}\frac{\partial z_3}{\partial z_2} = \bar{y}\frac{1}{z_2}$
  • $\bar{z}_3 = \frac{\partial y}{\partial z_3} = \frac{\partial y}{\partial y} = \bar{y} = 1$ (seed)
$\displaystyle \bar{x} = \bar{z}_1 2 x$
$\displaystyle \phantom{\bar{x}} = (\bar{z}_2\mathrm{cos}(z_1) ) 2 x$
$\displaystyle \phantom{\bar{x}} = \left(\left(\bar{z}_3 \frac{1}{z_2} \right) \mathrm{cos}(x^2) \right) 2 x$
$\displaystyle \phantom{\bar{x}} = \left(\left( \frac{1}{\mathrm{sin}(z_1)} \right) \mathrm{cos}(x^2) \right) 2 x$
$\displaystyle \phantom{\bar{x}} = \left(\left( \frac{1}{\mathrm{sin}(x^2)} \right) \mathrm{cos}(x^2) \right) 2 x$
$\displaystyle \phantom{\bar{x}} = \left( \mathrm{cot}(x^2) \right) 2 x = 2 x ~\mathrm{tan} \left( \frac{\pi}{2} - x^2\right)$

Reverse-Mode AD: Forward Tape & Backward Adjoints

Step through Phase 1 (Forward Primal Tape) and Phase 2 (Reverse Adjoint Pass).

Reverse-Mode AD in Matrix Form

Every matrix $\bm{Z}_i \in \RR^{r \times c}$ has an adjoint $\bar{\bm{Z}}_i \;\def\; \frac{\partial y}{\partial \bm{Z}_i} \in \RR^{r \times c}$ of the exact same shape! (Silva et al., Appendix III)
$$\begin{aligned} \text{Matrix Product:} \quad \bm{Z} &= \bm{A}\bm{B} &\implies \quad \bar{\bm{A}} &= \bar{\bm{Z}}\bm{B}^T, \qquad \bar{\bm{B}} = \bm{A}^T\bar{\bm{Z}} \\ \text{Hadamard Product:} \quad \bm{Z} &= \bm{A} \odot \bm{B} &\implies \quad \bar{\bm{A}} &= \bar{\bm{Z}} \odot \bm{B}, \qquad \bar{\bm{B}} = \bm{A} \odot \bar{\bm{Z}} \\ \text{Element-wise } \phi: \quad \bm{Z} &= \phi(\bm{A}) &\implies \quad \bar{\bm{A}} &= \bar{\bm{Z}} \odot \phi'(\bm{A}) \quad \left(\text{element-wise } \log(\bm{A}) \implies \bar{\bm{Z}} \oslash \bm{A}\right) \\ \text{Inverse & Log-Det:} \quad \bm{Z} &= \bm{A}^{-1} &\implies \quad \bar{\bm{A}} &= -\bm{A}^{-T}\bar{\bm{Z}}\bm{A}^{-T}; \quad z = \ln|\det\bm{A}| \implies \bar{\bm{A}} = \bar{z}\bm{A}^{-T} \\ \text{Sum & Transpose:} \quad \bm{Z} &= \bm{A} \pm \bm{B} &\implies \quad \bar{\bm{A}} &= \bar{\bm{Z}}, \;\; \bar{\bm{B}} = \pm\bar{\bm{Z}}; \quad \bm{Z} = \bm{A}^T \implies \bar{\bm{A}} = \bar{\bm{Z}}^T \end{aligned}$$
Dimension trick: If $\bm{Z}_{(r\times c)} = \bm{A}_{(r\times k)}\bm{B}_{(k\times c)}$, the only way shapes match is $\bar{\bm{A}}_{(r\times k)} = \bar{\bm{Z}}_{(r\times c)}\bm{B}^T_{(c\times k)}$!

Matrix Reverse-Mode AD: Clean Matrices vs. Scalar Mess

Step backward in Matrix Mode: watch dimensions lock together without a single scalar index!
$\textbf{Differentiating:}\;\; L(\bm{W}_1, \bm{W}_2) = \tfrac{1}{2}\big\|\underbrace{\bm{W}_2}_{(2\times 4)}\sigma(\underbrace{\bm{W}_1}_{(4\times 3)}\underbrace{\vec{x}}_{(3\times 1)}) - \underbrace{\vec{y}}_{(2\times 1)}\big\|_2^2 \;\xrightarrow{\text{Reverse AD}}\; \bar{\bm{W}}_1 = \tfrac{\partial L}{\partial \bm{W}_1} = \big[(\bm{W}_2^T(\bm{W}_2\bm{Z}_2 - \vec{y})) \odot \sigma'(\bm{Z}_1)\big]\vec{x}^T$

Forward JVP vs. Reverse VJP Complexity

Compare column-wise Forward JVP (O(n) passes) vs. row-wise Reverse VJP (O(m) passes).
Presets:
Fwd: -- Rev: -- Speedup: --

References

Take Home Points

  • 1. Exact at Machine Precision: AD is neither finite differences (no truncation/cancellation error) nor naive symbolic differentiation (no expression swell).
  • 2. Forward Mode ($\bm{J}\vec{v}$, Wengert 1964): Propagates dual numbers $v_i + \dot{v}_i\epsilon$ ($\epsilon^2=0$) forward; computes 1 Jacobian column per pass ($O(n)$ passes).
  • 3. Reverse Mode ($\bm{J}^T\bar{\vec{y}}$, Speelpenning 1980): Propagates adjoints $\bar{z}_i = \frac{\partial y}{\partial z_i}$ backward from $\bar{y}=1$; computes the full gradient $\nabla_{\vec{\theta}}\mathcal{L} \in \RR^n$ in 1 pass!
  • 4. Think in Matrices, Not Scalar Backprop: Using matrix adjoint rules ($\bm{Z}=\bm{A}\bm{B} \implies \bar{\bm{A}}=\bar{\bm{Z}}\bm{B}^T, \bar{\bm{B}}=\bm{A}^T\bar{\bm{Z}}$) makes complex gradients effortless.