Jump to content

Automatic differentiation

From Tilcayo Cats, the lean encyclopaedia by Tilcayo Knowledge Systems

Automatic differentiation (AD), also called algorithmic differentiation or autodiff, is a family of techniques for calculating derivatives of functions implemented as computer programs. It combines the known derivatives of elementary operations to obtain derivatives of a larger computation. It is used in optimisation, machine learning and scientific computing; neural-network backpropagation is an application of its reverse mode.[1]

Principle

AD applies the chain rule: if a quantity depends on an intermediate value, its derivative combines the derivative of that intermediate value with the derivative of the next operation. AD follows additions, multiplications and other supported operations without first expanding the entire computation into one formula. Loops and function calls can be handled through the operations they execute.[2]

For example, consider f(x) = x² + 3x. A forward calculation can carry both each intermediate value and its derivative:

Quantity Value at x = 2 Derivative with respect to x
x 2 1
u = x × x 4 x × 1 + 1 × x = 4
v = 3x 6 3
f = u + v 10 4 + 3 = 7

The input derivative is seeded with 1 because x changes one-for-one with itself. Each subsequent step uses the ordinary sum or product rule. This pairing of values and derivatives can be implemented using dual numbers, whose arithmetic carries the derivative alongside the original value.[2]

Other ways to obtain derivatives

Symbolic differentiation transforms an expression into another expression representing its derivative. AD instead evaluates derivative values while preserving the structure and reusable intermediate results of a program. Numerical differentiation estimates derivatives from nearby function evaluations, for example [f(x + h) − f(x)]/h. That estimate depends on the chosen step h and balances truncation against rounding error. AD does not introduce this finite-difference step or its truncation error.[1] Its calculations nevertheless remain subject to floating-point rounding; exact derivative rules do not make computer arithmetic exact.[2]

Forward and reverse modes

For a function with n inputs and m outputs, its Jacobian is the matrix of first partial derivatives. Forward mode propagates an input direction through the computation. If the direction is the vector v, the result is the Jacobian–vector product Jv. Selecting one coordinate direction obtains one column of the Jacobian; repeating this for all input directions can construct the full matrix.[3]

Reverse mode begins with an output weighting and propagates sensitivities backwards through the operations. It computes a vector–Jacobian product, equivalently JTw when vectors are written as columns. For a scalar output, choosing weight 1 yields the gradient with respect to all inputs. Forward mode is consequently attractive when there are few inputs and many outputs; reverse mode is particularly useful for a scalar objective with many parameters. Neither mode needs to construct the full Jacobian when only its product with a vector is wanted.[3]

Graphs, storage and implementation

A computation graph records operations and the dependencies between their values. In PyTorch's reverse-mode implementation, the forward calculation builds the graph and saves intermediate values needed by the backward calculation. The graph is rebuilt for each execution, allowing the executed Python control flow to determine its structure. The backward traversal then combines local derivative contributions along the recorded paths.[4]

Saving intermediates can make reverse-mode differentiation memory-intensive. Gradient checkpointing trades storage for additional computation: selected intermediate values are discarded and recomputed when needed during the backward pass. The placement of checkpoints matters. Recomputing an entire function unnecessarily can increase work without reducing peak memory. Implementations therefore choose which intermediate results to retain and which parts of the computation to repeat.[5]

Applications and limitations

AD supports gradient-based learning and engineering design optimisation, as well as sensitivity calculations in computational fluid dynamics and physical modelling.[1]

A returned gradient is not a guarantee that the mathematical problem is differentiable. At a corner or an undefined operation, a library may apply a documented convention, return a non-finite value or raise an error. PyTorch, for example, specifies rules for subgradients and undefined inputs. Masking an invalid division only after computing it can still leave that operation in the backward graph and produce invalid gradients.[4]

Discrete decisions create another limitation. A threshold can be constant on either side of a jump, producing zero local derivatives away from the discontinuity while having no ordinary derivative at the jump itself. AD does not turn such a decision into a useful smooth optimisation objective. A smooth replacement may provide usable gradients, but it changes the function being modelled.[6]

See also

References

  1. ↑ 1.0 1.1 1.2 Baydin, Atilim Gunes; Pearlmutter, Barak A.; Radul, Alexey Andreyevich; Siskind, Jeffrey Mark (2018). "Automatic Differentiation in Machine Learning: a Survey". Journal of Machine Learning Research. 18 (153): 1–43. Retrieved 2026-10-16.
  2. ↑ 2.0 2.1 2.2 "Lecture 5, Part 3: Forward and Reverse-Mode Automatic Differentiation" (PDF). MIT OpenCourseWare. 2023. Retrieved 2026-10-16.
  3. ↑ 3.0 3.1 "Forward- and reverse-mode autodiff in JAX". JAX documentation. Retrieved 2026-10-16.
  4. ↑ 4.0 4.1 "Autograd mechanics". PyTorch documentation. 2026-08-03. Retrieved 2026-10-16.
  5. ↑ "Gradient checkpointing with jax.checkpoint (jax.remat)". JAX documentation. Retrieved 2026-10-16.
  6. ↑ "Frequently asked questions (FAQ): Why are gradients zero for functions based on sort order?". JAX documentation. Retrieved 2026-10-16.