ArticleslgStudy

mathematics

Automatic differentiation

Automatic differentiation is a mathematics topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Automatic differentiation rather than just read about it. In short: In mathematics and computer algebra, automatic differentiation (auto-differentiation, autodiff, or AD), also called algorithmic differentiation, computational differentiation, and differentiation arithmetic is a set of techniques to evaluate the partial derivative of a function specified by a computer program. Automatic differentiation enables the simultaneous computation of the numerical values of arbitrarily compl…

Automatic differentiation — main illustration
Automatic differentiation — illustration

Key takeaways

  • Automatic differentiation belongs to mathematics; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Automatic differentiation to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Automatic differentiation from memory before moving on to harder problems.

Reference excerpt

In mathematics and computer algebra, automatic differentiation (auto-differentiation, autodiff, or AD), also called algorithmic differentiation, computational differentiation, and differentiation arithmetic is a set of techniques to evaluate the partial derivative of a function specified by a computer program. Automatic differentiation enables the simultaneous computation of the numerical values of arbitrarily complex functions and their derivatives using only the function itself; no symbolic derivative is required. Auto-differentiation is thus neither numeric nor symbolic, nor is it a combination of both. In contrast to the more traditional numerical methods based on finite differences, auto-differentiation is 'in theory' exact, and in comparison to symbolic algorithms, it is computationally inexpensive. Automatic differentiation exploits the fact that every computer calculation, no matter how complicated, executes a sequence of elementary arithmetic operations (addition, subtraction, multiplication, division, etc.) and elementary functions (exp, log, sin, cos, etc.). By applying the chain rule repeatedly to these operations, partial derivatives of arbitrary order can be computed automatically, accurately to working precision, and using at most a small constant factor of more arithmetic operations than the original program.

Difference from other differentiation methods

Automatic differentiation is distinct from symbolic differentiation and numerical differentiation. Symbolic differentiation faces the difficulty of converting a computer program into a single mathematical expression and can lead to inefficient code. Numerical differentiation (the method of finite differences) can introduce round-off errors in the discretization process and cancellation. These classical methods may have problems with calculating higher derivatives, where complexity and errors increase. Finally, both of these classical methods may be slow at computing partial derivatives of a function with respect to many inputs, as is needed for gradient-based optimization algorithms. Automatic differentiation solves all of these problems.

Applications Because of its efficiency and accuracy in computing first and higher order derivatives, auto-differentiation finds diverse applications in scientific computing and mathematics, and there are numerous computational implementations of auto-differentiation. Among these, one mentions INTLAB, Sollya, and InCLosure. In practice, there are two types (modes) of algorithmic differentiation: a forward-type and a reversed-type. Presently, the two types are highly correlated and complementary and both have a wide variety of applications in, e.g., non-linear optimization, sensitivity analysis, robotics, machine learning, computer graphics, and computer vision. Automatic differentiation is particularly important in the field of machine learning. For example, it allows one to implement backpropagation in a neural network without a manually-computed derivative.

Forward and reverse accumulation

Chain rule of partial derivatives of composite functions Fundamental to automatic differentiation is the decomposition of differentials provided by the chain rule of partial derivatives of composite functions. For the simple composition y=f(g(h(x)))=f(g(h(w0)))=f(g(w1))=f(w2)=w3w0=xw1=h(w0)w2=g(w1)w3=f(w2)=y the chain rule gives ∂y∂x=∂y∂w2∂w2∂w1∂w1∂x=∂f(w2)∂w2∂g(w1)∂w1∂h(w0)∂x

Two types of automatic differentiation Usually, two distinct modes of automatic differentiation are presented.

forward accumulation (also called bottom-up, forward mode, or tangent mode) reverse accumulation (also called top-down, reverse mode, or adjoint mode) Forward accumulation specifies that one traverses the chain rule from inside to outside (that is, first compute ∂w1∂x and then ∂w2∂w1 and lastly ∂y∂w2), while reverse accumulation traverses from outside to inside (first compute ∂y∂w2 and then ∂w2∂w1 and lastly ∂w1∂x). More succinctly,

Forward accumulation computes the recursive relation: ∂wi∂x=∂wi∂wi−1∂wi−1∂xwith w3=y, Reverse accumulation computes the recursive relation: ∂y∂wi=∂y∂wi+1∂wi+1∂wiwith w0=x. The value of the partial derivative, called the seed, is propagated forward or backward and is initially ∂x∂x=1 or ∂y∂y=1. Forward accumulation evaluates the function and calculates the derivative with respect to one independent variable in one pass. For each independent variable x1,x2,…,xn a separate pass is therefore necessary in which the derivative with respect to that independent variable is set to one (∂x1∂x1=1) and of all others to zero (∂x2∂x1=⋯=∂xn∂x1=0). In contrast, reverse accumulation requires the evaluated partial functions for the partial derivatives. Reverse accumulation therefore evaluates the function first and calculates the derivatives with respect to all independent variables in an additional pass. Which of these two types should be used depends on the sweep count. The computational complexity of one sweep is proportional to the complexity of the original code.

Forward accumulation is more efficient than reverse accumulation for functions f:ℝn→ℝm with n≪m as only n sweeps are necessary, compared to m sweeps for reverse accumulation. Reverse accumulation is more efficient than forward accumulation for functions f:ℝn→ℝm with n≫m as only m sweeps are necessary, compared to n sweeps for forward accumulation. Backpropagation of errors in multilayer perceptrons, a technique used in machine learning, is a special case of reverse accumulation. Forward accumulation was introduced by R. E. Wengert in 1964. According to Andreas Griewank, reverse accumulation has been suggested since the late 1960s, but the inventor is unknown. Seppo Linnainmaa published reverse accumulation in 1976.

Forward accumulation

… excerpt ends here. Continue reading the full article.

Illustrations

Automatic differentiation: Forward accumulation
Forward accumulation
Automatic differentiation: Figure 2: Example of forward accumulation with computational graph
Figure 2: Example of forward accumulation with computational graph
Automatic differentiation: Reverse accumulation
Reverse accumulation
Automatic differentiation: Figure 3: Example of reverse accumulation with computational graph
Figure 3: Example of reverse accumulation with computational graph
Automatic differentiation: Figure 4: Example of how source code transformation could work
Figure 4: Example of how source code transformation could work

Worked examples

Example 1 — a first encounter with Automatic differentiation

Start with the simplest possible case. Write down what Automatic differentiation claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In mathematics, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Automatic differentiation before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Automatic differentiation ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Automatic differentiation

In research
Automatic differentiation appears in mathematics research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Automatic differentiation in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Automatic differentiation is common in secondary-school and first-year university syllabi. It links to neighbouring topics Computer algebra, Differential calculus, so understanding it makes those chapters shorter.
In everyday life
Look for Automatic differentiation outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Automatic differentiation” →

How to study Automatic differentiation in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Automatic differentiation means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Automatic differentiation out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Automatic differentiation in simple terms?

In mathematics and computer algebra, automatic differentiation (auto-differentiation, autodiff, or AD), also called algorithmic differentiation, computational differentiation, and differentiation arithmetic is a set of techniques to evaluate the partial derivative of a function specified by a compu…

Why does Automatic differentiation matter?

Because it connects several mathematics ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Automatic differentiation?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Automatic differentiation.

Tags

  • Computer algebra
  • Differential calculus

Keep exploring