The Elements of Differentiable Programming
Part I Fundamentals

Chapter 2Differentiation

In this chapter, we review key differentiation concepts. In particular, we emphasize the fundamental role played by linear maps.

2.1 Univariate functions

2.1.1 Derivatives

To study functions, we need to capture their infinitesimal variations around points as defined by the notion of limit.

Definition 2.1 (Limit)

We say that c∈Rc \in \RR is the limit of f ⁣:R→Rf \colon \RR \to \RR as v∈Rv \in \RR approaches w∈Rw \in \RR, denoted lim⁡v→wf(v)=c, \lim_{v \rightarrow w} f(v) = c, if, for any ε>0\varepsilon > 0, there exists R>0R > 0 such that for any v∈Rv \in \RR satisfying 0<∣v−w∣≤R0<|v -w| \leq R, we have ∣f(v)−c∣≤ε|f(v) - c| \leq \varepsilon.

We can also write f(v)→cf(v) \to c as v→wv \to w. Limits are preserved under additions and multiplications. Namely, if lim⁡v→wf(v)=c\lim_{v \rightarrow w} f(v) = c and lim⁡v→wg(v)=d\lim_{v \rightarrow w} g(v) = d, then denoting (af+bg)(w)≔af(w)+bg(w)(af + bg)(w) \coloneqq af(w) + bg(w) for any a,b∈Ra, b \in \RR and (fg)(w)≔f(w)g(w)(fg)(w) \coloneqq f(w) g(w), we have by definition of the limit, lim⁡v→w(af+bg)(v)=ac+bd\lim_{v \rightarrow w} (af + bg)(v) = ac + bd and lim⁡v→w(fg)(v)=cd\lim_{v \rightarrow w} (fg)(v) = cd. The preservation of the limit under addition and multiplication by a scalar is generally referred to as the linearity of the limit, a property that many definitions in the sequel inherit.

With the notion of limit, we can already delineate a class of “well-behaved” functions: functions whose limit at any point equals the value of the function at that point. Functions satisfying this property are called continuous.

Definition 2.2 (Continuous function)

A function f:R→Rf:\RR \rightarrow \RR is continuous at a point w∈Rw \in \RR if lim⁡v→wf(v)=f(w). \lim_{v \rightarrow w} f(v) = f(w). A function ff is said to be continuous if it is continuous at all points in its domain.

Although the notion of continuity appears to be a benign assumption, several functions commonly used in machine learning, such as the Heaviside step function (displayed in the left panel of Fig. 2.2), are not continuous and require special treatment.

Remark 2.1 (Little oo notation)

In the following, we will make use of Landau’s little oo notation. We write g(v)=o(f(v)) as v→w g(v) = o(f(v)) \ \mbox{as} \ v \rightarrow w if lim⁡v→w∣g(v)∣∣f(v)∣=0. \lim_{v\rightarrow w} \frac{|g(v)|}{|f(v)|} = 0. That is, the function ff dominates gg in the limit v→wv \to w. For example, ff is continuous at ww if and only if f(w+δ)=f(w)+o(1) as δ→0. f(w+\delta) = f(w) + o(1) \ \mbox{as} \ \delta \rightarrow 0.

We now explain derivatives. Consider a function f:R→Rf: \RR\rightarrow \RR. As illustrated in Fig. 2.1, its value on an interval [w0,w0+δ][w_0, w_0+\delta] can be approximated by the secant between its values f(w0)f(w_0) and f(w0+δ)f(w_0+\delta), a linear function with slope (f(w0+δ)−f(w0))/δ(f(w_0+\delta) - f(w_0))/\delta. In the limit of an infinitesimal variation δ\delta around w0w_0, the secant converges to the tangent of ff at w0w_0 and the resulting slope defines the derivative of ff at w0w_0. The definition below formalizes this intuition.

Definition 2.3 (Derivative)

The derivative of f:R→Rf:\RR \rightarrow\RR at w∈Rw\in \RR is defined as f′(w)≔lim⁡δ→0f(w+δ)−f(w)δ,(2.1) f'(w) \coloneqq \lim_{\delta\rightarrow 0} \frac{f(w+\delta)- f(w)}{\delta}, \tag{2.1} provided that the limit exists. If f′(w)f'(w) is well-defined at a particular ww, we say that the function ff is differentiable at ww. If ff is differentiable at any w∈Rw\in \RR, we say that it is differentiable everywhere or differentiable for short.

Figure 2.1: A function ff can be locally approximated around a point w0w_0 by a secant, a linear function w↦aw+bw \mapsto aw + b with slope aa and intercept bb, crossing ff at w0w_0 with value u0=f(w0)u_0= f(w_0) and crossing at w0+δw_0 + \delta with value uδ=f(w0+δ)u_\delta=f(w_0+\delta). Using u0=aw0+bu_0 = a w_0 + b and uδ=a(w0+δ)+bu_\delta = a(w_0 + \delta) + b, we find that its slope is a=(f(w0+δ)−f(w0))/δa = (f(w_0 + \delta) - f(w_0)) / \delta and the intercept is b=f(w0)−aw0b=f(w_0) -aw_0. The derivative f′(w)f'(w) of a function ff at a point w0w_0 is then defined as the limit of the slope aa when δ→0\delta \to 0. It is the slope of the tangent of ff at w0w_0. The value f(w)f(w) of the function at ww can then be locally approximated around w0w_0 by w↦f′(w0)w+f(w0)−f′(w0)w0=f(w0)+f′(w0)(w−w0)w \mapsto f'(w_0) w + f(w_0) - f'(w_0) w_0 = f(w_0) + f'(w_0)(w - w_0).

If ff is differentiable at a given ww, then it is necessarily continuous at ww as shown in the following proposition. Non-differentiability of a continuous function at a given point ww is generally illustrated by a kink, as shown in Fig. 2.2.

Proposition 2.1 (Differentiability implies continuity)

If f:R→Rf:\RR \rightarrow \RR is differentiable at w∈Rw\in \RR, then it is continuous at w∈Rw\in \RR.

Proof

In little oo notation, ff is differentiable at ww if there exists f′(w)∈Rf'(w) \in \RR, such that f(w+δ)=f(w)+f′(w)δ+o(δ) as δ→0. f(w+\delta) = f(w) + f'(w)\delta + o(\delta) \ \mbox{as} \ \delta \rightarrow 0. Since f′(w)δ+o(δ)=o(1)f'(w)\delta + o(\delta) = o(1) as δ→0\delta \rightarrow 0, ff is continuous at ww. ◻

Figure 2.2: Illustration of discontinuity and non-differentiability. Left. A discontinuous function, with a jump in function value at 00. Center. A continuous but non-differentiable everywhere function, with kinks at −1-1 and 11. Right. A differentiable everywhere function.

In addition to enabling the construction of a linear approximation of ff in a neighborhood of ww, since it is the slope of the tangent of ff at ww, the derivative f′f' informs us about the monotonicity of ff around ww. If f′(w)f'(w) is positive, the function is increasing around ww. Conversely, if f′(w)f'(w) is negative, the function is decreasing. Such information can be used to develop iterative algorithms such as gradient descent, which seeks to minimize ff by computing iterates of the form wt+1=wt−γf′(wt)w_{t+1}= w_t - \gamma f'(w_t) for γ>0\gamma>0, thereby moving along descent directions of ff around wtw_t.

For several elementary functions such as wnw^n, ewe^w, ln⁡w\ln w, cos⁡w\cos w or sin⁡w\sin w, their derivatives can be obtained directly by applying the definition of the derivative in Eq. (2.1) as we now illustrate.

Example 2.1 (Derivative of power function)

Consider f(w)=wnf(w) = w^n for w∈Rw \in \RR, n∈N∖{0}n\in \N\setminus\{0\}. For any δ∈R\delta \in \RR, we have f(w+δ)−f(w)δ=(w+δ)n−wnδ=∑k=0n(nk)δkwn−k−wnδ=∑k=1n(nk)δk−1wn−k=(n1)wn−1+∑k=2n(nk)δk−1wn−k,\begin{align*} \frac{f(w+\delta) - f(w)}{\delta} & = \frac{(w+\delta)^n - w^n}{\delta} \\ & = \frac{\sum_{k=0}^{n} \binom{n}{k} \delta^k w^{n-k} - w^n}{\delta} \\ & = \sum_{k=1}^{n} \binom{n}{k} \delta^{k-1} w^{n-k} \\ & = \binom{n}{1} w^{n-1} + \sum_{k=2}^{n} \binom{n}{k} \delta^{k-1} w^{n-k}, \end{align*} where, in the second line, we used the binomial theorem. Since (n1)=n \binom{n}{1} = n and lim⁡δ→0∑k=2n(nk)δk−1wn−k=0\lim_{\delta \rightarrow 0} \sum_{k=2}^{n} \binom{n}{k} \delta^{k-1} w^{n-k} =0, we get f′(w)=nwn−1f'(w)= n w^{n-1}.

Remark 2.2 (Functions on a subset U\cU of R\RR)

For simplicity, we presented the definition of the derivative for a function defined on the whole set of real numbers R\RR. If a function f:U→Rf:\cU\rightarrow\RR is defined on a subset U⊆R\cU\subseteq \RR of the real numbers, as it is the case for f(w)=wf(w)=\sqrt w defined on U=R+\cU=\RR_+, the derivative of ff at w∈Uw\in \cU is defined by the limit in Eq. (2.1) provided that the function ff is well defined on a neighborhood of ww, that is, there exists r>0r>0 such that w+δ∈Uw+\delta \in \cU for any ∣δ∣≤r|\delta|\leq r. The function ff is then said to be differentiable everywhere or differentiable for short if it is differentiable at any point ww in the interior of U\cU, the set of points w∈Uw\in \cU such that {w+δ:∣δ∣≤r}⊆U\{w+\delta: |\delta|\leq r\} \subseteq \cU for rr sufficiently small. For points lying at the boundary of U\cU (such as aa and bb if U=[a,b]\cU=[a, b]), one may define the right and left derivatives of ff at aa and bb, meaning that the limit is taken by approaching aa from the right or bb from the left.

2.1.2 Calculus rules

For a given w∈Rw\in \RR and two functions f:R→Rf:\RR\rightarrow\RR and g:R→Rg:\RR\rightarrow \RR, the derivative of elementary operations on ff and gg such as their sums, products or compositions can easily be derived from the definition of the derivative, under appropriate conditions on the differentiability of ff and gg at ww. For example, if the derivatives of ff and gg exist at ww, then the derivatives of their weighted sum or product exist, and satisfy the rules ∀a,b∈R, (af+bg)′(w)=af′(w)+bg′(w)(fg)′(w)=f′(w)g(w)+f(w)g′(w),\begin{align*} \forall a, b \in \RR, \ (af+bg)'(w) & = af'(w) +bg'(w) \tag{Linearity}\\ (fg)'(w) &= f'(w) g(w) + f(w)g'(w), \tag{Product rule} \end{align*} where (fg)(w)≔f(w)g(w)(fg)(w) \coloneqq f(w)g(w). The linearity can be verified directly from the linearity of the limits. For the product rule, in little oo notation, we have, as δ→0\delta\rightarrow 0, (fg)(w+δ)=(f(w)+f′(w)δ+o(δ))(g(w)+g′(w)δ+o(δ))=f(w)g(w)+f′(w)g(w)δ+f(w)g′(w)δ+o(δ),\begin{align*} (fg)(w+\delta) & = (f(w) + f'(w)\delta+ o(\delta))(g(w) + g'(w)\delta + o(\delta)) \\ & = f(w)g(w) + f'(w)g(w)\delta + f(w)g'(w)\delta + o(\delta), \end{align*} hence the result.

If the derivatives of gg at ww and of ff at g(w)g(w) exist, then the derivative of the composition (f∘g)(w)≔f(g(w))(f\circ g)(w) \coloneqq f(g(w)) at ww exists and is given by (f∘g)′(w)=f′(g(w))g′(w).\begin{align*} \hspace{54pt} (f\circ g)'(w) = f'(g(w)) g'(w). \tag{Chain rule} \end{align*} We prove this result more generally in Proposition 2.2. As seen in the sequel, the linearity and the product rule can be seen as byproducts of the chain rule, making the chain rule the cornerstone of differentiation.

Consider a function that can be expressed using sums, products or compositions of elementary functions, such as f(w)≔ewln⁡w+cos⁡w2f(w) \coloneqq e^w\ln w+\cos w^2. Its derivative can be computed by applying the aforementioned rules on the decomposition of ff into elementary operations and functions.

Example 2.2 (Applying rules of differentiation)

Consider f(w)≔ewln⁡w+cos⁡w2f(w) \coloneqq e^w\ln w+\cos w^2. The derivative of ff at w>0w>0 can be computed step by step as follows, denoting sq⁡(w)≔w2\sqfn(w) \coloneqq w^2, f′(w)=(exp⁡⋅ln⁡)′(w)+(cos⁡∘sq⁡)′(w)(exp⁡⋅ln⁡)′(w)=exp⁡′(w)⋅ln⁡(w)+exp⁡(w)⋅ln⁡′(w)(cos⁡∘sq⁡)′(w)=cos⁡′(sq⁡(w))sq⁡′(w)exp⁡′(w)=exp⁡(w),ln⁡′(w)=1/w,sq⁡′(w)=2w,cos⁡′(w)=−sin⁡(w).\begin{align*} \hspace*{-20pt} f'(w) & = (\exp \cdot \ln)'(w) + (\cos \circ \sqfn)'(w) \tag{Linearity}\\ \hspace*{-20pt} (\exp \cdot \ln)'(w) & = \exp'(w) \cdot \ln(w) + \exp(w) \cdot \ln'(w) \tag{Product rule} \\ \hspace*{-20pt} \quad (\cos \circ \sqfn)'(w) & = \cos'(\sqfn(w)) \sqfn'(w) \tag{Chain rule} \\ \hspace*{-20pt} \exp'(w) & = \exp(w), \hspace{30pt} \ln'(w) = 1/w, \tag{Elem. func.} \\ \hspace*{-20pt} \sqfn'(w) & = 2 w, \hspace{44pt} \cos'(w) =- \sin(w). \hspace{-3pt} \tag{Elem. func.} \end{align*} We therefore obtain that f′(w)=ewln⁡w+ew/w−2wsin⁡w2f'(w) = e^w \ln w+ e^w/w - 2w\sin w^2.

Such a process is purely mechanical and lends itself to an automated procedure, which is the main idea of automatic differentiation presented in Chapter 8.

2.1.3 Leibniz’s notation

The notion of derivative was first introduced independently by Newton and Leibniz in the 18th century (Ball, 1960). The latter considered derivatives as the quotient of infinitesimal variations. Namely, denoting u=f(w)u = f(w) a variable depending on ww through ff, Leibniz considered the derivative of ff as the quotient f′=dudwwithf′(w)=dudw∣w f' = \frac{du}{dw} \quad \mbox{with} \quad f'(w) = \left.\frac{du}{dw}\right|_w where dudu and dwdw denote infinitesimal variations of uu and ww respectively and the symbol ∣w|_w denotes the evaluation of the derivative at a given point ww. This notation simplifies the statement of the chain rule first discovered by Leibniz (Rodriguez and Lopez Fernandez, 2010) as we have for v=g(w)v = g(w) and u=f(v)u = f(v) dudw=dudv⋅dvdw. \frac{du}{dw} = \frac{du}{dv} \cdot \frac{dv}{dw}. This hints that derivatives are multiplied when considering compositions. At evaluation, the chain rule in Leibniz notation recovers the formula presented above as dudw∣w=dudv∣g(w)dvdw∣w=f′(g(w))g′(w)=(f∘g)′(w). \left.\frac{du}{dw}\right|_w = \left.\frac{du}{dv}\right|_{g(w)} \left.\frac{dv}{dw}\right|_w = f'(g(w)) g'(w) = (f\circ g)'(w). The ability of Leibniz’s notation to capture the chain rule as a mere product of quotients made it popular throughout the centuries, especially in mechanics (Ball, 1960). The rationale behind Leibniz’s notation, the concept of “infinitesimal variations”, was questioned by later mathematicians for its potential logical issues (Ball, 1960). The notation f′(w)f'(w) first introduced by Euler and further popularized by Lagrange (Cajori, 1993) has then taken over in numerous mathematical textbooks. The concept of infinitesimal variations has been rigorously defined by using the set of hyperreal numbers. They extend the set of real numbers by considering each number as a sum of a non-infinitesimal part and an infinitesimal part (Hewitt, 1948). The formalism of infinitesimal variations further underlies the development of automatic differentiation algorithms through the concept of dual numbers.

2.2 Multivariate functions

2.2.1 Directional derivatives

Let us now consider a function f:RP→Rf:\RR^P \rightarrow \RR with multi-dimensional input w≔(w1,…,wP)∈RP\w \coloneqq (w_1, \ldots, w_P) \in \RR^P. The most important example in machine learning is a function which, to the parameters w∈RP\w \in \RR^P of a neural network, associates a loss value in R\RR. Variations of ff need to be defined along specific directions, such as the variation f(w+δv)−f(w)f(\w+\delta\v) {-}f(\w) of ff around w∈RP\w \in \RR^P in the direction v∈RP\v \in \RR^P by an amount δ>0\delta>0. This consideration naturally leads to the definition of the directional derivative.

Definition 2.4 (Directional derivative)

The directional derivative of ff at w\w in the direction v\v is given by ∂f(w)[v]≔lim⁡δ→0f(w+δv)−f(w)δ, \partial f(\w)[\v] \coloneqq \lim_{\delta \rightarrow 0} \frac{f(\w+ \delta \v) - f(\w)}{\delta}, provided that the limit exists.

We use the notation [v][\v] to emphasize that, for a given input w\w, we can see v↦∂f(w)[v]\v \mapsto \partial f(\w)[\v] as a function. This is essential to define the differentiability of ff (Definition 2.6) at w\w and to later define linear maps (Section 2.3).

One example of directional derivative consists in computing the derivative of a function ff at w\w in any of the canonical directions ei≔(0,…,0,1⏟i,0,…,0). \e_i \coloneqq (0, \ldots, 0, \underbrace{1}_i, 0, \ldots, 0) . This allows us to define the notion of partial derivatives, denoted for i∈[P]i \in [P] ∂if(w)≔∂f(w)[ei]=lim⁡δ→0f(w+δei)−f(w)δ. \partial_i f(\w) \coloneqq \partial f(\w)[\ev_i] = \lim_{\delta \rightarrow 0} \frac{f(\w+ \delta \e_i) - f(\w)}{\delta}. This is also denoted in Leibniz’s notation as ∂if(w)=∂f(w)∂wi\partial_i f(\w) = \frac{\partial f(\w)}{\partial w_i} or ∂if(w)=∂wif(w)\partial_i f(\w) = \partial_{w_i} f(\w). By moving along only the iith coordinate of the function, the partial derivative is akin to differentiating the function ωi↦f(w1,…,ωi,…,wP)\omega_i \mapsto f(w_1, \ldots, \omega_i, \ldots, w_P) around ωi\omega_i, letting all other coordinates fixed at their values wiw_i.

2.2.2 Gradients

We now introduce the gradient vector, which gathers the partial derivatives. We first recall the definitions of linear map and linear form.

Definition 2.5 (Linear map, linear form)

A function l:RP→RMl: \RR^P \rightarrow \RR^M is a linear map if for any a1,a2∈Ra_1, a_2 \in \RR, v1,v2∈RD\v_1, \v_2 \in \RR^D, l[a1v1+a2v2]=a1l[v1]+a2l[v2]. l[a_1 \v_1 + a_2 \v_2] = a_1 l[\v_1] + a_2 l[\v_2]. A linear map with values in R\RR, l:RP→Rl: \RR^P \rightarrow \RR, is called a linear form.

Linearity plays a crucial role in the differentiability of a function.

Definition 2.6 (Differentiability, single-output case)

A function f:RP→Rf: \RR^P \rightarrow \RR is differentiable at w∈RP\w\in \RR^P if its directional derivative is defined along any direction, is linear in any direction v\v, and if lim⁡∥v∥2→0∣f(w+v)−f(w)−∂f(w)[v]∣∥v∥2=0, \lim_{\|\v\|_2 \rightarrow 0} \frac{|f(\w+ \v) - f(\w) - \partial f(\w)[\v]|}{\|\v\|_2} = 0, where ∥v∥2≔⟨v,v⟩\|\v\|_2 \coloneqq \sqrt{\langle \v, \v \rangle}.

We can now introduce the gradient.

Definition 2.7 (Gradient)

The gradient of a differentiable function f:RP→Rf: \RR^P \rightarrow \RR at a point w∈RP\w \in \RR^P is defined as the vector of partial derivatives ∇f(w)≔(∂1f(w)⋮∂Pf(w))=(∂f(w)[e1]⋮∂f(w)[eP])∈RP. \nabla f(\w) \coloneqq \begin{pmatrix} \partial_1 f(\w) \\ \vdots \\ \partial_P f(\w) \end{pmatrix} = \begin{pmatrix} \partial f(\w)[\e_1] \\ \vdots \\ \partial f(\w)[\e_P] \end{pmatrix} \in \RR^P. By linearity, the directional derivative of ff at w\w in the direction v=∑i=1Pviei\v=\sum_{i=1}^P v_i \ev_i is then given by ∂f(w)[v]=∑i=1Pvi∂f(w)[ei]=⟨v,∇f(w)⟩∈R. \hspace{-1ex}\partial f(\w)[\v] = \sum_{i=1}^P v_i \partial f(\w)[\ev_i] = \langle \v, \nabla f(\w)\rangle \in \RR.

Here, ⟨⋅,⋅⟩\langle \cdot, \cdot \rangle denotes the inner product. We provide its definition in Euclidean spaces in Section 2.3.2.

In the definition above, the fact that the gradient can be used to compute the directional derivative is a mere consequence of the linearity of ∂f(w)[v]\partial f(\w)[\v] w.r.t. v\v. However, in more abstract cases presented in later sections, the gradient is defined directly through this property.

As a simple example, any linear function of the form f(w)=⟨a,w⟩=∑i=1Paiwif(\w) = \langle \a, \w \rangle = \sum_{i=1}^P a_i w_i is differentiable as we have (⟨a,w+v⟩−⟨a,w⟩−⟨a,v⟩)/∥v∥2=0(\langle \a, \w+\v \rangle - \langle \a, \w \rangle - \langle \a, \v \rangle)/\|\v\|_2 = 0 for any v\v and in particular for ∥v∥→0\|\v\| \rightarrow 0. Moreover, its gradient is naturally given by ∇f(w)=a\nabla f(\w) = \a.

More generally, to show that a function is differentiable and find its gradient, one approach is to approximate f(w+v)f(\w + \v) around v=0\v = \zeros. If we can find a vector g\g such that f(w+v)=f(w)+⟨g,v⟩+o(∥v∥2), f(\w+\v) = f(\w) + \langle \g, \v\rangle + o(\|\v\|_2), then ff is differentiable at w\w, since ⟨g,⋅⟩\langle \g, \cdot \rangle is linear. Moreover, g\g is then the gradient of ff at w\w.

Remark 2.3 (Gateaux and Fréchet differentiability)

Multiple definitions of differentiability exist. The one presented in Definition 2.6 is that of Fréchet differentiable functions. Alternatively, if f:RP→Rf:\RR^P \rightarrow \RR has well-defined directional derivatives along any direction then the function is Gateaux differentiable. Note that the existence of directional derivatives in any direction is not a sufficient condition for the function to be differentiable. In other words, any Fréchet differentiable function is Gateaux differentiable, but the converse is not true. As a counter-example, one can verify that the function f(x1,x2)≔x13/(x12+x22)f(x_1, x_2) \coloneqq x_1^3/(x_1^2 + x_2^2) is Gateaux differentiable at 00 but not (Fréchet) differentiable at 00 (because the directional derivative at 0 is not linear).

Some authors also require Gateaux differentiable functions to have linear directional derivatives along any direction. These are still not Fréchet differentiable functions. Indeed, the limit in Definition 2.6 is over any vectors tending to 00 (potentially in a pathological way), while directional derivatives look at such limits uniquely in terms of a single direction.

In the remainder of this chapter, all definitions of differentiability are in terms of Fréchet differentiability.

The next example illustrates how to compute the gradient of the logistic loss and validates its differentiability.

Example 2.3 (Gradient of logistic loss)

Consider the logistic loss ℓ(θ,y)≔−⟨y,θ⟩+log⁡∑i=1Meθi\ell(\thetav, \y) \coloneqq - \langle \y, \thetav \rangle + \log \sum_{i=1}^M e^{\theta_i}, that measures the prediction error of the logits θ∈RM\thetav \in \RR^M w.r.t. the correct label y∈{e1,…,eM}\y \in \{\ev_1, \dots, \ev_M\}. Let us compute the gradient of this loss w.r.t. θ\thetav for fixed y\y, i.e., we want to compute the gradient of f(θ)≔ℓ(θ,y)f(\thetav) \coloneqq \ell(\thetav, \y). Let us decompose ff as f=l+logsumexpf = l + \mathrm{logsumexp} with l(θ)≔⟨−y,θ⟩l(\thetav) \coloneqq\langle -\y, \thetav\rangle and logsumexp(θ)≔log⁡∑i=1Mexp⁡(θi), \mathrm{logsumexp}(\thetav) \coloneqq \log \sum_{i=1}^M \exp(\theta_i), the log-sum-exp function. The function ll is linear so differentiable with gradient ∇l(θ)=−y\nabla l(\thetav) = -\y. We therefore focus on logsumexp\mathrm{logsumexp}. Denoting exp⁡(θ)=(exp⁡(θ1),…,exp⁡(θM))\exp(\thetav) = (\exp(\theta_1), \ldots, \exp(\theta_M)), using that exp⁡(x)=1+x+o(x)\exp(x) = 1 + x + o(x), log⁡(1+x)=x+o(x)\log(1+ x) = x + o(x), and denoting ⊙\odot the elementwise product, we get logsumexp(θ+v)=log⁡(⟨exp⁡(θ+v),1⁡⟩)=log⁡(⟨exp⁡(θ)⊙exp⁡(v),1⁡⟩)=log⁡(⟨exp⁡(θ)⊙(1⁡+v+o(∥v∥2)),1⁡⟩)=log⁡(⟨exp⁡(θ),1⁡⟩+⟨exp⁡(θ),v⟩+o(∥v∥2))=log⁡(⟨exp⁡(θ),1⁡⟩)+⟨exp⁡(θ)⟨exp⁡(θ),1⁡⟩,v⟩+o(∥v∥2).\begin{align*} \mathrm{logsumexp}(\thetav + \v) & = \log\left(\langle \exp(\thetav + \v), \ones \rangle\right) \\ & = \log\left(\langle \exp(\thetav) \odot \exp(\v), \ones\rangle\right) \\ & = \log\left(\langle \exp(\thetav) \odot (\ones + \v + o(\|\v\|_2)), \ones\rangle\right) \\ & = \log\left(\langle \exp(\thetav), \ones\rangle + \langle \exp(\thetav), \v \rangle + o(\|\v\|_2)\right)\\ & = \log\left(\langle \exp(\thetav), \ones\rangle \right) + \left\langle \frac{\exp(\thetav)}{\langle \exp(\thetav), \ones\rangle} , \v\right\rangle + o(\|\v\|_2). \end{align*} The above decomposition of logsumexp(θ+v)\mathrm{logsumexp}(\thetav + \v) shows that it is differentiable, and that ∇logsumexp(θ)=softargmax(θ)\nabla \mathrm{logsumexp}(\thetav) = \softargmax(\thetav), where softargmax(θ)≔(eθ1/(∑j=1Meθj),…,eθM/(∑j=1Meθj)).\begin{align*} \softargmax(\thetav) & \coloneqq \left(e^{\theta_1}/\left(\sum_{j=1}^{M}e^{\theta_j}\right), \ldots, e^{\theta_M}/\left(\sum_{j=1}^M e^{\theta_j}\right)\right). \end{align*} Overall, we get that ∇f(θ)=−y+softargmax(θ)\nabla f(\thetav) = -\y + \mathrm{softargmax}(\thetav).

Linearity of gradients

The notion of differentiability for multi-input functions naturally inherits from the linearity of derivatives for single-input functions. For any u1,…,uM∈Ru_1, \ldots, u_M \in \RR and any multi-input functions f1,…,fMf_1, \ldots, f_M differentiable at w\w, the function u1f1+…+uMfMu_1 f_1 + \ldots + u_M f_M is differentiable at w\w and its gradient is ∇(u1f1+…+uMfM)(w)=u1∇f1(w)+…+uM∇fM(w). \nabla (u_1 f_1 + \ldots + u_M f_M)(\w) = u_1 \nabla f_1(\w) + \ldots + u_M \nabla f_M(\w).

Why is the gradient useful?

When ff is differentiable, we say that v\v is an ascent direction of ff from w\w if ⟨v,∇f(w)⟩>0. \langle \v, \nabla f(\w) \rangle > 0. Conversely, we say that v\v is a descent direction of ff from w\w if ⟨v,∇f(w)⟩<0. \langle \v, \nabla f(\w) \rangle < 0. Using this definition, the gradient leads to the steepest ascent direction of ff from w\w. To see why, we note that arg max⁡v∈RP,∥v∥2≤1⟨v,∇f(w)⟩=arg max⁡v∈RP,∥v∥2≤1∂f(w)[v]=∇f(w)/∥∇f(w)∥2, \begin{aligned} \argmax_{\v\in \RR^P, \|\v\|_2\leq 1} \langle \v, \nabla f(\w) \rangle &= \argmax_{\v\in \RR^P, \|\v\|_2\leq 1} \partial f(\w)[\v] \\ &= \nabla f(\w)/\|\nabla f(\w)\|_2, \end{aligned} where we assumed ∇f(w)≠0\nabla f(\w) \neq \mathbf{0}. The gradient ∇f(w)\nabla f(\w) is orthogonal to the level set of the function (the set of points w\w sharing the same value f(w)f(\w)) and points towards higher values of ff, as illustrated in Fig. 2.3. Conversely, the negative gradient −∇f(w)-\nabla f(\w) points towards lower values of ff. This observation motivates the development of optimization algorithms such as gradient descent. It is based on iteratively performing the update wt+1≔wt−γ∇f(wt)\w_{t+1} \coloneqq \w_t -\gamma \nabla f(\w_t), for γ>0\gamma>0. It therefore seeks for a minimizer of ff by moving along the steepest descent direction around wt\w_t given, up to a multiplicative factor, by −∇f(wt)-\nabla f(\w_t). See also Definition 17.1 for more details.

Figure 2.3: The gradient of a function f:R2→Rf: \RR^2\rightarrow \RR at (w1,w2)(w_1, w_2) is the normal vector to the tangent space of the level set Lf(w1,w2)={(w1′,w2′):f(w1′,w2′)=f(w1,w2)}L_{f(w_1, w_2)} = \{(w_1', w_2'): f(w_1', w_2') = f(w_1, w_2)\} and points towards points with higher function values.

Figure 2.4: The directional derivative of a parametric curve f:R→R2f:\RR \rightarrow\RR^2 at ww is the tangent to the curve at the point f(w)∈R2f(w)\in \RR^2.

2.2.3 Jacobians

Let us now consider a multi-output function f:RP→RMf:\RR^P \rightarrow \RR^M defined by f(w)≔(f1(w),…,fM(w))f(\w) \coloneqq (f_1(\w), \dots, f_M(\w)), where fj ⁣:RP→Rf_j \colon \RR^P \to \RR. A typical example in machine learning is a neural network. The notion of directional derivative can be extended to such function by defining it as the vector composed of the coordinate-wise directional derivatives: ∂f(w)[v]≔lim⁡δ→0f(w+δv)−f(w)δ=lim⁡δ→0(f1(w+δv)−f1(w)δ⋮fM(w+δv)−fM(w)δ)∈RM, \partial f(\w)[\v] \coloneqq \lim_{\delta \rightarrow 0} \frac{f(\w+ \delta \v) - f(\w)}{\delta} = \lim_{\delta \rightarrow 0}\begin{pmatrix} \frac{f_1(\w+ \delta \v) - f_1(\w)}{\delta}\\ \vdots\\ \frac{f_M(\w+ \delta \v) - f_M(\w)}{\delta} \end{pmatrix} \in \RR^M, where the limits (provided that they exist) are applied coordinate-wise. The directional derivative of ff in the direction v∈RP\v \in \RR^P is therefore the vector that gathers the directional derivative of each fjf_j, i.e., ∂f(w)[v]=(∂fj(w)[v])j=1M\partial f(\w)[\v] = (\partial f_j(\w)[\v])_{j=1}^M. In particular, we can define the partial derivatives of ff at w\w as the vectors ∂if(w)≔∂f(w)[ei]=(∂if1(w)⋮∂ifM(w))∈RM. \partial_i f(\w) \coloneqq \partial f(\w)[\ev_i] = \begin{pmatrix} \partial_i f_1(\w) \\ \vdots \\ \partial_i f_M(\w) \end{pmatrix}\in \RR^M. As for the usual definition of the derivative, the directional derivative can provide a linear approximation of a function around a current input, as illustrated in Fig. 2.4 for a parametric curve f:R→R2f:\RR \rightarrow \RR^2.

Just as in the single-output case, differentiability is defined not only as the existence of directional derivatives in any direction but also by the linearity in the chosen direction.

Definition 2.8 (Differentiability, multi-output case)

A function f:RP→RMf:\RR^P\rightarrow \RR^M is (Fréchet) differentiable at a point w∈RP\w \in \RR^P if its directional derivative is defined along any direction, is linear in any direction, and, lim⁡∥v∥2→0∥f(w+v)−f(w)−∂f(w)[v]∥2∥v∥2=0. \lim_{\|\v\|_2 \rightarrow 0} \frac{\|f(\w+ \v) - f(\w) - \partial f(\w)[\v]\|_2}{\|\v\|_2} = 0.

The partial derivatives of each coordinate’s function are gathered in the Jacobian matrix.

Definition 2.9 (Jacobian)

The Jacobian of a differentiable function f:RP→RMf:\RR^P\rightarrow \RR^M at w\w is defined as the matrix gathering partial derivatives of each coordinate’s function provided they exist, ∂f(w)≔(∂1f1(w)…∂Pf1(w)⋮⋱⋮∂1fM(w)…∂PfM(w))∈RM×P.\begin{align*} \jac f (\w) & \coloneqq \begin{pmatrix} \partial_1 f_1(\w) & \ldots & \partial_P f_1(\w) \\ \vdots & \ddots & \vdots \\ \partial_1 f_M(\w) & \ldots & \partial_P f_M(\w) \end{pmatrix} \in \RR^{M \times P}. \end{align*} The Jacobian can be represented by stacking columns of partial derivatives or rows of gradients, ∂f(w)=(∂1f(w),…,∂Pf(w))=(∇f1(w)⊤⋮∇fM(w)⊤)∈RM×P.\begin{align*} \jac f (\w) & = \begin{pmatrix} \partial_1 f(\w), \ldots, \partial_P f(\w) \end{pmatrix} = \begin{pmatrix} \nabla f_1(\w)^\top \\ \vdots \\ \nabla f_M(\w)^\top \end{pmatrix} \in \RR^{M \times P}. \end{align*} By linearity, the directional derivative of ff at w\w along any input direction v=∑i=1Pviei∈RP\v= \sum_{i=1}^P v_i\ev_i \in \RR^P is then given by ∂f(w)[v]=∑i=1Pvi∂if(w)=∂f(w)v∈RM. \partial f(\w)[\v] = \sum_{i=1}^P v_i \partial_i f(\w) = \jac f(\w) \v \in \RR^M.

Notice that we use bold ∂\jac to indicate the Jacobian, seen as a matrix. The Jacobian matrix naturally generalizes the concepts of derivatives and gradients presented earlier. As for the single input case, to show that a function is differentiable, one approach is to approximate f(w+v)f(\w+ \v) around v=0\v=\zeros. If we find a linear map ll such that f(w+v)=f(w)+l[v]+o(∥v∥2), f(\w+\v) = f(\w) + l[\v] + o(\|\v\|_2), then ff is differentiable at w\w. Moreover, if ll is represented by matrix J\J such that l[v]=Jvl[\v] = \J\v then J=∂f(w)\J = \jac f(\w).

As a simple example, any linear function f(w)=Awf(\w) = \A\w for A∈RM×P\A \in \RR^{M\times P} is differentiable, since all its coordinate-wise components are single-output linear functions, and the Jacobian of ff at any w\w is given by ∂f(w)=A\jac f(\w) = \A.

Remark 2.4 (Special cases of the Jacobian)

For single-output functions f:RP→Rf:\RR^P \rightarrow\RR, i.e., M=1M=1, the Jacobian matrix reduces to a row vector identified as the transpose of the gradient, ∂f(w)=∇f(w)⊤∈R1×P. \jac f(\w) = \nabla f(\w)^\top \in \RR^{1\times P}. For a single-input function f:R→RMf: \RR\rightarrow \RR^M, the Jacobian reduces to a single column vector of directional derivatives, denoted ∂f(w)=f′(w)≔(f1′(w)⋮fM′(w))∈RM×1. \jac f(w) = f'(w) \coloneqq \begin{pmatrix} f_1'(w) \\ \vdots \\ f_M'(w) \end{pmatrix} \in \RR^{M\times 1}. For a single-input single-output function f:R→Rf:\RR\rightarrow\RR, the Jacobian reduces to the derivative of ff, i.e., ∂f(w)=f′(w)∈R. \jac f(w) = f'(w) \in \RR.

The next example illustrates the form of the Jacobian matrix for the element-wise application of a differentiable function σ\sigma, such as the softplus activation (see Section 4.4 for a review of activation functions). In this case, the Jacobian takes a simple diagonal matrix form. As a consequence, the directional derivative associated with this function is simply given by an element-wise product: a full matrix-vector product is not needed, as would suggest Definition 2.9. We will revisit this point in Section 2.3.

Example 2.4 (Jacobian matrix of the softplus activation)

Consider the element-wise application of the softplus defined for w∈RP\w \in \RR^P by f(w)≔(σ(w1)⋮σ(wP))∈RPwhereσ(w)≔log⁡(1+ew). f(\w) \coloneqq \begin{pmatrix} \sigma(w_1) \\ \vdots \\ \sigma(w_P) \end{pmatrix}\in \RR^P \quad \mbox{where} \quad \sigma(w) \coloneqq \log(1+e^w). Since σ\sigma is differentiable, each coordinate of this function is differentiable and the overall function is differentiable. The jjth coordinate of ff is independent of the iith coordinate of w\w for i≠ji\neq j, so ∂ifj(w)=0\partial_i f_j(\w) = 0 for i≠ji\neq j. For i=ji=j, the result boils down to the derivative of σ\sigma at wjw_j. That is, ∂jfj(w)=σ′(wj)\partial_j f_j(\w) = \sigma'(w_j), where σ′(w)=ew/(1+ew)\sigma'(w) = e^{w}/(1+e^w). The Jacobian of ff is therefore a diagonal matrix ∂f(w)=diag⁡(σ′(w1),…,σ′(wP))≔(σ′(w1)0…00⋱⋱⋮⋮⋱⋱00…0σ′(wP)). \jac f(\w) = \diag(\sigma'(w_1), \dots, \sigma'(w_P)) \coloneqq \begin{pmatrix} \sigma'(w_1) & 0 & \ldots & 0 \\ 0 & \ddots & \ddots & \vdots \\ \vdots & \ddots & \ddots & 0 \\ 0 & \ldots & 0 & \sigma'(w_P) \end{pmatrix}.

Chain rule

Equipped with a generic definition of differentiability and the associated objects, gradients and Jacobians, we can now generalize the chain rule, previously introduced for single-input single-output functions.

Proposition 2.2 (Chain rule)

Consider f:RP→RMf:\RR^P \rightarrow\RR^M and g:RM→RRg:\RR^M \rightarrow \RR^R. If ff is differentiable at w∈RP\w \in \RR^P and gg is differentiable at f(w)∈RMf(\w) \in \RR^M, then the composition g∘f ⁣:RP→RRg \circ f \colon \RR^P \to \RR^R is differentiable at w∈RP\w \in \RR^P and its Jacobian is given by ∂(g∘f)(w)⏟R×P=∂g(f(w))⏟R×M∂f(w)⏟M×P. \underbrace{\jac (g\circ f)(\w)}_{R \times P} = \underbrace{\jac g(f(\w))}_{R \times M} \underbrace{\jac f(\w)}_{M \times P}.

Proof

We progressively approximate g∘f(w+v)g\circ f(\w +\v) using the differentiability of ff at w\w and gg at f(w)f(\w), g(f(w+v))=g(f(w)+∂f(w)v+o(∥v∥))=g(f(w))+∂g(f(w))∂f(w)v+o(∥v∥).\begin{align*} g(f(\w+\v)) & = g(f(\w) + \jac f(\w)\v + o(\|\v\|)) \\ & = g(f(\w)) + \jac g(f(\w)) \jac f(\w)\v + o(\|\v\|). \end{align*} Hence, g∘fg\circ f is differentiable at w\w with Jacobian ∂g(f(w))∂f(w)\jac g(f(\w)) \jac f(\w). ◻

Proposition 2.2 can be seen as the cornerstone of any derivative computations. For example, it can be used to rederive the linearity and product rules associated to the derivatives of single-input single-output functions.

When gg is scalar-valued, combined with Remark 2.4, we obtain a simple expression for ∇(g∘f)\nabla (g \circ f).

Proposition 2.3 (Chain rule, scalar-valued case)

Consider f:RP→RMf:\RR^P \rightarrow\RR^M and g:RM→Rg:\RR^M \rightarrow \RR. The gradient of the composition is given by ∇(g∘f)(w)=∂f(w)⊤∇g(f(w)). \nabla (g\circ f)(\w) = \jac f(\w)^\top \nabla g(f(\w)).

This is a very useful identity in machine learning, as we often need to compose a vector-valued model function and a scalar-valued loss function. We illustrate this with linear regression below.

Example 2.5 (Linear regression)

Consider a linear regression of NN inputs x1,…,xN∈RD\x_1, \ldots, \x_N \in \RR^D onto NN targets y1,…,yN∈Ry_1, \ldots, y_N\in \RR, using a parameter vector w∈RD\w \in \RR^{D}. The loss is defined as the sum of squared residuals, L(w)≔∥Xw−y∥22=∑i=1N(⟨xi,w⟩−yi)2L(\w) \coloneqq \|\X \w -\y\|_2^2 = \sum_{i=1}^N (\langle \x_i, \w \rangle -y_i)^2 where X≔(x1,…,xN)⊤∈RN×D\X \coloneqq (\x_1, \ldots, \x_N)^\top \in \RR^{N\times D} and y≔(y1,…,yN)⊤∈RN\y \coloneqq (y_1, \ldots, y_N)^\top \in \R^N.

The function LL can be decomposed into a linear mapping f(w)≔Xwf(\w) \coloneqq \X\w and a squared error ℓ(p)≔∥p−y∥22\ell(\p) \coloneqq \|\p- \yv\|_2^2, so that L=ℓ∘fL= \ell \circ f. We can then apply the chain rule in Proposition 2.3 to get ∇L(w)=∂f(w)⊤∇ℓ(f(w)) \nabla L(\w) = \jac f(\w)^\top \nabla \ell(f(\w)) provided that ff and ℓ\ell are differentiable at w\w and f(w)f(\w), respectively.

The function ff is linear so differentiable with Jacobian ∂f(w)=X\jac f(\w) = \X. On the other hand the partial derivatives of ℓ\ell are given by ∂jℓ(p)=2(pj−yj)\partial_j \ell(\p)= 2(p_j - y_j) for j∈{1,…,N}j \in \{1, \ldots, N\}. Therefore, ℓ\ell is differentiable at any p\p and its gradient is ∇ℓ(p)=2(p−y)\nabla \ell(\p) = 2(\p - \yv). By combining the two, we then get the gradient of LL as ∇L(w)=2X⊤(f(w)−y)=2X⊤(Xw−y). \nabla L(\w) = 2\X^\top (f(\w) - \y) = 2\X^\top(\X \w -\y).

2.3 Linear maps

The Jacobian matrix is useful as a representation of the partial derivatives. However, the core idea underlying the definition of differentiable functions, as well as their implementation in an autodiff framework, lies in the access to two key linear maps. These two maps encode infinitesimal variations along input or output directions and are referred to, respectively, as Jacobian-vector product (JVP) and Vector-jacobian product (VJP). This section formalizes these notions, in the context of Euclidean spaces.

2.3.1 The need for linear maps

So far, we have focused on functions f ⁣:RP→RMf \colon \RR^P \to \RR^M, that take a vector as input and produce a vector as output. However, functions that use matrix or even tensor inputs/outputs are commonplace in neural networks. For example, consider the function of matrices of the form f(W)≔Wxf(\W) \coloneqq \W \x, where x∈RD\x \in \RR^D and W∈RM×D\Wv \in \RR^{M\times D}. This function takes a matrix as input, not a vector. Of course, a matrix W∈RM×D\W \in \RR^{M\times D} can always be “flattened” into a vector w∈RMD\w \in \RR^{MD}, by stacking the columns of W\W. We denote this operation by w=vec⁡(W)\w = \Vec(\W) and its inverse by W=vec⁡−1(w)\W = \Vec^{-1}(\w). We can then equivalently write f(W)f(\W) as f~(w)=f(vec⁡−1(w))=vec⁡−1(w)x\tilde f(\w) = f(\Vec^{-1}(\w)) = \Vec^{-1}(\w) \x, so that the previous framework applies. However, we will now see that this would be inefficient.

Indeed, the resulting Jacobian of f~\tilde f at any w\w consists in a matrix of size RM×MD\RR^{M \times MD}, which, after some computations, can be observed to be mostly filled with zeros. Getting the directional derivative of ff at W∈RM×D\W \in \RR^{M\times D} in a direction V∈RM×D\V \in \RR^{M\times D} would consist in (i) vectorizing V\V into v=vec⁡(V)\v = \Vec(\V), (ii) computing the matrix-vector product ∂f~(w)v\jac \tilde f(\w)\v at a cost of M3D2M^3D^2 computations (ignoring the fact that the Jacobian has many zero entries), (iii) re-shaping the result into a matrix.

On the other hand, since ff is linear in its matrix input, we can infer that the directional derivative of ff at any W∈RM×D\W \in \RR^{M\times D} in any direction V∈RM×D\V\in \RR^{M\times D} is simply given by the function itself applied on V\V. Namely, we have ∂f(W)[V]=f(V)=Vx\partial f(\W)[\V] = f(\V) = \V\x, which is simple to implement and clearly only requires MDMD operations. Note that the cost would have been the same, had we ignored the non-zero entries of ∂f~(w)\jac \tilde f(\w). The point here is that by considering the operations associated to the differentiation of a function as linear maps rather than using the associated representation as a Jacobian matrix, we can efficiently exploit the underlying input or output space structure. To that end, we now recall the main abstractions necessary to extend the previous definitions in the context of Euclidean spaces.

2.3.2 Euclidean spaces

Linear spaces, a.k.a. vector spaces, are spaces equipped with and closed under an addition rule compatible with multiplication by a scalar (we limit ourselves to the field of reals). Namely, in a linear space E\cE, there exist operations ++ and ⋅\cdot, such that for any u\u, v∈E\v \in \cE, and a∈Ra \in \RR, we have u+v∈E\u +\v \in \cE and a⋅u∈Ea \cdot \u \in \cE.

Euclidean spaces are linear spaces equipped with a basis e1,…,eP∈E\e_1, \ldots, \e_P \in \cE. Any element v∈E\v \in \cE can be decomposed as v=∑i=1Pviei\v = \sum_{i=1}^{P} v_i \e_i for some unique scalars v1,…,vP∈Rv_1, \ldots, v_P \in \RR. A canonical example of Euclidean space is the set RP\RR^P of all vectors of size PP that we already covered. The set of matrices RP1×P2\RR^{P_1 \times P_2} of size P1×P2P_1\times P_2 is also naturally a Euclidean space generated by the set of canonical matrices Eij∈{0,1}P1×P2\E_{ij} \in \{0,1\}^{P_1\times P_2} for i∈[P1],j∈[P2]i \in [P_1], j\in [P_2] filled with zero except at the (i,j)(i, j)th entry filled with one. For example, W∈RP1×P2\W \in \RR^{P_1 \times P_2} can be written W=∑i,j=1P1,P2WijEij\W = \sum_{i, j=1}^{P_1, P_2} W_{i j} \E_{ij}. Euclidean spaces are naturally equipped with a notion of inner product.

Definition 2.10 (Inner product)

An inner product on a linear space E\cE is a function ⟨⋅,⋅⟩:E×E→R\langle \cdot, \cdot \rangle: \cE\times \cE \rightarrow \R that is

  • bilinear: x↦⟨x,w⟩\x \mapsto \langle \x, \w \rangle and y↦⟨v,y⟩\y \mapsto \langle \v, \y\rangle are linear for any w,v∈E\w, \v \in \cE,

  • symmetric: ⟨w,v⟩=⟨v,w⟩\langle \w, \v \rangle = \langle \v, \w\rangle for any w,v∈E\w, \v \in \cE,

  • positive definite: ⟨w,w⟩≥0\langle \w, \w\rangle\geq 0 for any w∈E\w \in \cE, and ⟨w,w⟩=0\langle \w, \w \rangle = 0 if and only if w=0\w=0.

An inner product defines a norm ∥w∥≔⟨w,w⟩\|\w\| \coloneqq \sqrt{\langle \w, \w \rangle}.

The norm induced by an inner product defines a distance ∥w−v∥\|\w-\v\| between w,v∈E\w, \v \in \cE, and therefore a notion of convergence.

For vectors, where E=RP\cE = \RR^P, the inner product is the usual one ⟨w,v⟩=∑i=1Pwivi. \langle \w, \v\rangle = \sum_{i=1}^{P} w_iv_i. For matrices, where E=RP1×P2\cE=\RR^{P_1\times P_2}, the inner product is the so-called Frobenius inner product. It is defined for any W,V∈RP1×P2\W, \V \in \RR^{P_1 \times P_2} by ⟨W,V⟩≔⟨vec⁡(W),vec⁡(V)⟩=∑i,j=1P1,P2WijVij=tr⁡(W⊤V), \langle \W, \V \rangle \coloneqq \langle \Vec(\W), \Vec(\V) \rangle = \sum_{i,j=1}^{P_1, P_2} W_{ij} V_{ij} = \Tr(\W^\top \V), where tr⁡(Z)≔∑i=1PZii\Tr(\Z) \coloneqq \sum_{i=1}^P Z_{ii} is the trace operator defined for square matrices Z∈RP×P\Zv \in \RR^{P \times P}. For tensors of order RR, which generalize matrices to E=RP1×…×PR\cE=\RR^{P_1\times \ldots \times P_R}, the inner product is defined similarly for W,V∈RP1×…×PR\Wt, \Vt \in \RR^{P_1\times \ldots \times P_R} by ⟨W,V⟩≔⟨vec⁡(W),vec⁡(V)⟩=∑i1,…,iR=1P1,…,PRWi1…iRVi1…iR, \langle \Wt, \Vt \rangle \coloneqq \langle \Vec(\Wt), \Vec(\Vt) \rangle = \sum_{i_1,\ldots, i_R=1}^{P_1, \ldots, P_R} \Wt_{i_1\ldots i_R} \Vt_{i_1\ldots i_R}, where Wi1…iR\Wt_{i_1\ldots i_R} is the (i1,…,iR)(i_1,\ldots, i_R)th entry of W\Wt.

2.3.3 Linear maps and their adjoints

The notion of linear map in Definition 2.5 naturally extends to Euclidean spaces. Namely, a function l:E→Fl:\cE\rightarrow \cF from a Euclidean space E\cE onto a Euclidean space F\cF is a linear map if for any w,v∈E\w, \v\in \cE and a,b∈Ra, b \in \RR, we have l[aw+bv]=a⋅l[w]+b⋅l[v]l[a\w + b\v] = a \cdot l[\w] + b \cdot l[\v]. When E=RP\cE = \RR^P and F=RM\cF=\RR^M, there always exists a matrix A∈RM×P\A \in \RR^{M \times P} such that l[v]=Avl[\v] = \A \v. Therefore, we can think of A\A as the “materialization” as a matrix of ll.

Example 2.6 (Linear map)

Consider the linear map l[v]≔(ab⊤)vl[\v] \coloneqq (\a \b^\top)\v, where a∈RM\a \in \RR^M, b∈RP\b \in \RR^P and v∈RP\v \in \RR^P. This is a function from RP\RR^P to RM\RR^M. We can always materialize the linear map as a matrix A≔ab⊤∈RM×P\A \coloneqq \a \b^\top \in \RR^{M \times P} and write l[v]=Avl[\v] = \A \v. However, applying a linear map on a vector v\v often does not require materializing the corresponding matrix. In this example, we can simply write l[v]=(b⊤v)al[\v] = (\b^\top \v) \a, since (b⊤v)(\b^\top \v) is a scalar value. This only requires an inner product and an element-wise multiplication, and is more efficient than materializing A\A then computing Av\A\v, which requires an outer product and a matrix-vector multiplication.

We can define the adjoint operator of a linear map.

Definition 2.11 (Adjoint operator)

Given two Euclidean spaces E\cE and F\cF equipped with inner products ⟨⋅,⋅⟩E\langle\cdot, \cdot \rangle_\cE and ⟨⋅,⋅⟩F\langle \cdot, \cdot \rangle_\cF, the adjoint of a linear map l:E→Fl:\cE\rightarrow \cF is the unique linear map l∗:F→El^*: \cF\rightarrow \cE such that for any v∈E\v \in \cE and u∈F\u \in \cF, ⟨l[v],u⟩F=⟨v,l∗[u]⟩E. \langle l[\v], \u \rangle_\cF = \langle \v, l^*[\u] \rangle_\cE.

The adjoint can be thought of as the counterpart of the matrix transpose for linear maps. When l[v]=Avl[\v] = \A \v, we have l∗[u]=A⊤ul^*[\u] = \A^\top \u since ⟨l[v],u⟩F=⟨Av,u⟩F=⟨v,A⊤u⟩E=⟨v,l∗[u]⟩E. \langle l[\v], \u \rangle_\cF = \langle \A \v, \u \rangle_\cF = \langle \v, \Av^\top \u \rangle_\cE = \langle \v, l^*[\u] \rangle_\cE.

Example 2.7 (Adjoint linear map)

Using the linear map l[v]l[\v] from the previous example, we have for all u∈RM\u \in \RR^M and v∈RP\v \in \RR^P, ⟨ab⊤v,u⟩=⟨v,ba⊤u⟩. \langle \a \b^\top \v, \u \rangle = \langle \v, \b \a^\top \u \rangle. Therefore, the adjoint linear map is l∗[u]=(ba⊤)ul^*[\u] = (\b \a^\top) \u. This is a function from RM\RR^M to RP\RR^P. It can be materialized as the matrix A⊤=ba⊤∈RP×M\A^\top = \b \a^\top \in \RR^{P \times M}. Applying l∗[u]l^*[\u] can be done efficiently as l∗[u]=(a⊤u)bl^*[\u] = (\a^\top \u) \b.

2.3.4 Jacobian-vector products

We now define the directional derivative using linear maps, leading to the notion of Jacobian-vector product (JVP). This can be used to facilitate the treatment of functions on tensors or for further extensions to infinite-dimensional spaces. In the following, E\cE and F\cF denote two Euclidean spaces equipped with inner products ⟨⋅,⋅⟩E\langle \cdot, \cdot\rangle_\cE and ⟨⋅,⋅⟩F\langle \cdot, \cdot \rangle_\cF. We start by defining differentiability in general Euclidean spaces.

Definition 2.12 (Differentiability in Euclidean spaces)

A function f:E→Ff:\cE\rightarrow \cF is differentiable at a point w∈E\w \in \cE if the directional derivative along v∈E\v \in \cE ∂f(w)[v]≔lim⁡δ→0f(w+δv)−f(w)δ \partial f(\w)[\v] \coloneqq \lim_{\delta \rightarrow 0} \frac{f(\w+ \delta \v) - f(\w)}{\delta} is well-defined for any v∈E\v \in \cE, linear in v\v and if lim⁡∥v∥E→0∥f(w+v)−f(w)−∂f(w)[v]∥F∥v∥E=0. \lim_{\|\v\|_\cE \rightarrow 0} \frac{\|f(\w+ \v) - f(\w) - \partial f(\w)[\v]\|_\cF}{\|\v\|_\cE} = 0.

We can now formally define the Jacobian-vector product.

Definition 2.13 (Jacobian-vector product)

For a differentiable function f:E→Ff: \cE \rightarrow \cF, the linear map ∂f(w):E→F\partial f(\w): \cE \to \cF, mapping v\v to ∂f(w)[v]\partial f(\w)[\v], is called the Jacobian-vector product (JVP). From this perspective, the function ∂f\partial f is a function from E\cE to a linear map from E\cE to F\cF. That is, we have ∂f ⁣:E→(E→F). \partial f \colon \cE \to (\cE \to \cF).

We emphasize again that the directional derivative ∂f(w)[v]∈F\partial f(\w)[\v] \in \cF is a value, while the JVP v↦∂f(w)[v]\v \mapsto \partial f(\w)[\v] is a function. Strictly speaking, v\v can belong to any Euclidean space E\cE and does not need to be limited to a vector, as the JVP acronym would suggest. We adopt the name JVP, as it is now standard.

Recovering the gradient

Previously, we saw that for differentiable functions with vector input and scalar output, the directional derivative is equal to the inner product between the direction and the gradient. The same applies when considering differentiable functions from a Euclidean space with single outputs, except that the gradient is now an element of the input space and the inner product is the one associated with the input space.

Proposition 2.4 (Gradient)

If a function f:E→Rf:\cE \rightarrow \RR is differentiable at w∈E\w \in \cE, then there exists ∇f(w)∈E\nabla f(\w) \in \cE, called the gradient of ff at w\w such that the directional derivative of ff at w\w along any input direction v∈E\v \in \cE is given by ∂f(w)[v]=⟨∇f(w),v⟩E. \partial f(\w)[\v] = \langle \nabla f(\w), \v\rangle_\cE.

In Euclidean spaces, the existence of the gradient can simply be shown by decomposing the partial derivative along a basis of E\cE. Such a definition generalizes to infinite-dimensional (e.g., Hilbert spaces) spaces as discussed in Section 2.3.9.

2.3.5 Vector-Jacobian products

Consider a function f ⁣:RP→RMf \colon \RR^P \to \RR^M. Instead of variations of ff along an input direction v∈RP\v \in \RR^P, we may also consider the variations of ff along an output direction u∈RM\u \in \RR^M, namely, computing the gradient ∇⟨u,f⟩(w)\nabla \langle \u, f \rangle(\w) of the scalar-valued function ⟨u,f⟩(w)≔⟨u,f(w)⟩∈R. \langle \u, f \rangle(\w) \coloneqq \langle \u, f(\w) \rangle \in \RR. Equivalently, we may compute the gradients ∇fj(w)\nabla f_j(\w) of each coordinate function fj≔⟨ej,f⟩f_j \coloneqq \langle \ev_j, f \rangle at w\w, where ej\ev_j is the jjth canonical vector in RM\RR^M. The infinitesimal variations of ff at w\w along any output direction u=∑j=1Mujej∈RM\u= \sum_{j=1}^M u_j\ev_j \in \RR^M are given by ∇⟨u,f⟩(w)=∑j=1Muj∇fj(w)=∂f(w)⊤u∈RP, \nabla \langle \u, f \rangle(\w) = \sum_{j=1}^M u_j \nabla f_j(\w) = \jac f(\w)^\top \u \in \RR^P, where ∂f(w)⊤∈RP×M\jac f(\w)^\top \in \RR^{P \times M} is the Jacobian’s transpose. Using the definition of derivative as a limit, we may also write for i∈[P]i \in [P] ∇i⟨u,f⟩(w)=[∂f(w)⊤u]i=lim⁡δ→0⟨u,f(w+δei)−f(w)⟩δ, \nabla_i \langle \u, f \rangle(\w) = [\jac f(\w)^\top \u]_i = \lim_{\delta \rightarrow 0} \frac{\langle \u, f(\w + \delta \e_i) -f(\w) \rangle}{\delta}, where ei\ev_i is the iith canonical vector in RP\RR^P.

For generic Euclidean spaces E\cE and F\cF, the counterpart of the transpose is the adjoint operator, leading to the notion of vector-Jacobian product.

Proposition 2.5 (Vector-Jacobian product)

If a function f:E→Ff:\cE \rightarrow \cF is differentiable at w∈E\w \in \cE, then its infinitesimal variation along an output direction u∈F\u \in \cF is given by the adjoint map ∂f(w)∗ ⁣:F→E\partial f(\w)^* \colon \cF \to \cE of the JVP, called the vector-Jacobian product (VJP). It satisfies ∇⟨u,f⟩F(w)=∂f(w)∗[u],\begin{align*} \nabla \langle \u, f\rangle_\cF (\w) = \partial f(\w)^*[\u], \end{align*} where we denoted ⟨u,f⟩F(w)≔⟨u,f(w)⟩F\langle \u, f\rangle_\cF(\w) \coloneqq \langle \u, f(\w)\rangle_\cF. The function ∂f(⋅)∗\partial f(\cdot)^* is a function from E\cE to a linear map from F\cF to E\cE. That is, we have ∂f(⋅)∗ ⁣:E→(F→E). \partial f(\cdot)^* \colon \cE \to (\cF \to \cE).

Proof

The chain rule presented in Proposition 2.2 naturally generalizes to Euclidean spaces (see Proposition 2.6). Since ⟨u,⋅⟩F\langle \u, \cdot\rangle_\cF is linear, its directional derivative is itself. Therefore, the directional derivative of ⟨u,f⟩F\langle \u, f\rangle_\cF is ∂(⟨u,f⟩F)(w)[v]=⟨u,∂f(w)[v]⟩F=⟨∂f(w)∗[u],v⟩E.\begin{align*} \partial (\langle \u, f\rangle_\cF)(\w)[\v] & = \langle \u, \partial f(\w)[\v]\rangle_\cF \\ & = \langle \partial f(\w)^*[\u], \v \rangle_\cE. \end{align*} As this is true for any v∈E\v \in \cE, ∂f(w)∗[u]\partial f(\w)^*[\u] is the gradient of ⟨u,f⟩F\langle \u, f\rangle_\cF per Proposition 2.4. ◻

We illustrate the JVP and VJP linear maps in Fig. 2.5.

Figure 2.5: Jacobian-vector product (JVP) v↦∂f(w)[v]\v \mapsto \partial f(\w)[\v] and vector-Jacobian product (VJP) u↦∂f(w)∗[u]\u \mapsto \partial f(\w)^*[\u], seen as linear maps.

2.3.6 Chain rule using linear maps

The chain rule presented before in terms of Jacobian matrices can readily be formulated to take advantage of the implementations of the JVP and VJP as linear maps.

Proposition 2.6 (Chain rule, general case)

Consider f:E→Ff:\cE \rightarrow \cF and g:F→Gg:\cF \rightarrow \cG, where E\cE, F\cF and G\cG are Euclidean spaces. If ff is differentiable at w∈E\w \in \cE and gg is differentiable at f(w)∈Ff(\w) \in \cF, then the composition g∘fg \circ f is differentiable at w∈E\w \in \cE. Its JVP is given for all v∈E\v \in \cE by ∂(g∘f)(w)[v]=∂g(f(w))[∂f(w)[v]] \partial (g\circ f)(\w)[\v] = \partial g(f(\w))[\partial f(\w)[\v]] and its VJP is given for all u∈G\u \in \cG by ∂(g∘f)(w)∗[u]=∂f(w)∗[∂g(f(w))∗[u]]. \partial (g\circ f)(\w)^*[\u] = \partial f(\w)^*[\partial g(f(\w))^*[\u]].

The proof follows the one of Proposition 2.2. This is illustrated in Fig. 2.6. When the last function is scalar-valued, which is often the case in machine learning, we obtain the following simplified result.

Proposition 2.7 (Chain rule, scalar case)

Consider f:E→Ff:\cE \rightarrow \cF and g:F→Rg: \cF \rightarrow \RR, the gradient of the composition is given by ∇(g∘f)(w)=∂f(w)∗[∇g(f(w))]. \nabla (g\circ f)(\w) = \partial f(\w)^* [\nabla g(f(\w))].

Figure 2.6: Chain rule using JVP and VJP linear maps.

2.3.7 Functions of multiple inputs (fan-in)

Oftentimes, the inputs of a function do not belong to only one Euclidean space but to a product of them. An example is f(x,W)≔Wxf(\x, \W) \coloneqq \W \x, which is defined on E≔RD×RM×D\cE \coloneqq \RR^D \times \RR^{M \times D}. In such a case, it is convenient to generalize the notion of partial derivatives to handle blocks of inputs.

Consider a function f(w1,…,wS)f(\w_1, \ldots, \w_S) defined on E≔E1×…×ES\cE \coloneqq \cE_1 \times \ldots \times \cE_S, where wi∈Ei\w_i \in \cE_i. We denote the partial derivative with respect to the iith input wi\w_i along vi∈Ei\v_i \in \cE_i as ∂if(w1,…,wS)[vi]\partial_i f(\w_1, \ldots, \w_S)[\v_i]. Equipped with this notation, we can analyze how JVPs or VJPs are decomposed along several inputs.

Proposition 2.8 (Multiple inputs)

Consider a differentiable function of the form f(w)=f(w1,…,wS)f(\w) = f(\w_1, \dots, \w_S) with signature f ⁣:E→Ff \colon \cE \to \cF, where w≔(w1,…,wS)∈E\w \coloneqq (\w_1, \dots, \w_S) \in \cE and E≔E1×⋯×ES\cE \coloneqq \cE_1 \times \dots \times \cE_S. Then the JVP with the input direction v=(v1,…,vS)∈E\v = (\v_1, \dots, \v_S) \in \cE is given by ∂f(w)[v]=∂f(w1,…,wS)[v1,…,vS]∈F=∑i=1S∂if(w1,…,wS)[vi].\begin{align*} \partial f(\w)[\v] &= \partial f(\w_1, \dots, \w_S)[\v_1, \dots, \v_S] \in \cF \\ &= \sum_{i=1}^S \partial_i f(\w_1, \dots, \w_S)[\v_i]. \end{align*} The VJP with the output direction u∈F\u \in \cF is given by ∂f(w)∗[u]=∂f(w1,…,wS)∗[u]∈E=(∂1f(w1,…,wS)∗[u],…,∂Sf(w1,…,wS)∗[u]).\begin{align*} \partial f(\w)^*[\u] &= \partial f(\w_1, \dots, \w_S)^*[\u] \in \cE \\ &= (\partial_1 f(\w_1, \dots, \w_S)^*[\u], \dots, \partial_S f(\w_1, \dots, \w_S)^*[\u]). \end{align*}

Example 2.8 (Matrix-vector product)

Consider f(x,W)≔Wxf(\x, \W) \coloneqq \W \x, where W∈RM×D\W \in \RR^{M \times D} and x∈RD\x \in \RR^D. This corresponds to setting E≔E1×E2≔RD×RM×D\cE \coloneqq \cE_1 \times \cE_2 \coloneqq \RR^D \times \RR^{M \times D} and F≔RM\cF \coloneqq \RR^M. For the JVP, letting v∈E1\v \in \cE_1 and V∈E2\V \in \cE_2, we obtain ∂f(x,W)[v,V]=Wv+Vx∈F. \partial f(\x, \W)[\v, \V] = \W \v + \V \x \in \cF. We can also access the individual JVPs as ∂1f(x,W)[v]=Wv∈F,∂2f(x,W)[V]=Vx∈F. \begin{aligned} \partial_1 f(\x, \W)[\v] &= \W \v \in \cF, \\ \partial_2 f(\x, \W)[\V] &= \V \x \in \cF. \end{aligned} For the VJP, letting u∈F\u \in \cF, we obtain ∂f(x,W)∗[u]=(W⊤u,ux⊤)∈E. \partial f(\x, \W)^*[\u] = (\W^\top \u, \u \x^\top) \in \cE. We can access the individual VJPs by ∂1f(x,W)∗[u]=W⊤u∈E1,∂2f(x,W)∗[u]=ux⊤∈E2. \begin{aligned} \partial_1 f(\x, \W)^*[\u] &= \W^\top \u \in \cE_1, \\ \partial_2 f(\x, \W)^*[\u] &= \u \x^\top \in \cE_2. \end{aligned}

Remark 2.5 (Nested inputs)

It is sometimes convenient to group inputs into meaningful parts. For instance, if the input is naturally broken down into two parts x=(x1,x2)\x = (\x_1, \x_2), where x1\x_1 is a text part and x2\x_2 is an image part, and the network parameters are naturally grouped into three layers w=(w1,w2,w3)\w = (\w_1, \w_2, \w_3), we can write f(x,w)=f((x1,x2),(w1,w2,w3))f(\x, \w) = f((\x_1, \x_2), (\w_1, \w_2, \w_3)). This is mostly a convenience and we can again reduce it to a function of a single input, thanks to the linear map perspective in Euclidean spaces.

Remark 2.6 (Hiding away inputs)

It will often be convenient to ignore inputs when differentiating. We use the semicolon for this purpose. For instance, a function of the form L(w;x,y)L(\w; \x, \y) (notice the semicolon) has signature L ⁣:W→RL \colon \cW \to \RR because we treat x\x and y\y as constants. Therefore, the gradient is ∇L(w;x,y)∈W\nabla L(\w; \x, \y) \in \cW. On the other hand, the function L(w,x,y)L(\w, \x, \y) (notice the comma) has signature L ⁣:W×X×Y→RL \colon \cW \times \cX \times \cY \to \RR so its gradient is ∇L(w,x,y)∈W×X×Y\nabla L(\w, \x, \y) \in \cW \times \cX \times \cY. If we need to access partial gradients, we use indexing, e.g., ∇1L(w,x,y)∈W\nabla_1 L(\w, \x, \y) \in \cW or ∇wL(w,x,y)∈W\nabla_\w L(\w, \x, \y) \in \cW when there is no ambiguity.

2.3.8 Functions with multiple outputs (fan-out)

Similarly, it is often convenient to deal with functions that have multiple outputs.

Proposition 2.9 (Multiple outputs)

Consider a differentiable function of the form f(w)≔(f1(w),…,fT(w))f(\w) \coloneqq (f_1(\w), \dots, f_T(\w)), with signatures f ⁣:E→Ff \colon \cE \to \cF and fi ⁣:E→Fif_i \colon \cE \to \cF_i, where F≔F1×⋯×FT\cF \coloneqq \cF_1 \times \dots \times \cF_T. Then the JVP with the input direction v∈E\v \in \cE is given by ∂f(w)[v]=(∂f1(w)[v],…,∂fT(w)[v])∈F.\begin{align*} \partial f(\w)[\v] = (\partial f_1(\w)[\v], \dots, \partial f_T(\w)[\v]) \in \cF. \end{align*} The VJP with the output direction u=(u1,…,uT)∈F\u = (\u_1, \dots, \u_T) \in \cF is ∂f(w)∗[u]=∂f(w)∗[u1,…,uT]∈E=∑i=1T∂fi(w)∗[ui].\begin{align*} \partial f(\w)^*[\u] &= \partial f(\w)^*[\u_1, \dots, \u_T] \in \cE \\ &= \sum_{i=1}^T \partial f_i(\w)^*[\u_i]. \end{align*}

Combined with the chain rule, we obtain that the Jacobian of h(w)≔g(f(w))=g(f1(w),…,fT(w)) h(\w) \coloneqq g(f(\w)) = g(f_1(\w), \dots, f_T(\w)) is ∂h(w)=∑i=1T∂ig(f(w))∘∂fi(w)\partial h(\w) = \sum_{i=1}^T \partial_i g(f(\w)) \circ \partial f_i(\w) and therefore the JVP is ∂h(w)[v]=∑i=1T∂ig(f(w))[∂fi(w)[v]]. \partial h(\w) [\v] = \sum_{i=1}^T \partial_i g(f(\w)) [\partial f_i(\w)[\v]].

2.3.9 Extensions to non-Euclidean linear spaces

So far, we focused on Euclidean spaces, i.e., linear spaces with a finite basis. However, the notions studied earlier can be generalized to more generic spaces.

For example, directional derivatives (see Definition 2.12) can be defined in any linear space equipped with a norm and complete with respect to this norm. Such spaces are called Banach spaces. Completeness is a technical assumption that requires that any Cauchy sequence converges (a Cauchy sequence is a sequence whose elements become arbitrarily close to each other as the sequence progresses). A function f:E→Ff: \cE \rightarrow \cF defined from a Banach space E\cE onto a Banach space F\cF is then called Gateaux differentiable if its directional derivative is defined along any direction (where limits are defined w.r.t. the norm in F\cF). Some authors also require the directional derivative to be linear to define a Gateaux differentiable function.

Fréchet differentiability can also naturally be generalized to Banach spaces. The only difference is that, in generic Banach spaces, the linear map ll satisfying Definition 2.12 must be continuous, i.e., there must exist C>0C>0, such that l[v]≤C∥v∥l[\v] \leq C \|\v\|, where ∥⋅∥\|\cdot\| is the norm in the Banach space E\cE.

The definitions of gradient and VJPs require in addition a notion of inner product. They can be defined in Hilbert spaces, that is, linear spaces equipped with an inner product and complete with respect to the norm induced by the inner product (they could also be defined in a Banach space by considering operations in the dual space, see, e.g. (Clarke et al., 2008)). The existence of the gradient is ensured by Riesz’s representation theorem which states that any continuous linear form in a Hilbert space can be represented by the inner product with a vector. Since for a differentiable function f:E→Rf: \cE \rightarrow \RR, the JVP ∂f(w):E→R\partial f(\w): \cE \rightarrow \RR is a linear form, Riesz’s representation theorem ensures the existence of the gradient as the element g∈Eg \in \cE such that ∂f(w)v=⟨g,v⟩\partial f(\w) \v = \langle g, \v\rangle for any v∈E\v \in \cE. The VJP is also well-defined as the adjoint of the JVP w.r.t. the inner product of the Hilbert space.

As an example, the space of squared integrable functions on R\RR is a Hilbert space equipped with the inner product ⟨a,b⟩≔∫a(x)b(x)dx. \langle a, b \rangle \coloneqq \int a(x)b(x) dx. Here, we cannot find a finite number of functions that can express all possible functions on R\RR. Therefore, this space is not a mere Euclidean space. Nevertheless, we can consider functions on this Hilbert space (called functionals to distinguish them from the elements of the space). The associated directional derivatives and gradients, can be defined and are called respectively, functional derivative and functional gradient, see, e.g., Frigyik et al. (2008) and references therein.

2.4 Second-order differentiation

2.4.1 Second derivatives

For a single-input, single-output differentiable function f:R→Rf:\RR \rightarrow \RR, its derivative at any point is itself a function f′:R→Rf': \RR \rightarrow \RR. We may then consider the derivative of the derivative at any point: the second derivative.

Definition 2.14 (Second derivative)

The second derivative f(2)(w)f^{(2)}(w) of a differentiable function f:R→Rf: \RR \rightarrow \RR at w∈Rw \in \RR is defined as the derivative of f′f' at ww, that is, f(2)(w)≔lim⁡δ→0f′(w+δ)−f′(w)δ, f^{(2)}(w) \coloneqq \lim_{\delta \rightarrow 0} \frac{f'(w+\delta) - f'(w)}{\delta}, provided that the limit is well-defined. If the second derivative of a function ff is well-defined at ww, the function is said to be twice differentiable at ww. The second derivative is also denoted f′′f''.

If ff has a small second derivative at a given ww, the derivative around ww is almost constant. That is, the function behaves like a line around ww, as illustrated in Fig. 2.7. Hence, the second derivative is usually interpreted as the curvature of the function at a given point.

Figure 2.7: Points at which the second derivative is small are points along which the function is well approximated by its tangent line. On the other hand, points with large second derivative tend to be badly approximated by the tangent line.

2.4.2 Second directional derivatives

For a multi-input function f:RP→Rf:\RR^P \rightarrow \RR, we saw that the directional derivative encodes infinitesimal variations of ff along a given direction. To analyze the second derivative, the curvature of the function at a given point w\w, we can consider the variations along a pair of directions, as defined below.

Definition 2.15 (Second directional derivative)

The second directional derivative of f:RP→Rf:\RR^P \rightarrow \RR at w∈RP\w\in \RR^P along v,v′∈RP\v, \v' \in \RR^P is defined as the directional derivative of w↦∂f(w)[v]\w \mapsto \partial f(\w)[\v] along v′\v', that is, ∂2f(w)[v,v′]≔lim⁡δ→0∂f(w+δv′)[v]−∂f(w)[v]δ, \partial^2 f(\w)[\v, \v'] \coloneqq \lim_{\delta \rightarrow 0} \frac{\partial f(\w + \delta \v')[\v] - \partial f(\w)[\v]}{\delta}, provided that ∂f(w)[v]\partial f(\w)[\v] is well-defined around w\w and that the limit exists.

Of particular interest are the variations of a function around the canonical directions: the second partial derivatives, defined as ∂ij2f(w)≔∂2f(w)[ei,ej] \partial_{i j}^2 f(\w) \coloneqq \partial^2 f(\w)[\e_i, \e_j] for ei\e_i, ej\e_j the iith and jjth canonical directions in RP\RR^P, respectively. In Leibniz notation, the second partial derivatives are denoted ∂ij2f(w)=∂2f(w)∂wi∂wj. \partial_{i j}^2 f(\w) = \frac{\partial^2 f(\w)}{\partial w_i \partial w_j}.

2.4.3 Hessians

For a multi-input function, twice differentiability is simply defined as the differentiability of any directional derivative ∂f(w)[v]\partial f(\w)[\v] w.r.t. w\w.

Definition 2.16 (Twice differentiability)

A function f:RP→Rf: \RR^P \rightarrow \RR is twice differentiable at w∈RP\w \in \RR^P if it is differentiable and ∂f:RP→(RP→R)\partial f: \RR^P \rightarrow (\RR^P \rightarrow\RR) is also differentiable at w\w.

As a result, the second directional derivative is a bilinear form.

Definition 2.17 (Bilinear map, bilinear form)

A function b:RP×RP→RMb: \RR^P \times \RR^P \rightarrow \RR^M is a bilinear map if b[v,⋅]:RP→Rb[\v, \cdot]: \RR^P \rightarrow \RR is linear for any v\v and b[⋅,v′]b[\cdot, \v'] is linear for any v′\v'. That is, b[v,v′]=∑i=1Pvib[ei,v′]=∑i=1P∑j=1Pvivj′b[ei,ej], b[\v, \v'] = \sum_{i=1}^P v_i b[\e_i, \v'] = \sum_{i=1}^{P} \sum_{j=1}^P v_iv_j' b[\e_i, \e_j], for v=∑i=1Pviei\v = \sum_{i=1}^P v_i \e_i and v′=∑i=1Pvi′ei\v' = \sum_{i=1}^P v_i' \e_i. A bilinear map with values in R\RR, b:RP×RP→Rb: \RR^P \times \RR^P \rightarrow \RR, is called a bilinear form.

The second partial derivatives are gathered in the Hessian and the second directional derivatives can be computed from it.

Definition 2.18 (Hessian)

The Hessian of a twice differentiable function f:RP→Rf:\RR^P \rightarrow \RR at w\w is the P×PP \times P matrix gathering all second partial derivatives, ∇2f(w)≔(∂11f(w)…∂1Pf(w)⋮⋱⋮∂P1f(w)…∂PPf(w))∈RP×P, \nabla^2 f(\w) \coloneqq \begin{pmatrix} \partial_{11} f(\w) & \ldots & \partial_{1P} f(\w) \\ \vdots & \ddots & \vdots \\ \partial_{P1} f(\w) & \ldots & \partial_{PP} f(\w) \end{pmatrix} \in \RR^{P \times P}, provided that all second partial derivatives are well-defined.

The second directional derivative at w\w is bilinear in any direction v=∑i=1Pviei\v = \sum_{i=1}^P v_i \e_i and v′=∑i=1Pvi′ei\v' = \sum_{i=1}^P v_i' \e_i. Therefore, ∂2f(w)[v,v′]=∑i,j=1Pvivj′∂2f(w)[ei,ej]=⟨v,∇2f(w)v′⟩. \partial^2 f(\w)[\v, \v'] = \sum_{i, j = 1}^{P} v_i v_j'\partial^2 f(\w)[\e_i, \e_j] = \langle \v, \nabla^2 f(\w) \v'\rangle.

Given the gradient of ff, the Hessian is equivalent to the transpose of the Jacobian of the gradient. By slightly generalizing the notation ∇\nabla to denote the transpose of the Jacobian of a function (which matches its definition for single-output functions), we have that the Hessian can be expressed as ∇2f(w)=∇(∇f)(w)\nabla^2 f(\w) = \nabla (\nabla f)(\w), which justifies its notation.

Similarly as for the differentiability of a function ff, twice differentiability of ff at w\w is equivalent to having the second partial derivatives not only defined but also continuous in a neighborhood of w\w. Remarkably, by requiring twice differentiability, i.e., continuous second partial derivatives, the Hessian is guaranteed to be symmetric (Schwarz, 1873).

Proposition 2.10 (Symmetry of the Hessian)

If a function f:RP→Rf:\RR^P \rightarrow \RR is twice differentiable at w\w, then its Hessian ∇2f(w)\nabla^2 f(\w) is symmetric, that is, ∂ij2f(w)=∂ji2f(w)\partial_{i j}^2 f(\w) = \partial_{j i}^2 f(\w) for any i,j∈{1,…P}i, j \in \{1, \ldots P\}.

The symmetry of the Hessian means that it can alternatively be written as ∇2f(w)=(∂ji2f(w))i,j=1P=∂(∇f)(w)\nabla^2 f(\w) = (\partial_{ji}^2f(\w))_{i, j=1}^P = \jac (\nabla f)(\w), i.e., the Jacobian of the gradient of ff.

2.4.4 Hessian-vector products

Similarly to the Jacobian, we can exploit the formal definition of the Hessian as a bilinear form to extend its definition to Euclidean spaces. In particular, we can define the notion of Hessian-vector product.

Definition 2.19 (Hessian-vector product)

If a function f:E→Rf: \cE \rightarrow \R defined on a Euclidean space E\cE with inner product ⟨⋅,⋅⟩\langle\cdot, \cdot\rangle, is twice differentiable at w∈E\w \in \cE, then for any v∈E\v \in \cE, there exists v↦∇2f(w)[v]\v \mapsto \nabla^2 f(\w)[\v], called the Hessian-vector product (HVP) of ff at w\w along v\v, such that for any v′∈E\v' \in \cE, ∂2f(w)[v,v′]=⟨v′,∇2f(w)[v]⟩. \partial^2 f(\w)[\v, \v'] = \langle \v', \nabla^2 f(\w)[\v]\rangle. In particular for E=RP\cE = \RR^P, the HVP is ∇2f(w)[v]=(∂2f(w)[v,ei])i=1P\nabla^2 f(\w)[\v]= (\partial^2 f(\w)[\v, \e_i])_{i=1}^P.

From an autodiff point of view, the HVP can be implemented in four different ways, as explained in Section 9.1.

2.4.5 Second-order Jacobians

The previous definitions naturally extend to multi-output functions f:E→Ff: \cE \rightarrow \cF, where f≔(f1,…,fM)f \coloneqq (f_1, \dots, f_M), fj ⁣:E→Fjf_j \colon \cE \to \cF_j and F≔F1×⋯×FM\cF \coloneqq \cF_1 \times \dots \times \cF_M. The second directional derivative is defined by gathering the second derivatives of each coordinate’s function. That is, for w,v,v′∈E\w, \v, \v' \in \cE, ∂2f(w)[v,v′]=(∂2fj(w)[v,v′])j=1M∈F. \partial^2 f(\w)[\v, \v'] = (\partial^2 f_j(\w)[\v, \v'])_{j=1}^M \in \cF. The function ff is twice differentiable if and only if all its coordinates are twice differentiable. The second directional derivative is then a bilinear map. We can then compute second directional derivatives as ∂2f(w)[v,v′]=∑i,j=1Pvivj′∂2f(w)[ei,ej]=(⟨v,∇2fj(w)v′⟩)j=1M. \partial^2 f(\w)[\v, \v'] = \sum_{i,j=1}^P v_i v_j' \partial^2 f(\w)[\e_i, \e_j] = (\langle \v, \nabla^2 f_j(\w) \v'\rangle)_{j=1}^M. When E=RP\cE = \RR^P and Fj=R\cF_j = \RR, so that F=RM\cF = \RR^M, the bilinear map can be materialized as a tensor ∂2f(w)=(∂2f(w)[ei,ej])i,j=1P∈RM×P×P, \partial^2 f(\w) = (\partial^2 f(\w)[\e_i, \e_j])_{i,j=1}^P \in \RR^{M \times P \times P}, the “second-order Jacobian” of ff. However, similarly to the Hessian, it is usually more convenient to apply the bilinear map to prescribed vectors v\v and v′\v' than to materialize the second partial derivatives as a tensor.

2.5 Higher-order differentiation

2.5.1 Higher-order derivatives

Derivatives can be extended to any order. Formally, the nnth derivative can be defined inductively as follows for a single-input, single-output function.

Definition 2.20 (nnth order derivative)

The nnth derivative f(n)f^{(n)} of a function f:R→Rf:\RR \rightarrow \RR at w∈Rw \in \RR is defined as f(n)(w)≔(f(n−1))′(w)=lim⁡δ→0f(n−1)(w+δ)−f(n−1)(w)δ f^{(n)}(w) \coloneqq (f^{(n-1)})'(w) = \lim_{\delta \rightarrow 0} \frac{f^{(n-1)}(w+\delta) - f^{(n-1)}(w)}{\delta} provided that f(n−1)f^{(n-1)} is differentiable around ww and that the limit exists. In such a case, the function is said to be nn times differentiable at ww.

2.5.2 Higher-order directional derivatives

For a multi-input function ff, we can naturally extend the notion of directional derivative as follows.

Definition 2.21 (nnth order directional derivative)

The nnth directional derivative of f:RP→Rf: \RR^P \rightarrow \R at w∈RP\w \in \RR^P along v1,…,vn\v_1, \ldots, \v_n is defined as ∂nf(w)[v1,…,vn]=∂(∂n−1f(w)[v1,…,vn−1])[vn]=lim⁡δ→0∂f(w+δvn)[v1,…,vn−1]−∂f(w)[v1,…,vn−1]δ\begin{align*} & \partial^n f(\w)[\v_1, \ldots, \v_n] \\ & = \partial (\partial^{n-1}f(\w)[\v_1, \ldots, \v_{n-1}])[\v_n] \\ & = \lim_{\delta \rightarrow 0} \frac{ \partial f(\w + \delta \v_n)[\v_1, \ldots, \v_{n-1}] - \partial f(\w)[\v_1, \ldots, \v_{n-1}] }{ \delta } \end{align*}

A multi-input function ff is nn-times differentiable if it is n−1n-1 differentiable and its n−1n-1 directional derivative along any direction is differentiable. As a consequence the nnth directional derivative is a multilinear form.

Definition 2.22 (Multilinear map, multilinear form)

A function c:⊗i=1nRP→RMc: \otimes_{i=1}^n \RR^P \rightarrow \RR^M is a multilinear map if it is linear in each coordinate given all others fixed, that is, if vj↦c[v1,…,vj,…,vn]\v_j \mapsto c[\v_1,\ldots,\v_j,\ldots, \v_n] is linear in vj\v_j for any j∈[n]j \in [n]. It is a multilinear form if it has values in R\RR.

The nnth order directional derivative is then given by ∂nf(w)[v1,…,vn]=∑i1,…,in=1Pv1,i1…vn,in∂nf(w)[ei1,…,ein]. \partial^n f(\w)[\v_1, \ldots, \v_n] = \sum_{i_1, \ldots, i_n=1}^P v_{1,i_1} \ldots v_{n, i_n} \partial^n f(\w)[\e_{i_1}, \ldots, \e_{i_n}]. The nnth order partial derivatives can be materialized as an nnth order tensor ∇nf(w)=(∂nf(w)[ei1,…,ein])i1,…,in=1P∈RP×…×P. \nabla^n f(\w) = (\partial^n f(\w)[\e_{i_1}, \ldots, \e_{i_n}])_{i_1, \ldots, i_n=1}^P \in \RR^{P \times \ldots \times P}.

2.5.3 Higher-order Jacobians

All above definitions extend directly to the case of multi-output functions f:E→Ff:\cE \rightarrow \cF, where F≔F1×⋯×FM\cF \coloneqq \cF_1 \times \dots \times \cF_M. The nnth directional derivatives are then ∂nf(w)[v1,…,vn]=(∂nfj(w)[v1,…,vn])j=1M. \partial^n f(\w)[\v_1, \ldots, \v_n] = (\partial^n f_j(\w)[\v_1, \ldots, \v_n])_{j=1}^M. The function ff is then nn times differentiable if it is n−1n-1 differentiable and its n−1n-1 directional derivative along any direction is differentiable. As a consequence, the nnth directional derivative is a multilinear map. The nnth directional derivative can be decomposed into partial derivatives as ∂nf(w)[v1,…,vn]=∑i1,…,in=1Pv1,i1…vn,in∂nf(w)[ei1,…,ein].\partial^n f(\w)[\v_1, \ldots, \v_n] = \sum_{i_1, \ldots, i_n=1}^P v_{1,i_1} \ldots v_{n, i_n} \partial^n f(\w)[\e_{i_1}, \ldots, \e_{i_n}]. When E=RP\cE = \RR^P and F=RM\cF = \RR^M, the nnth order partial derivatives can be materialized by an (n+1)(n+1)th order tensor ∂nf(w)=(∂nfj(w)[ei1,…,ein])j=1,i1,…,in=1M,P,…,P∈RM×P×…×P. \partial^n f(\w) = (\partial^n f_j(\w)[\e_{i_1}, \ldots, \e_{i_n}])_{j=1, i_1, \ldots, i_n=1}^{M, P, \ldots, P} \in \RR^{M \times P \times \ldots \times P}.

2.5.4 Taylor expansions

With Landau’s little oo notation, we have seen that if a function is differentiable, it is approximated by a linear function in v\v, f(w+v)=f(w)+⟨∇f(w),v⟩+o(∥v∥2). f(\w+\v) = f(\w) + \langle \nabla f(\w), \v\rangle + o(\|\v\|_2). Such an expansion of the function up to its first derivative is called the first-order Taylor expansion of ff around w\w.

If the function ff is twice differentiable, we can approximate it by a quadratic in v\v, leading to the second-order Taylor expansion of ff around w\w, f(w+v)=f(w)+⟨∇f(w),v⟩+12⟨v,∇2f(w)v⟩+o(∥v∥22). f(\w+\v) = f(\w) + \langle \nabla f(\w), \v \rangle + \frac{1}{2}\langle \v, \nabla^2 f(\w) \v\rangle + o(\|\v\|_2^2). Compared to the first-order Taylor approximation, it is naturally more accurate around w\w, as reflected by the fact that ∥v∥23≤∥v∥22\|\v\|_2^3 \leq \|\v\|_2^2 for ∥v∥2≤1\|\v\|_2 \le 1.

More generally, we can build the n\nth order Taylor expansion of a nn times differentiable function f ⁣:RP→RMf \colon \RR^P \to \RR^M around w∈RP\w \in \RR^P by f(w+v)=f(w)+∂f(w)[v]+12∂2f(w)[v,v]+…+1n!∂nf(w)[v,…,v⏟n times]+o(∥v∥2n).\begin{align*} f(\w+\v) & = f(\w) + \partial f(\w)[\v] + \frac{1}{2} \partial^2 f(\w)[\v, \v] + \ldots \\ & + \frac{1}{n!} \partial^n f(\w)[\underbrace{\v, \ldots, \v}_{n \, \mathrm{times}}] + o(\|\v\|_2^{n}). \end{align*} Note that, using the change of variable w′=w+v  ⟺  v=w′−w\w' = \w + \v \iff \v = \w' - \w, it is often convenient to write the nnth Taylor expansion of f(w′)f(\w') around w\w as f(w′)=f(w)+∑j=1n1j!∂jf(w)[w′−w,…,w′−w⏟j times]+o(∥w′−w∥2n). f(\w') = f(\w) + \sum_{j=1}^{n} \frac{1}{j!} \partial^j f(\w)[\underbrace{\w' - \w, \ldots, \w' - \w}_{j \, \mathrm{times}}] + o(\|\w' - \w\|_2^{n}). Taylor expansions will prove useful in Chapter 7 for computing derivatives by finite differences.

2.6 Differential geometry

In this chapter, we progressively generalized the notion of derivative from real numbers to vectors and variables living in a linear space (a.k.a. vector space), either finite dimensional or infinite dimensional. We can further generalize these notions by considering a local notion of linearity. This is formalized by smooth manifolds in differential geometry, whose terminology is commonly adopted in the automatic differentiation literature and software. In this section, we give a brief overview of derivatives on smooth manifolds (simply referred to as manifolds), and refer to Boumal (2023) for a complete introduction.

2.6.1 Differentiability on manifolds

Essentially, a manifold is a set that can be locally approximated by a Euclidean space. The most common example is a sphere like the Earth. Seen from the Moon, the Earth is not a plane, but locally, at a human level, it can be seen as a flat surface. Euclidean spaces are also trivial examples of manifolds. A formal characterization of the sphere as a manifold is presented in Example 2.9. For now, we may think of a “manifold” as some set (e.g., the sphere) contained in some ambient Euclidean space; note however that manifolds can be defined generally without being contained in a Euclidean space (Boumal, 2023, 8). Differentiability in manifolds is simply inherited from the notion of differentiability in the ambient Euclidean space.

Definition 2.23 (Differentiability of restricted functions)

Let M\cM and N\cN be manifolds. A function f:M→Nf: \mathcal{M} \rightarrow \mathcal{N} defined from M⊆E\mathcal{M} \subseteq \cE to N⊆F\mathcal{N} \subseteq \cF, with E\cE and F\cF Euclidean spaces, is differentiable if ff is the restriction of a differentiable function fˉ:E→F\bar f:\cE \rightarrow \cF, so that ff coincides with fˉ\bar f on M\mathcal{M}.

Our objective is to formalize the directional derivatives and gradients for functions defined on manifolds. This formalization leads to the definitions of tangent spaces and cotangent spaces, and the associated generalizations of JVP and VJP operators as pushforward and pullback operators, respectively.

2.6.2 Tangent spaces and pushforward operators

To generalize the notion of directional derivatives of a function ff, the one property we want to preserve is the chain rule. Rather than starting from the variations of ff at a given point along a direction, we start with the variations of ff along curves. Namely, on a manifold like the sphere SP\cS^P in RP\RR^P, we can look at curves α:R→SP\alpha: \R \rightarrow \cS^P passing through w∈SP\w \in \cS^P at time 00, that is, α(0)=w\alpha(0) = \w. For single-input functions like α\alpha, we denote for simplicity α′(0)≔(α1′(0),…,αP′(0))\alpha'(0) \coloneqq (\alpha_1'(0), \ldots, \alpha_P'(0)). The directional derivative of a function ff must typically serve to define the derivative of f∘αf\circ \alpha at 00, such that (f∘α)′(0)=∂f(w)[α′(0)](f\circ \alpha)'(0) = \partial f(\w)[\alpha'(0)]. In the case of the sphere, as illustrated in Fig. 2.8, the derivative α′(0)\alpha'(0) of a curve α\alpha passing through a point w\w is always tangent to the sphere at w\w. The tangent plane to the sphere at w\w then captures all possible relevant vectors to pass to the JVP we are building. To define the directional derivative of a function ff on a manifold, we therefore restrict ourselves to an operator defined on the tangent space TwM\cT_\w\cM, whose definition below is simplified for our purposes.

Definition 2.24 (Tangent space)

The tangent space of a manifold M\cM at w∈M\w \in \cM is defined as TwM≔{v=α′(0) for any α:R→M differentiable s.t. α(0)=w}. \cT_\w \mathcal{M} \coloneqq \{\v = \alpha'(0) \ \mbox{for any} \ \alpha: \RR \rightarrow \cM \ \mbox{differentiable s.t.} \ \alpha(0) = \w \}.

In the case of the sphere in Fig. 2.8, the tangent space is a plane, that is, a Euclidean space. This property is generally true: tangent spaces are Euclidean spaces, enabling us to define directional derivatives as linear maps. Now, if ff is differentiable and goes from a manifold M\cM to a manifold N\cN, then f∘αf\circ \alpha is a differentiable curve in N\cN. Therefore, (f∘α)′(0)(f\circ \alpha)'(0) is the derivative of a curve passing through f(w)f(\w) at 00 and is tangent to N\cN at f(w)f(\w). Hence, the directional derivative of f:M→Nf: \cM \rightarrow \cN at w\w can be defined as a function from the tangent space TwM\cT_\w \mathcal{M} of M\cM at w\w onto the tangent space Tf(w)N\cT_{f(\w)} \mathcal{N} of N\cN at f(w)f(\w). Overall, we built the directional derivative (JVP) by considering how a composition of ff with any curve α\alpha pushes forward the derivative of α\alpha into the derivative of f∘αf\circ \alpha. The resulting JVP is called a pushforward operator in differentiable geometry.

Definition 2.25 (Pushforward operator)

Given two manifolds M\cM and N\cN, the pushforward operator of a differentiable function f:M→Nf: \mathcal{M} \rightarrow \mathcal{N} at w∈M\w \in \mathcal{M} is the linear map ∂f(w):TwM→Tf(w)N\partial f(\w): \mathcal{T}_\w \mathcal{M} \rightarrow \mathcal{T}_{f(\w)} \mathcal{N} defined by ∂f(w)[v]≔(f∘α)′(0), \partial f(\w)[\v] \coloneqq (f\circ \alpha)'(0), for any v∈TwM\v \in \cT_\w \cM such that v=α′(0)\v = \alpha'(0), where α:R→M\alpha: \RR \rightarrow \cM is a differentiable curve passing through w\w at 00, i.e., α(0)=w\alpha(0) = \w.

Figure 2.8: A differentiable function ff defined from a sphere M\cM to a sphere N\cN defines a push-forward operator that maps tangent vectors (derivatives of functions on the sphere passing through w\w) in the tangent space TwM\cT_{\w} \cM to tangent vectors of N\cN at f(w)f(\w) in the tangent space Tf(w)N\cT_{f(\w)}\cN.

2.6.3 Cotangent spaces and pullback operators

To generalize the JVP, we composed f:M→Nf: \cM \rightarrow \cN with any single-input function α:R→M\alpha: \RR\rightarrow \cM giving values on the manifold. The derivative of any such α\alpha is then pushed forward from TwM\cT_\w \mathcal{M} to Tf(w)N\cT_{f(\w)} \mathcal{N} by the action of ff. To define the VJP, we take a symmetric approach. We consider all single-output differentiable functions β:N→R\beta: \cN \rightarrow \RR defined on y∈N\y \in \cN with y=f(w)\y=f(\w) for some w∈M\w \in \cM. We then want to pull back the derivatives of β\beta when precomposing it by ff. Therefore, the space on which the VJP acts is the space of directional derivatives of any β:N→R\beta: \cN \rightarrow \RR at y\y, defining the cotangent space.

Definition 2.26 (Cotangent space)

The cotangent space of a manifold N\cN at y∈N\y \in \cN is defined as Ty∗N={u=∂β(y) for any β:N→R differentiable}={u:TyN→R for any linear map u},\begin{align*} \cT_\y^* \mathcal{N} & = \{ u = \partial \beta(\y) \ \mbox{for any} \ \beta: \cN \rightarrow \RR \ \mbox{differentiable} \} \\ & = \{u: \cT_\y \mathcal{N} \rightarrow \RR \ \mbox{for any linear map} \ u \}, \end{align*}

Note that elements of the cotangent space are linear mappings, not vectors. This distinction is important to define the pullback operator as an operator on functions as done in measure theory. From a linear algebra viewpoint, the cotangent space is exactly the dual space of TyN\cT_\y\mathcal{N}, that is, the set of linear maps from TyN\cT_\y \mathcal{N} to R\RR, called linear forms. As TyN\cT_\y \mathcal{N} is a Euclidean space, its dual space Ty∗N\cT_\y^* \mathcal{N} is also a Euclidean space. The pullback operator is then defined as the operator that gives access to directional derivatives of β∘f\beta \circ f given the directional derivative of β\beta at f(w)f(\w).

Definition 2.27 (Pullback operator)

Given two manifolds M\cM and N\cN, the pullback operator of a differentiable function f:M→Nf: \mathcal{M} \rightarrow \mathcal{N} at w∈M\w \in \mathcal{M} is the linear map ∂f(w)⋆:Tf(w)∗N→Tw∗M\partial f(\w)^\star: \mathcal{T}_{f(\w)}^*\mathcal{N} \rightarrow \mathcal{T}_{\w}^* \mathcal{M} defined by ∂f(w)⋆u≔∂(β∘f)(w), \partial f(\w)^\star u \coloneqq \partial (\beta \circ f)(\w), for any u∈Tf(w)N∗u \in \cT_{f(\w)} \mathcal{N}^* such that ∂β(f(w))=u\partial \beta( f(\w)) = u, for a differentiable function β:N→R\beta: \cN \rightarrow \RR.

Contrary to the pushforward operator that acts on vectors, the pullback operator acts on linear forms. Hence, the slight difference in notation between ∂f(w)⋆\partial f(\w)^\star and ∂f(w)∗\partial f(\w)^*, the adjoint operator of ∂f(w)\partial f(\w). To properly define the adjoint operator ∂f(w)∗\partial f(\w)^*, we need a notion of inner product. Since tangent spaces are Euclidean spaces, we can define an inner product ⟨⋅,⋅⟩w\langle \cdot, \cdot \rangle_\w for each TwM\cT_\w \cM and w∈M\w\in \cM, making M\cM a Riemannian manifold. Equipped with these inner products, the cotangent space can be identified with the tangent space, and we can define gradients.

Definition 2.28 (Gradients in Riemannian manifolds)

Let M\cM be a Riemannian manifold equipped with inner products ⟨⋅,⋅⟩w\langle \cdot, \cdot\rangle_\w . For any cotangent vector u∈Tw∗Mu \in \cT_\w^* \mathcal{M}, with w∈M\w \in \cM, there exists a unique tangent vector u∈TwM\u \in \cT_\w \mathcal{M} such that ∀v∈TwM, u[v]=⟨u,v⟩w. \forall \v \in \cT_\w \mathcal{M}, \ u[\v] = \langle \u, \v\rangle_\w. In particular for any differentiable function f:M→Rf: \cM \rightarrow \RR, we can define the gradient of ff as the unique tangent vector ∇f(w)∈TwM\nabla f(\w) \in \cT_\w \mathcal{M} such that ∀v∈TwM, ∂f(w)[v]=⟨∇f(w),v⟩. \forall \v \in \cT_\w \mathcal{M}, \ \partial f(\w)[\v] = \langle \nabla f(\w), \v\rangle.

Therefore, rather than pulling back directional derivatives, we can pull back gradients. The corresponding operator is then naturally the adjoint ∂f(w)∗\partial f(\w)^* of the pushforward operator. Namely, given two Riemannian manifolds M\cM and N\cN, and a differentiable function f:M→Nf: \mathcal{M} \rightarrow \mathcal{N}, we have (∂f(w)⋆u)[v]=⟨∂f(w)∗[u],v⟩ for any v∈TwM \left(\partial f(\w)^\star u\right)[\v] = \langle \partial f(\w)^*[\u], \v\rangle \ \mbox{for any} \ \v \in \cT_\w \mathcal{M} for u=⟨⋅,u⟩∈Tf(w)∗Nu = \langle \cdot, \u\rangle \in \cT_{f(\w)}^* \mathcal{N} represented by u∈Tf(w)N\u \in \cT_{f(\w)} \mathcal{N}.

Function ff M→N\cM \rightarrow \cN
Push-forward ∂f(w)\partial f(\w) TwM→Tf(w)N\cT_\w\mathcal{M} \rightarrow \cT_{f(\w)} \mathcal{N}
Pullback ∂f(w)⋆\partial f(\w)^\star Tf(w)∗N→Tw∗M\cT_{f(\w)}^*\mathcal{N} \rightarrow \cT_\w^* \mathcal{M}
Adjoint of pushforward ∂f(w)∗\partial f(\w)^* Tf(w)N→TwM\cT_{f(\w)} \mathcal{N} \rightarrow \cT_\w \mathcal{M}

Table 2.1: For a differentiable function ff defined from a manifold M\cM onto a manifold N\cN, the JVP is generalized with the notion of pushforward ∂f(w)\partial f(\w). The counterpart of the pushforward is the pullback operation ∂f(w)⋆\partial f(\w)^\star that acts on linear forms in the tangent spaces. For Riemannian manifolds, the pullback operation can be identified with the adjoint operator ∂f(w)∗\partial f(\w)^* of the pushforward operator as any linear form is represented by a vector.

Example 2.9 (The sphere as a manifold)

The sphere SP\cS^P in RP\RR^P is defined as the set of points w∈RP\w\in \RR^P, satisfying c(w)≔⟨w,w⟩−1=0c(\w) \coloneqq \langle \w, \w \rangle - 1 = 0, with JVP ∂c(w)[v]=2⟨w,v⟩\partial c(\w)[\v] = 2\langle \w, \v\rangle.

Let us first understand why we may locally see the sphere SP\cS^P as a space of dimension P−1P-1. Take the hemisphere H={w∈SP:wP≥0}\cH = \{\w \in \cS^P: w_P \geq 0\}, it can be entirely described by using the first P−1P-1 coordinates of each point, the last one is simply given by wP=1−∑i=1P−1wi2w_P= \sqrt{1- \sum_{i=1}^{P-1}w_i^2}. Formally, for any vector v=(v1,…,vP−1)\v = (v_1, \ldots, v_{P-1}) of P−1P-1 coordinates such that ∑i=1P−1vi2≤1\sum_{i=1}^{P-1} v_i^2 \leq 1, we can define ψ1(v)≔1−∑i=1P−1vi2\psi_1(\v) \coloneqq \sqrt{1- \sum_{i=1}^{P-1} v_i^2} and ψ(v)≔(v1,…,vP−1,ψ1(v))\psiv(\v) \coloneqq (v_1, \ldots, v_{P-1}, \psi_1(\v)). We have then ⟨ψ(v),ψ(v)⟩=1\langle \psiv(\v), \psiv(\v)\rangle = 1, that is c(ψ(v))=0c(\psiv(\v)) = 0. We can then describe the hemisphere H\cH as the image of the unit ball B={v=(v1,…,vP−1):∑i=1P−1vi2≤1}B = \{\v = (v_1, \ldots, v_{P-1}): \sum_{i=1}^{P-1} v_i^2 \leq 1\} in dimension P−1P-1 through the mapping ψ\psi, that is, H=ψ(B)\cH = \psi(B). In particular, any open set on the hemisphere can be described as the mapping of an open set in RP−1\RR^{P-1}. The reasoning applies to all hemispheres with different mappings, hence the local aspect of this viewpoint.

The tangent space can be naturally characterized in terms of the constraining function cc. Namely, the curve α:R→SP\alpha: \RR \rightarrow \cS^P such that α(0)=w\alpha(0) =\w satisfies for any δ∈R\delta\in \RR, c(α(δ))=0c(\alpha(\delta)) = \zeros. Hence, differentiating the implicit equation, we have (c∘α)′(0)=∂c(w)[α′(0)]. (c\circ \alpha)'(0) = \partial c(\w)[\alpha'(0)]. That is, α′(0)\alpha'(0) is in the null space of ∂c(w)\partial c(\w), denoted Null(∂c(w))≔{v∈RP:∂c(w)[v]=0}. \mathrm{Null}(\partial c(\w)) \coloneqq \{\v \in \RR^P: \partial c(\w)[\v] =0\}. The tangent space of SP\cS^P at w\w is then TwM=Null(2⟨w,⋅⟩)={v∈RP:⟨w,v⟩=0}\begin{align*} \cT_\w \mathcal{M} & = \mathrm{Null}(2 \langle \w, \cdot\rangle) \\ & = \{ \v \in \RR^P: \langle \w, \v\rangle = 0\} \end{align*} We naturally recover that the tangent space is a Euclidean space of dimension P−1P-1, defined as the set of points orthogonal to w\w.

2.7 Generalized derivatives

While we largely focus on differentiable functions in this book, it is important to characterize non-differentiable functions. We distinguish here two cases: continuous functions and non-continuous functions. For the former case, there exist generalizations of the notion of directional derivative, gradient and Jacobian, presented below. For non-continuous functions, even if derivatives exist almost everywhere, they may be uninformative. For example, piecewise-constant functions, encountered in e.g. control flows (Chapter 5), are almost everywhere differentiable but with zero derivatives. In such cases, surrogate functions can be defined to ensure the differentiability of a program (Part IV).

2.7.1 Rademacher’s theorem

We first recall the definition of (locally) Lipschitz continuous function.

Definition 2.29 ((Locally) Lipschitz continuous function)

A function f:E→Ff: \cE \rightarrow \cF, is Lipschitz continuous if there exists C≥0C\geq 0 such that for any x,y∈E\x, \y \in \cE, ∥f(x)−f(y)∥≤C∥x−y∥. \|f(\x)- f(\y)\| \leq C\|\x-\y\|. A function f:E→Ff: \cE \rightarrow \cF is locally Lipschitz continuous if for any x∈E\x \in \cE, there exists a neighborhood U\cU of x\x such that ff restricted to U\cU is Lipschitz continuous.

Rademacher’s theorem (Rademacher, 1919) then ensures that ff is differentiable almost everywhere.

Proposition 2.11 (Rademacher’s theorem)

Let E\cE and F\cF denote Euclidean spaces. If f:E→Ff: \cE \rightarrow \cF is locally Lipschitz-continuous, then ff is almost everywhere differentiable, that is, the set of points in E\cE at which ff is not differentiable is of (Lebesgue) measure zero.

See also Morrey Jr (2009) for a standard proof.

2.7.2 Clarke derivatives

Rademacher’s theorem hints that the definitions of directional derivatives, gradients and Jacobians may be generalized to locally Lipschitz continuous functions. This is what Clarke (1975) did in his seminal work, which laid the foundation of nonsmooth analysis. The first building block is a notion of generalized directional derivative.

Definition 2.30 (Clarke generalized directional derivative)

The Clarke generalized directional derivative of a locally Lipschitz continuous function f:E→Rf: \cE \rightarrow \RR at w∈E\w \in \cE in the direction v∈E\v \in \cE is ∂Cf(w)[v]≔lim sup⁡u→wδ↘0f(u+δv)−f(u)δ, \partial_C f(\w)[\v] \coloneqq \limsup_{\substack{\u \rightarrow \w \\ \delta \searrow 0}} \frac{f(\u + \delta \v) - f(\u)}{\delta}, provided that the limit exists, where δ↘0\delta \searrow 0 means that δ\delta approaches 00 by non-negative values, and where the limit superior is defined as lim sup⁡x→af(x)≔lim⁡ε→0sup⁡{f(x):x∈B(a,ε)∖{a}} \limsup_{\x \rightarrow \a} f(\x) \coloneqq \lim_{\varepsilon \rightarrow 0} \sup\{f(\x) : \x \in B(\a, \varepsilon)\setminus\{\a\}\} for B(a,ε)≔{x∈E:∥x−a∥≤ε}B(\a, \varepsilon) \coloneqq \{\x \in \cE: \|\x-\a\|\leq \varepsilon \} the ball centered at a\a of radius ε\varepsilon.

There are two differences with the usual definition of a directional derivative: (i) we consider slopes of the function in a neighborhood of the point rather than at the given point, (ii) we take a limit superior rather than a usual limit. The first point is rather natural in the light of Rademacher’s theorem: we can properly characterize variations on points where the function is differentiable, therefore we may take the limits of these slopes as a candidate slope for the point of interest. The second point is more technical but essential: it allows us to characterize the directional derivative as the supremum of some linear forms (Clarke et al., 2008). These linear forms in turn define a set of generalized gradients (Clarke et al., 2008, 2).

Definition 2.31 (Clarke generalized gradient)

A Clarke generalized gradient of a locally Lipschitz function f:E→Rf: \cE\rightarrow \RR at w∈E\w \in \cE is a point g∈E\g \in \cE such that ∀v∈E\forall \v \in \cE ∂Cf(w)[v]≥⟨g,v⟩. \partial_C f(\w)[\v] \geq \langle \g, \v \rangle. The set of Clarke generalized gradients is called the Clarke subdifferential of ff at w\w.

Definition 2.30 and Definition 2.31 can be used in non-Euclidean spaces, such as Banach or Hilbert spaces (Clarke et al., 2008). In Euclidean spaces, the Clarke generalized gradients can be characterized more simply thanks to Rademacher’s theorem (Clarke et al., 2008, Theorem 8.1). Namely, as shown below, they can be defined as a convex combination of limits of gradients of ff evaluated at a sequence in E∖Ω\cE\setminus \Omega that converges to w\w.

Proposition 2.12 (Characterization of Clarke generalized gradients)

Let f:E→Rf: \cE\rightarrow \RR be a locally Lipschitz continuous and denote Ω\Omega the set of points at which ff is not differentiable (Proposition 2.11). An element g∈E\g \in \cE is a Clarke generalized gradient of ff at w∈E\w \in \cE if and only if g∈conv⁡({lim⁡n→+∞∇f(vn) ⁣:(vn)n=1+∞ s.t. vn∈E∖Ω, vn→n→+∞w}).\begin{align*} \g & \in \conv\left(\left\{ \lim_{n \rightarrow +\infty} \nabla f(\v_n) \colon (\v_n)_{n=1}^{+\infty} \ \mbox{s.t.} \ \v_n \in \cE\setminus \Omega, \ \v_n \underset{n \rightarrow + \infty}{\rightarrow} \w \right\}\right). \end{align*}

In the above, the convex hull of a set S⊆ES\subseteq \cE, the set of convex combinations of elements of SS, is denoted conv⁡(S)≔{λ1s1+…+λmsm  ⁣:m∈N, λi≥0,∑i=1mλi=1,si∈S}. \conv(S) \coloneqq \{\lambda_1 \s_1 + \ldots + \lambda_m \s_m \ \colon m \in \NN, \ \lambda_i \geq 0, \sum_{i=1}^{m} \lambda_i = 1, \s_i \in S\}.

The Jacobian of a function f:E→Ff: \cE \rightarrow \cF between two Euclidean spaces can be generalized similarly (Clarke et al., 2008, Section 3.3).

Definition 2.32 (Clarke generalized Jacobian)

Let f:E→Ff: \cE\rightarrow \cF be locally Lipschitz continuous and denote Ω\Omega the set of points at which ff is not differentiable (Proposition 2.11). A Clarke generalized Jacobian of ff at w∈E\w \in \cE is an element J\mathbf{J} of conv⁡({lim⁡n→+∞∂f(vn) ⁣:(vn)n=1+∞ s.t. vn∈E∖Ω, vn→n→+∞w}). \conv\left(\left\{ \lim_{n \rightarrow +\infty} \jac f(\v_n) \colon (\v_n)_{n=1}^{+\infty} \ \mbox{s.t.} \ \v_n \in \cE\setminus \Omega, \ \v_n \underset{n \rightarrow + \infty}{\rightarrow} \w \right\}\right).

For a continuously differentiable function f:E→Ff: \cE \rightarrow \cF or f:E→Rf:\cE \rightarrow \RR, there is a unique generalized gradient, recovering the usual gradient (Clarke et al., 2008, Proposition 3.1, page 78). The chain rule can be generalized to these objects (Clarke et al., 2008). Recently, (Bolte et al., 2022; Bolte and Pauwels; 2020) further generalized Clarke gradients through the definition of conservative gradients to define automatic differentiation schemes for nonsmooth functions.

2.8 Summary