Chapter 11Differentiating through optimization
In this chapter, we study how to differentiate through optimization problems, and more generally through nonlinear systems of equations.
11.1 Implicit functions
Implicit functions are functions that do not enjoy an explicit decomposition into elementary functions, for which automatic differentiation, as studied in Chapter 8, can therefore not be directly applied. We describe in this chapter techniques to differentiate through such functions and how to integrate them into an autodiff framework.
Formally, we will denote an implicit function by w⋆(λ), where w⋆:Λ→W. One question is then how to compute the Jacobian ∂w⋆(λ). As a first application one can consider sensitivity analysis of a system. For example, w⋆(λ) could correspond to the equilibrium state of a physical system and in this case, ∂w⋆(λ) would tell us about the sensitivity of the system to some parameters λ∈Λ.
11.1.1 Optimization problems
Another example is a function implicitly defined as the solution (assumed unique) of an optimization problem w⋆(λ):=w∈Wargmaxf(w,λ), where f:W×Λ→R and W denotes a constraint set. Note that we use an argmax for convenience, but the same applies when using an argmin.
11.1.2 Nonlinear equations
More generally, w⋆(λ) can be defined as the root of some function F:W×Λ→W, i.e., w⋆(λ) is implicitly defined as the value satisfying the (potentially nonlinear) system of equations F(w,λ)=0 for all λ∈Λ.
11.1.3 Application to bilevel optimization
Besides sensitivity analysis, another example of application is bilevel optimization. Many times, we want to minimize a function defined as the composition of a fixed function and the solution of an optimization problem. Formally, let f,g:W×Λ→R. We consider the composition h(λ) defined as h(λ):=g(w⋆(λ),λ),wherew⋆(λ):=w∈Wargmaxf(w,λ).(11.1) This includes for instance hyperparameter optimization, where f is an inner log-likelihood objective, g is an outer validation loss, w∈W are model parameters and λ∈Λ are model hyperparameters, such as regularization strength, as illustrated in Fig. 11.1. To minimize h(λ), one generally resorts to a gradient descent scheme w.r.t. λ, which requires computing ∇h(λ). Assuming that w⋆(λ) is differentiable at λ, by the chain rule, we obtain the Jacobian ∂h(λ)=∂1g(w⋆(λ),λ)∂w⋆(λ)+∂2g(w⋆(λ),λ). Using ∂h(λ)⊤=∇h(λ) (see Remark 2.4), we obtain the gradient ∇h(λ)=∂w⋆(λ)⊤∇1g(w⋆(λ),λ)+∇2g(w⋆(λ),λ). The only problematic term is ∂w⋆(λ), as it requires argmax differentiation. Indeed, most of the time, there is no explicit formula for w⋆(λ) and it does not decompose into elementary functions.

Figure 11.1: Hyperparameter optimization in nonlinear regression can be cast as a bi-level optimization problem. Each line corresponds to the estimator obtained by fitting some training data (in blue circles) using a different hyperparameter λ. Formally, denoting f the training objective, the estimators are w⋆(λ):=argminwf(w;λ). The goal is to find the best hyperparameter that fits some validation data (here in cyan diamonds), that is, minimizing h(λ):=g(w⋆(λ),λ), where g is the validation objective. A too small λ1 leads to overfitting the training objective and performs badly on validation objective. Conversely, a larger λ2 underfits both training and validation objectives. The optimal parameter λ⋆ minimizes the validation objective and may be obtained by iterating gradient descent w.r.t. λ. This requires gradients of h(λ)=g(w⋆(λ),λ) w.r.t. λ.
11.2 Envelope theorems
In the special case g=f, the composition h defined in Eq. (11.1) is simply given by h(λ)=f(w⋆(λ),λ)=w∈Wmaxf(w,λ). That is, we no longer need argmax differentiation, but only max differentiation, which, as we shall now see is much easier. The function h is often called a value function (Fleming and Rishel, 2012). The reason for the name “envelope” is illustrated in Fig. 11.2. We emphasize that there is not one, but several envelope theorems, depending on the assumptions on f.

Figure 11.2: The graph of h(λ)=maxw∈Wf(w,λ) is the upper-envelope of the graphs of the functions λ↦f(w,λ) for all w∈W.
11.2.1 Bertsekas’ theorem
The following theorem is due to Bertsekas (1997, Proposition B.25).
Theorem 11.1 (Bertsekas’ theorem)
Let h(λ):=w∈Wmaxf(w,λ) and w⋆(λ):=w∈Wargmaxf(w,λ), where f:W×Λ→R is concave in w and W is a compact convex set. If the maximum w⋆(λ) is unique, then the function h is differentiable with gradient ∇h(λ)=∇2f(w⋆(λ),λ). If furthermore f is convex in λ, then h(λ) is convex.
A variant of the theorem can be stated when W=RP.
Theorem 11.2 (Unbounded case)
Suppose W=RP. If f is strongly concave in w, then the function h is differentiable with gradient ∇h(λ)=∇2f(w⋆(λ),λ). If furthermore f is convex in λ, then h(λ) is convex.
Informally, Theorem 11.1 and Theorem 11.2 mean that we can treat w⋆(λ) as if it were a constant of λ. In other words, we do not need to differentiate through w⋆(λ), even though it depends on λ.
Remark 11.1 (Historical note)
Bertsekas’ theorem or its unbounded variant are often incorrectly cited as Danskin’s theorem in the machine learning literature, because Bertsekas himself called it that way (Bertsekas, 1997, Proposition B.25). However, because the assumptions differ, they should rather be thought as variants or corollaries of Danskin’s original theorem. See Section 11.2.3 for more details.
We now illustrate Theorem 11.2, to differentiate through a minimum, h(λ)=minw∈Wf(w,λ), assuming f(w,λ) is strongly convex in w.
Example 11.1 (Differentiating the minimum of a convex quadratic)
Let us define h(λ):=minw∈Rf(w,λ), where f(w,λ):=2λw2+bw+c and λ>0. The derivative of f w.r.t. w is ∇1f(w,λ)=λw+b. The second derivative of f w.r.t. w is ∇12f(w,λ)=λ. Therefore f is strongly convex in w. Let w⋆(λ) be the minimum of f in w. Setting ∇1f(w,λ) to zero, we get w⋆(λ)=−λb. The derivative of f w.r.t. λ is ∇2f(w,λ)=21w2. From Theorem 11.2, we have h′(λ)=∇2f(w⋆(λ),λ)=21(w⋆(λ))2=21λ2b2. Let us check that the derivative is indeed correct. Plugging w⋆(λ) back into f(w,λ), we get the analytical formula h(λ)=−21λb2+c. Using (λ1)′=−λ21, we indeed obtain the same expression for h′(λ).
Theorem 11.1 has a simple interpretation for functions that are affine in λ as shown below.
Example 11.2 (Convex conjugate)
Let f(w,λ):=⟨w,λ⟩−Ω(w) with Ω convex. We then have h(λ)=maxw∈W ⟨w,λ⟩−Ω(w)=:Ω∗(λ), where Ω∗ denotes the convex conjugate of Ω. Since f is convex and assuming W is convex compact, the conditions of Theorem 11.1 are met. Since ∇2f(w,λ)=w, we obtain ∇h(λ)=∇Ω∗(λ)=w⋆(λ). In other words, in this special case, the gradient of the max is equal to the argmax. This is due to the fact that f(w,λ) is affine in λ.
Another application is saddle point optimization.
Example 11.3 (Saddle point problem)
Consider the saddle point problem minλ∈Λmaxw∈Wf(w,λ). If it is difficult to minimize w.r.t. λ but easy to maximize w.r.t. w, we can rewrite the problem as minλ∈Λh(λ), where h(λ):=maxw∈Wf(w,λ), and use ∇h(λ) to perform (projected) gradient descent w.r.t. λ.
11.2.2 Rockafellar’s theorem
A related theorem (Rockafellar and Wets, 2009, Theorem 10.31) can be proved under different assumptions on f.
Theorem 11.3 (Rockafellar’s envelope theorem)
Let f:W×Λ→R and W be a compact convex set. Let h(λ):=w∈Wmaxf(w,λ) and w⋆(λ):=w∈Wargmaxf(w,λ). If f is continuously differentiable in λ for all w∈W, ∇1f is continuous and the maximum w⋆(λ) is unique, then the function h is differentiable with gradient ∇h(λ)=∇2f(w⋆(λ),λ).
Compared to Bertsekas’ theorem, Rockafellar’s theorem does not require f to be concave in w, but it requires stronger assumptions on the differentiability of f.
11.2.3 Danskin’s theorem
Finally, we state Danskin’s original theorem (Danskin, 1966).
Theorem 11.4 (Danskin’s theorem)
Let W be a compact topological space. Let f:W×Λ→R be continuous and continuously differentiable with respect to λ∈Λ. Let the value function and the set of maximizers be defined as h(λ):=w∈Wmaxf(w,λ)andW⋆(λ):=w∈Wargmaxf(w,λ). Then, the directional derivative of h at λ∈Λ in direction d∈Λ, denoted h′(λ;d), exists and is given by h′(λ;d)=w∈W⋆(λ)max⟨∇2f(w,λ),d⟩.
We sum up envelope theorems and their assumptions in Table 11.1.
Table 11.1: Comparison of different envelope theorems.
| Theorem | Unique w⋆? | Concave in w? | Smooth in w? | Result |
|---|---|---|---|---|
| Danskin | No | No | No | Dir. Deriv. |
| Bertsekas | Yes | Yes | No | Gradient |
| Rockafellar | Yes | No | Yes | Gradient |
11.3 Implicit function theorem
11.3.1 Univariate functions
The implicit function theorem (IFT) provides conditions under which an implicit relationship of the form F(w,λ)=0 can be rewritten as a function w=w⋆(λ) locally, and provides a way to compute its derivative w.r.t. λ.
Theorem 11.5 (Implicit function theorem, univariate case)
Let
F:R×R→R. Assume F(w,λ) is a continuously differentiable function in a neighborhood U of (w0,λ0) such that F(w0,λ0)=0 and ∂1F(w0,λ0)=0. Then there exists a neighborhood V⊆U of (w0,λ0) in which there is a function w⋆(λ) such that
w⋆(λ0)=w0,
F(w⋆(λ),λ)=0 for all λ in the neighborhood V,
∂w⋆(λ)=−∂1F(w⋆(λ),λ)∂2F(w⋆(λ),λ).
We postpone the proof to the multivariate case and begin with a classical example of application of the theorem.

Figure 11.3: The circle equation F(x,y):=x2+y2−1=0 is not a function from y∈[−1,1] to x∈[−1,1], as there are always two possible x values, x=1−y2 or x=−1−y2. However, locally around some point (x0,y0), e.g., such that x0>0 and y0>0 (upper-right quadrant), the function x=x⋆(y)=1−y2 is well-defined. The implicit function theorem gives conditions for such a function to exist locally and provides a way to compute its derivative.
Example 11.4 (Equation of the unit circle)
We use w≡x and λ≡y for clarity. Let F(x,y):=x2+y2−1. In general, we cannot rewrite the unit circle equation F(x,y)=0 as a function from y to x, because for every y∈[−1,1], there are always two possible x values, namely, x=1−y2 or x=−1−y2. However, locally around some point (x0,y0), e.g., such that x0>0 and y0>0 (upper-right quadrant), the function x=x⋆(y)=1−y2 is well-defined. Using ∂1F(x,y)=2x and ∂2F(x,y)=2y, we get ∂x⋆(y)=−∂1F(x⋆(y),y)∂2F(x⋆(y),y)=−2x⋆(y)2y=−1−y2y in that neighborhood (the upper right quadrant in this case). This is indeed the same derivative expression as if we used the chain rule on 1−y2 and is well-defined on y∈[0,1).
In the above simple example, we can easily derive an explicit function relating y to x in a given neighborhood, but this is not always the case. The IFT gives us conditions guaranteeing that such function exists and a way to differentiate it, but not a way to construct such a function. In fact, finding w⋆(λ) such that F(w⋆(λ),λ)=0 typically involves a root finding algorithm, an optimization algorithm, a nonlinear system solver, etc.
Example 11.5 (Polynomial)
Let F(w,λ)=w5+w3+w−λ. According to the Abel-Ruffini theorem (Tignol, 2015), quintics (polynomials of degree 5) do not enjoy roots in terms of radicals and one must resort to numerical root finding. In addition, odd-degree polynomials have real roots. Moreover, ∂1F(w,λ)=5w4+3w2+1 is strictly positive. Therefore, by the intermediate value theorem, there must be only one root w⋆(λ) such that F(w⋆(λ),λ)=0. This unique root can for example be found by bisection. Using the IFT, its derivative is found to be ∂w⋆(λ)=(5w⋆(λ)4+3w⋆(λ)2+1)−1.
While an implicit function is differentiable at a point if the assumptions of the IFT hold in a neighborhood of that point, the reciprocal is not true: failure of the IFT assumptions does not necessarily mean that the implicit function is not differentiable, as we now illustrate.
Example 11.6 (IFT conditions are not necessary for differentiability)
Consider F(w,λ)=(w−λ)2. We clearly have that F(w⋆(λ),λ)=0 if we define w⋆(λ)=λ, the identity function. It is clearly differentiable for all λ, yet the assumptions of the IFT fail, since we have ∂1F(w,λ)=2(w−λ) and therefore ∂1F(0,0)=0.
11.3.2 Multivariate functions
We now present the IFT in the general multivariate setting. Informally, if F(w⋆(λ),λ)=0, then by the chain rule, we have ∂1F(w⋆(λ),λ)∂w⋆(λ)+∂2F(w⋆(λ),λ)=0, meaning that the Jacobian ∂w⋆(λ), assuming that it exists, satisfies −∂1F(w⋆(λ),λ)∂w⋆(λ)=∂2F(w⋆(λ),λ). The IFT gives us conditions for the existence of ∂w⋆(λ).
Theorem 11.6 (Implicit function theorem, multivariate case)
Let us define F:W×Λ→W. Assume F(w,λ) is a continuously differentiable function in a neighborhood of (w0,λ0) such that F(w0,λ0)=0 and ∂1F(w0,λ0) is invertible, i.e., its determinant is nonzero. Then there exists a neighborhood of λ0 in which there is a function w⋆(λ) such that
w⋆(λ0)=w0,
F(w⋆(λ),λ)=0 for all λ in the neighborhood,
−∂1F(w⋆(λ),λ)∂w⋆(λ)=∂2F(w⋆(λ),λ)
⟺∂w⋆(λ)=−∂1F(w⋆(λ),λ)−1∂2F(w⋆(λ),λ).
We begin with a simple unconstrained optimization algorithm.
Example 11.7 (Unconstrained optimization)
Assume we want to differentiate through w⋆(λ)=argminw∈RPf(w,λ), where f is strictly convex in w, which ensures that the solution is unique. From the stationary conditions, if we define F(w,λ):=∇1f(w,λ), then w⋆(λ) is uniquely characterized as the root of F in the first argument, i.e., F(w⋆(λ),λ)=0. We have ∂1F(w,λ)=∇12f(w,λ), the Hessian of f in w, and ∂2F(w,λ)=∂2∇1f(w,λ), the cross derivatives of f in w and λ. Therefore, assuming that the Hessian is well-defined and invertible at (w⋆(λ),λ), we can use the IFT to differentiate through w⋆(λ) and obtain ∂w⋆(λ)=−(∇12f(w⋆(λ),λ))−1∂2∇1f(w⋆(λ),λ).
Next, we generalize the previous example, by allowing constraints in the optimization problem.
Example 11.8 (Constrained optimization)
Now, assume we want to differentiate through w⋆(λ)=argminw∈Cf(w,λ), where f is strictly convex in w and C⊆W is a convex set. A solution is characterized by the fixed point equation w⋆(λ)=PC(w⋆(λ)−η∇1f(w⋆(λ),λ)), for any η>0, where P(y):=argminx∈C∥x−y∥22 is the Euclidean projection of y onto C. Therefore, w⋆(λ) is the root of F(w,λ)=w−PC(w−η∇1f(w,λ)) (see Chapter 16). We can differentiate through w⋆(λ) using the IFT, assuming that the conditions of the theorem apply. Note that ∂1F(w,λ) requires the expression of the Jacobian ∂PC(y). Fortunately, PC(y) and its Jacobian are easy to compute for many sets C (Blondel et al., 2021).
11.3.3 JVP and VJP of implicit functions
To integrate an implicit function w⋆(λ) in an autodiff framework, we need to be able to compute its JVP or VJP. This is the purpose of the next proposition.
Proposition 11.1 (JVP and VJP of implicit functions)
Let w⋆:Λ→W be a function implicitly defined as the solution of F(w⋆(λ),λ)=0, for some function F:W×Λ→W. Define AB:=−∂1F(w⋆(λ),λ):=∂2F(w⋆(λ),λ). Assume the assumptions of the IFT hold. The JVP t:=∂w⋆(λ)v in the input direction v∈Λ is obtained by solving the linear system At=Bv. The VJP ∂w⋆(λ)∗u in the output direction u∈W is obtained by solving the linear system A∗r=u. Using the solution r, we get ∂w⋆(λ)∗u=∂w⋆(λ)∗A∗r=B∗r.
Note that in the above linear systems, we can access A and B as linear maps, the JVPs of F. Their adjoints, A∗ and B∗, correspond to the VJPs of F. To solve these systems, we can therefore use matrix-free solvers as detailed in Section 9.4. For example, when A is symmetric positive semi-definite, we can use the conjugate gradient method (Hestenes et al., 1952). When A is not symmetric positive definite, we can use GMRES (Saad and Schultz, 1986) or BiCGSTAB (Vorst and Vorst, 1992).
11.3.4 Proof of the implicit function theorem
We prove the theorem using the inverse function theorem presented in Theorem 11.7. Define f(λ,w)=(λ,F(w,λ)) which goes from RQ×RP onto RQ×RP. The Jacobian of f is ∂f(λ,w)=(I∂2F(w,λ)0∂1F(w,λ)). So at w0,λ0, we have det(∂f(λ0,w0))=det(I)det(∂1F(w0,λ0))=0 since we assumed ∂1F(w0,λ0) invertible. By the inverse function theorem, the function f is then invertible in a neighborhood N of f(λ0,w0)=(λ0,0). In particular, it is invertible in N∩{(λ,0),λ∈RQ}. The solution of the implicit equation in a neighborhood of λ0 is then (λ,w∗(λ))=f−1(λ,0). By the inverse function theorem, f−1 is continuously differentiable inverse and so is w∗(λ). The derivative ∂w∗(λ) is obtained from the differential of the inverse as (∼∂w∗(λ)∼∼)=∂f−1(λ,0), and by the inverse function theorem, we have ∂f−1(λ,0)=(∂f(λ,w∗(λ)))−1. Using the block matrix inversion formula, we therefore obtain (ACBD)−1=(∼−(D−CA−1B)−1CA−1∼∼), we get the claimed expression. Though we expressed the proof in terms of Jacobians and matrices, the result naturally holds for the corresponding linear operators, JVPs, VJPs, and their inverses.
11.4 Adjoint state method
11.4.1 Differentiating nonlinear equations
We describe in this section the adjoint state method (a.k.a. adjoint method, method of adjoints, adjoint sensitivity method). The method can be used to compute the gradient of the composition of an explicit function and an implicit function, defined through an equality constraint (e.g., a nonlinear equation). The method dates back to Céa (1986).
Suppose a variable s∈S (which corresponds to a state in optimal control) is implicitly defined given some parameters w∈W through the (potentially nonlinear) equation c(s,w)=0, where c:S×W→S. Assuming s is uniquely determined for all w∈W, this defines an implicit function s⋆(w) from W to S such that c(s⋆(w),w)=0. Given an objective function L:S×W→R, the goal of the adjoint state method is then to compute the gradient of L(w):=L(s⋆(w),w). However, this is not trivial as s⋆(w) is an implicit function. For instance, this can be used to convert the equality-constrained problem w∈WminL(s,w)s.t.c(s,w)=0. into the unconstrained problem w∈WminL(s⋆(w),w). Access to ∇L(w) allows us to solve this problem by gradient descent.
Proposition 11.2 (Adjoint state method)
Let c:S×W→S be a mapping defining constraints of the form c(s,w). Assume that for each w∈W, there exists a unique s⋆(w) satisfying c(s⋆(w),w)=0 and that s⋆(w) is differentiable. The gradient of L(w):=L(s⋆(w),w), for some differentiable function L:S×W→R, is given by ∇L(w)=∇2L(s⋆(w),w)+∂2c(s⋆(w),w)∗r⋆(w), where r⋆(w) is the solution of the linear system ∂1c(s⋆(w),w)∗r=−∇1L(s⋆(w),w).
As shown in the proof below, r⋆(w) corresponds to a Lagrange multiplier. The linear system can be solved using matrix-free solvers.
11.4.2 Relation with envelope theorems
Because s is uniquely determined for any w∈W by c(s,w)=0, we can alternatively rewrite L(w) as the trivial minimization or maximization, L(w)=s∈SminL(s,w)s.t.c(s,w)=0=s∈SmaxL(s,w)s.t.c(s,w)=0. Therefore, the adjoint state method can be seen as an envelope theorem for computing ∇L(w), for the case when w is involved in both the objective function and in the equality constraint.
11.4.3 Proof using the method of Lagrange multipliers
Classically, the adjoint state method is derived using the method of Lagrange multipliers. Let us introduce the Lagrangian associated with L and c, L(s,w,r):=L(s,w)+⟨r,c(s,w)⟩, where r∈S is the Lagrange multiplier associated with the equality constraint c(s,w)=0. In the optimal control literature, r is often called the adjoint variable or adjoint state. The gradients of the Lagrangian are ∇sL(s,w,r)∇wL(s,w,r)∇rL(s,w,r)=∇1L(s,w)+∂1c(s,w)∗r=∇2L(s,w)+∂2c(s,w)∗r=c(s,w), where ∂ic(s,w)∗ are the adjoint operators. Setting ∇rL(s,w,r) to zero gives the constraint c(s,w)=0. Setting ∇sL(s,w,r) to zero gives the so-called adjoint state equation ∂1c(s,w)∗r=−∇1L(s,w). Solving this linear system w.r.t. r at s=s⋆(w) gives the adjoint variable r⋆(w). We then get ∇L(w)=∇2L(s⋆(w),w,r⋆(w))=∇2L(s⋆(w),w)+∂2c(s⋆(w),w)∗r⋆(w), which concludes the proof.
11.4.4 Proof using the implicit function theorem
A more direct proof is possible thanks to the implicit function theorem (Section 11.3). Using the chain rule, we get ∇L(w)=∇2L(s⋆(w),w)+∂s⋆(w)∗∇1L(s⋆(w),w), where ∂s⋆(w)∗ is the VJP of s⋆, a linear map from S to W.
Computationally, the main difficulty is to apply ∂s⋆(w)∗ to the vector u=∇1L(s⋆(w),w)∈S. Using the implicit function theorem (Section 11.3) on the implicit function c(s⋆(w),w)=0, and Proposition 11.1, we get the linear system A∗r=u, where A∗:=∂1c(s⋆(w),w)∗ is a linear map from S to S. After solving for r, we get ∂s⋆(w)∗u=B∗r, where B∗:=∂2c(s⋆(w),w)∗ is a linear map from S to W. Putting everything together, we get ∇L(w)=∇2L(s⋆(w),w)+∂2c(s⋆(w),w)∗r.
11.4.5 Reverse mode as adjoint method with backsubstitution
In this section, we revisit reverse-mode autodiff from the perspective of the adjoint state method. For clarity, we focus our exposition on feedforward networks with input x∈X and network weights w=(w1,…,wk)∈W1×…×WK, defined by s0s1sKf(w):=x∈X:=f1(s0,w1)∈S1⋮:=fK(sK−1,wK)∈SK:=sK.(11.2) Here we focus on gradients with respect to the parameters w, hence the notation f(w). We can use the adjoint state method to recover reverse-mode autodiff, and prove its correctness in the process. While we focus for simplicity on feedforward networks, our exposition can be generalized to computation graphs.
Feedforward networks as the solution of a nonlinear equation
While we defined the set of intermediate computations s=(s1,…,sK)∈S1×…×SK as a sequence of operations, they can also be defined as the unique solution of the nonlinear equation c(s,w)=0, where c(s,w):=s1−f1(x,w1)s2−f2(s1,w2)⋮sK−fK(sK−1,wK). This defines an implicit function s⋆(w)=(s1⋆(w),…,sK⋆(w)), the solution of this nonlinear system, which is given by the variables s1,…,sK defined in Eq. (11.2). The output of the feedforward network is then f(w)=sK⋆(w).
In machine learning, the final layer sK⋆(w) is typically fed into a loss ℓ, to define L(w):=ℓ(sK⋆(w);y). Note that an alternative is to write L(w) as L(w)=s∈Sminℓ(s;y)s.t.c(s,w)=0.
More generally, if we just want to compute the VJP of sK⋆(w) in some direction uK∈SK, we can define the scalar-valued function L(w):=ℓ(sK⋆(w);uK):=⟨sK⋆(w),uK⟩ so that ∂f(w)∗uK=∇L(w). Let us define u∈S1×⋯×SK−1×SK as u:=(0,…,0,∇1ℓ(f(w);y)) (gradient of the loss ℓ case) or u:=(0,…,0,uK) (VJP of f in the direction uK case). Using the adjoint state method, we know that the gradient of this objective is obtained as ∇L(w)=∂2c(s(w),w)∗r⋆(w), for r⋆(w) the solution of the linear system ∂1c(s(w),w)∗r=−u.
Solving the linear system using backsubtitution
The JVP of the constraint function c at s⋆(w), materialized as a matrix, takes the form of a block lower-triangular matrix ∂1c(s⋆(w),w)=I−A10⋮00I−A2⋱……⋱I⋱0…⋱⋱−AK0⋮⋮0I, where Ak:=∂1fk(sk−1,wk). Crucially the triangular structure of the JVP stems from the fact that each intermediate activation only depends on the past intermediate activations. Therefore, the constraints, corresponding to the lines of the Jacobian, cannot introduce non-zero values beyond its diagonal. The VJP takes the form of a block upper-triangular matrix ∂1c(s⋆(w),w)∗=I0⋮⋮0−A1∗I⋱…0−A2∗I⋱……⋱⋱⋱00⋮0−AK∗I. Solving an upper triangular system like ∂1c(s(w),w)∗r=u can then be done efficiently by backsubstitution. Starting from the last adjoint state rK=u, we can compute each adjoint state rk from that computed at k+1. Namely, for k∈(K−1,…,1), we have rk−Ak+1∗rk+1=0⟺rk=∂fk+1(sk,wk+1)∗rk+1. The VJPs with respect to the parameters are then obtained by ∂2c(s(w),w)∗r=∂2f1(x,w1)∗r1∂2f2(s1(w),w2)∗r2⋮∂2fK(sK−1(w),wK)∗rK, recovering reverse-mode autodiff.
The Lagrangian perspective of backpropagation for networks with separate parameters w=(w1,…,wK) is well-known; see for instance LeCun (1988) or Recht (2016). The Lagrangian perspective of backpropagation through time (Werbos, 1990) for networks with shared parameter w is discussed for instance by Franceschi et al. (2017). Our exposition uses the adjoint state method, which can itself be proved either using the method of Lagrange multipliers (Section 11.4.3) or by the implicit function theorem (Section 11.4.4), combined with backsubtitution for solving the upper-triangular linear system. Past works often minimize over w but we do not require this, as gradients are not necessarily used for optimization. Our exposition also supports computing the VJP of any vector-valued function f, while existing works derive the gradient of a scalar-valued loss function.
11.5 Inverse function theorem
11.5.1 Differentiating inverse functions
In some cases (see for instance Section 12.4.4), it is useful to compute the Jacobian of an inverse function f−1. The inverse function theorem below allows us to relate the Jacobian of f−1 with the Jacobian of f.
Theorem 11.7 (Inverse function theorem)
Assume f:W→W is continuously differentiable with invertible Jacobian ∂f(w0) at w0. Then f is bijective from a neighborhood of w0 to a neighborhood of f(w0). Moreover, the inverse f−1 is continuously differentiable near ω0=f(w0) and the Jacobian of the inverse ∂f−1(ω) is ∂f(w)∂f−1(ω)=I⇔∂f−1(ω)=(∂f(w))−1, with w=f−1(ω).
11.5.2 Link with the implicit function theorem
The inverse function theorem can be used to prove the implicit function theorem; see proof of Theorem 11.6. Conversely, recall that, in order to use the implicit function theorem, we need to choose a root objective F:W×Λ→W. If we set W=Λ=RQ and F(w,ω)=f(w)−ω, with f:RQ→RQ, then we have that the root w⋆(ω) satisfying F(w⋆(ω),ω)=0 is exactly w⋆(ω)=f−1(ω). Moreover, ∂1F(w,ω)=∂f(w) and ∂2F(w,ω)=−I. By applying the implicit function theorem with this F, we indeed recover the inverse function theorem.
11.5.3 Proof of inverse function theorem
We first give a proof of the formula assuming that f−1 is well-defined and continuously differentiable in a neighborhood of f(w0). In that case, we have for any ω in a neighborhood of f(w0), f∘f−1(ω)=ω. Differentiating both sides w.r.t. ω, we get ∂f(f−1(ω))∂f−1(ω)=I, where I is the identity function in RQ. In particular, for w=f−1(ω) we recover the formula presented in the statement.
Now, it remains to show that invertibility of the JVP ensures that the function is invertible in a neighborhood of f(w0) and that the inverse is continuously differentiable. For that, denote l=∂f(w0) such that l−1 is well-defined by definition. f is invertible with continuously differentiable inverse, if and only if l−1(f(w))−f(w0) is invertible with continuously differentiable inverse. So without loss of generality, we consider ∂f(w0)=I, f(w0)=0, w0=0.
As f is continuously differentiable, there exists a neighborhood N:={w:∥w−w0∥2≤δ} on which we have ∥∂f(w)−I∥2≤1/2. In this neighborhood, the function g(w)=f(w)−w is contractive by the mean value theorem with contraction factor 1/2. For any ω such that ∥ω−f(w0)∥2≤δ/2, the sequence wk+1=wk−f(wk)−ω′ remains in N and converges (since it is a Cauchy sequence by the contraction of g) to a unique fixed point w∞ satisfying w∞=w∞−f(w∞)−ω⟺f(w∞)=ω. This shows the existence of the inverse in the neighborhood M:={ω:∥ω−ω0∥2≤δ/2} of ω0=f(w0) onto N.
We tackle now the differentiability (hence the continuity) of f−1. For any ω in the neighborhood of ω0 with inverse w:=f−1(ω)∈N, the JVP of f at w satisfies by assumption ∥∂f(w)−I∥2≤1/2. Hence, a=∂f(w)−I defines a convergent series b=∑k=0+∞ak and one verifies easily that b=∂f(w)−1, that is ∂f(w) is invertible and ∥∂f(w)−1∥≤2. To compute the JVP of the inverse, we consider then ∂f(w)−1 as the candidate JVP and examine ∥η∥2∥f−1(ω+η)−f(ω)−(∂f(w))−1η∥2. Denote then v such that f−1(ω+η)=w+v. As g(w)=f(w)−w is 1/2-contractive in N, we have ∥v−η∥2=∥g(w+v)−g(w)∥2≤1/2∥v∥2. So ∥v∥2≥∥η∥/2. We then get ∥η∥2∥f−1(ω+η)−f(ω)−(∂f(w))−1η∥2=∥η∥2∥v−(∂f(w))−1(f(w+v)−f(w))∥2≤4∥v∥2∥f(w+v)−f(w)−∂f(w)v∥2 As ∥η∥2→0, we have ∥v∥2→0 and so ∥f(w+v)−f(w)−∂f(w)v∥2/∥v∥2→0. Hence, f−1 is differentiable with JVP ∂f−1(ω)=(∂f(w))−1=(∂f(f−1(ω)))−1. This shows that f−1 is continuous and so ∂f−1(ω) is continuous as a composition of continuous functions.
11.6 Summary
Implicit functions are functions that cannot be decomposed into elementary operations and for which autodiff can therefore not be directly applied. Examples are optimization problems and nonlinear equations.
Envelope theorems can be used for differentiating through the min or max value (not solution) of a function.
More generally, the implicit function theorem allows us to differentiate through implicit functions. It gives conditions for the existence of derivatives and how to obtain them.
The adjoint state method can be used to obtain the gradient of the composition of an explicit function and of an implicit function, specified by equality constraints. It can be used to prove the correctness of reverse-mode autodiff.
The inverse function theorem can be used to differentiate function inverses.
In a sense, the implicit function theorem can be thought of as the mother theorem, as it can be used to prove envelope theorems, the adjoint state method and the inverse function theorem.