automatic differentiation background
what is automatic differentiation?
automatic differentiation (also known as autodiff, ad, or algorithmic differentiation) is a widely used tool in optimization. the function uses automatic differentiation by default in problem-based optimization for general nonlinear objective functions and constraints; see automatic differentiation in optimization toolbox.
automatic differentiation is a set of techniques for evaluating derivatives (gradients) numerically. the method uses symbolic rules for differentiation, which are more accurate than finite difference approximations. unlike a purely symbolic approach, automatic differentiation evaluates expressions numerically early in the computations, rather than carrying out large symbolic computations. in other words, automatic differentiation evaluates derivatives at particular numeric values; it does not construct symbolic expressions for derivatives.
forward mode automatic differentiation evaluates a numerical derivative by performing elementary derivative operations concurrently with the operations of evaluating the function itself. as detailed in the next section, the software performs these computations on a computational graph.
reverse mode automatic differentiation uses an extension of the forward mode computational graph to enable the computation of a gradient by a reverse traversal of the graph. as the software runs the code to compute the function and its derivative, it records operations in a data structure called a trace.
as many researchers have noted (for example, baydin, pearlmutter, radul, and
siskind [1]), for a scalar
function of many variables, reverse mode calculates the gradient more efficiently
than forward mode. because an objective function is scalar,
automatic differentiation uses reverse mode for scalar optimization. however, for
vector-valued functions such as nonlinear least squares and equation solving,
solve
uses forward mode for some calculations. see automatic differentiation in optimization toolbox.
forward mode
consider the problem of evaluating this function and its gradient:
automatic differentiation works at particular points. in this case, take x1 = 2, x2 = 1/2.
the following computational graph encodes the calculation of the function f(x).
to compute the gradient of f(x) using forward mode, you compute the same graph in the same direction, but modify the computation based on the elementary rules of differentiation. to further simplify the calculation, you fill in the value of the derivative of each subexpression ui as you go. to compute the entire gradient, you must traverse the graph twice, once for the partial derivative with respect to each independent variable. each subexpression in the chain rule has a numeric value, so the entire expression has the same sort of evaluation graph as the function itself.
the computation is a repeated application of the chain rule. in this example, the derivative of f with respect to x1 expands to this expression:
let represent the derivative of the expression ui with respect to x1. using the evaluated values of the ui from the function evaluation, you compute the partial derivative of f with respect to x1 as shown in the following figure. notice that all the values of the become available as you traverse the graph from top to bottom.
to compute the partial derivative with respect to x2, you traverse a similar computational graph. therefore, when you compute the gradient of the function, the number of graph traversals is the same as the number of variables. this process can be slow for many applications, when the objective function or nonlinear constraints depend on many variables.
reverse mode
reverse mode uses one forward traversal of a computational graph to set up the trace. then it computes the entire gradient of the function in one traversal of the graph in the opposite direction. for problems with many variables, this mode is far more efficient.
the theory behind reverse mode is also based on the chain rule, along with associated adjoint variables denoted with an overbar. the adjoint variable for ui is
in terms of the computational graph, each outgoing arrow from a variable contributes to the corresponding adjoint variable by its term in the chain rule. for example, the variable u–1 has outgoing arrows to two variables, u1 and u6. the graph has the associated equation
in this calculation, recalling that and u6 = u5u–1, you obtain
during the forward traversal of the graph, the software calculates the intermediate variables ui. during the reverse traversal, starting from the seed value , the reverse mode computation obtains the adjoint values for all variables. therefore, reverse mode computes the gradient in just one computation, saving a great deal of time compared to forward mode.
the following figure shows the computation of the gradient in reverse mode for the function
again, the computation takes x1 = 2, x2 = 1/2. the reverse mode computation relies on the ui values that are obtained during the computation of the function in the original computational graph. in the right portion of the figure, the computed values of the adjoint variables appear next to the adjoint variable names, using the formulas from the left portion of the figure.
the final gradient values appear as and .
for more details, see baydin, pearlmutter, radul, and siskind [1] or the wikipedia article on automatic differentiation [2].
automatic differentiation in optimization toolbox
automatic differentiation (ad) applies to the and functions under the following conditions:
the objective and constraint functions are supported, as described in . they do not require use of the function.
the solver called by
solve
is , , , or .for optimization problems, the
'objectivederivative'
and'constraintderivative'
name-value pair arguments forsolve
orprob2struct
are set to'auto'
(default),'auto-forward'
, or'auto-reverse'
.for equation problems, the
'equationderivative'
option is set to'auto'
(default),'auto-forward'
, or'auto-reverse'
.
when ad applies | all constraint functions supported | one or more constraints not supported |
---|---|---|
objective function supported | ad used for objective and constraints | ad used for objective only |
objective function not supported | ad used for constraints only | ad not used |
when these conditions are not satisfied, solve
estimates gradients by
finite differences, and prob2struct
does not create gradients in its
generated function files.
solvers choose the following type of ad by default:
for a general nonlinear objective function,
fmincon
defaults to reverse ad for the objective function.fmincon
defaults to reverse ad for the nonlinear constraint function when the number of nonlinear constraints is less than the number of variables. otherwise,fmincon
defaults to forward ad for the nonlinear constraint function.for a general nonlinear objective function,
fminunc
defaults to reverse ad.for a least-squares objective function,
fmincon
andfminunc
default to forward ad for the objective function. for the definition of a problem-based least-squares objective function, see .lsqnonlin
defaults to forward ad when the number of elements in the objective vector is greater than or equal to the number of variables. otherwise,lsqnonlin
defaults to reverse ad.fsolve
defaults to forward ad when the number of equations is greater than or equal to the number of variables. otherwise,fsolve
defaults to reverse ad.
note
to use automatic derivatives in a problem converted by prob2struct
, pass options specifying these derivatives.
options = optimoptions('fmincon','specifyobjectivegradient',true,... 'specifyconstraintgradient',true); problem.options = options;
currently, ad works only for first derivatives; it does not apply to second or higher
derivatives. so, for example, if you want to use an analytic hessian to speed your
optimization, you cannot use solve
directly, and must instead use the
approach described in .
references
[1] baydin, atilim gunes, barak a. pearlmutter, alexey andreyevich radul, and jeffrey mark siskind. "automatic differentiation in machine learning: a survey." the journal of machine learning research, 18(153), 2018, pp. 1–43. available at .
[2] automatic differentiation. wikipedia. available at .
see also
|