目录微分

第 2 章 微分Chapter 2 Differentiation

在本章中,我们回顾关键的微分概念。特别地,我们强调线性映射所起的基础性作用。In this chapter, we review key differentiation concepts. In particular, we emphasize the fundamental role played by linear maps.

2.1 一元函数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.

定义 2.1.1(极限)。Definition 2.1.1 (Limit).
我们说,当 v∈Rv\in\mathbb{R} 趋近于 w∈Rw\in\mathbb{R} 时,c∈Rc\in\mathbb{R} 是 f ⁣:R→Rf\colon\mathbb{R}\to\mathbb{R} 的极限,记作We say that c∈Rc\in\mathbb{R} is the limit of f ⁣:R→Rf\colon\mathbb{R}\to\mathbb{R} as v∈Rv\in\mathbb{R} approaches w∈Rw\in\mathbb{R}, denoted lim⁡v→wf(v)=c,\lim_{v\rightarrow w}f(v)=c, 若对任意 ε>0\varepsilon>0,存在 R>0R>0,使得对任意满足 0<∣v−w∣≤R0<|v-w|\leq R 的 v∈Rv\in\mathbb{R},都有 ∣f(v)−c∣≤ε|f(v)-c|\leq\varepsilon。if, for any ε>0\varepsilon>0, there exists R>0R>0 such that for any v∈Rv\in\mathbb{R} satisfying 0<∣v−w∣≤R0<|v-w|\leq R, we have ∣f(v)−c∣≤ε|f(v)-c|\leq\varepsilon.

我们也可以将 f(v)→cf(v)\to c 写作 v→wv\to w。极限在加法与乘法下保持不变。即,若 lim⁡v→wf(v)=c\lim_{v\rightarrow w}f(v)=c 且 lim⁡v→wg(v)=d\lim_{v\rightarrow w}g(v)=d,则对任意 a,b∈Ra,b\in\mathbb{R} 与 (fg)(w):=f(w)g(w)(fg)(w)\coloneqq f(w)g(w),记 (af+bg)(w):=af(w)+bg(w)(af+bg)(w)\coloneqq af(w)+bg(w),根据极限的定义,我们有 lim⁡v→w(af+bg)(v)=ac+bd\lim_{v\rightarrow w}(af+bg)(v)=ac+bd 与 lim⁡v→w(fg)(v)=cd\lim_{v\rightarrow w}(fg)(v)=cd。极限在加法与数乘下保持不变,这通常被称为极限的线性性,后续许多定义都继承了这一性质。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\mathbb{R} 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.

定义 2.1.2(连续函数)。Definition 2.1.2 (Continuous function).
函数 f:R→Rf:\mathbb{R}\rightarrow\mathbb{R} 在点 w∈Rw\in\mathbb{R} 处连续,若A function f:R→Rf:\mathbb{R}\rightarrow\mathbb{R} is continuous at a point w∈Rw\in\mathbb{R} if lim⁡v→wf(v)=f(w).\lim_{v\rightarrow w}f(v)=f(w). 若函数 ff 在其定义域内的所有点处都连续,则称该函数为连续函数。A function ff is said to be continuous if it is continuous at all points in its domain.

尽管连续性这一概念看似是一个温和的假设,但机器学习中常用的若干函数,例如 Heaviside 阶跃函数(见图 2.2 左图),并不连续,需要特殊处理。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.

注 2.1.3(小 oo 记号)。Remark 2.1.3 (Little oo notation).
下面我们将使用兰道小 oo 记号。我们写In the following, we will make use of Landau’s little oo notation. We write g(v)=o(f(v)) as v→wg(v)=o(f(v))\ \text{as}\ v\rightarrow w 如果if lim⁡v→w∣g(v)∣∣f(v)∣=0.\lim_{v\rightarrow w}\frac{|g(v)|}{|f(v)|}=0. 也就是说,函数ff在极限v→wv\to w下支配gg。例如,ff在ww处连续,当且仅当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)\ \text{as}\ \delta\rightarrow 0.

现在我们解释导数。考虑一个函数 f:R→Rf:\mathbb{R}\rightarrow\mathbb{R}。如图 2.1 所示,该函数在区间 [w0,w0+δ][w_{0},w_{0}+\delta] 上的值可以用其取值 f(w0)f(w_{0}) 与 f(w0+δ)f(w_{0}+\delta) 之间的割线来近似,这是一条斜率为 (f(w0+δ)−f(w0))/δ(f(w_{0}+\delta)-f(w_{0}))/\delta 的线性函数。在 w0w_{0} 附近的无穷小变化 δ\delta 的极限下,割线收敛到 ff 在 w0w_{0} 处的切线,由此得到的斜率定义了 ff 在 w0w_{0} 处的导数。下面的定义将这一直观形式化。We now explain derivatives. Consider a function f:R→Rf:\mathbb{R}\rightarrow\mathbb{R}. 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.

定义 2.1.4(导数)。Definition 2.1.4 (Derivative).
f:R→Rf:\mathbb{R}\rightarrow\mathbb{R}在w∈Rw\in\mathbb{R}处的导数定义为The derivative of f:R→Rf:\mathbb{R}\rightarrow\mathbb{R} at w∈Rw\in\mathbb{R} is defined as f′(w):=lim⁡δ→0f(w+δ)−f(w)δ,f^{\prime}(w)\coloneqq\lim_{\delta\rightarrow 0}\frac{f(w+\delta)-f(w)}{\delta}, (2.1) 前提是该极限存在。若 f′(w)f^{\prime}(w) 在某个 ww 处有良定义,则称函数 ff 在 ww 处可微。若 ff 在任意 w∈Rw\in\mathbb{R} 处可微,则称其处处可微,或简称可微。provided that the limit exists. If f′(w)f^{\prime}(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\mathbb{R}, we say that it is differentiable everywhere or differentiable for short.
图 2.1:函数 ff 可以在点 w0w_{0} 附近由一条割线局部近似,该割线是斜率为 aa、截距为 bb 的线性函数 w↦aw+bw\mapsto aw+b,它在 w0w_{0} 处穿过 ff 且取值为 u0=f(w0)u_{0}=f(w_{0}),并在 w0+δw_{0}+\delta 处穿过且取值为 uδ=f(w0+δ)u_{\delta}=f(w_{0}+\delta)。利用 u0=aw0+bu_{0}=aw_{0}+b 与 uδ=a(w0+δ)+bu_{\delta}=a(w_{0}+\delta)+b,我们发现其斜率为 a=(f(w0+δ)−f(w0))/δa=(f(w_{0}+\delta)-f(w_{0}))/\delta,截距为 b=f(w0)−aw0b=f(w_{0})-aw_{0}。函数 ff 在点 w0w_{0} 处的导数 f′(w)f^{\prime}(w) 则定义为当 δ→0\delta\to 0 时斜率 aa 的极限。它是 ff 在 w0w_{0} 处切线的斜率。于是,函数在 ww 处的值 f(w)f(w) 可以在 w0w_{0} 附近由 w↦f′(w0)w+f(w0)−f′(w0)w0=f(w0)+f′(w0)(w−w0)w\mapsto f^{\prime}(w_{0})w+f(w_{0})-f^{\prime}(w_{0})w_{0}=f(w_{0})+f^{\prime}(w_{0})(w-w_{0}) 局部近似。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}=aw_{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^{\prime}(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^{\prime}(w_{0})w+f(w_{0})-f^{\prime}(w_{0})w_{0}=f(w_{0})+f^{\prime}(w_{0})(w-w_{0}).

若 ff 在给定的 ww 处可微,则它在该点 ww 处必然连续,如下面的命题所示。连续函数在给定点 ww 处不可微,通常表现为一个折角,如图 2.2 所示。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.

命题 2.1.5(可微蕴含连续)。Proposition 2.1.5 (Differentiability implies continuity).
若 f:R→Rf:\mathbb{R}\rightarrow\mathbb{R} 在 w∈Rw\in\mathbb{R} 处可微,则它在 w∈Rw\in\mathbb{R} 处连续。If f:R→Rf:\mathbb{R}\rightarrow\mathbb{R} is differentiable at w∈Rw\in\mathbb{R}, then it is continuous at w∈Rw\in\mathbb{R}.
证明 2.1.6。Proof 2.1.6.

用小 oo 记号表示时,ff 在 ww 处可微,如果存在 f′(w)∈Rf^{\prime}(w)\in\mathbb{R},使得In little oo notation, ff is differentiable at ww if there exists f′(w)∈Rf^{\prime}(w)\in\mathbb{R}, such that

f(w+δ)=f(w)+f′(w)δ+o(δ) as δ→0.f(w+\delta)=f(w)+f^{\prime}(w)\delta+o(\delta)\ \text{as}\ \delta\rightarrow 0.

由于当 δ→0\delta\rightarrow 0 时 f′(w)δ+o(δ)=o(1)f^{\prime}(w)\delta+o(\delta)=o(1),故 ff 在 ww 处连续。Since f′(w)δ+o(δ)=o(1)f^{\prime}(w)\delta+o(\delta)=o(1) as δ→0\delta\rightarrow 0, ff is continuous at ww.

图 2.2:不连续与不可微的图示。左图:一个不连续函数,其函数值在 00 处发生跳跃。中图:一个处处连续却处处不可微的函数,在 −1-1 与 11 处有折角。右图:一个处处可微的函数。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.

导数 f′f^{\prime} 是 ff 在 ww 处切线的斜率,因而它除了能够构造 ff 在 ww 邻域内的线性近似之外,还能揭示 ff 在 ww 附近的单调性。若 f′(w)f^{\prime}(w) 为正,则函数在 ww 附近递增;反之,若 f′(w)f^{\prime}(w) 为负,则函数递减。这类信息可用于设计梯度下降等迭代算法:该类算法通过计算形如 wt+1=wt−γf′(wt)w_{t+1}=w_{t}-\gamma f^{\prime}(w_{t})(其中 γ>0\gamma>0)的迭代值,沿着 ff 在 wtw_{t} 附近的下降方向不断移动,以最小化 ff。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^{\prime} informs us about the monotonicity of ff around ww. If f′(w)f^{\prime}(w) is positive, the function is increasing around ww. Conversely, if f′(w)f^{\prime}(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^{\prime}(w_{t}) for γ>0\gamma>0, thereby moving along descent directions of ff around wtw_{t}.

对于若干初等函数,例如 wnw^{n}、ewe^{w}、ln⁡w\ln w、cos⁡w\cos w 或 sin⁡w\sin w,其导数均可直接通过应用式 2.1 中导数的定义求得,下面我们对此加以说明。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.

例 2.1.7(幂函数的导数)。Example 2.1.7 (Derivative of power function).
考虑针对 w∈Rw\in\mathbb{R} 的 f(w)=wnf(w)=w^{n},n∈N∖{0}n\in\mathbb{N}\setminus\{0\}。对任意 δ∈R\delta\in\mathbb{R},有Consider f(w)=wnf(w)=w^{n} for w∈Rw\in\mathbb{R}, n∈N∖{0}n\in\mathbb{N}\setminus\{0\}. For any δ∈R\delta\in\mathbb{R}, we have f(w+δ)−f(w)δ\displaystyle\frac{f(w+\delta)-f(w)}{\delta} =(w+δ)n−wnδ\displaystyle=\frac{(w+\delta)^{n}-w^{n}}{\delta} =∑k=0n(nk)δkwn−k−wnδ\displaystyle=\frac{\sum_{k=0}^{n}\binom{n}{k}\delta^{k}w^{n-k}-w^{n}}{\delta} =∑k=1n(nk)δk−1wn−k\displaystyle=\sum_{k=1}^{n}\binom{n}{k}\delta^{k-1}w^{n-k} =(n1)wn−1+∑k=2n(nk)δk−1wn−k,\displaystyle=\binom{n}{1}w^{n-1}+\sum_{k=2}^{n}\binom{n}{k}\delta^{k-1}w^{n-k}, 其中第二行我们使用了二项式定理。由于 (n1)=n\binom{n}{1}=n 且 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 ,我们得到 f′(w)=nwn−1f^{\prime}(w)=nw^{n-1} 。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^{\prime}(w)=nw^{n-1}.
注 2.1.8(定义在 R\mathbb{R} 的子集 U\mathcal{U} 上的函数)。Remark 2.1.8 (Functions on a subset U\mathcal{U} of R\mathbb{R}).
为简单起见,我们给出的是定义在整个实数集R\mathbb{R}上的函数的导数定义。若函数f:U→Rf:\mathcal{U}\rightarrow\mathbb{R}定义在实数集的一个子集U⊆R\mathcal{U}\subseteq\mathbb{R}上——正如f(w)=wf(w)=\sqrt{w}定义在U=R+\mathcal{U}=\mathbb{R}_{+}上那样——则ff在w∈Uw\in\mathcal{U}处的导数由式 2.1 中的极限定义,前提是函数ff在ww的某个邻域上有良定义,即存在r>0r>0,使得对任意∣δ∣≤r|\delta|\leq r都有w+δ∈Uw+\delta\in\mathcal{U}。若函数ff在U\mathcal{U}内部的任意点ww处都可微,则称其处处可微,或简称可微;其中内部是指满足如下条件的点w∈Uw\in\mathcal{U}的集合:对充分小的rr,有{w+δ:∣δ∣≤r}⊆U\{w+\delta:|\delta|\leq r\}\subseteq\mathcal{U}。对于位于U\mathcal{U}边界上的点(例如当U=[a,b]\mathcal{U}=[a,b]时,aa与bb),可以定义ff在aa处与bb处的右导数与左导数,这意味着极限是通过从右侧逼近aa或从左侧逼近bb来取的。For simplicity, we presented the definition of the derivative for a function defined on the whole set of real numbers R\mathbb{R}. If a function f:U→Rf:\mathcal{U}\rightarrow\mathbb{R} is defined on a subset U⊆R\mathcal{U}\subseteq\mathbb{R} of the real numbers, as it is the case for f(w)=wf(w)=\sqrt{w} defined on U=R+\mathcal{U}=\mathbb{R}_{+}, the derivative of ff at w∈Uw\in\mathcal{U} 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\mathcal{U} 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\mathcal{U}, the set of points w∈Uw\in\mathcal{U} such that {w+δ:∣δ∣≤r}⊆U\{w+\delta:|\delta|\leq r\}\subseteq\mathcal{U} for rr sufficiently small. For points lying at the boundary of U\mathcal{U} (such as aa and bb if U=[a,b]\mathcal{U}=[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

给定 w∈Rw\in\mathbb{R} 以及两个函数 f:R→Rf:\mathbb{R}\rightarrow\mathbb{R} 与 g:R→Rg:\mathbb{R}\rightarrow\mathbb{R},只要 ff 与 gg 在 ww 处的可微性满足适当条件,便可根据导数的定义容易地推出对 ff 与 gg 所做初等运算(如求和、求积或复合)的导数。例如,若 ff 与 gg 的导数在 ww 处存在,则其加权和或乘积的导数存在,并满足以下法则For a given w∈Rw\in\mathbb{R} and two functions f:R→Rf:\mathbb{R}\rightarrow\mathbb{R} and g:R→Rg:\mathbb{R}\rightarrow\mathbb{R}, 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)\displaystyle\forall a,b\in\mathbb{R},\ (af+bg)^{\prime}(w) =af′(w)+bg′(w)\displaystyle=af^{\prime}(w)+bg^{\prime}(w) (线性性)(Linearity)
(fg)′(w)\displaystyle(fg)^{\prime}(w) =f′(w)g(w)+f(w)g′(w),\displaystyle=f^{\prime}(w)g(w)+f(w)g^{\prime}(w), (乘积法则)(Product rule)

其中 (fg)(w):=f(w)g(w)(fg)(w)\coloneqq f(w)g(w)。线性性可直接由极限的线性性验证。至于乘积法则,采用小 oo 记号,当 δ→0\delta\rightarrow 0 时,我们有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+δ)\displaystyle(fg)(w+\delta) =(f(w)+f′(w)δ+o(δ))(g(w)+g′(w)δ+o(δ))\displaystyle=(f(w)+f^{\prime}(w)\delta+o(\delta))(g(w)+g^{\prime}(w)\delta+o(\delta))
=f(w)g(w)+f′(w)g(w)δ+f(w)g′(w)δ+o(δ),\displaystyle=f(w)g(w)+f^{\prime}(w)g(w)\delta+f(w)g^{\prime}(w)\delta+o(\delta),

由此得证。hence the result.

若 gg 在 ww 处的导数与 ff 在 g(w)g(w) 处的导数均存在,则复合函数 (f∘g)(w):=f(g(w))(f\circ g)(w)\coloneqq f(g(w)) 在 ww 处的导数存在,且由下式给出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).\displaystyle\hskip 54.0pt(f\circ g)^{\prime}(w)=f^{\prime}(g(w))g^{\prime}(w). (链式法则)(Chain rule)

我们将在命题 2.2.11 中对这一结果给出更一般的证明。正如后文所示,线性性与乘积法则可视为链式法则的推论,因此链式法则构成了微分学的基石。We prove this result more generally in Proposition 2.2.11. 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.

考虑一个可由初等函数的和、积或复合表示的函数,例如 f(w):=ewln⁡w+cos⁡w2f(w)\coloneqq e^{w}\ln w+\cos w^{2}。将 ff 分解为初等运算与初等函数,再应用上述法则,即可计算其导数。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.

例 2.1.9(应用求导法则)。Example 2.1.9 (Applying rules of differentiation).
考虑f(w):=ewln⁡w+cos⁡w2f(w)\coloneqq e^{w}\ln w+\cos w^{2}。ff在w>0w>0处的导数可按如下步骤逐步计算,记sq⁡(w):=w2\operatorname{sq}(w)\coloneqq w^{2},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\operatorname{sq}(w)\coloneqq w^{2}, f′(w)\displaystyle f^{\prime}(w) =(exp⁡⋅ln⁡)′(w)+(cos⁡∘sq⁡)′(w)\displaystyle=(\exp\cdot\ln)^{\prime}(w)+(\cos\circ\operatorname{sq})^{\prime}(w) (Linearity) (exp⁡⋅ln⁡)′(w)\displaystyle (\exp\cdot\ln)^{\prime}(w) =exp⁡′(w)⋅ln⁡(w)+exp⁡(w)⋅ln⁡′(w)\displaystyle=\exp^{\prime}(w)\cdot\ln(w)+\exp(w)\cdot\ln^{\prime}(w) (Product rule) (cos⁡∘sq⁡)′(w)\displaystyle \quad(\cos\circ\operatorname{sq})^{\prime}(w) =cos⁡′(sq⁡(w))sq⁡′(w)\displaystyle=\cos^{\prime}(\operatorname{sq}(w))\operatorname{sq}^{\prime}(w) (Chain rule) exp⁡′(w)\displaystyle \exp^{\prime}(w) =exp⁡(w),ln⁡′(w)=1/w,\displaystyle=\exp(w),\hskip 30.0pt\ln^{\prime}(w)=1/w, (Elem. func.) sq⁡′(w)\displaystyle \operatorname{sq}^{\prime}(w) =2w,cos⁡′(w)=−sin⁡(w).\displaystyle=2w,\hskip 44.0pt\cos^{\prime}(w)=-\sin(w). (Elem. func.) 因此我们得到f′(w)=ewln⁡w+ew/w−2wsin⁡w2f^{\prime}(w)=e^{w}\ln w+e^{w}/w-2w\sin w^{2}。We therefore obtain that f′(w)=ewln⁡w+ew/w−2wsin⁡w2f^{\prime}(w)=e^{w}\ln w+e^{w}/w-2w\sin w^{2}.

这一过程完全是机械性的,因而适合交由自动化流程完成,这正是第 8 章所介绍的自动微分的主要思想。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

导数的概念最早由牛顿与莱布尼茨于 18 世纪各自独立提出 [9]。后者将导数视为无穷小变化量之商。也就是说,记 u=f(w)u=f(w) 为通过 ff 依赖于 ww 的变量,莱布尼茨便将 ff 的导数视为如下商The notion of derivative was first introduced independently by Newton and Leibniz in the 18th century [9]. 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∣wf^{\prime}=\frac{du}{dw}\quad\text{with}\quad f^{\prime}(w)=\left.\frac{du}{dw}\right|_{w}

其中 dudu 与 dwdw 分别表示 uu 与 ww 的无穷小变化量,符号 ∣w|_{w} 则表示在给定点 ww 处对导数求值。这一记号简化了莱布尼茨最早发现的链式法则 [170] 的表述,因为对于 v=g(w)v=g(w) 与 u=f(v)u=f(v),我们有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 [170] 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^{\prime}(g(w))g^{\prime}(w)=(f\circ g)^{\prime}(w).

莱布尼茨记号能将链式法则表达为商之乘积,这一能力使其在数个世纪以来广受欢迎,尤其是在力学领域 [9]。莱布尼茨记号背后的原理,即“无穷小变化量”这一概念,因其潜在的逻辑问题而受到后来数学家的质疑 [9]。由欧拉首次引入、后经拉格朗日进一步推广的记号 f′(w)f^{\prime}(w) [40] 随之在众多数学教材中占据主流。无穷小变化量的概念已借助超实数集得到严格定义。超实数集将每个数视为非无穷小部分与无穷小部分之和,从而扩展了实数集 [94]。无穷小变化量的形式化还通过对偶数这一概念,进一步支撑了自动微分算法的发展。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 [9]. The rationale behind Leibniz’s notation, the concept of “infinitesimal variations”, was questioned by later mathematicians for its potential logical issues [9]. The notation f′(w)f^{\prime}(w) first introduced by Euler and further popularized by Lagrange [40] 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 [94]. The formalism of infinitesimal variations further underlies the development of automatic differentiation algorithms through the concept of dual numbers.

2.2 多元函数2.2 Multivariate functions

2.2.1 方向导数Directional derivatives

现在我们考虑一个输入为多维的 f:RP→Rf:\mathbb{R}^{P}\rightarrow\mathbb{R} 函数,其输入为 w:=(w1,…,wP)∈RP{\bm{w}}\coloneqq(w_{1},\ldots,w_{P})\in\mathbb{R}^{P}。机器学习中最重要的例子,是这样一个函数:它将神经网络的参数 w∈RP{\bm{w}}\in\mathbb{R}^{P} 映射为 R\mathbb{R} 中的损失值。ff 的变化需要沿特定方向定义,例如 ff 在 w∈RP{\bm{w}}\in\mathbb{R}^{P} 附近沿方向 v∈RP{\bm{v}}\in\mathbb{R}^{P} 发生大小为 δ>0\delta>0 的变化 f(w+δv)−f(w)f({\bm{w}}+\delta{\bm{v}}){-}f({\bm{w}})。这一考虑自然引出了方向导数的定义。Let us now consider a function f:RP→Rf:\mathbb{R}^{P}\rightarrow\mathbb{R} with multi-dimensional input w:=(w1,…,wP)∈RP{\bm{w}}\coloneqq(w_{1},\ldots,w_{P})\in\mathbb{R}^{P}. The most important example in machine learning is a function which, to the parameters w∈RP{\bm{w}}\in\mathbb{R}^{P} of a neural network, associates a loss value in R\mathbb{R}. Variations of ff need to be defined along specific directions, such as the variation f(w+δv)−f(w)f({\bm{w}}+\delta{\bm{v}}){-}f({\bm{w}}) of ff around w∈RP{\bm{w}}\in\mathbb{R}^{P} in the direction v∈RP{\bm{v}}\in\mathbb{R}^{P} by an amount δ>0\delta>0. This consideration naturally leads to the definition of the directional derivative.

定义 2.2.1(方向导数)。Definition 2.2.1 (Directional derivative).
ff在w{\bm{w}}处沿方向v{\bm{v}}的方向导数为The directional derivative of ff at w{\bm{w}} in the direction v{\bm{v}} is given by ∂f(w)[v]:=lim⁡δ→0f(w+δv)−f(w)δ,\partial f({\bm{w}})[{\bm{v}}]\coloneqq\lim_{\delta\rightarrow 0}\frac{f({\bm{w}}+\delta{\bm{v}})-f({\bm{w}})}{\delta}, 前提是该极限存在。provided that the limit exists.

我们采用记号 [v][{\bm{v}}],以强调对给定的输入 w{\bm{w}},可将 v↦∂f(w)[v]{\bm{v}}\mapsto\partial f({\bm{w}})[{\bm{v}}] 视为一个函数。这对于定义 ff 在 w{\bm{w}} 处的可微性(定义 2.2.3),以及后文定义线性映射(2.3 节),都是必不可少的。We use the notation [v][{\bm{v}}] to emphasize that, for a given input w{\bm{w}}, we can see v↦∂f(w)[v]{\bm{v}}\mapsto\partial f({\bm{w}})[{\bm{v}}] as a function. This is essential to define the differentiability of ff (Definition 2.2.3) at w{\bm{w}} and to later define linear maps (Section 2.3).

方向导数的一个例子是:求函数 ff 在 w{\bm{w}} 处沿任一标准方向的导数One example of directional derivative consists in computing the derivative of a function ff at w{\bm{w}} in any of the canonical directions

ei:=(0,…,0,1⏟i,0,…,0).{\bm{e}}_{i}\coloneqq(0,\ldots,0,\underbrace{1}_{i},0,\ldots,0).

据此便可定义偏导数的概念,对 i∈[P]i\in[P] 记作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({\bm{w}})\coloneqq\partial f({\bm{w}})[{\bm{e}}_{i}]=\lim_{\delta\rightarrow 0}\frac{f({\bm{w}}+\delta{\bm{e}}_{i})-f({\bm{w}})}{\delta}.

用莱布尼茨记号,这也可写作 ∂if(w)=∂f(w)∂wi\partial_{i}f({\bm{w}})=\frac{\partial f({\bm{w}})}{\partial w_{i}} 或 ∂if(w)=∂wif(w)\partial_{i}f({\bm{w}})=\partial_{w_{i}}f({\bm{w}})。由于只沿函数的第 ii 个坐标移动,偏导数相当于在其余坐标均固定于 wiw_{i} 的情形下,对 ωi\omega_{i} 附近的函数 ωi↦f(w1,…,ωi,…,wP)\omega_{i}\mapsto f(w_{1},\ldots,\omega_{i},\ldots,w_{P}) 求导。This is also denoted in Leibniz’s notation as ∂if(w)=∂f(w)∂wi\partial_{i}f({\bm{w}})=\frac{\partial f({\bm{w}})}{\partial w_{i}} or ∂if(w)=∂wif(w)\partial_{i}f({\bm{w}})=\partial_{w_{i}}f({\bm{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.

定义 2.2.2(线性映射、线性泛函)。Definition 2.2.2 (Linear map, linear form).
函数 l:RP→RMl:\mathbb{R}^{P}\rightarrow\mathbb{R}^{M} 是线性映射,如果对任意 a1,a2∈Ra_{1},a_{2}\in\mathbb{R},v1,v2∈RD{\bm{v}}_{1},{\bm{v}}_{2}\in\mathbb{R}^{D},A function l:RP→RMl:\mathbb{R}^{P}\rightarrow\mathbb{R}^{M} is a linear map if for any a1,a2∈Ra_{1},a_{2}\in\mathbb{R}, v1,v2∈RD{\bm{v}}_{1},{\bm{v}}_{2}\in\mathbb{R}^{D}, l[a1v1+a2v2]=a1l[v1]+a2l[v2].l[a_{1}{\bm{v}}_{1}+a_{2}{\bm{v}}_{2}]=a_{1}l[{\bm{v}}_{1}]+a_{2}l[{\bm{v}}_{2}]. 取值于R\mathbb{R}、l:RP→Rl:\mathbb{R}^{P}\rightarrow\mathbb{R}的线性映射称为线性型。A linear map with values in R\mathbb{R}, l:RP→Rl:\mathbb{R}^{P}\rightarrow\mathbb{R}, is called a linear form.

线性性在函数的可微性中起着关键作用。Linearity plays a crucial role in the differentiability of a function.

定义 2.2.3(可微性,单输出情形)。Definition 2.2.3 (Differentiability, single-output case).
函数f:RP→Rf:\mathbb{R}^{P}\rightarrow\mathbb{R}在w∈RP{\bm{w}}\in\mathbb{R}^{P}处可微,如果其沿任意方向的方向导数都有定义、关于任意方向v{\bm{v}}都是线性的,并且如果A function f:RP→Rf:\mathbb{R}^{P}\rightarrow\mathbb{R} is differentiable at w∈RP{\bm{w}}\in\mathbb{R}^{P} if its directional derivative is defined along any direction, is linear in any direction v{\bm{v}}, and if lim⁡∥v∥2→0∣f(w+v)−f(w)−∂f(w)[v]∣∥v∥2=0,\lim_{\|{\bm{v}}\|_{2}\rightarrow 0}\frac{|f({\bm{w}}+{\bm{v}})-f({\bm{w}})-\partial f({\bm{w}})[{\bm{v}}]|}{\|{\bm{v}}\|_{2}}=0, 其中∥v∥2:=⟨v,v⟩\|{\bm{v}}\|_{2}\coloneqq\sqrt{\langle{\bm{v}},{\bm{v}}\rangle}。where ∥v∥2:=⟨v,v⟩\|{\bm{v}}\|_{2}\coloneqq\sqrt{\langle{\bm{v}},{\bm{v}}\rangle}.

现在可以引入梯度了。We can now introduce the gradient.

定义 2.2.4(梯度)。Definition 2.2.4 (Gradient).
可微函数f:RP→Rf:\mathbb{R}^{P}\rightarrow\mathbb{R}在点w∈RP{\bm{w}}\in\mathbb{R}^{P}处的梯度定义为由偏导数构成的向量The gradient of a differentiable function f:RP→Rf:\mathbb{R}^{P}\rightarrow\mathbb{R} at a point w∈RP{\bm{w}}\in\mathbb{R}^{P} is defined as the vector of partial derivatives ∇f(w):=(∂1f(w)⋮∂Pf(w))=(∂f(w)[e1]⋮∂f(w)[eP])∈RP.\nabla f({\bm{w}})\coloneqq\begin{pmatrix}\partial_{1}f({\bm{w}})\\ \vdots\\ \partial_{P}f({\bm{w}})\end{pmatrix}=\begin{pmatrix}\partial f({\bm{w}})[{\bm{e}}_{1}]\\ \vdots\\ \partial f({\bm{w}})[{\bm{e}}_{P}]\end{pmatrix}\in\mathbb{R}^{P}. 由线性性可知,ff在w{\bm{w}}处沿方向v=∑i=1Pviei{\bm{v}}=\sum_{i=1}^{P}v_{i}{\bm{e}}_{i}的方向导数即为By linearity, the directional derivative of ff at w{\bm{w}} in the direction v=∑i=1Pviei{\bm{v}}=\sum_{i=1}^{P}v_{i}{\bm{e}}_{i} is then given by ∂f(w)[v]=∑i=1Pvi∂f(w)[ei]=⟨v,∇f(w)⟩∈R. \partial f({\bm{w}})[{\bm{v}}]=\sum_{i=1}^{P}v_{i}\partial f({\bm{w}})[{\bm{e}}_{i}]=\langle{\bm{v}},\nabla f({\bm{w}})\rangle\in\mathbb{R}.

此处 ⟨⋅,⋅⟩\langle\cdot,\cdot\rangle 表示内积,其定义见 2.3.2 节关于欧几里得空间的讨论。Here, ⟨⋅,⋅⟩\langle\cdot,\cdot\rangle denotes the inner product. We provide its definition in Euclidean spaces in Section 2.3.2.

在上述定义中,梯度之所以能用来计算方向导数,不过是 ∂f(w)[v]\partial f({\bm{w}})[{\bm{v}}] 关于 v{\bm{v}} 的线性性的一个推论。然而在后文所介绍的更为抽象的情形中,梯度正是通过这一性质直接定义的。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({\bm{w}})[{\bm{v}}] w.r.t. v{\bm{v}}. However, in more abstract cases presented in later sections, the gradient is defined directly through this property.

作为一个简单的例子,任何形如 f(w)=⟨a,w⟩=∑i=1Paiwif({\bm{w}})=\langle{\bm{a}},{\bm{w}}\rangle=\sum_{i=1}^{P}a_{i}w_{i} 的线性函数都是可微的,因为对任意 v{\bm{v}} 都有 (⟨a,w+v⟩−⟨a,w⟩−⟨a,v⟩)/∥v∥2=0(\langle{\bm{a}},{\bm{w}}+{\bm{v}}\rangle-\langle{\bm{a}},{\bm{w}}\rangle-\langle{\bm{a}},{\bm{v}}\rangle)/\|{\bm{v}}\|_{2}=0,特别是当 ∥v∥→0\|{\bm{v}}\|\rightarrow 0 时也成立。此外,其梯度自然由 ∇f(w)=a\nabla f({\bm{w}})={\bm{a}} 给出。As a simple example, any linear function of the form f(w)=⟨a,w⟩=∑i=1Paiwif({\bm{w}})=\langle{\bm{a}},{\bm{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{\bm{a}},{\bm{w}}+{\bm{v}}\rangle-\langle{\bm{a}},{\bm{w}}\rangle-\langle{\bm{a}},{\bm{v}}\rangle)/\|{\bm{v}}\|_{2}=0 for any v{\bm{v}} and in particular for ∥v∥→0\|{\bm{v}}\|\rightarrow 0. Moreover, its gradient is naturally given by ∇f(w)=a\nabla f({\bm{w}})={\bm{a}}.

更一般地,若要证明一个函数可微并求出其梯度,一种方法是在 v=0{\bm{v}}={\mathbf{0}} 附近对 f(w+v)f({\bm{w}}+{\bm{v}}) 作近似。如果我们能找到一个向量 g{\bm{g}},使得More generally, to show that a function is differentiable and find its gradient, one approach is to approximate f(w+v)f({\bm{w}}+{\bm{v}}) around v=0{\bm{v}}={\mathbf{0}}. If we can find a vector g{\bm{g}} such that

f(w+v)=f(w)+⟨g,v⟩+o(∥v∥2),f({\bm{w}}+{\bm{v}})=f({\bm{w}})+\langle{\bm{g}},{\bm{v}}\rangle+o(\|{\bm{v}}\|_{2}),

那么 ff 在 w{\bm{w}} 处可微,因为 ⟨g,⋅⟩\langle{\bm{g}},\cdot\rangle 是线性的。此外,此时 g{\bm{g}} 正是 ff 在 w{\bm{w}} 处的梯度。then ff is differentiable at w{\bm{w}}, since ⟨g,⋅⟩\langle{\bm{g}},\cdot\rangle is linear. Moreover, g{\bm{g}} is then the gradient of ff at w{\bm{w}}.

注 2.2.5(Gateaux 可微性与 Fréchet 可微性)。Remark 2.2.5 (Gateaux and Fréchet differentiability).
可微性有多种定义。定义 2.2.3 给出的是 Fréchet 可微函数的定义。另一种定义是:若f:RP→Rf:\mathbb{R}^{P}\rightarrow\mathbb{R}沿任意方向的方向导数均有良定义,则称该函数为 Gateaux 可微。需要注意的是,任意方向的方向导数存在,并不足以保证函数可微。换言之,Fréchet 可微函数必为 Gateaux 可微,反之则不然。作为反例,可以验证:函数f(x1,x2):=x13/(x12+x22)f(x_{1},x_{2})\coloneqq x_{1}^{3}/(x_{1}^{2}+x_{2}^{2})在00处 Gateaux 可微,但在00处并非(Fréchet)可微(因为其在 0 处的方向导数不是线性的)。Multiple definitions of differentiability exist. The one presented in Definition 2.2.3 is that of Fréchet differentiable functions. Alternatively, if f:RP→Rf:\mathbb{R}^{P}\rightarrow\mathbb{R} 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). 有些作者还要求 Gateaux 可微函数沿任意方向的方向导数均为线性。即便如此,这类函数仍不一定是 Fréchet 可微函数。究其原因,定义 2.2.3 中的极限是对趋于00的任意向量取的(可能以某种病态的方式趋近),而方向导数只沿单一方向考察此类极限。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.2.3 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. 在本章余下的部分中,所有可微性的定义均指 Fréchet 可微性。In the remainder of this chapter, all definitions of differentiability are in terms of Fréchet differentiability.

下一个例子说明如何计算 logistic 损失的梯度,并验证其可微性。The next example illustrates how to compute the gradient of the logistic loss and validates its differentiability.

例 2.2.6(logistic 损失的梯度)。Example 2.2.6 (Gradient of logistic loss).
考虑 logistic 损失ℓ(θ,y):=−⟨y,θ⟩+log⁡∑i=1Meθi\ell({\bm{\theta}},{\bm{y}})\coloneqq-\langle{\bm{y}},{\bm{\theta}}\rangle+\log\sum_{i=1}^{M}e^{\theta_{i}},它度量 logits θ∈RM{\bm{\theta}}\in\mathbb{R}^{M}相对于正确标签y∈{e1,…,eM}{\bm{y}}\in\{{\bm{e}}_{1},\dots,{\bm{e}}_{M}\}的预测误差。下面计算该损失在固定y{\bm{y}}时关于θ{\bm{\theta}}的梯度,也就是说,我们希望计算f(θ):=ℓ(θ,y)f({\bm{\theta}})\coloneqq\ell({\bm{\theta}},{\bm{y}})的梯度。将ff分解为f=l+logsumexpf=l+\mathrm{logsumexp},其中l(θ):=⟨−y,θ⟩l({\bm{\theta}})\coloneqq\langle-{\bm{y}},{\bm{\theta}}\rangle,且Consider the logistic loss ℓ(θ,y):=−⟨y,θ⟩+log⁡∑i=1Meθi\ell({\bm{\theta}},{\bm{y}})\coloneqq-\langle{\bm{y}},{\bm{\theta}}\rangle+\log\sum_{i=1}^{M}e^{\theta_{i}}, that measures the prediction error of the logits θ∈RM{\bm{\theta}}\in\mathbb{R}^{M} w.r.t. the correct label y∈{e1,…,eM}{\bm{y}}\in\{{\bm{e}}_{1},\dots,{\bm{e}}_{M}\}. Let us compute the gradient of this loss w.r.t. θ{\bm{\theta}} for fixed y{\bm{y}}, i.e., we want to compute the gradient of f(θ):=ℓ(θ,y)f({\bm{\theta}})\coloneqq\ell({\bm{\theta}},{\bm{y}}). Let us decompose ff as f=l+logsumexpf=l+\mathrm{logsumexp} with l(θ):=⟨−y,θ⟩l({\bm{\theta}})\coloneqq\langle-{\bm{y}},{\bm{\theta}}\rangle and logsumexp(θ):=log⁡∑i=1Mexp⁡(θi),\mathrm{logsumexp}({\bm{\theta}})\coloneqq\log\sum_{i=1}^{M}\exp(\theta_{i}), log-sum-exp 函数。函数ll是线性的,因而可微,其梯度为∇l(θ)=−y\nabla l({\bm{\theta}})=-{\bm{y}}。于是我们着眼于logsumexp\mathrm{logsumexp}。记exp⁡(θ)=(exp⁡(θ1),…,exp⁡(θM))\exp({\bm{\theta}})=(\exp(\theta_{1}),\ldots,\exp(\theta_{M})),并利用exp⁡(x)=1+x+o(x)\exp(x)=1+x+o(x)、log⁡(1+x)=x+o(x)\log(1+x)=x+o(x),再记⊙\odot为逐元素乘积,可得the log-sum-exp function. The function ll is linear so differentiable with gradient ∇l(θ)=−y\nabla l({\bm{\theta}})=-{\bm{y}}. We therefore focus on logsumexp\mathrm{logsumexp}. Denoting exp⁡(θ)=(exp⁡(θ1),…,exp⁡(θM))\exp({\bm{\theta}})=(\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)\displaystyle\mathrm{logsumexp}({\bm{\theta}}+{\bm{v}}) =log⁡(⟨exp⁡(θ+v),1⁡⟩)\displaystyle=\log\left(\langle\exp({\bm{\theta}}+{\bm{v}}),\operatorname{\mathbf{1}}\rangle\right) =log⁡(⟨exp⁡(θ)⊙exp⁡(v),1⁡⟩)\displaystyle=\log\left(\langle\exp({\bm{\theta}})\odot\exp({\bm{v}}),\operatorname{\mathbf{1}}\rangle\right) =log⁡(⟨exp⁡(θ)⊙(1⁡+v+o(∥v∥2)),1⁡⟩)\displaystyle=\log\left(\langle\exp({\bm{\theta}})\odot(\operatorname{\mathbf{1}}+{\bm{v}}+o(\|{\bm{v}}\|_{2})),\operatorname{\mathbf{1}}\rangle\right) =log⁡(⟨exp⁡(θ),1⁡⟩+⟨exp⁡(θ),v⟩+o(∥v∥2))\displaystyle=\log\left(\langle\exp({\bm{\theta}}),\operatorname{\mathbf{1}}\rangle+\langle\exp({\bm{\theta}}),{\bm{v}}\rangle+o(\|{\bm{v}}\|_{2})\right) =log⁡(⟨exp⁡(θ),1⁡⟩)+⟨exp⁡(θ)⟨exp⁡(θ),1⁡⟩,v⟩+o(∥v∥2).\displaystyle=\log\left(\langle\exp({\bm{\theta}}),\operatorname{\mathbf{1}}\rangle\right)+\left\langle\frac{\exp({\bm{\theta}})}{\langle\exp({\bm{\theta}}),\operatorname{\mathbf{1}}\rangle},{\bm{v}}\right\rangle+o(\|{\bm{v}}\|_{2}). 上述对logsumexp(θ+v)\mathrm{logsumexp}({\bm{\theta}}+{\bm{v}})的分解表明它可微,并且∇logsumexp(θ)=softargmax(θ)\nabla\mathrm{logsumexp}({\bm{\theta}})=\mathrm{softargmax}({\bm{\theta}}),其中The above decomposition of logsumexp(θ+v)\mathrm{logsumexp}({\bm{\theta}}+{\bm{v}}) shows that it is differentiable, and that ∇logsumexp(θ)=softargmax(θ)\nabla\mathrm{logsumexp}({\bm{\theta}})=\mathrm{softargmax}({\bm{\theta}}), where softargmax(θ)\displaystyle\mathrm{softargmax}({\bm{\theta}}) :=(eθ1/(∑j=1Meθj),…,eθM/(∑j=1Meθj)).\displaystyle\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). 综上,我们得到∇f(θ)=−y+softargmax(θ)\nabla f({\bm{\theta}})=-{\bm{y}}+\mathrm{softargmax}({\bm{\theta}})。Overall, we get that ∇f(θ)=−y+softargmax(θ)\nabla f({\bm{\theta}})=-{\bm{y}}+\mathrm{softargmax}({\bm{\theta}}).

梯度的线性性Linearity of gradients

多输入函数的可微性概念,自然地继承自单输入函数导数的线性性。对任意 u1,…,uM∈Ru_{1},\ldots,u_{M}\in\mathbb{R} 以及任意在 w{\bm{w}} 处可微的多输入函数 f1,…,fMf_{1},\ldots,f_{M},函数 u1f1+…+uMfMu_{1}f_{1}+\ldots+u_{M}f_{M} 在 w{\bm{w}} 处可微,且其梯度为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\mathbb{R} and any multi-input functions f1,…,fMf_{1},\ldots,f_{M} differentiable at w{\bm{w}}, the function u1f1+…+uMfMu_{1}f_{1}+\ldots+u_{M}f_{M} is differentiable at w{\bm{w}} and its gradient is

∇(u1f1+…+uMfM)(w)=u1∇f1(w)+…+uM∇fM(w).\nabla(u_{1}f_{1}+\ldots+u_{M}f_{M})({\bm{w}})=u_{1}\nabla f_{1}({\bm{w}})+\ldots+u_{M}\nabla f_{M}({\bm{w}}).

梯度为何有用?Why is the gradient useful?

当 ff 可微时,若满足……则称 v{\bm{v}} 是 ff 在 w{\bm{w}} 处的上升方向:When ff is differentiable, we say that v{\bm{v}} is an ascent direction of ff from w{\bm{w}} if

⟨v,∇f(w)⟩>0.\langle{\bm{v}},\nabla f({\bm{w}})\rangle>0.

反之,若满足下式,则称 v{\bm{v}} 是 ff 自 w{\bm{w}} 出发的一个下降方向:Conversely, we say that v{\bm{v}} is a descent direction of ff from w{\bm{w}} if

⟨v,∇f(w)⟩<0.\langle{\bm{v}},\nabla f({\bm{w}})\rangle<0.

利用这一定义可知,梯度给出了从 w{\bm{w}} 出发的 ff 的最陡上升方向。为说明其原因,我们注意到Using this definition, the gradient leads to the steepest ascent direction of ff from w{\bm{w}}. To see why, we note that

arg max⁡v∈RP,∥v∥2≤1⟨v,∇f(w)⟩\displaystyle\argmax_{{\bm{v}}\in\mathbb{R}^{P},\|{\bm{v}}\|_{2}\leq 1}\langle{\bm{v}},\nabla f({\bm{w}})\rangle =arg max⁡v∈RP,∥v∥2≤1∂f(w)[v]\displaystyle=\argmax_{{\bm{v}}\in\mathbb{R}^{P},\|{\bm{v}}\|_{2}\leq 1}\partial f({\bm{w}})[{\bm{v}}]
=∇f(w)/∥∇f(w)∥2,\displaystyle=\nabla f({\bm{w}})/\|\nabla f({\bm{w}})\|_{2},

这里我们假设了 ∇f(w)≠0\nabla f({\bm{w}})\neq\mathbf{0}。梯度 ∇f(w)\nabla f({\bm{w}}) 垂直于该函数的水平集(即满足 f(w)f({\bm{w}}) 的同一取值的点 w{\bm{w}} 所构成的集合),并指向 ff 取值更大的方向,如图 2.4 所示。反之,负梯度 −∇f(w)-\nabla f({\bm{w}}) 则指向 ff 取值更小的方向。正是这一观察推动了梯度下降等优化算法的产生。这类算法对 γ>0\gamma>0 反复执行更新 wt+1:=wt−γ∇f(wt){\bm{w}}_{t+1}\coloneqq{\bm{w}}_{t}-\gamma\nabla f({\bm{w}}_{t}),从而沿最陡下降方向在 wt{\bm{w}}_{t} 附近逐步移动,以寻找 ff 的极小值点;该方向相差一个乘性因子即由 −∇f(wt)-\nabla f({\bm{w}}_{t}) 给出。更多细节参见定义 17.1.1。where we assumed ∇f(w)≠0\nabla f({\bm{w}})\neq\mathbf{0}. The gradient ∇f(w)\nabla f({\bm{w}}) is orthogonal to the level set of the function (the set of points w{\bm{w}} sharing the same value f(w)f({\bm{w}})) and points towards higher values of ff, as illustrated in Fig. 2.4. Conversely, the negative gradient −∇f(w)-\nabla f({\bm{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){\bm{w}}_{t+1}\coloneqq{\bm{w}}_{t}-\gamma\nabla f({\bm{w}}_{t}), for γ>0\gamma>0. It therefore seeks for a minimizer of ff by moving along the steepest descent direction around wt{\bm{w}}_{t} given, up to a multiplicative factor, by −∇f(wt)-\nabla f({\bm{w}}_{t}). See also Definition 17.1.1 for more details.

图 2.3:函数 f:R2→Rf:\mathbb{R}^{2}\rightarrow\mathbb{R} 在 (w1,w2)(w_{1},w_{2}) 处的梯度,是水平集 Lf(w1,w2)={(w1′,w2′):f(w1′,w2′)=f(w1,w2)}L_{f(w_{1},w_{2})}=\{(w_{1}^{\prime},w_{2}^{\prime}):f(w_{1}^{\prime},w_{2}^{\prime})=f(w_{1},w_{2})\} 的切空间之法向量,指向函数值更大的点。Figure 2.3: The gradient of a function f:R2→Rf:\mathbb{R}^{2}\rightarrow\mathbb{R} 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}^{\prime},w_{2}^{\prime}):f(w_{1}^{\prime},w_{2}^{\prime})=f(w_{1},w_{2})\} and points towards points with higher function values.
图 2.4:参数曲线 f:R→R2f:\mathbb{R}\rightarrow\mathbb{R}^{2} 在 ww 处的方向导数,是曲线在点 f(w)∈R2f(w)\in\mathbb{R}^{2} 处的切线。Figure 2.4: The directional derivative of a parametric curve f:R→R2f:\mathbb{R}\rightarrow\mathbb{R}^{2} at ww is the tangent to the curve at the point f(w)∈R2f(w)\in\mathbb{R}^{2}.

2.2.3 雅可比矩阵Jacobians

下面考虑由 f(w):=(f1(w),…,fM(w))f({\bm{w}})\coloneqq(f_{1}({\bm{w}}),\dots,f_{M}({\bm{w}})) 定义的多输出函数 f:RP→RMf:\mathbb{R}^{P}\rightarrow\mathbb{R}^{M},其中 fj ⁣:RP→Rf_{j}\colon\mathbb{R}^{P}\to\mathbb{R}。机器学习中的典型例子是神经网络。方向导数的概念可推广到这类函数:将其定义为各坐标方向导数所构成的向量,即Let us now consider a multi-output function f:RP→RMf:\mathbb{R}^{P}\rightarrow\mathbb{R}^{M} defined by f(w):=(f1(w),…,fM(w))f({\bm{w}})\coloneqq(f_{1}({\bm{w}}),\dots,f_{M}({\bm{w}})), where fj ⁣:RP→Rf_{j}\colon\mathbb{R}^{P}\to\mathbb{R}. 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({\bm{w}})[{\bm{v}}]\coloneqq\lim_{\delta\rightarrow 0}\frac{f({\bm{w}}+\delta{\bm{v}})-f({\bm{w}})}{\delta}=\lim_{\delta\rightarrow 0}\begin{pmatrix}\frac{f_{1}({\bm{w}}+\delta{\bm{v}})-f_{1}({\bm{w}})}{\delta}\\ \vdots\\ \frac{f_{M}({\bm{w}}+\delta{\bm{v}})-f_{M}({\bm{w}})}{\delta}\end{pmatrix}\in\mathbb{R}^{M},

式中极限(若存在)按坐标逐一施加。因此,ff 沿方向 v∈RP{\bm{v}}\in\mathbb{R}^{P} 的方向导数,就是汇集各 fjf_{j} 方向导数的向量,即 ∂f(w)[v]=(∂fj(w)[v])j=1M\partial f({\bm{w}})[{\bm{v}}]=(\partial f_{j}({\bm{w}})[{\bm{v}}])_{j=1}^{M}。特别地,可将 ff 在 w{\bm{w}} 处的偏导数定义为如下向量where the limits (provided that they exist) are applied coordinate-wise. The directional derivative of ff in the direction v∈RP{\bm{v}}\in\mathbb{R}^{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({\bm{w}})[{\bm{v}}]=(\partial f_{j}({\bm{w}})[{\bm{v}}])_{j=1}^{M}. In particular, we can define the partial derivatives of ff at w{\bm{w}} as the vectors

∂if(w):=∂f(w)[ei]=(∂if1(w)⋮∂ifM(w))∈RM.\partial_{i}f({\bm{w}})\coloneqq\partial f({\bm{w}})[{\bm{e}}_{i}]=\begin{pmatrix}\partial_{i}f_{1}({\bm{w}})\\ \vdots\\ \partial_{i}f_{M}({\bm{w}})\end{pmatrix}\in\mathbb{R}^{M}.

与通常的导数定义一样,方向导数可给出函数在当前输入附近的线性近似,如图 2.4 中参数曲线 f:R→R2f:\mathbb{R}\rightarrow\mathbb{R}^{2} 所示。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:\mathbb{R}\rightarrow\mathbb{R}^{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.

定义 2.2.7(可微性,多输出情形)。Definition 2.2.7 (Differentiability, multi-output case).
函数f:RP→RMf:\mathbb{R}^{P}\rightarrow\mathbb{R}^{M}在点w∈RP{\bm{w}}\in\mathbb{R}^{P}处是(Fréchet)可微的,若其沿任意方向的方向导数都有定义、关于任意方向均为线性,并且,A function f:RP→RMf:\mathbb{R}^{P}\rightarrow\mathbb{R}^{M} is (Fréchet) differentiable at a point w∈RP{\bm{w}}\in\mathbb{R}^{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_{\|{\bm{v}}\|_{2}\rightarrow 0}\frac{\|f({\bm{w}}+{\bm{v}})-f({\bm{w}})-\partial f({\bm{w}})[{\bm{v}}]\|_{2}}{\|{\bm{v}}\|_{2}}=0.

各坐标函数的偏导数汇集于雅可比矩阵之中。The partial derivatives of each coordinate’s function are gathered in the Jacobian matrix.

定义 2.2.8(雅可比矩阵)。Definition 2.2.8 (Jacobian).
可微函数f:RP→RMf:\mathbb{R}^{P}\rightarrow\mathbb{R}^{M}在w{\bm{w}}处的雅可比矩阵,定义为由各坐标函数的偏导数构成的矩阵(前提是这些偏导数存在),The Jacobian of a differentiable function f:RP→RMf:\mathbb{R}^{P}\rightarrow\mathbb{R}^{M} at w{\bm{w}} is defined as the matrix gathering partial derivatives of each coordinate’s function provided they exist, ∂f(w)\displaystyle\bm{\partial}f({\bm{w}}) :=(∂1f1(w)…∂Pf1(w)⋮⋱⋮∂1fM(w)…∂PfM(w))∈RM×P.\displaystyle\coloneqq\begin{pmatrix}\partial_{1}f_{1}({\bm{w}})&\ldots&\partial_{P}f_{1}({\bm{w}})\\ \vdots&\ddots&\vdots\\ \partial_{1}f_{M}({\bm{w}})&\ldots&\partial_{P}f_{M}({\bm{w}})\end{pmatrix}\in\mathbb{R}^{M\times P}. 雅可比矩阵可以通过按列堆叠偏导数或按行堆叠梯度来表示,The Jacobian can be represented by stacking columns of partial derivatives or rows of gradients, ∂f(w)\displaystyle\bm{\partial}f({\bm{w}}) =(∂1f(w),…,∂Pf(w))=(∇f1(w)⊤⋮∇fM(w)⊤)∈RM×P.\displaystyle=\begin{pmatrix}\partial_{1}f({\bm{w}}),\ldots,\partial_{P}f({\bm{w}})\end{pmatrix}=\begin{pmatrix}\nabla f_{1}({\bm{w}})^{\top}\\ \vdots\\ \nabla f_{M}({\bm{w}})^{\top}\end{pmatrix}\in\mathbb{R}^{M\times P}. 由线性性,ff在w{\bm{w}}处沿任意输入方向v=∑i=1Pviei∈RP{\bm{v}}=\sum_{i=1}^{P}v_{i}{\bm{e}}_{i}\in\mathbb{R}^{P}的方向导数即为By linearity, the directional derivative of ff at w{\bm{w}} along any input direction v=∑i=1Pviei∈RP{\bm{v}}=\sum_{i=1}^{P}v_{i}{\bm{e}}_{i}\in\mathbb{R}^{P} is then given by ∂f(w)[v]=∑i=1Pvi∂if(w)=∂f(w)v∈RM.\partial f({\bm{w}})[{\bm{v}}]=\sum_{i=1}^{P}v_{i}\partial_{i}f({\bm{w}})=\bm{\partial}f({\bm{w}}){\bm{v}}\in\mathbb{R}^{M}.

注意,我们用粗体 ∂\bm{\partial} 表示雅可比矩阵,即将其视为矩阵。雅可比矩阵自然地推广了前文所述的导数与梯度概念。与单输入情形一样,要证明一个函数可微,一种方法是考虑 f(w+v)f({\bm{w}}+{\bm{v}}) 在 v=0{\bm{v}}={\mathbf{0}} 附近的近似。若能找到一个线性映射 ll 使得Notice that we use bold ∂\bm{\partial} 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({\bm{w}}+{\bm{v}}) around v=0{\bm{v}}={\mathbf{0}}. If we find a linear map ll such that

f(w+v)=f(w)+l[v]+o(∥v∥2),f({\bm{w}}+{\bm{v}})=f({\bm{w}})+l[{\bm{v}}]+o(\|{\bm{v}}\|_{2}),

则 ff 在 w{\bm{w}} 处可微;此外,若 ll 由矩阵 J{\bm{J}} 表示,且满足 l[v]=Jvl[{\bm{v}}]={\bm{J}}{\bm{v}},则 J=∂f(w){\bm{J}}=\bm{\partial}f({\bm{w}})。then ff is differentiable at w{\bm{w}}. Moreover, if ll is represented by matrix J{\bm{J}} such that l[v]=Jvl[{\bm{v}}]={\bm{J}}{\bm{v}} then J=∂f(w){\bm{J}}=\bm{\partial}f({\bm{w}}).

举一个简单的例子:任意线性函数 f(w)=Awf({\bm{w}})={\bm{A}}{\bm{w}}(其中 A∈RM×P{\bm{A}}\in\mathbb{R}^{M\times P})都是可微的,因为它的各个按坐标分量都是单输出线性函数,且 ff 在任意 w{\bm{w}} 处的雅可比矩阵由 ∂f(w)=A\bm{\partial}f({\bm{w}})={\bm{A}} 给出。As a simple example, any linear function f(w)=Awf({\bm{w}})={\bm{A}}{\bm{w}} for A∈RM×P{\bm{A}}\in\mathbb{R}^{M\times P} is differentiable, since all its coordinate-wise components are single-output linear functions, and the Jacobian of ff at any w{\bm{w}} is given by ∂f(w)=A\bm{\partial}f({\bm{w}})={\bm{A}}.

注 2.2.9(雅可比矩阵的特殊情形)。Remark 2.2.9 (Special cases of the Jacobian).
对于单输出函数f:RP→Rf:\mathbb{R}^{P}\rightarrow\mathbb{R},即M=1M=1,雅可比矩阵退化为一个行向量,该行向量也就是梯度的转置,For single-output functions f:RP→Rf:\mathbb{R}^{P}\rightarrow\mathbb{R}, 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.\bm{\partial}f({\bm{w}})=\nabla f({\bm{w}})^{\top}\in\mathbb{R}^{1\times P}. 对于单输入函数f:R→RMf:\mathbb{R}\rightarrow\mathbb{R}^{M},雅可比矩阵退化为由方向导数构成的单列向量,记作For a single-input function f:R→RMf:\mathbb{R}\rightarrow\mathbb{R}^{M}, the Jacobian reduces to a single column vector of directional derivatives, denoted ∂f(w)=f′(w):=(f1′(w)⋮fM′(w))∈RM×1.\bm{\partial}f(w)=f^{\prime}(w)\coloneqq\begin{pmatrix}f_{1}^{\prime}(w)\\ \vdots\\ f_{M}^{\prime}(w)\end{pmatrix}\in\mathbb{R}^{M\times 1}. 对于单输入单输出函数f:R→Rf:\mathbb{R}\rightarrow\mathbb{R},雅可比矩阵退化为ff的导数,即For a single-input single-output function f:R→Rf:\mathbb{R}\rightarrow\mathbb{R}, the Jacobian reduces to the derivative of ff, i.e., ∂f(w)=f′(w)∈R.\bm{\partial}f(w)=f^{\prime}(w)\in\mathbb{R}.

下例展示了对可微函数 σ\sigma 逐元素作用时雅可比矩阵的形式,例如 softplus 激活函数(激活函数的相关回顾见 4.4 节)。此时雅可比矩阵呈现简单的对角矩阵形式。因此,该函数对应的方向导数只需由逐元素乘积给出:无需像定义 2.2.8 所提示的那样进行完整的矩阵—向量乘积。我们将在 2.3 节再次讨论这一点。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.2.8. We will revisit this point in Section 2.3.

例 2.2.10(softplus 激活函数的雅可比矩阵)。Example 2.2.10 (Jacobian matrix of the softplus activation).
考虑 softplus 的逐元素应用,其对w∈RP{\bm{w}}\in\mathbb{R}^{P}定义为Consider the element-wise application of the softplus defined for w∈RP{\bm{w}}\in\mathbb{R}^{P} by f(w):=(σ(w1)⋮σ(wP))∈RPwhereσ(w):=log⁡(1+ew).f({\bm{w}})\coloneqq\begin{pmatrix}\sigma(w_{1})\\ \vdots\\ \sigma(w_{P})\end{pmatrix}\in\mathbb{R}^{P}\quad\text{where}\quad\sigma(w)\coloneqq\log(1+e^{w}). 由于σ\sigma可微,该函数的每个坐标均可微,从而整个函数可微。对于i≠ji\neq j,ff的第jj个坐标与w{\bm{w}}的第ii个坐标无关,因此当i≠ji\neq j时有∂ifj(w)=0\partial_{i}f_{j}({\bm{w}})=0。对于i=ji=j,结果归结为σ\sigma在wjw_{j}处的导数。也就是说,∂jfj(w)=σ′(wj)\partial_{j}f_{j}({\bm{w}})=\sigma^{\prime}(w_{j}),其中σ′(w)=ew/(1+ew)\sigma^{\prime}(w)=e^{w}/(1+e^{w})。因此,ff的雅可比矩阵是一个对角矩阵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{\bm{w}} for i≠ji\neq j, so ∂ifj(w)=0\partial_{i}f_{j}({\bm{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}({\bm{w}})=\sigma^{\prime}(w_{j}), where σ′(w)=ew/(1+ew)\sigma^{\prime}(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)).\bm{\partial}f({\bm{w}})=\operatorname{\bf diag}(\sigma^{\prime}(w_{1}),\dots,\sigma^{\prime}(w_{P}))\coloneqq\begin{pmatrix}\sigma^{\prime}(w_{1})&0&\ldots&0\\ 0&\ddots&\ddots&\vdots\\ \vdots&\ddots&\ddots&0\\ 0&\ldots&0&\sigma^{\prime}(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.

命题 2.2.11(链式法则)。Proposition 2.2.11 (Chain rule).
考虑f:RP→RMf:\mathbb{R}^{P}\rightarrow\mathbb{R}^{M}与g:RM→RRg:\mathbb{R}^{M}\rightarrow\mathbb{R}^{R}。若ff在w∈RP{\bm{w}}\in\mathbb{R}^{P}处可微,且gg在f(w)∈RMf({\bm{w}})\in\mathbb{R}^{M}处可微,则复合函数g∘f ⁣:RP→RRg\circ f\colon\mathbb{R}^{P}\to\mathbb{R}^{R}在w∈RP{\bm{w}}\in\mathbb{R}^{P}处可微,其雅可比矩阵为Consider f:RP→RMf:\mathbb{R}^{P}\rightarrow\mathbb{R}^{M} and g:RM→RRg:\mathbb{R}^{M}\rightarrow\mathbb{R}^{R}. If ff is differentiable at w∈RP{\bm{w}}\in\mathbb{R}^{P} and gg is differentiable at f(w)∈RMf({\bm{w}})\in\mathbb{R}^{M}, then the composition g∘f ⁣:RP→RRg\circ f\colon\mathbb{R}^{P}\to\mathbb{R}^{R} is differentiable at w∈RP{\bm{w}}\in\mathbb{R}^{P} and its Jacobian is given by ∂(g∘f)(w)⏟R×P=∂g(f(w))⏟R×M∂f(w)⏟M×P.\underbrace{\bm{\partial}(g\circ f)({\bm{w}})}_{R\times P}=\underbrace{\bm{\partial}g(f({\bm{w}}))}_{R\times M}\underbrace{\bm{\partial}f({\bm{w}})}_{M\times P}.
证明 2.2.12。Proof 2.2.12.

利用 ff 在 w{\bm{w}} 处的可微性以及 gg 在 f(w)f({\bm{w}}) 处的可微性,我们逐步近似 g∘f(w+v)g\circ f({\bm{w}}+{\bm{v}}),We progressively approximate g∘f(w+v)g\circ f({\bm{w}}+{\bm{v}}) using the differentiability of ff at w{\bm{w}} and gg at f(w)f({\bm{w}}),

g(f(w+v))\displaystyle g(f({\bm{w}}+{\bm{v}})) =g(f(w)+∂f(w)v+o(∥v∥))\displaystyle=g(f({\bm{w}})+\bm{\partial}f({\bm{w}}){\bm{v}}+o(\|{\bm{v}}\|))
=g(f(w))+∂g(f(w))∂f(w)v+o(∥v∥).\displaystyle=g(f({\bm{w}}))+\bm{\partial}g(f({\bm{w}}))\bm{\partial}f({\bm{w}}){\bm{v}}+o(\|{\bm{v}}\|).

因此,g∘fg\circ f 在 w{\bm{w}} 处可微,其雅可比矩阵为 ∂g(f(w))∂f(w)\bm{\partial}g(f({\bm{w}}))\bm{\partial}f({\bm{w}})。Hence, g∘fg\circ f is differentiable at w{\bm{w}} with Jacobian ∂g(f(w))∂f(w)\bm{\partial}g(f({\bm{w}}))\bm{\partial}f({\bm{w}}).

命题 2.2.11 堪称一切导数计算的基石。例如,可以借助它重新推导单输入单输出函数导数所对应的线性性与乘积法则。Proposition 2.2.11 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.

当 gg 为标量值时,结合注 2.2.9,便可得到 ∇(g∘f)\nabla(g\circ f) 的一个简洁表达式。When gg is scalar-valued, combined with Remark 2.2.9, we obtain a simple expression for ∇(g∘f)\nabla(g\circ f).

命题 2.2.13(链式法则,标量值情形)。Proposition 2.2.13 (Chain rule, scalar-valued case).
考虑f:RP→RMf:\mathbb{R}^{P}\rightarrow\mathbb{R}^{M}与g:RM→Rg:\mathbb{R}^{M}\rightarrow\mathbb{R}。复合函数的梯度由下式给出Consider f:RP→RMf:\mathbb{R}^{P}\rightarrow\mathbb{R}^{M} and g:RM→Rg:\mathbb{R}^{M}\rightarrow\mathbb{R}. The gradient of the composition is given by ∇(g∘f)(w)=∂f(w)⊤∇g(f(w)).\nabla(g\circ f)({\bm{w}})=\bm{\partial}f({\bm{w}})^{\top}\nabla g(f({\bm{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.

例 2.2.14(线性回归)。Example 2.2.14 (Linear regression).
考虑将NN个输入x1,…,xN∈RD{\bm{x}}_{1},\ldots,{\bm{x}}_{N}\in\mathbb{R}^{D}回归到NN个目标y1,…,yN∈Ry_{1},\ldots,y_{N}\in\mathbb{R}的线性回归,所用参数向量为w∈RD{\bm{w}}\in\mathbb{R}^{D}。损失定义为残差平方和,即L(w):=∥Xw−y∥22=∑i=1N(⟨xi,w⟩−yi)2L({\bm{w}})\coloneqq\|{\bm{X}}{\bm{w}}-{\bm{y}}\|_{2}^{2}=\sum_{i=1}^{N}(\langle{\bm{x}}_{i},{\bm{w}}\rangle-y_{i})^{2},其中X:=(x1,…,xN)⊤∈RN×D{\bm{X}}\coloneqq({\bm{x}}_{1},\ldots,{\bm{x}}_{N})^{\top}\in\mathbb{R}^{N\times D},y:=(y1,…,yN)⊤∈RN{\bm{y}}\coloneqq(y_{1},\ldots,y_{N})^{\top}\in\mathbb{R}^{N}。Consider a linear regression of NN inputs x1,…,xN∈RD{\bm{x}}_{1},\ldots,{\bm{x}}_{N}\in\mathbb{R}^{D} onto NN targets y1,…,yN∈Ry_{1},\ldots,y_{N}\in\mathbb{R}, using a parameter vector w∈RD{\bm{w}}\in\mathbb{R}^{D}. The loss is defined as the sum of squared residuals, L(w):=∥Xw−y∥22=∑i=1N(⟨xi,w⟩−yi)2L({\bm{w}})\coloneqq\|{\bm{X}}{\bm{w}}-{\bm{y}}\|_{2}^{2}=\sum_{i=1}^{N}(\langle{\bm{x}}_{i},{\bm{w}}\rangle-y_{i})^{2} where X:=(x1,…,xN)⊤∈RN×D{\bm{X}}\coloneqq({\bm{x}}_{1},\ldots,{\bm{x}}_{N})^{\top}\in\mathbb{R}^{N\times D} and y:=(y1,…,yN)⊤∈RN{\bm{y}}\coloneqq(y_{1},\ldots,y_{N})^{\top}\in\mathbb{R}^{N}. 函数LL可分解为线性映射f(w):=Xwf({\bm{w}})\coloneqq{\bm{X}}{\bm{w}}与平方误差ℓ(p):=∥p−y∥22\ell({\bm{p}})\coloneqq\|{\bm{p}}-{\bm{y}}\|_{2}^{2},从而L=ℓ∘fL=\ell\circ f。于是可应用命题 2.2.13 中的链式法则,得到The function LL can be decomposed into a linear mapping f(w):=Xwf({\bm{w}})\coloneqq{\bm{X}}{\bm{w}} and a squared error ℓ(p):=∥p−y∥22\ell({\bm{p}})\coloneqq\|{\bm{p}}-{\bm{y}}\|_{2}^{2}, so that L=ℓ∘fL=\ell\circ f. We can then apply the chain rule in Proposition 2.2.13 to get ∇L(w)=∂f(w)⊤∇ℓ(f(w))\nabla L({\bm{w}})=\bm{\partial}f({\bm{w}})^{\top}\nabla\ell(f({\bm{w}})) 前提是ff与ℓ\ell分别在w{\bm{w}}与f(w)f({\bm{w}})处可微。provided that ff and ℓ\ell are differentiable at w{\bm{w}} and f(w)f({\bm{w}}), respectively. 函数ff是线性的,因此可微,其雅可比矩阵为∂f(w)=X\bm{\partial}f({\bm{w}})={\bm{X}}。另一方面,ℓ\ell的偏导数由∂jℓ(p)=2(pj−yj)\partial_{j}\ell({\bm{p}})=2(p_{j}-y_{j})给出,其中j∈{1,…,N}j\in\{1,\ldots,N\}。因此,ℓ\ell在任意p{\bm{p}}处可微,其梯度为∇ℓ(p)=2(p−y)\nabla\ell({\bm{p}})=2({\bm{p}}-{\bm{y}})。将两者结合,便得到LL的梯度为The function ff is linear so differentiable with Jacobian ∂f(w)=X\bm{\partial}f({\bm{w}})={\bm{X}}. On the other hand the partial derivatives of ℓ\ell are given by ∂jℓ(p)=2(pj−yj)\partial_{j}\ell({\bm{p}})=2(p_{j}-y_{j}) for j∈{1,…,N}j\in\{1,\ldots,N\}. Therefore, ℓ\ell is differentiable at any p{\bm{p}} and its gradient is ∇ℓ(p)=2(p−y)\nabla\ell({\bm{p}})=2({\bm{p}}-{\bm{y}}). By combining the two, we then get the gradient of LL as ∇L(w)=2X⊤(f(w)−y)=2X⊤(Xw−y).\nabla L({\bm{w}})=2{\bm{X}}^{\top}(f({\bm{w}})-{\bm{y}})=2{\bm{X}}^{\top}({\bm{X}}{\bm{w}}-{\bm{y}}).

2.3 线性映射2.3 Linear maps

雅可比矩阵作为偏导数的一种表示固然有用。然而,可微函数这一概念的核心,以及它在自动微分框架中的实现方式,都在于对两个关键线性映射的获取。这两个映射分别刻画沿输入方向与输出方向的无穷小变化,被称作雅可比—向量乘积(JVP)与向量—雅可比乘积(VJP)。本节将在欧几里得空间的语境下把这些概念形式化。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

迄今为止,我们讨论的函数 f ⁣:RP→RMf\colon\mathbb{R}^{P}\to\mathbb{R}^{M} 都以向量为输入、以向量为输出。然而在神经网络中,以矩阵乃至张量作为输入或输出的函数同样屡见不鲜。例如,考虑定义在形如 f(W):=Wxf({\bm{W}})\coloneqq{\bm{W}}{\bm{x}} 的矩阵上的函数,其中 x∈RD{\bm{x}}\in\mathbb{R}^{D},W∈RM×D{\bm{W}}\in\mathbb{R}^{M\times D}。该函数的输入是矩阵而非向量。当然,我们总可以通过堆叠 W{\bm{W}} 的各列,将矩阵 W∈RM×D{\bm{W}}\in\mathbb{R}^{M\times D} “展平”为向量 w∈RMD{\bm{w}}\in\mathbb{R}^{MD}。记该操作为 w=vec⁡(W){\bm{w}}=\operatorname{vec}({\bm{W}}),其逆操作为 W=vec⁡−1(w){\bm{W}}=\operatorname{vec}^{-1}({\bm{w}})。于是,我们可以等价地把 f(W)f({\bm{W}}) 写作 f~(w)=f(vec⁡−1(w))=vec⁡−1(w)x\tilde{f}({\bm{w}})=f(\operatorname{vec}^{-1}({\bm{w}}))=\operatorname{vec}^{-1}({\bm{w}}){\bm{x}},从而沿用先前的框架。然而我们接下来就会看到,这样做效率并不高。So far, we have focused on functions f ⁣:RP→RMf\colon\mathbb{R}^{P}\to\mathbb{R}^{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({\bm{W}})\coloneqq{\bm{W}}{\bm{x}}, where x∈RD{\bm{x}}\in\mathbb{R}^{D} and W∈RM×D{\bm{W}}\in\mathbb{R}^{M\times D}. This function takes a matrix as input, not a vector. Of course, a matrix W∈RM×D{\bm{W}}\in\mathbb{R}^{M\times D} can always be “flattened” into a vector w∈RMD{\bm{w}}\in\mathbb{R}^{MD}, by stacking the columns of W{\bm{W}}. We denote this operation by w=vec⁡(W){\bm{w}}=\operatorname{vec}({\bm{W}}) and its inverse by W=vec⁡−1(w){\bm{W}}=\operatorname{vec}^{-1}({\bm{w}}). We can then equivalently write f(W)f({\bm{W}}) as f~(w)=f(vec⁡−1(w))=vec⁡−1(w)x\tilde{f}({\bm{w}})=f(\operatorname{vec}^{-1}({\bm{w}}))=\operatorname{vec}^{-1}({\bm{w}}){\bm{x}}, so that the previous framework applies. However, we will now see that this would be inefficient.

事实上,由此得到的 f~\tilde{f} 在任意 w{\bm{w}} 处的雅可比矩阵是一个 RM×MD\mathbb{R}^{M\times MD} 大小的矩阵;稍作计算便可看出,其中大部分元素都是零。若要求 ff 在 W∈RM×D{\bm{W}}\in\mathbb{R}^{M\times D} 处沿方向 V∈RM×D{\bm{V}}\in\mathbb{R}^{M\times D} 的方向导数,则需:(i) 将 V{\bm{V}} 向量化为 v=vec⁡(V){\bm{v}}=\operatorname{vec}({\bm{V}});(ii) 以 M3D2M^{3}D^{2} 次计算的代价计算矩阵—向量乘积 ∂f~(w)v\bm{\partial}\tilde{f}({\bm{w}}){\bm{v}}(此处忽略雅可比矩阵含有大量零元素这一事实);(iii) 将结果重新整形为矩阵。Indeed, the resulting Jacobian of f~\tilde{f} at any w{\bm{w}} consists in a matrix of size RM×MD\mathbb{R}^{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{\bm{W}}\in\mathbb{R}^{M\times D} in a direction V∈RM×D{\bm{V}}\in\mathbb{R}^{M\times D} would consist in (i) vectorizing V{\bm{V}} into v=vec⁡(V){\bm{v}}=\operatorname{vec}({\bm{V}}), (ii) computing the matrix-vector product ∂f~(w)v\bm{\partial}\tilde{f}({\bm{w}}){\bm{v}} at a cost of M3D2M^{3}D^{2} computations (ignoring the fact that the Jacobian has many zero entries), (iii) re-shaping the result into a matrix.

另一方面,由于 ff 对其矩阵输入是线性的,可以推知:ff 在任意 W∈RM×D{\bm{W}}\in\mathbb{R}^{M\times D} 处沿任意方向 V∈RM×D{\bm{V}}\in\mathbb{R}^{M\times D} 的方向导数,恰好就是将该函数自身作用于 V{\bm{V}} 的结果。也就是说,有 ∂f(W)[V]=f(V)=Vx\partial f({\bm{W}})[{\bm{V}}]=f({\bm{V}})={\bm{V}}{\bm{x}};它易于实现,且显然只需 MDMD 次运算。注意,即便忽略 ∂f~(w)\bm{\partial}\tilde{f}({\bm{w}}) 中的非零元素,代价也依然相同。这里的要点在于:与其使用对应的雅可比矩阵表示,不如把与函数求导相关的运算视为线性映射,如此便能高效利用底层的输入空间或输出空间结构。为此,我们下面回顾在欧几里得空间语境下推广前述定义所必需的主要抽象。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{\bm{W}}\in\mathbb{R}^{M\times D} in any direction V∈RM×D{\bm{V}}\in\mathbb{R}^{M\times D} is simply given by the function itself applied on V{\bm{V}}. Namely, we have ∂f(W)[V]=f(V)=Vx\partial f({\bm{W}})[{\bm{V}}]=f({\bm{V}})={\bm{V}}{\bm{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)\bm{\partial}\tilde{f}({\bm{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

线性空间又称向量空间,是配备加法规则、且对加法封闭的空间,该加法规则与标量乘法相容(此处我们只限于实数域)。也就是说,在线性空间 E\mathcal{E} 中,存在运算 ++ 和 ⋅\cdot,使得对任意 u{\bm{u}}、v∈E{\bm{v}}\in\mathcal{E} 和 a∈Ra\in\mathbb{R},均有 u+v∈E{\bm{u}}+{\bm{v}}\in\mathcal{E} 和 a⋅u∈Ea\cdot{\bm{u}}\in\mathcal{E}。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\mathcal{E}, there exist operations ++ and ⋅\cdot, such that for any u{\bm{u}}, v∈E{\bm{v}}\in\mathcal{E}, and a∈Ra\in\mathbb{R}, we have u+v∈E{\bm{u}}+{\bm{v}}\in\mathcal{E} and a⋅u∈Ea\cdot{\bm{u}}\in\mathcal{E}.

欧几里得空间是配备一组基 e1,…,eP∈E{\bm{e}}_{1},\ldots,{\bm{e}}_{P}\in\mathcal{E} 的线性空间。任意元素 v∈E{\bm{v}}\in\mathcal{E} 均可分解为 v=∑i=1Pviei{\bm{v}}=\sum_{i=1}^{P}v_{i}{\bm{e}}_{i},其中标量 v1,…,vP∈Rv_{1},\ldots,v_{P}\in\mathbb{R} 唯一确定。欧几里得空间的典型例子,是我们已经讨论过的、由所有大小为 PP 的向量构成的集合 RP\mathbb{R}^{P}。大小为 P1×P2P_{1}\times P_{2} 的矩阵集合 RP1×P2\mathbb{R}^{P_{1}\times P_{2}} 同样自然地构成一个欧几里得空间,它由标准矩阵集 Eij∈{0,1}P1×P2{\bm{E}}_{ij}\in\{0,1\}^{P_{1}\times P_{2}} 生成:对 i∈[P1],j∈[P2]i\in[P_{1}],j\in[P_{2}],这些矩阵除第 (i,j)(i,j) 个元素为 1 之外,其余元素全为零。例如,W∈RP1×P2{\bm{W}}\in\mathbb{R}^{P_{1}\times P_{2}} 可写作 W=∑i,j=1P1,P2WijEij{\bm{W}}=\sum_{i,j=1}^{P_{1},P_{2}}W_{ij}{\bm{E}}_{ij}。欧几里得空间天然具有内积的概念。Euclidean spaces are linear spaces equipped with a basis e1,…,eP∈E{\bm{e}}_{1},\ldots,{\bm{e}}_{P}\in\mathcal{E}. Any element v∈E{\bm{v}}\in\mathcal{E} can be decomposed as v=∑i=1Pviei{\bm{v}}=\sum_{i=1}^{P}v_{i}{\bm{e}}_{i} for some unique scalars v1,…,vP∈Rv_{1},\ldots,v_{P}\in\mathbb{R}. A canonical example of Euclidean space is the set RP\mathbb{R}^{P} of all vectors of size PP that we already covered. The set of matrices RP1×P2\mathbb{R}^{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{\bm{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{\bm{W}}\in\mathbb{R}^{P_{1}\times P_{2}} can be written W=∑i,j=1P1,P2WijEij{\bm{W}}=\sum_{i,j=1}^{P_{1},P_{2}}W_{ij}{\bm{E}}_{ij}. Euclidean spaces are naturally equipped with a notion of inner product.

定义 2.3.1(内积)。Definition 2.3.1 (Inner product).
线性空间E\mathcal{E}上的内积是一个函数⟨⋅,⋅⟩:E×E→R\langle\cdot,\cdot\rangle:\mathcal{E}\times\mathcal{E}\rightarrow\mathbb{R},它满足An inner product on a linear space E\mathcal{E} is a function ⟨⋅,⋅⟩:E×E→R\langle\cdot,\cdot\rangle:\mathcal{E}\times\mathcal{E}\rightarrow\mathbb{R} that is • 双线性:x↦⟨x,w⟩{\bm{x}}\mapsto\langle{\bm{x}},{\bm{w}}\rangle与y↦⟨v,y⟩{\bm{y}}\mapsto\langle{\bm{v}},{\bm{y}}\rangle对任意w,v∈E{\bm{w}},{\bm{v}}\in\mathcal{E}均为线性,bilinear: x↦⟨x,w⟩{\bm{x}}\mapsto\langle{\bm{x}},{\bm{w}}\rangle and y↦⟨v,y⟩{\bm{y}}\mapsto\langle{\bm{v}},{\bm{y}}\rangle are linear for any w,v∈E{\bm{w}},{\bm{v}}\in\mathcal{E}, • 对称性:对任意w,v∈E{\bm{w}},{\bm{v}}\in\mathcal{E},有⟨w,v⟩=⟨v,w⟩\langle{\bm{w}},{\bm{v}}\rangle=\langle{\bm{v}},{\bm{w}}\rangle,symmetric: ⟨w,v⟩=⟨v,w⟩\langle{\bm{w}},{\bm{v}}\rangle=\langle{\bm{v}},{\bm{w}}\rangle for any w,v∈E{\bm{w}},{\bm{v}}\in\mathcal{E}, • 正定性:对任意w∈E{\bm{w}}\in\mathcal{E},有⟨w,w⟩≥0\langle{\bm{w}},{\bm{w}}\rangle\geq 0,且⟨w,w⟩=0\langle{\bm{w}},{\bm{w}}\rangle=0当且仅当w=0{\bm{w}}=0。positive definite: ⟨w,w⟩≥0\langle{\bm{w}},{\bm{w}}\rangle\geq 0 for any w∈E{\bm{w}}\in\mathcal{E}, and ⟨w,w⟩=0\langle{\bm{w}},{\bm{w}}\rangle=0 if and only if w=0{\bm{w}}=0. 内积定义了范数∥w∥:=⟨w,w⟩\|{\bm{w}}\|\coloneqq\sqrt{\langle{\bm{w}},{\bm{w}}\rangle}。An inner product defines a norm ∥w∥:=⟨w,w⟩\|{\bm{w}}\|\coloneqq\sqrt{\langle{\bm{w}},{\bm{w}}\rangle}.

由内积诱导的范数定义了 w,v∈E{\bm{w}},{\bm{v}}\in\mathcal{E} 之间的距离 ∥w−v∥\|{\bm{w}}-{\bm{v}}\|,从而也给出了收敛性的概念。The norm induced by an inner product defines a distance ∥w−v∥\|{\bm{w}}-{\bm{v}}\| between w,v∈E{\bm{w}},{\bm{v}}\in\mathcal{E}, and therefore a notion of convergence.

对于向量,其中 E=RP\mathcal{E}=\mathbb{R}^{P},内积即通常的内积 ⟨w,v⟩=∑i=1Pwivi.\langle{\bm{w}},{\bm{v}}\rangle=\sum_{i=1}^{P}w_{i}v_{i}.。对于矩阵,其中 E=RP1×P2\mathcal{E}=\mathbb{R}^{P_{1}\times P_{2}},内积即所谓的 Frobenius 内积。它对任意 W,V∈RP1×P2{\bm{W}},{\bm{V}}\in\mathbb{R}^{P_{1}\times P_{2}} 定义为For vectors, where E=RP\mathcal{E}=\mathbb{R}^{P}, the inner product is the usual one ⟨w,v⟩=∑i=1Pwivi.\langle{\bm{w}},{\bm{v}}\rangle=\sum_{i=1}^{P}w_{i}v_{i}. For matrices, where E=RP1×P2\mathcal{E}=\mathbb{R}^{P_{1}\times P_{2}}, the inner product is the so-called Frobenius inner product. It is defined for any W,V∈RP1×P2{\bm{W}},{\bm{V}}\in\mathbb{R}^{P_{1}\times P_{2}} by

⟨W,V⟩:=⟨vec⁡(W),vec⁡(V)⟩=∑i,j=1P1,P2WijVij=tr⁡(W⊤V),\langle{\bm{W}},{\bm{V}}\rangle\coloneqq\langle\operatorname{vec}({\bm{W}}),\operatorname{vec}({\bm{V}})\rangle=\sum_{i,j=1}^{P_{1},P_{2}}W_{ij}V_{ij}=\operatorname{tr}({\bm{W}}^{\top}{\bm{V}}),

其中 tr⁡(Z):=∑i=1PZii\operatorname{tr}({\bm{Z}})\coloneqq\sum_{i=1}^{P}Z_{ii} 是对方阵 Z∈RP×P{\bm{Z}}\in\mathbb{R}^{P\times P} 定义的迹算子。对于 RR 阶张量——它将矩阵推广到 E=RP1×…×PR\mathcal{E}=\mathbb{R}^{P_{1}\times\ldots\times P_{R}}——内积同样地对 W,V∈RP1×…×PR\bm{\mathsf{W}},\bm{\mathsf{V}}\in\mathbb{R}^{P_{1}\times\ldots\times P_{R}} 以如下方式定义:where tr⁡(Z):=∑i=1PZii\operatorname{tr}({\bm{Z}})\coloneqq\sum_{i=1}^{P}Z_{ii} is the trace operator defined for square matrices Z∈RP×P{\bm{Z}}\in\mathbb{R}^{P\times P}. For tensors of order RR, which generalize matrices to E=RP1×…×PR\mathcal{E}=\mathbb{R}^{P_{1}\times\ldots\times P_{R}}, the inner product is defined similarly for W,V∈RP1×…×PR\bm{\mathsf{W}},\bm{\mathsf{V}}\in\mathbb{R}^{P_{1}\times\ldots\times P_{R}} by

⟨W,V⟩:=⟨vec⁡(W),vec⁡(V)⟩=∑i1,…,iR=1P1,…,PRWi1…iRVi1…iR,\langle\bm{\mathsf{W}},\bm{\mathsf{V}}\rangle\coloneqq\langle\operatorname{vec}(\bm{\mathsf{W}}),\operatorname{vec}(\bm{\mathsf{V}})\rangle=\sum_{i_{1},\ldots,i_{R}=1}^{P_{1},\ldots,P_{R}}\bm{\mathsf{W}}_{i_{1}\ldots i_{R}}\bm{\mathsf{V}}_{i_{1}\ldots i_{R}},

其中 Wi1…iR\bm{\mathsf{W}}_{i_{1}\ldots i_{R}} 是 W\bm{\mathsf{W}} 的第 (i1,…,iR)(i_{1},\ldots,i_{R}) 个元素。where Wi1…iR\bm{\mathsf{W}}_{i_{1}\ldots i_{R}} is the (i1,…,iR)(i_{1},\ldots,i_{R})th entry of W\bm{\mathsf{W}}.

2.3.3 线性映射及其伴随Linear maps and their adjoints

定义 2.2.2 中的线性映射概念可直接推广到欧几里得空间。也就是说,从欧几里得空间 E\mathcal{E} 到欧几里得空间 F\mathcal{F} 的函数 l:E→Fl:\mathcal{E}\rightarrow\mathcal{F} 是线性映射,如果对任意 w,v∈E{\bm{w}},{\bm{v}}\in\mathcal{E} 和 a,b∈Ra,b\in\mathbb{R},都有 l[aw+bv]=a⋅l[w]+b⋅l[v]l[a{\bm{w}}+b{\bm{v}}]=a\cdot l[{\bm{w}}]+b\cdot l[{\bm{v}}]。当 E=RP\mathcal{E}=\mathbb{R}^{P} 且 F=RM\mathcal{F}=\mathbb{R}^{M} 时,总存在矩阵 A∈RM×P{\bm{A}}\in\mathbb{R}^{M\times P} 使得 l[v]=Avl[{\bm{v}}]={\bm{A}}{\bm{v}}。因此,我们可以把 A{\bm{A}} 看作 ll 作为矩阵的“具体化”。The notion of linear map in Definition 2.2.2 naturally extends to Euclidean spaces. Namely, a function l:E→Fl:\mathcal{E}\rightarrow\mathcal{F} from a Euclidean space E\mathcal{E} onto a Euclidean space F\mathcal{F} is a linear map if for any w,v∈E{\bm{w}},{\bm{v}}\in\mathcal{E} and a,b∈Ra,b\in\mathbb{R}, we have l[aw+bv]=a⋅l[w]+b⋅l[v]l[a{\bm{w}}+b{\bm{v}}]=a\cdot l[{\bm{w}}]+b\cdot l[{\bm{v}}]. When E=RP\mathcal{E}=\mathbb{R}^{P} and F=RM\mathcal{F}=\mathbb{R}^{M}, there always exists a matrix A∈RM×P{\bm{A}}\in\mathbb{R}^{M\times P} such that l[v]=Avl[{\bm{v}}]={\bm{A}}{\bm{v}}. Therefore, we can think of A{\bm{A}} as the “materialization” as a matrix of ll.

例 2.3.2(线性映射)。Example 2.3.2 (Linear map).
考虑线性映射l[v]:=(ab⊤)vl[{\bm{v}}]\coloneqq({\bm{a}}{\bm{b}}^{\top}){\bm{v}},其中a∈RM{\bm{a}}\in\mathbb{R}^{M}、b∈RP{\bm{b}}\in\mathbb{R}^{P}、v∈RP{\bm{v}}\in\mathbb{R}^{P}。这是一个从RP\mathbb{R}^{P}到RM\mathbb{R}^{M}的函数。我们总可以将该线性映射具体化为矩阵A:=ab⊤∈RM×P{\bm{A}}\coloneqq{\bm{a}}{\bm{b}}^{\top}\in\mathbb{R}^{M\times P},并写成l[v]=Avl[{\bm{v}}]={\bm{A}}{\bm{v}}。然而,对向量v{\bm{v}}应用线性映射,往往并不需要具体化相应的矩阵。在此例中,我们可以直接写成l[v]=(b⊤v)al[{\bm{v}}]=({\bm{b}}^{\top}{\bm{v}}){\bm{a}},因为(b⊤v)({\bm{b}}^{\top}{\bm{v}})是一个标量值。这只需要一个内积和一次逐元素乘积,比先具体化A{\bm{A}}再计算Av{\bm{A}}{\bm{v}}更为高效,而后者需要一次外积和一次矩阵—向量乘法。Consider the linear map l[v]:=(ab⊤)vl[{\bm{v}}]\coloneqq({\bm{a}}{\bm{b}}^{\top}){\bm{v}}, where a∈RM{\bm{a}}\in\mathbb{R}^{M}, b∈RP{\bm{b}}\in\mathbb{R}^{P} and v∈RP{\bm{v}}\in\mathbb{R}^{P}. This is a function from RP\mathbb{R}^{P} to RM\mathbb{R}^{M}. We can always materialize the linear map as a matrix A:=ab⊤∈RM×P{\bm{A}}\coloneqq{\bm{a}}{\bm{b}}^{\top}\in\mathbb{R}^{M\times P} and write l[v]=Avl[{\bm{v}}]={\bm{A}}{\bm{v}}. However, applying a linear map on a vector v{\bm{v}} often does not require materializing the corresponding matrix. In this example, we can simply write l[v]=(b⊤v)al[{\bm{v}}]=({\bm{b}}^{\top}{\bm{v}}){\bm{a}}, since (b⊤v)({\bm{b}}^{\top}{\bm{v}}) is a scalar value. This only requires an inner product and an element-wise multiplication, and is more efficient than materializing A{\bm{A}} then computing Av{\bm{A}}{\bm{v}}, which requires an outer product and a matrix-vector multiplication.

我们可以定义线性映射的伴随算子。We can define the adjoint operator of a linear map.

定义 2.3.3(伴随算子)。Definition 2.3.3 (Adjoint operator).
给定两个欧几里得空间E\mathcal{E}与F\mathcal{F},分别配备内积⟨⋅,⋅⟩E\langle\cdot,\cdot\rangle_{\mathcal{E}}与⟨⋅,⋅⟩F\langle\cdot,\cdot\rangle_{\mathcal{F}},线性映射l:E→Fl:\mathcal{E}\rightarrow\mathcal{F}的伴随是唯一的线性映射l∗:F→El^{*}:\mathcal{F}\rightarrow\mathcal{E},使得对任意v∈E{\bm{v}}\in\mathcal{E}与u∈F{\bm{u}}\in\mathcal{F},有Given two Euclidean spaces E\mathcal{E} and F\mathcal{F} equipped with inner products ⟨⋅,⋅⟩E\langle\cdot,\cdot\rangle_{\mathcal{E}} and ⟨⋅,⋅⟩F\langle\cdot,\cdot\rangle_{\mathcal{F}}, the adjoint of a linear map l:E→Fl:\mathcal{E}\rightarrow\mathcal{F} is the unique linear map l∗:F→El^{*}:\mathcal{F}\rightarrow\mathcal{E} such that for any v∈E{\bm{v}}\in\mathcal{E} and u∈F{\bm{u}}\in\mathcal{F}, ⟨l[v],u⟩F=⟨v,l∗[u]⟩E.\langle l[{\bm{v}}],{\bm{u}}\rangle_{\mathcal{F}}=\langle{\bm{v}},l^{*}[{\bm{u}}]\rangle_{\mathcal{E}}.

伴随可以看作矩阵转置在线性映射上的对应物。当 l[v]=Avl[{\bm{v}}]={\bm{A}}{\bm{v}} 时,有 l∗[u]=A⊤ul^{*}[{\bm{u}}]={\bm{A}}^{\top}{\bm{u}},这是因为The adjoint can be thought of as the counterpart of the matrix transpose for linear maps. When l[v]=Avl[{\bm{v}}]={\bm{A}}{\bm{v}}, we have l∗[u]=A⊤ul^{*}[{\bm{u}}]={\bm{A}}^{\top}{\bm{u}} since

⟨l[v],u⟩F=⟨Av,u⟩F=⟨v,A⊤u⟩E=⟨v,l∗[u]⟩E.\langle l[{\bm{v}}],{\bm{u}}\rangle_{\mathcal{F}}=\langle{\bm{A}}{\bm{v}},{\bm{u}}\rangle_{\mathcal{F}}=\langle{\bm{v}},{\bm{A}}^{\top}{\bm{u}}\rangle_{\mathcal{E}}=\langle{\bm{v}},l^{*}[{\bm{u}}]\rangle_{\mathcal{E}}.
例 2.3.4(伴随线性映射)。Example 2.3.4 (Adjoint linear map).
利用上一例中的线性映射l[v]l[{\bm{v}}],对所有u∈RM{\bm{u}}\in\mathbb{R}^{M}与v∈RP{\bm{v}}\in\mathbb{R}^{P},有,Using the linear map l[v]l[{\bm{v}}] from the previous example, we have for all u∈RM{\bm{u}}\in\mathbb{R}^{M} and v∈RP{\bm{v}}\in\mathbb{R}^{P}, ⟨ab⊤v,u⟩=⟨v,ba⊤u⟩.\langle{\bm{a}}{\bm{b}}^{\top}{\bm{v}},{\bm{u}}\rangle=\langle{\bm{v}},{\bm{b}}{\bm{a}}^{\top}{\bm{u}}\rangle. 因此,伴随线性映射为l∗[u]=(ba⊤)ul^{*}[{\bm{u}}]=({\bm{b}}{\bm{a}}^{\top}){\bm{u}}。这是一个从RM\mathbb{R}^{M}到RP\mathbb{R}^{P}的函数。它可以具体化为矩阵A⊤=ba⊤∈RP×M{\bm{A}}^{\top}={\bm{b}}{\bm{a}}^{\top}\in\mathbb{R}^{P\times M}。而应用l∗[u]l^{*}[{\bm{u}}]时,可以高效地写成l∗[u]=(a⊤u)bl^{*}[{\bm{u}}]=({\bm{a}}^{\top}{\bm{u}}){\bm{b}}。Therefore, the adjoint linear map is l∗[u]=(ba⊤)ul^{*}[{\bm{u}}]=({\bm{b}}{\bm{a}}^{\top}){\bm{u}}. This is a function from RM\mathbb{R}^{M} to RP\mathbb{R}^{P}. It can be materialized as the matrix A⊤=ba⊤∈RP×M{\bm{A}}^{\top}={\bm{b}}{\bm{a}}^{\top}\in\mathbb{R}^{P\times M}. Applying l∗[u]l^{*}[{\bm{u}}] can be done efficiently as l∗[u]=(a⊤u)bl^{*}[{\bm{u}}]=({\bm{a}}^{\top}{\bm{u}}){\bm{b}}.

2.3.4 雅可比矩阵—向量乘积Jacobian-vector products

现在我们用线性映射来定义方向导数,由此引出雅可比—向量乘积(JVP)的概念。这有助于处理定义在张量上的函数,或进一步推广到无穷维空间。下文以 E\mathcal{E} 和 F\mathcal{F} 表示两个分别配备内积 ⟨⋅,⋅⟩E\langle\cdot,\cdot\rangle_{\mathcal{E}} 和 ⟨⋅,⋅⟩F\langle\cdot,\cdot\rangle_{\mathcal{F}} 的欧几里得空间。我们首先在一般欧几里得空间中定义可微性。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\mathcal{E} and F\mathcal{F} denote two Euclidean spaces equipped with inner products ⟨⋅,⋅⟩E\langle\cdot,\cdot\rangle_{\mathcal{E}} and ⟨⋅,⋅⟩F\langle\cdot,\cdot\rangle_{\mathcal{F}}. We start by defining differentiability in general Euclidean spaces.

定义 2.3.5(欧几里得空间中的可微性)。Definition 2.3.5 (Differentiability in Euclidean spaces).
函数f:E→Ff:\mathcal{E}\rightarrow\mathcal{F}在点w∈E{\bm{w}}\in\mathcal{E}处可微,若沿v∈E{\bm{v}}\in\mathcal{E}的方向导数A function f:E→Ff:\mathcal{E}\rightarrow\mathcal{F} is differentiable at a point w∈E{\bm{w}}\in\mathcal{E} if the directional derivative along v∈E{\bm{v}}\in\mathcal{E} ∂f(w)[v]:=lim⁡δ→0f(w+δv)−f(w)δ\partial f({\bm{w}})[{\bm{v}}]\coloneqq\lim_{\delta\rightarrow 0}\frac{f({\bm{w}}+\delta{\bm{v}})-f({\bm{w}})}{\delta} 对任意v∈E{\bm{v}}\in\mathcal{E}均有定义,且关于v{\bm{v}}为线性,并且is well-defined for any v∈E{\bm{v}}\in\mathcal{E}, linear in v{\bm{v}} and if lim⁡∥v∥E→0∥f(w+v)−f(w)−∂f(w)[v]∥F∥v∥E=0.\lim_{\|{\bm{v}}\|_{\mathcal{E}}\rightarrow 0}\frac{\|f({\bm{w}}+{\bm{v}})-f({\bm{w}})-\partial f({\bm{w}})[{\bm{v}}]\|_{\mathcal{F}}}{\|{\bm{v}}\|_{\mathcal{E}}}=0.

现在我们可以正式定义雅可比矩阵—向量乘积。We can now formally define the Jacobian-vector product.

定义 2.3.6(雅可比矩阵—向量乘积)。Definition 2.3.6 (Jacobian-vector product).
对于可微函数f:E→Ff:\mathcal{E}\rightarrow\mathcal{F},将v{\bm{v}}映射到∂f(w)[v]\partial f({\bm{w}})[{\bm{v}}]的线性映射∂f(w):E→F\partial f({\bm{w}}):\mathcal{E}\to\mathcal{F},称为雅可比—向量乘积(JVP)。由此看来,函数∂f\partial f是从E\mathcal{E}到从E\mathcal{E}到F\mathcal{F}的线性映射的函数。也就是说,我们有For a differentiable function f:E→Ff:\mathcal{E}\rightarrow\mathcal{F}, the linear map ∂f(w):E→F\partial f({\bm{w}}):\mathcal{E}\to\mathcal{F}, mapping v{\bm{v}} to ∂f(w)[v]\partial f({\bm{w}})[{\bm{v}}], is called the Jacobian-vector product (JVP). From this perspective, the function ∂f\partial f is a function from E\mathcal{E} to a linear map from E\mathcal{E} to F\mathcal{F}. That is, we have ∂f ⁣:E→(E→F).\partial f\colon\mathcal{E}\to(\mathcal{E}\to\mathcal{F}).

我们再次强调,方向导数 ∂f(w)[v]∈F\partial f({\bm{w}})[{\bm{v}}]\in\mathcal{F} 是一个值,而 JVP v↦∂f(w)[v]{\bm{v}}\mapsto\partial f({\bm{w}})[{\bm{v}}] 是一个函数。严格说来,v{\bm{v}} 可以属于任意欧几里得空间 E\mathcal{E},并不一定局限于向量,尽管 JVP 这一缩写会给人这样的暗示。我们之所以采用 JVP 这一名称,是因为它如今已成为标准。We emphasize again that the directional derivative ∂f(w)[v]∈F\partial f({\bm{w}})[{\bm{v}}]\in\mathcal{F} is a value, while the JVP v↦∂f(w)[v]{\bm{v}}\mapsto\partial f({\bm{w}})[{\bm{v}}] is a function. Strictly speaking, v{\bm{v}} can belong to any Euclidean space E\mathcal{E} 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.

命题 2.3.7(梯度)。Proposition 2.3.7 (Gradient).
若函数f:E→Rf:\mathcal{E}\rightarrow\mathbb{R}在w∈E{\bm{w}}\in\mathcal{E}处可微,则存在∇f(w)∈E\nabla f({\bm{w}})\in\mathcal{E},称为ff在w{\bm{w}}处的梯度,使得ff在w{\bm{w}}处沿任意输入方向v∈E{\bm{v}}\in\mathcal{E}的方向导数由下式给出If a function f:E→Rf:\mathcal{E}\rightarrow\mathbb{R} is differentiable at w∈E{\bm{w}}\in\mathcal{E}, then there exists ∇f(w)∈E\nabla f({\bm{w}})\in\mathcal{E}, called the gradient of ff at w{\bm{w}} such that the directional derivative of ff at w{\bm{w}} along any input direction v∈E{\bm{v}}\in\mathcal{E} is given by ∂f(w)[v]=⟨∇f(w),v⟩E.\partial f({\bm{w}})[{\bm{v}}]=\langle\nabla f({\bm{w}}),{\bm{v}}\rangle_{\mathcal{E}}.

在欧几里得空间中,只需沿 E\mathcal{E} 的一组基分解偏导数,即可证明梯度的存在性。这种定义可推广到无穷维空间(例如希尔伯特空间),详见 2.3.9 节。In Euclidean spaces, the existence of the gradient can simply be shown by decomposing the partial derivative along a basis of E\mathcal{E}. 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

考察函数 f ⁣:RP→RMf\colon\mathbb{R}^{P}\to\mathbb{R}^{M}。除了考察 ff 沿输入方向 v∈RP{\bm{v}}\in\mathbb{R}^{P} 的变化,我们还可以考察 ff 沿输出方向 u∈RM{\bm{u}}\in\mathbb{R}^{M} 的变化,即计算标量值函数的梯度 ∇⟨u,f⟩(w)\nabla\langle{\bm{u}},f\rangle({\bm{w}})Consider a function f ⁣:RP→RMf\colon\mathbb{R}^{P}\to\mathbb{R}^{M}. Instead of variations of ff along an input direction v∈RP{\bm{v}}\in\mathbb{R}^{P}, we may also consider the variations of ff along an output direction u∈RM{\bm{u}}\in\mathbb{R}^{M}, namely, computing the gradient ∇⟨u,f⟩(w)\nabla\langle{\bm{u}},f\rangle({\bm{w}}) of the scalar-valued function

⟨u,f⟩(w):=⟨u,f(w)⟩∈R.\langle{\bm{u}},f\rangle({\bm{w}})\coloneqq\langle{\bm{u}},f({\bm{w}})\rangle\in\mathbb{R}.

等价地,我们也可以计算各坐标函数 fj:=⟨ej,f⟩f_{j}\coloneqq\langle{\bm{e}}_{j},f\rangle 在 w{\bm{w}} 处的梯度 ∇fj(w)\nabla f_{j}({\bm{w}}),其中 ej{\bm{e}}_{j} 是 RM\mathbb{R}^{M} 中的第 jj 个标准向量。ff 在 w{\bm{w}} 处沿任意输出方向 u=∑j=1Mujej∈RM{\bm{u}}=\sum_{j=1}^{M}u_{j}{\bm{e}}_{j}\in\mathbb{R}^{M} 的无穷小变化由下式给出Equivalently, we may compute the gradients ∇fj(w)\nabla f_{j}({\bm{w}}) of each coordinate function fj:=⟨ej,f⟩f_{j}\coloneqq\langle{\bm{e}}_{j},f\rangle at w{\bm{w}}, where ej{\bm{e}}_{j} is the jjth canonical vector in RM\mathbb{R}^{M}. The infinitesimal variations of ff at w{\bm{w}} along any output direction u=∑j=1Mujej∈RM{\bm{u}}=\sum_{j=1}^{M}u_{j}{\bm{e}}_{j}\in\mathbb{R}^{M} are given by

∇⟨u,f⟩(w)=∑j=1Muj∇fj(w)=∂f(w)⊤u∈RP,\nabla\langle{\bm{u}},f\rangle({\bm{w}})=\sum_{j=1}^{M}u_{j}\nabla f_{j}({\bm{w}})=\bm{\partial}f({\bm{w}})^{\top}{\bm{u}}\in\mathbb{R}^{P},

其中 ∂f(w)⊤∈RP×M\bm{\partial}f({\bm{w}})^{\top}\in\mathbb{R}^{P\times M} 为雅可比矩阵的转置。利用导数作为极限的定义,对于 i∈[P]i\in[P],也可写成where ∂f(w)⊤∈RP×M\bm{\partial}f({\bm{w}})^{\top}\in\mathbb{R}^{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{\bm{u}},f\rangle({\bm{w}})=[\bm{\partial}f({\bm{w}})^{\top}{\bm{u}}]_{i}=\lim_{\delta\rightarrow 0}\frac{\langle{\bm{u}},f({\bm{w}}+\delta{\bm{e}}_{i})-f({\bm{w}})\rangle}{\delta},

其中 ei{\bm{e}}_{i} 是 RP\mathbb{R}^{P} 中的第 ii 个标准向量。where ei{\bm{e}}_{i} is the iith canonical vector in RP\mathbb{R}^{P}.

对于一般的欧几里得空间 E\mathcal{E} 与 F\mathcal{F},转置的对应物是伴随算子,由此引出向量—雅可比乘积的概念。For generic Euclidean spaces E\mathcal{E} and F\mathcal{F}, the counterpart of the transpose is the adjoint operator, leading to the notion of vector-Jacobian product.

命题 2.3.8(向量—雅可比矩阵乘积)。Proposition 2.3.8 (Vector-Jacobian product).
若函数f:E→Ff:\mathcal{E}\rightarrow\mathcal{F}在w∈E{\bm{w}}\in\mathcal{E}处可微,则其沿输出方向u∈F{\bm{u}}\in\mathcal{F}的无穷小变化由 JVP 的伴随映射∂f(w)∗ ⁣:F→E\partial f({\bm{w}})^{*}\colon\mathcal{F}\to\mathcal{E}给出,该映射称为向量—雅可比乘积(VJP)。它满足If a function f:E→Ff:\mathcal{E}\rightarrow\mathcal{F} is differentiable at w∈E{\bm{w}}\in\mathcal{E}, then its infinitesimal variation along an output direction u∈F{\bm{u}}\in\mathcal{F} is given by the adjoint map ∂f(w)∗ ⁣:F→E\partial f({\bm{w}})^{*}\colon\mathcal{F}\to\mathcal{E} of the JVP, called the vector-Jacobian product (VJP). It satisfies ∇⟨u,f⟩F(w)=∂f(w)∗[u],\displaystyle\nabla\langle{\bm{u}},f\rangle_{\mathcal{F}}({\bm{w}})=\partial f({\bm{w}})^{*}[{\bm{u}}], 其中我们记⟨u,f⟩F(w):=⟨u,f(w)⟩F\langle{\bm{u}},f\rangle_{\mathcal{F}}({\bm{w}})\coloneqq\langle{\bm{u}},f({\bm{w}})\rangle_{\mathcal{F}}。函数∂f(⋅)∗\partial f(\cdot)^{*}是从E\mathcal{E}到从F\mathcal{F}到E\mathcal{E}的线性映射的函数。也就是说,我们有where we denoted ⟨u,f⟩F(w):=⟨u,f(w)⟩F\langle{\bm{u}},f\rangle_{\mathcal{F}}({\bm{w}})\coloneqq\langle{\bm{u}},f({\bm{w}})\rangle_{\mathcal{F}}. The function ∂f(⋅)∗\partial f(\cdot)^{*} is a function from E\mathcal{E} to a linear map from F\mathcal{F} to E\mathcal{E}. That is, we have ∂f(⋅)∗ ⁣:E→(F→E).\partial f(\cdot)^{*}\colon\mathcal{E}\to(\mathcal{F}\to\mathcal{E}).
证明 2.3.9。Proof 2.3.9.

命题 2.2.11 给出的链式法则可自然地推广到欧几里得空间(见命题 2.3.10)。由于 ⟨u,⋅⟩F\langle{\bm{u}},\cdot\rangle_{\mathcal{F}} 是线性的,其方向导数就是其自身。因此,⟨u,f⟩F\langle{\bm{u}},f\rangle_{\mathcal{F}} 的方向导数为The chain rule presented in Proposition 2.2.11 naturally generalizes to Euclidean spaces (see Proposition 2.3.10). Since ⟨u,⋅⟩F\langle{\bm{u}},\cdot\rangle_{\mathcal{F}} is linear, its directional derivative is itself. Therefore, the directional derivative of ⟨u,f⟩F\langle{\bm{u}},f\rangle_{\mathcal{F}} is

∂(⟨u,f⟩F)(w)[v]\displaystyle\partial(\langle{\bm{u}},f\rangle_{\mathcal{F}})({\bm{w}})[{\bm{v}}] =⟨u,∂f(w)[v]⟩F\displaystyle=\langle{\bm{u}},\partial f({\bm{w}})[{\bm{v}}]\rangle_{\mathcal{F}}
=⟨∂f(w)∗[u],v⟩E.\displaystyle=\langle\partial f({\bm{w}})^{*}[{\bm{u}}],{\bm{v}}\rangle_{\mathcal{E}}.

由于上式对任意 v∈E{\bm{v}}\in\mathcal{E} 都成立,故依命题 2.3.7,∂f(w)∗[u]\partial f({\bm{w}})^{*}[{\bm{u}}] 即为 ⟨u,f⟩F\langle{\bm{u}},f\rangle_{\mathcal{F}} 的梯度。As this is true for any v∈E{\bm{v}}\in\mathcal{E}, ∂f(w)∗[u]\partial f({\bm{w}})^{*}[{\bm{u}}] is the gradient of ⟨u,f⟩F\langle{\bm{u}},f\rangle_{\mathcal{F}} per Proposition 2.3.7.

图 2.5 展示了 JVP 与 VJP 这两个线性映射。We illustrate the JVP and VJP linear maps in Fig. 2.5.

图 2.5:雅可比—向量乘积(JVP)v↦∂f(w)[v]{\bm{v}}\mapsto\partial f({\bm{w}})[{\bm{v}}] 与向量—雅可比乘积(VJP)u↦∂f(w)∗[u]{\bm{u}}\mapsto\partial f({\bm{w}})^{*}[{\bm{u}}],均视为线性映射。Figure 2.5: Jacobian-vector product (JVP) v↦∂f(w)[v]{\bm{v}}\mapsto\partial f({\bm{w}})[{\bm{v}}] and vector-Jacobian product (VJP) u↦∂f(w)∗[u]{\bm{u}}\mapsto\partial f({\bm{w}})^{*}[{\bm{u}}], seen as linear maps.

2.3.6 用线性映射表述的链式法则Chain rule using linear maps

前面用雅可比矩阵表述的链式法则,可以很方便地改写为以 JVP 与 VJP 线性映射实现的形式。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.

命题 2.3.10(链式法则,一般情形)。Proposition 2.3.10 (Chain rule, general case).
考虑f:E→Ff:\mathcal{E}\rightarrow\mathcal{F}与g:F→Gg:\mathcal{F}\rightarrow\mathcal{G},其中E\mathcal{E}、F\mathcal{F}与G\mathcal{G}均为欧几里得空间。若ff在w∈E{\bm{w}}\in\mathcal{E}处可微,且gg在f(w)∈Ff({\bm{w}})\in\mathcal{F}处可微,则复合函数g∘fg\circ f在w∈E{\bm{w}}\in\mathcal{E}处可微。其对所有v∈E{\bm{v}}\in\mathcal{E}的 JVP 由下式给出Consider f:E→Ff:\mathcal{E}\rightarrow\mathcal{F} and g:F→Gg:\mathcal{F}\rightarrow\mathcal{G}, where E\mathcal{E}, F\mathcal{F} and G\mathcal{G} are Euclidean spaces. If ff is differentiable at w∈E{\bm{w}}\in\mathcal{E} and gg is differentiable at f(w)∈Ff({\bm{w}})\in\mathcal{F}, then the composition g∘fg\circ f is differentiable at w∈E{\bm{w}}\in\mathcal{E}. Its JVP is given for all v∈E{\bm{v}}\in\mathcal{E} by ∂(g∘f)(w)[v]=∂g(f(w))[∂f(w)[v]]\partial(g\circ f)({\bm{w}})[{\bm{v}}]=\partial g(f({\bm{w}}))[\partial f({\bm{w}})[{\bm{v}}]] 且其对所有u∈G{\bm{u}}\in\mathcal{G}的 VJP 由下式给出and its VJP is given for all u∈G{\bm{u}}\in\mathcal{G} by ∂(g∘f)(w)∗[u]=∂f(w)∗[∂g(f(w))∗[u]].\partial(g\circ f)({\bm{w}})^{*}[{\bm{u}}]=\partial f({\bm{w}})^{*}[\partial g(f({\bm{w}}))^{*}[{\bm{u}}]].

其证明沿用命题 2.2.11 的证明,图 2.6 给出了相应图示。当最后一个函数取标量值时(这在机器学习中很常见),便得到如下简化结果。The proof follows the one of Proposition 2.2.11. 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.

命题 2.3.11(链式法则,标量情形)。Proposition 2.3.11 (Chain rule, scalar case).
考虑f:E→Ff:\mathcal{E}\rightarrow\mathcal{F}与g:F→Rg:\mathcal{F}\rightarrow\mathbb{R},复合函数的梯度由下式给出:Consider f:E→Ff:\mathcal{E}\rightarrow\mathcal{F} and g:F→Rg:\mathcal{F}\rightarrow\mathbb{R}, the gradient of the composition is given by ∇(g∘f)(w)=∂f(w)∗[∇g(f(w))].\nabla(g\circ f)({\bm{w}})=\partial f({\bm{w}})^{*}[\nabla g(f({\bm{w}}))].
图 2.6:用 JVP 与 VJP 线性映射表述的链式法则。Figure 2.6: Chain rule using JVP and VJP linear maps.

2.3.7 多输入函数(扇入)Functions of multiple inputs (fan-in)

函数的输入常常并不只属于一个欧几里得空间,而是属于若干个欧几里得空间的乘积。一个例子是 f(x,W):=Wxf({\bm{x}},{\bm{W}})\coloneqq{\bm{W}}{\bm{x}},它定义在 E:=RD×RM×D\mathcal{E}\coloneqq\mathbb{R}^{D}\times\mathbb{R}^{M\times D} 上。在这种情况下,将偏导数的概念推广到处理分块输入会较为方便。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({\bm{x}},{\bm{W}})\coloneqq{\bm{W}}{\bm{x}}, which is defined on E:=RD×RM×D\mathcal{E}\coloneqq\mathbb{R}^{D}\times\mathbb{R}^{M\times D}. In such a case, it is convenient to generalize the notion of partial derivatives to handle blocks of inputs.

考虑定义在 E:=E1×…×ES\mathcal{E}\coloneqq\mathcal{E}_{1}\times\ldots\times\mathcal{E}_{S} 上的函数 f(w1,…,wS)f({\bm{w}}_{1},\ldots,{\bm{w}}_{S}),其中 wi∈Ei{\bm{w}}_{i}\in\mathcal{E}_{i}。我们将关于第 ii 个输入 wi{\bm{w}}_{i} 沿 vi∈Ei{\bm{v}}_{i}\in\mathcal{E}_{i} 的偏导数记为 ∂if(w1,…,wS)[vi]\partial_{i}f({\bm{w}}_{1},\ldots,{\bm{w}}_{S})[{\bm{v}}_{i}]。借助这一记号,可以分析 JVP 或 VJP 如何沿多个输入进行分解。Consider a function f(w1,…,wS)f({\bm{w}}_{1},\ldots,{\bm{w}}_{S}) defined on E:=E1×…×ES\mathcal{E}\coloneqq\mathcal{E}_{1}\times\ldots\times\mathcal{E}_{S}, where wi∈Ei{\bm{w}}_{i}\in\mathcal{E}_{i}. We denote the partial derivative with respect to the iith input wi{\bm{w}}_{i} along vi∈Ei{\bm{v}}_{i}\in\mathcal{E}_{i} as ∂if(w1,…,wS)[vi]\partial_{i}f({\bm{w}}_{1},\ldots,{\bm{w}}_{S})[{\bm{v}}_{i}]. Equipped with this notation, we can analyze how JVPs or VJPs are decomposed along several inputs.

命题 2.3.12(多输入)。Proposition 2.3.12 (Multiple inputs).
考虑形如f(w)=f(w1,…,wS)f({\bm{w}})=f({\bm{w}}_{1},\dots,{\bm{w}}_{S})的可微函数,其签名写作f ⁣:E→Ff\colon\mathcal{E}\to\mathcal{F},其中w:=(w1,…,wS)∈E{\bm{w}}\coloneqq({\bm{w}}_{1},\dots,{\bm{w}}_{S})\in\mathcal{E},E:=E1×⋯×ES\mathcal{E}\coloneqq\mathcal{E}_{1}\times\dots\times\mathcal{E}_{S}。则相对于输入方向v=(v1,…,vS)∈E{\bm{v}}=({\bm{v}}_{1},\dots,{\bm{v}}_{S})\in\mathcal{E}的 JVP 由下式给出:Consider a differentiable function of the form f(w)=f(w1,…,wS)f({\bm{w}})=f({\bm{w}}_{1},\dots,{\bm{w}}_{S}) with signature f ⁣:E→Ff\colon\mathcal{E}\to\mathcal{F}, where w:=(w1,…,wS)∈E{\bm{w}}\coloneqq({\bm{w}}_{1},\dots,{\bm{w}}_{S})\in\mathcal{E} and E:=E1×⋯×ES\mathcal{E}\coloneqq\mathcal{E}_{1}\times\dots\times\mathcal{E}_{S}. Then the JVP with the input direction v=(v1,…,vS)∈E{\bm{v}}=({\bm{v}}_{1},\dots,{\bm{v}}_{S})\in\mathcal{E} is given by ∂f(w)[v]\displaystyle\partial f({\bm{w}})[{\bm{v}}] =∂f(w1,…,wS)[v1,…,vS]∈F\displaystyle=\partial f({\bm{w}}_{1},\dots,{\bm{w}}_{S})[{\bm{v}}_{1},\dots,{\bm{v}}_{S}]\in\mathcal{F} =∑i=1S∂if(w1,…,wS)[vi].\displaystyle=\sum_{i=1}^{S}\partial_{i}f({\bm{w}}_{1},\dots,{\bm{w}}_{S})[{\bm{v}}_{i}]. 相对于输出方向u∈F{\bm{u}}\in\mathcal{F}的 VJP 由下式给出:The VJP with the output direction u∈F{\bm{u}}\in\mathcal{F} is given by ∂f(w)∗[u]\displaystyle\partial f({\bm{w}})^{*}[{\bm{u}}] =∂f(w1,…,wS)∗[u]∈E\displaystyle=\partial f({\bm{w}}_{1},\dots,{\bm{w}}_{S})^{*}[{\bm{u}}]\in\mathcal{E} =(∂1f(w1,…,wS)∗[u],…,∂Sf(w1,…,wS)∗[u]).\displaystyle=(\partial_{1}f({\bm{w}}_{1},\dots,{\bm{w}}_{S})^{*}[{\bm{u}}],\dots,\partial_{S}f({\bm{w}}_{1},\dots,{\bm{w}}_{S})^{*}[{\bm{u}}]).
例 2.3.13(矩阵—向量乘积)。Example 2.3.13 (Matrix-vector product).
考虑f(x,W):=Wxf({\bm{x}},{\bm{W}})\coloneqq{\bm{W}}{\bm{x}},其中W∈RM×D{\bm{W}}\in\mathbb{R}^{M\times D},x∈RD{\bm{x}}\in\mathbb{R}^{D}。这相当于取E:=E1×E2:=RD×RM×D\mathcal{E}\coloneqq\mathcal{E}_{1}\times\mathcal{E}_{2}\coloneqq\mathbb{R}^{D}\times\mathbb{R}^{M\times D}与F:=RM\mathcal{F}\coloneqq\mathbb{R}^{M}。对 JVP,令v∈E1{\bm{v}}\in\mathcal{E}_{1},V∈E2{\bm{V}}\in\mathcal{E}_{2},便得到Consider f(x,W):=Wxf({\bm{x}},{\bm{W}})\coloneqq{\bm{W}}{\bm{x}}, where W∈RM×D{\bm{W}}\in\mathbb{R}^{M\times D} and x∈RD{\bm{x}}\in\mathbb{R}^{D}. This corresponds to setting E:=E1×E2:=RD×RM×D\mathcal{E}\coloneqq\mathcal{E}_{1}\times\mathcal{E}_{2}\coloneqq\mathbb{R}^{D}\times\mathbb{R}^{M\times D} and F:=RM\mathcal{F}\coloneqq\mathbb{R}^{M}. For the JVP, letting v∈E1{\bm{v}}\in\mathcal{E}_{1} and V∈E2{\bm{V}}\in\mathcal{E}_{2}, we obtain ∂f(x,W)[v,V]=Wv+Vx∈F.\partial f({\bm{x}},{\bm{W}})[{\bm{v}},{\bm{V}}]={\bm{W}}{\bm{v}}+{\bm{V}}{\bm{x}}\in\mathcal{F}. 我们也可以分别取得各个 JVP:We can also access the individual JVPs as ∂1f(x,W)[v]\displaystyle\partial_{1}f({\bm{x}},{\bm{W}})[{\bm{v}}] =Wv∈F,\displaystyle={\bm{W}}{\bm{v}}\in\mathcal{F}, ∂2f(x,W)[V]\displaystyle\partial_{2}f({\bm{x}},{\bm{W}})[{\bm{V}}] =Vx∈F.\displaystyle={\bm{V}}{\bm{x}}\in\mathcal{F}. 对 VJP,令u∈F{\bm{u}}\in\mathcal{F},便得到For the VJP, letting u∈F{\bm{u}}\in\mathcal{F}, we obtain ∂f(x,W)∗[u]=(W⊤u,ux⊤)∈E.\partial f({\bm{x}},{\bm{W}})^{*}[{\bm{u}}]=({\bm{W}}^{\top}{\bm{u}},{\bm{u}}{\bm{x}}^{\top})\in\mathcal{E}. 各个 VJP 可由下式分别取得:We can access the individual VJPs by ∂1f(x,W)∗[u]\displaystyle\partial_{1}f({\bm{x}},{\bm{W}})^{*}[{\bm{u}}] =W⊤u∈E1,\displaystyle={\bm{W}}^{\top}{\bm{u}}\in\mathcal{E}_{1}, ∂2f(x,W)∗[u]\displaystyle\partial_{2}f({\bm{x}},{\bm{W}})^{*}[{\bm{u}}] =ux⊤∈E2.\displaystyle={\bm{u}}{\bm{x}}^{\top}\in\mathcal{E}_{2}.
注 2.3.14(嵌套输入)。Remark 2.3.14 (Nested inputs).
有时把输入分成若干有意义的部分会更为方便。例如,若输入天然地分为两部分x=(x1,x2){\bm{x}}=({\bm{x}}_{1},{\bm{x}}_{2}),其中x1{\bm{x}}_{1}是文本部分,x2{\bm{x}}_{2}是图像部分,而网络参数天然地分为三层w=(w1,w2,w3){\bm{w}}=({\bm{w}}_{1},{\bm{w}}_{2},{\bm{w}}_{3}),则可写f(x,w)=f((x1,x2),(w1,w2,w3))f({\bm{x}},{\bm{w}})=f(({\bm{x}}_{1},{\bm{x}}_{2}),({\bm{w}}_{1},{\bm{w}}_{2},{\bm{w}}_{3}))。这主要是出于便利;借助欧几里得空间中的线性映射视角,我们仍可将其归结为单输入的函数。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){\bm{x}}=({\bm{x}}_{1},{\bm{x}}_{2}), where x1{\bm{x}}_{1} is a text part and x2{\bm{x}}_{2} is an image part, and the network parameters are naturally grouped into three layers w=(w1,w2,w3){\bm{w}}=({\bm{w}}_{1},{\bm{w}}_{2},{\bm{w}}_{3}), we can write f(x,w)=f((x1,x2),(w1,w2,w3))f({\bm{x}},{\bm{w}})=f(({\bm{x}}_{1},{\bm{x}}_{2}),({\bm{w}}_{1},{\bm{w}}_{2},{\bm{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.
注 2.3.15(隐藏输入)。Remark 2.3.15 (Hiding away inputs).
求导时,忽略某些输入往往较为方便。为此我们采用分号。例如,形如L(w;x,y)L({\bm{w}};{\bm{x}},{\bm{y}})的函数(注意分号)具有签名L ⁣:W→RL\colon\mathcal{W}\to\mathbb{R},因为我们将x{\bm{x}}与y{\bm{y}}视为常量。因此,其梯度为∇L(w;x,y)∈W\nabla L({\bm{w}};{\bm{x}},{\bm{y}})\in\mathcal{W}。另一方面,函数L(w,x,y)L({\bm{w}},{\bm{x}},{\bm{y}})(注意逗号)具有签名L ⁣:W×X×Y→RL\colon\mathcal{W}\times\mathcal{X}\times\mathcal{Y}\to\mathbb{R},故其梯度为∇L(w,x,y)∈W×X×Y\nabla L({\bm{w}},{\bm{x}},{\bm{y}})\in\mathcal{W}\times\mathcal{X}\times\mathcal{Y}。若需取得各偏梯度,我们使用索引,例如∇1L(w,x,y)∈W\nabla_{1}L({\bm{w}},{\bm{x}},{\bm{y}})\in\mathcal{W};在无歧义时则写作∇wL(w,x,y)∈W\nabla_{\bm{w}}L({\bm{w}},{\bm{x}},{\bm{y}})\in\mathcal{W}。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({\bm{w}};{\bm{x}},{\bm{y}}) (notice the semicolon) has signature L ⁣:W→RL\colon\mathcal{W}\to\mathbb{R} because we treat x{\bm{x}} and y{\bm{y}} as constants. Therefore, the gradient is ∇L(w;x,y)∈W\nabla L({\bm{w}};{\bm{x}},{\bm{y}})\in\mathcal{W}. On the other hand, the function L(w,x,y)L({\bm{w}},{\bm{x}},{\bm{y}}) (notice the comma) has signature L ⁣:W×X×Y→RL\colon\mathcal{W}\times\mathcal{X}\times\mathcal{Y}\to\mathbb{R} so its gradient is ∇L(w,x,y)∈W×X×Y\nabla L({\bm{w}},{\bm{x}},{\bm{y}})\in\mathcal{W}\times\mathcal{X}\times\mathcal{Y}. If we need to access partial gradients, we use indexing, e.g., ∇1L(w,x,y)∈W\nabla_{1}L({\bm{w}},{\bm{x}},{\bm{y}})\in\mathcal{W} or ∇wL(w,x,y)∈W\nabla_{\bm{w}}L({\bm{w}},{\bm{x}},{\bm{y}})\in\mathcal{W} 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.

命题 2.3.16(多输出)。Proposition 2.3.16 (Multiple outputs).
考虑形如f(w):=(f1(w),…,fT(w))f({\bm{w}})\coloneqq(f_{1}({\bm{w}}),\dots,f_{T}({\bm{w}}))的可微函数,其签名分别为f ⁣:E→Ff\colon\mathcal{E}\to\mathcal{F}与fi ⁣:E→Fif_{i}\colon\mathcal{E}\to\mathcal{F}_{i},其中F:=F1×⋯×FT\mathcal{F}\coloneqq\mathcal{F}_{1}\times\dots\times\mathcal{F}_{T}。则相对于输入方向v∈E{\bm{v}}\in\mathcal{E}的 JVP 由下式给出:Consider a differentiable function of the form f(w):=(f1(w),…,fT(w))f({\bm{w}})\coloneqq(f_{1}({\bm{w}}),\dots,f_{T}({\bm{w}})), with signatures f ⁣:E→Ff\colon\mathcal{E}\to\mathcal{F} and fi ⁣:E→Fif_{i}\colon\mathcal{E}\to\mathcal{F}_{i}, where F:=F1×⋯×FT\mathcal{F}\coloneqq\mathcal{F}_{1}\times\dots\times\mathcal{F}_{T}. Then the JVP with the input direction v∈E{\bm{v}}\in\mathcal{E} is given by ∂f(w)[v]=(∂f1(w)[v],…,∂fT(w)[v])∈F.\displaystyle\partial f({\bm{w}})[{\bm{v}}]=(\partial f_{1}({\bm{w}})[{\bm{v}}],\dots,\partial f_{T}({\bm{w}})[{\bm{v}}])\in\mathcal{F}. 相对于输出方向u=(u1,…,uT)∈F{\bm{u}}=({\bm{u}}_{1},\dots,{\bm{u}}_{T})\in\mathcal{F}的 VJP 为The VJP with the output direction u=(u1,…,uT)∈F{\bm{u}}=({\bm{u}}_{1},\dots,{\bm{u}}_{T})\in\mathcal{F} is ∂f(w)∗[u]\displaystyle\partial f({\bm{w}})^{*}[{\bm{u}}] =∂f(w)∗[u1,…,uT]∈E\displaystyle=\partial f({\bm{w}})^{*}[{\bm{u}}_{1},\dots,{\bm{u}}_{T}]\in\mathcal{E} =∑i=1T∂fi(w)∗[ui].\displaystyle=\sum_{i=1}^{T}\partial f_{i}({\bm{w}})^{*}[{\bm{u}}_{i}].

结合链式法则,我们得到……的雅可比矩阵Combined with the chain rule, we obtain that the Jacobian of

h(w):=g(f(w))=g(f1(w),…,fT(w))h({\bm{w}})\coloneqq g(f({\bm{w}}))=g(f_{1}({\bm{w}}),\dots,f_{T}({\bm{w}}))

的雅可比矩阵为 ∂h(w)=∑i=1T∂ig(f(w))∘∂fi(w)\partial h({\bm{w}})=\sum_{i=1}^{T}\partial_{i}g(f({\bm{w}}))\circ\partial f_{i}({\bm{w}}),因此 JVP 为is ∂h(w)=∑i=1T∂ig(f(w))∘∂fi(w)\partial h({\bm{w}})=\sum_{i=1}^{T}\partial_{i}g(f({\bm{w}}))\circ\partial f_{i}({\bm{w}}) and therefore the JVP is

∂h(w)[v]=∑i=1T∂ig(f(w))[∂fi(w)[v]].\partial h({\bm{w}})[{\bm{v}}]=\sum_{i=1}^{T}\partial_{i}g(f({\bm{w}}))[\partial f_{i}({\bm{w}})[{\bm{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.

例如,方向导数(见定义 2.3.5)可以在任何配备范数且关于该范数完备的线性空间中定义,这样的空间称为巴拿赫空间。完备性是一项技术性假设,它要求任何柯西序列都收敛(柯西序列是指随着序列推进,其元素彼此任意接近的序列)。从巴拿赫空间 E\mathcal{E} 到巴拿赫空间 F\mathcal{F} 的函数 f:E→Ff:\mathcal{E}\rightarrow\mathcal{F},若其方向导数沿任意方向都有定义(其中极限是关于 F\mathcal{F} 中的范数取的),则称为 Gateaux 可微的。有些作者还要求方向导数必须是线性的,才将函数定义为 Gateaux 可微。For example, directional derivatives (see Definition 2.3.5) 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:\mathcal{E}\rightarrow\mathcal{F} defined from a Banach space E\mathcal{E} onto a Banach space F\mathcal{F} 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\mathcal{F}). Some authors also require the directional derivative to be linear to define a Gateaux differentiable function.

Fréchet 可微性同样可以自然地推广到巴拿赫空间。唯一的区别在于,在一般巴拿赫空间中,满足定义 2.3.5 的线性映射 ll 必须是连续的,即必须存在 C>0C>0,使得 l[v]≤C∥v∥l[{\bm{v}}]\leq C\|{\bm{v}}\|,其中 ∥⋅∥\|\cdot\| 是巴拿赫空间 E\mathcal{E} 上的范数。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.3.5 must be continuous, i.e., there must exist C>0C>0, such that l[v]≤C∥v∥l[{\bm{v}}]\leq C\|{\bm{v}}\|, where ∥⋅∥\|\cdot\| is the norm in the Banach space E\mathcal{E}.

梯度与 VJP 的定义则还需要内积的概念。它们可以在希尔伯特空间中定义,即配备了内积、并关于由该内积诱导的范数完备的线性空间(也可以借助对偶空间中的运算,在巴拿赫空间中定义,参见例如 [45])。梯度的存在性由 Riesz 表示定理保证:该定理指出,希尔伯特空间中的任意连续线性泛函,都可以用与某个向量的内积来表示。由于对可微函数 f:E→Rf:\mathcal{E}\rightarrow\mathbb{R} 而言,JVP ∂f(w):E→R\partial f({\bm{w}}):\mathcal{E}\rightarrow\mathbb{R} 是一个线性泛函,Riesz 表示定理便保证了梯度的存在,即存在元素 g∈Eg\in\mathcal{E},使得对任意 v∈E{\bm{v}}\in\mathcal{E} 都有 ∂f(w)v=⟨g,v⟩\partial f({\bm{w}}){\bm{v}}=\langle g,{\bm{v}}\rangle。VJP 同样是良定义的,即作为 JVP 关于该希尔伯特空间内积的伴随。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. [45]). 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:\mathcal{E}\rightarrow\mathbb{R}, the JVP ∂f(w):E→R\partial f({\bm{w}}):\mathcal{E}\rightarrow\mathbb{R} is a linear form, Riesz’s representation theorem ensures the existence of the gradient as the element g∈Eg\in\mathcal{E} such that ∂f(w)v=⟨g,v⟩\partial f({\bm{w}}){\bm{v}}=\langle g,{\bm{v}}\rangle for any v∈E{\bm{v}}\in\mathcal{E}. The VJP is also well-defined as the adjoint of the JVP w.r.t. the inner product of the Hilbert space.

例如,R\mathbb{R} 上的平方可积函数空间,就是一个配备了内积 ⟨a,b⟩:=∫a(x)b(x)dx.\langle a,b\rangle\coloneqq\int a(x)b(x)dx. 的希尔伯特空间。在这里,我们找不到有限个函数,足以表示 R\mathbb{R} 上所有可能的函数。因此,这一空间并非单纯的欧几里得空间。尽管如此,我们仍可以考虑这个希尔伯特空间上的函数(称为泛函,以区别于该空间中的元素)。由此定义出的方向导数与梯度,分别称为泛函导数与泛函梯度,参见例如 [72] 及其中的参考文献。As an example, the space of squared integrable functions on R\mathbb{R} 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\mathbb{R}. 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., [72] and references therein.

2.4 二阶微分2.4 Second-order differentiation

2.4.1 二阶导数Second derivatives

对于单输入单输出的可微函数 f:R→Rf:\mathbb{R}\rightarrow\mathbb{R},它在任意一点处的导数本身就是一个函数 f′:R→Rf^{\prime}:\mathbb{R}\rightarrow\mathbb{R}。于是,我们可以进一步考虑该导数在任意一点处的导数:二阶导数。For a single-input, single-output differentiable function f:R→Rf:\mathbb{R}\rightarrow\mathbb{R}, its derivative at any point is itself a function f′:R→Rf^{\prime}:\mathbb{R}\rightarrow\mathbb{R}. We may then consider the derivative of the derivative at any point: the second derivative.

定义 2.4.1(二阶导数)。Definition 2.4.1 (Second derivative).
可微函数f:R→Rf:\mathbb{R}\rightarrow\mathbb{R}在w∈Rw\in\mathbb{R}处的二阶导数f(2)(w)f^{(2)}(w),定义为f′f^{\prime}在ww处的导数,即The second derivative f(2)(w)f^{(2)}(w) of a differentiable function f:R→Rf:\mathbb{R}\rightarrow\mathbb{R} at w∈Rw\in\mathbb{R} is defined as the derivative of f′f^{\prime} at ww, that is, f(2)(w):=lim⁡δ→0f′(w+δ)−f′(w)δ,f^{(2)}(w)\coloneqq\lim_{\delta\rightarrow 0}\frac{f^{\prime}(w+\delta)-f^{\prime}(w)}{\delta}, 前提是该极限有良定义。若函数 ff 的二阶导数在 ww 处有良定义,则称该函数在 ww 处二次可微。二阶导数也记作 f′′f^{\prime\prime}。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^{\prime\prime}.

若 ff 在给定的 ww 处二阶导数很小,则其导数在 ww 附近几乎为常数。也就是说,函数在 ww 附近的行为近似于一条直线,如图 2.7 所示。因此,二阶导数通常被解释为函数在给定点处的曲率。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.

图 2.7:二阶导数较小的点,是函数可由其切线良好近似的点;反之,二阶导数较大的点,往往难以用切线良好近似。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

对于多输入函数 f:RP→Rf:\mathbb{R}^{P}\rightarrow\mathbb{R},我们已经看到,方向导数刻画了 ff 沿给定方向的无穷小变化。为了分析二阶导数,即函数在给定点 w{\bm{w}} 处的曲率,我们可以考虑沿一对方向的变化,其定义如下。For a multi-input function f:RP→Rf:\mathbb{R}^{P}\rightarrow\mathbb{R}, 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{\bm{w}}, we can consider the variations along a pair of directions, as defined below.

定义 2.4.2(二阶方向导数)。Definition 2.4.2 (Second directional derivative).
f:RP→Rf:\mathbb{R}^{P}\rightarrow\mathbb{R} 在 w∈RP{\bm{w}}\in\mathbb{R}^{P} 处沿 v,v′∈RP{\bm{v}},{\bm{v}}^{\prime}\in\mathbb{R}^{P} 的二阶方向导数定义为 w↦∂f(w)[v]{\bm{w}}\mapsto\partial f({\bm{w}})[{\bm{v}}] 沿 v′{\bm{v}}^{\prime} 的方向导数,即The second directional derivative of f:RP→Rf:\mathbb{R}^{P}\rightarrow\mathbb{R} at w∈RP{\bm{w}}\in\mathbb{R}^{P} along v,v′∈RP{\bm{v}},{\bm{v}}^{\prime}\in\mathbb{R}^{P} is defined as the directional derivative of w↦∂f(w)[v]{\bm{w}}\mapsto\partial f({\bm{w}})[{\bm{v}}] along v′{\bm{v}}^{\prime}, that is, ∂2f(w)[v,v′]:=lim⁡δ→0∂f(w+δv′)[v]−∂f(w)[v]δ,\partial^{2}f({\bm{w}})[{\bm{v}},{\bm{v}}^{\prime}]\coloneqq\lim_{\delta\rightarrow 0}\frac{\partial f({\bm{w}}+\delta{\bm{v}}^{\prime})[{\bm{v}}]-\partial f({\bm{w}})[{\bm{v}}]}{\delta}, 前提是 ∂f(w)[v]\partial f({\bm{w}})[{\bm{v}}] 在 w{\bm{w}} 附近有良定义,且该极限存在。provided that ∂f(w)[v]\partial f({\bm{w}})[{\bm{v}}] is well-defined around w{\bm{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_{ij}^{2}f({\bm{w}})\coloneqq\partial^{2}f({\bm{w}})[{\bm{e}}_{i},{\bm{e}}_{j}]

其中 ei{\bm{e}}_{i},ej{\bm{e}}_{j} 分别为 RP\mathbb{R}^{P} 中的第 ii 个和第 jj 个标准方向。在莱布尼茨记号下,二阶偏导数记为for ei{\bm{e}}_{i}, ej{\bm{e}}_{j} the iith and jjth canonical directions in RP\mathbb{R}^{P}, respectively. In Leibniz notation, the second partial derivatives are denoted

∂ij2f(w)=∂2f(w)∂wi∂wj.\partial_{ij}^{2}f({\bm{w}})=\frac{\partial^{2}f({\bm{w}})}{\partial w_{i}\partial w_{j}}.

2.4.3 Hessian 矩阵Hessians

对于多输入函数,二次可微性可简单地定义为:任意方向导数 ∂f(w)[v]\partial f({\bm{w}})[{\bm{v}}] 关于 w{\bm{w}} 可微。For a multi-input function, twice differentiability is simply defined as the differentiability of any directional derivative ∂f(w)[v]\partial f({\bm{w}})[{\bm{v}}] w.r.t. w{\bm{w}}.

定义 2.4.3(二次可微性)。Definition 2.4.3 (Twice differentiability).
若函数 f:RP→Rf:\mathbb{R}^{P}\rightarrow\mathbb{R} 可微,且 ∂f:RP→(RP→R)\partial f:\mathbb{R}^{P}\rightarrow(\mathbb{R}^{P}\rightarrow\mathbb{R}) 在 w{\bm{w}} 处也可微,则称该函数在 w∈RP{\bm{w}}\in\mathbb{R}^{P} 处二次可微。A function f:RP→Rf:\mathbb{R}^{P}\rightarrow\mathbb{R} is twice differentiable at w∈RP{\bm{w}}\in\mathbb{R}^{P} if it is differentiable and ∂f:RP→(RP→R)\partial f:\mathbb{R}^{P}\rightarrow(\mathbb{R}^{P}\rightarrow\mathbb{R}) is also differentiable at w{\bm{w}}.

因此,二阶方向导数是一个双线性型。As a result, the second directional derivative is a bilinear form.

定义 2.4.4(双线性映射、双线性型)。Definition 2.4.4 (Bilinear map, bilinear form).
若对任意 v{\bm{v}},b[v,⋅]:RP→Rb[{\bm{v}},\cdot]:\mathbb{R}^{P}\rightarrow\mathbb{R} 均为线性,且对任意 v′{\bm{v}}^{\prime},b[⋅,v′]b[\cdot,{\bm{v}}^{\prime}] 均为线性,则称函数 b:RP×RP→RMb:\mathbb{R}^{P}\times\mathbb{R}^{P}\rightarrow\mathbb{R}^{M} 为双线性映射。也就是说,A function b:RP×RP→RMb:\mathbb{R}^{P}\times\mathbb{R}^{P}\rightarrow\mathbb{R}^{M} is a bilinear map if b[v,⋅]:RP→Rb[{\bm{v}},\cdot]:\mathbb{R}^{P}\rightarrow\mathbb{R} is linear for any v{\bm{v}} and b[⋅,v′]b[\cdot,{\bm{v}}^{\prime}] is linear for any v′{\bm{v}}^{\prime}. That is, b[v,v′]=∑i=1Pvib[ei,v′]=∑i=1P∑j=1Pvivj′b[ei,ej],b[{\bm{v}},{\bm{v}}^{\prime}]=\sum_{i=1}^{P}v_{i}b[{\bm{e}}_{i},{\bm{v}}^{\prime}]=\sum_{i=1}^{P}\sum_{j=1}^{P}v_{i}v_{j}^{\prime}b[{\bm{e}}_{i},{\bm{e}}_{j}], 对于 v=∑i=1Pviei{\bm{v}}=\sum_{i=1}^{P}v_{i}{\bm{e}}_{i} 与 v′=∑i=1Pvi′ei{\bm{v}}^{\prime}=\sum_{i=1}^{P}v_{i}^{\prime}{\bm{e}}_{i}。取值于 R\mathbb{R} 的双线性映射 b:RP×RP→Rb:\mathbb{R}^{P}\times\mathbb{R}^{P}\rightarrow\mathbb{R} 称为双线性型。for v=∑i=1Pviei{\bm{v}}=\sum_{i=1}^{P}v_{i}{\bm{e}}_{i} and v′=∑i=1Pvi′ei{\bm{v}}^{\prime}=\sum_{i=1}^{P}v_{i}^{\prime}{\bm{e}}_{i}. A bilinear map with values in R\mathbb{R}, b:RP×RP→Rb:\mathbb{R}^{P}\times\mathbb{R}^{P}\rightarrow\mathbb{R}, is called a bilinear form.

二阶偏导数汇集于Hessian 矩阵中,而二阶方向导数可由该矩阵计算得到。The second partial derivatives are gathered in the Hessian and the second directional derivatives can be computed from it.

定义 2.4.5(Hessian 矩阵)。Definition 2.4.5 (Hessian).
二次可微函数 f:RP→Rf:\mathbb{R}^{P}\rightarrow\mathbb{R} 在 w{\bm{w}} 处的 Hessian 矩阵,是汇集其所有二阶偏导数的 P×PP\times P 矩阵,The Hessian of a twice differentiable function f:RP→Rf:\mathbb{R}^{P}\rightarrow\mathbb{R} at w{\bm{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({\bm{w}})\coloneqq\begin{pmatrix}\partial_{11}f({\bm{w}})&\ldots&\partial_{1P}f({\bm{w}})\\ \vdots&\ddots&\vdots\\ \partial_{P1}f({\bm{w}})&\ldots&\partial_{PP}f({\bm{w}})\end{pmatrix}\in\mathbb{R}^{P\times P}, 前提是所有二阶偏导数均有良定义。provided that all second partial derivatives are well-defined. 在 w{\bm{w}} 处的二阶方向导数对任意方向 v=∑i=1Pviei{\bm{v}}=\sum_{i=1}^{P}v_{i}{\bm{e}}_{i} 与 v′=∑i=1Pvi′ei{\bm{v}}^{\prime}=\sum_{i=1}^{P}v_{i}^{\prime}{\bm{e}}_{i} 均为双线性的。因此,The second directional derivative at w{\bm{w}} is bilinear in any direction v=∑i=1Pviei{\bm{v}}=\sum_{i=1}^{P}v_{i}{\bm{e}}_{i} and v′=∑i=1Pvi′ei{\bm{v}}^{\prime}=\sum_{i=1}^{P}v_{i}^{\prime}{\bm{e}}_{i}. Therefore, ∂2f(w)[v,v′]=∑i,j=1Pvivj′∂2f(w)[ei,ej]=⟨v,∇2f(w)v′⟩.\partial^{2}f({\bm{w}})[{\bm{v}},{\bm{v}}^{\prime}]=\sum_{i,j=1}^{P}v_{i}v_{j}^{\prime}\partial^{2}f({\bm{w}})[{\bm{e}}_{i},{\bm{e}}_{j}]=\langle{\bm{v}},\nabla^{2}f({\bm{w}}){\bm{v}}^{\prime}\rangle.

给定 ff 的梯度,Hessian 矩阵等价于该梯度雅可比矩阵的转置。将记号 ∇\nabla 稍作推广,用以表示函数雅可比矩阵的转置(这与单输出函数下的定义相符),我们便可将 Hessian 矩阵表示为 ∇2f(w)=∇(∇f)(w)\nabla^{2}f({\bm{w}})=\nabla(\nabla f)({\bm{w}}),这也解释了其记号。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({\bm{w}})=\nabla(\nabla f)({\bm{w}}), which justifies its notation.

与函数 ff 的可微性类似,ff 在 w{\bm{w}} 处的二次可微性等价于:二阶偏导数不仅在 w{\bm{w}} 的邻域内有定义,而且在该邻域内连续。值得注意的是,要求二次可微,即二阶偏导数连续,就能保证 Hessian 矩阵是对称的 [181]。Similarly as for the differentiability of a function ff, twice differentiability of ff at w{\bm{w}} is equivalent to having the second partial derivatives not only defined but also continuous in a neighborhood of w{\bm{w}}. Remarkably, by requiring twice differentiability, i.e., continuous second partial derivatives, the Hessian is guaranteed to be symmetric [181].

命题 2.4.6(Hessian 矩阵的对称性)。Proposition 2.4.6 (Symmetry of the Hessian).
若函数 f:RP→Rf:\mathbb{R}^{P}\rightarrow\mathbb{R} 在 w{\bm{w}} 处二次可微,则其 Hessian 矩阵 ∇2f(w)\nabla^{2}f({\bm{w}}) 是对称的,即对任意 i,j∈{1,…P}i,j\in\{1,\ldots P\},有 ∂ij2f(w)=∂ji2f(w)\partial_{ij}^{2}f({\bm{w}})=\partial_{ji}^{2}f({\bm{w}})。If a function f:RP→Rf:\mathbb{R}^{P}\rightarrow\mathbb{R} is twice differentiable at w{\bm{w}}, then its Hessian ∇2f(w)\nabla^{2}f({\bm{w}}) is symmetric, that is, ∂ij2f(w)=∂ji2f(w)\partial_{ij}^{2}f({\bm{w}})=\partial_{ji}^{2}f({\bm{w}}) for any i,j∈{1,…P}i,j\in\{1,\ldots P\}.

Hessian 矩阵的对称性意味着它也可以写成 ∇2f(w)=(∂ji2f(w))i,j=1P=∂(∇f)(w)\nabla^{2}f({\bm{w}})=(\partial_{ji}^{2}f({\bm{w}}))_{i,j=1}^{P}=\bm{\partial}(\nabla f)({\bm{w}}),即 ff 的梯度的雅可比矩阵。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({\bm{w}})=(\partial_{ji}^{2}f({\bm{w}}))_{i,j=1}^{P}=\bm{\partial}(\nabla f)({\bm{w}}), i.e., the Jacobian of the gradient of ff.

2.4.4 Hessian 矩阵—向量乘积Hessian-vector products

与雅可比矩阵类似,我们可以利用Hessian 矩阵作为双线性型的形式定义,将其定义推广到欧几里得空间。特别地,我们可以定义Hessian 矩阵—向量乘积这一概念。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.

定义 2.4.7(Hessian 矩阵—向量乘积)。Definition 2.4.7 (Hessian-vector product).
若定义在具有内积 ⟨⋅,⋅⟩\langle\cdot,\cdot\rangle 的欧几里得空间 E\mathcal{E} 上的函数 f:E→Rf:\mathcal{E}\rightarrow\mathbb{R} 在 w∈E{\bm{w}}\in\mathcal{E} 处二次可微,则对任意 v∈E{\bm{v}}\in\mathcal{E},存在 v↦∇2f(w)[v]{\bm{v}}\mapsto\nabla^{2}f({\bm{w}})[{\bm{v}}],称为 ff 在 w{\bm{w}} 处沿 v{\bm{v}} 的 Hessian-向量乘积(HVP),使得对任意 v′∈E{\bm{v}}^{\prime}\in\mathcal{E},If a function f:E→Rf:\mathcal{E}\rightarrow\mathbb{R} defined on a Euclidean space E\mathcal{E} with inner product ⟨⋅,⋅⟩\langle\cdot,\cdot\rangle, is twice differentiable at w∈E{\bm{w}}\in\mathcal{E}, then for any v∈E{\bm{v}}\in\mathcal{E}, there exists v↦∇2f(w)[v]{\bm{v}}\mapsto\nabla^{2}f({\bm{w}})[{\bm{v}}], called the Hessian-vector product (HVP) of ff at w{\bm{w}} along v{\bm{v}}, such that for any v′∈E{\bm{v}}^{\prime}\in\mathcal{E}, ∂2f(w)[v,v′]=⟨v′,∇2f(w)[v]⟩.\partial^{2}f({\bm{w}})[{\bm{v}},{\bm{v}}^{\prime}]=\langle{\bm{v}}^{\prime},\nabla^{2}f({\bm{w}})[{\bm{v}}]\rangle. 特别地,对于 E=RP\mathcal{E}=\mathbb{R}^{P},其 HVP 为 ∇2f(w)[v]=(∂2f(w)[v,ei])i=1P\nabla^{2}f({\bm{w}})[{\bm{v}}]=(\partial^{2}f({\bm{w}})[{\bm{v}},{\bm{e}}_{i}])_{i=1}^{P}。In particular for E=RP\mathcal{E}=\mathbb{R}^{P}, the HVP is ∇2f(w)[v]=(∂2f(w)[v,ei])i=1P\nabla^{2}f({\bm{w}})[{\bm{v}}]=(\partial^{2}f({\bm{w}})[{\bm{v}},{\bm{e}}_{i}])_{i=1}^{P}.

从自动微分的角度看,HVP 可以用四种不同的方式实现,如 9.1 节所述。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

前面的定义自然推广到多输出函数 f:E→Ff:\mathcal{E}\rightarrow\mathcal{F},其中 f:=(f1,…,fM)f\coloneqq(f_{1},\dots,f_{M}),fj ⁣:E→Fjf_{j}\colon\mathcal{E}\to\mathcal{F}_{j},且 F:=F1×⋯×FM\mathcal{F}\coloneqq\mathcal{F}_{1}\times\dots\times\mathcal{F}_{M}。二阶方向导数通过汇集各坐标函数的二阶导数来定义。也就是说,对于 w,v,v′∈E{\bm{w}},{\bm{v}},{\bm{v}}^{\prime}\in\mathcal{E},The previous definitions naturally extend to multi-output functions f:E→Ff:\mathcal{E}\rightarrow\mathcal{F}, where f:=(f1,…,fM)f\coloneqq(f_{1},\dots,f_{M}), fj ⁣:E→Fjf_{j}\colon\mathcal{E}\to\mathcal{F}_{j} and F:=F1×⋯×FM\mathcal{F}\coloneqq\mathcal{F}_{1}\times\dots\times\mathcal{F}_{M}. The second directional derivative is defined by gathering the second derivatives of each coordinate’s function. That is, for w,v,v′∈E{\bm{w}},{\bm{v}},{\bm{v}}^{\prime}\in\mathcal{E},

∂2f(w)[v,v′]=(∂2fj(w)[v,v′])j=1M∈F.\partial^{2}f({\bm{w}})[{\bm{v}},{\bm{v}}^{\prime}]=(\partial^{2}f_{j}({\bm{w}})[{\bm{v}},{\bm{v}}^{\prime}])_{j=1}^{M}\in\mathcal{F}.

函数 ff 二次可微,当且仅当其所有坐标都二次可微。此时,二阶方向导数是双线性映射。因此,我们可以按如下方式计算二阶方向导数: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({\bm{w}})[{\bm{v}},{\bm{v}}^{\prime}]=\sum_{i,j=1}^{P}v_{i}v_{j}^{\prime}\partial^{2}f({\bm{w}})[{\bm{e}}_{i},{\bm{e}}_{j}]=(\langle{\bm{v}},\nabla^{2}f_{j}({\bm{w}}){\bm{v}}^{\prime}\rangle)_{j=1}^{M}.

当 E=RP\mathcal{E}=\mathbb{R}^{P} 且 Fj=R\mathcal{F}_{j}=\mathbb{R},从而 F=RM\mathcal{F}=\mathbb{R}^{M} 时,该双线性映射可以具体化为一个张量When E=RP\mathcal{E}=\mathbb{R}^{P} and Fj=R\mathcal{F}_{j}=\mathbb{R}, so that F=RM\mathcal{F}=\mathbb{R}^{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({\bm{w}})=(\partial^{2}f({\bm{w}})[{\bm{e}}_{i},{\bm{e}}_{j}])_{i,j=1}^{P}\in\mathbb{R}^{M\times P\times P},

即 ff 的“二阶雅可比矩阵”。然而,与Hessian 矩阵类似,通常更便于将双线性映射作用于给定向量 v{\bm{v}} 和 v′{\bm{v}}^{\prime},而不是将二阶偏导数具体化为张量。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{\bm{v}} and v′{\bm{v}}^{\prime} than to materialize the second partial derivatives as a tensor.

2.5 高阶微分2.5 Higher-order differentiation

2.5.1 高阶导数Higher-order derivatives

导数可以推广到任意阶。形式上,对于单输入单输出函数,第 nn 阶导数可以归纳定义如下。Derivatives can be extended to any order. Formally, the nnth derivative can be defined inductively as follows for a single-input, single-output function.

定义 2.5.1(第 nn 阶导数)。Definition 2.5.1 (nnth order derivative).
函数 f:R→Rf:\mathbb{R}\rightarrow\mathbb{R} 在 w∈Rw\in\mathbb{R} 处的第 nn 阶导数 f(n)f^{(n)} 定义为The nnth derivative f(n)f^{(n)} of a function f:R→Rf:\mathbb{R}\rightarrow\mathbb{R} at w∈Rw\in\mathbb{R} 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)})^{\prime}(w)=\lim_{\delta\rightarrow 0}\frac{f^{(n-1)}(w+\delta)-f^{(n-1)}(w)}{\delta} 其中要求 f(n−1)f^{(n-1)} 在 ww 附近可微,且该极限存在。此时称该函数在 ww 处 nn 次可微。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

对于多输入函数 ff,我们可以按如下方式自然推广方向导数的概念。For a multi-input function ff, we can naturally extend the notion of directional derivative as follows.

定义 2.5.2(第 nn 阶方向导数)。Definition 2.5.2 (nnth order directional derivative).
f:RP→Rf:\mathbb{R}^{P}\rightarrow\mathbb{R} 在 w∈RP{\bm{w}}\in\mathbb{R}^{P} 处沿 v1,…,vn{\bm{v}}_{1},\ldots,{\bm{v}}_{n} 的第 nn 阶方向导数定义为The nnth directional derivative of f:RP→Rf:\mathbb{R}^{P}\rightarrow\mathbb{R} at w∈RP{\bm{w}}\in\mathbb{R}^{P} along v1,…,vn{\bm{v}}_{1},\ldots,{\bm{v}}_{n} is defined as ∂nf(w)[v1,…,vn]\displaystyle\partial^{n}f({\bm{w}})[{\bm{v}}_{1},\ldots,{\bm{v}}_{n}] =∂(∂n−1f(w)[v1,…,vn−1])[vn]\displaystyle=\partial(\partial^{n-1}f({\bm{w}})[{\bm{v}}_{1},\ldots,{\bm{v}}_{n-1}])[{\bm{v}}_{n}] =lim⁡δ→0∂f(w+δvn)[v1,…,vn−1]−∂f(w)[v1,…,vn−1]δ\displaystyle=\lim_{\delta\rightarrow 0}\frac{\partial f({\bm{w}}+\delta{\bm{v}}_{n})[{\bm{v}}_{1},\ldots,{\bm{v}}_{n-1}]-\partial f({\bm{w}})[{\bm{v}}_{1},\ldots,{\bm{v}}_{n-1}]}{\delta}

多输入函数 ff 是 nn 次可微的,如果它 n−1n-1 可微,并且它沿任意方向的 n−1n-1 阶方向导数都可微。因此,第 nn 阶方向导数是多重线性型。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.

定义 2.5.3(多重线性映射、多重线性型)。Definition 2.5.3 (Multilinear map, multilinear form).
若函数 c:⊗i=1nRP→RMc:\otimes_{i=1}^{n}\mathbb{R}^{P}\rightarrow\mathbb{R}^{M} 在固定其余坐标时关于每个坐标均为线性,则称之为多重线性映射;也就是说,若对任意 j∈[n]j\in[n],vj↦c[v1,…,vj,…,vn]{\bm{v}}_{j}\mapsto c[{\bm{v}}_{1},\ldots,{\bm{v}}_{j},\ldots,{\bm{v}}_{n}] 关于 vj{\bm{v}}_{j} 是线性的。若其取值落在 R\mathbb{R} 中,则称之为多重线性型。A function c:⊗i=1nRP→RMc:\otimes_{i=1}^{n}\mathbb{R}^{P}\rightarrow\mathbb{R}^{M} is a multilinear map if it is linear in each coordinate given all others fixed, that is, if vj↦c[v1,…,vj,…,vn]{\bm{v}}_{j}\mapsto c[{\bm{v}}_{1},\ldots,{\bm{v}}_{j},\ldots,{\bm{v}}_{n}] is linear in vj{\bm{v}}_{j} for any j∈[n]j\in[n]. It is a multilinear form if it has values in R\mathbb{R}.

第 nn 阶方向导数则由下式给出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({\bm{w}})[{\bm{v}}_{1},\ldots,{\bm{v}}_{n}]=\sum_{i_{1},\ldots,i_{n}=1}^{P}v_{1,i_{1}}\ldots v_{n,i_{n}}\partial^{n}f({\bm{w}})[{\bm{e}}_{i_{1}},\ldots,{\bm{e}}_{i_{n}}].

第 nn 阶偏导数可以具体化为一个 nn 阶张量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({\bm{w}})=(\partial^{n}f({\bm{w}})[{\bm{e}}_{i_{1}},\ldots,{\bm{e}}_{i_{n}}])_{i_{1},\ldots,i_{n}=1}^{P}\in\mathbb{R}^{P\times\ldots\times P}.

2.5.3 高阶雅可比矩阵Higher-order Jacobians

上述所有定义都直接推广到多输出函数 f:E→Ff:\mathcal{E}\rightarrow\mathcal{F} 的情形,其中 F:=F1×⋯×FM\mathcal{F}\coloneqq\mathcal{F}_{1}\times\dots\times\mathcal{F}_{M}。此时,第 nn 阶方向导数为All above definitions extend directly to the case of multi-output functions f:E→Ff:\mathcal{E}\rightarrow\mathcal{F}, where F:=F1×⋯×FM\mathcal{F}\coloneqq\mathcal{F}_{1}\times\dots\times\mathcal{F}_{M}. The nnth directional derivatives are then

∂nf(w)[v1,…,vn]=(∂nfj(w)[v1,…,vn])j=1M.\partial^{n}f({\bm{w}})[{\bm{v}}_{1},\ldots,{\bm{v}}_{n}]=(\partial^{n}f_{j}({\bm{w}})[{\bm{v}}_{1},\ldots,{\bm{v}}_{n}])_{j=1}^{M}.

函数 ff 称为 nn 次可微的,当它 n−1n-1 可微、且沿任意方向的 n−1n-1 阶方向导数都可微时。因此,第 nn 阶方向导数是多重线性映射。第 nn 阶方向导数可以分解为偏导数,如下式所示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({\bm{w}})[{\bm{v}}_{1},\ldots,{\bm{v}}_{n}]=\sum_{i_{1},\ldots,i_{n}=1}^{P}v_{1,i_{1}}\ldots v_{n,i_{n}}\partial^{n}f({\bm{w}})[{\bm{e}}_{i_{1}},\ldots,{\bm{e}}_{i_{n}}].

当 E=RP\mathcal{E}=\mathbb{R}^{P} 且 F=RM\mathcal{F}=\mathbb{R}^{M} 时,第 nn 阶偏导数可以通过一个 (n+1)(n+1) 阶张量具体化When E=RP\mathcal{E}=\mathbb{R}^{P} and F=RM\mathcal{F}=\mathbb{R}^{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({\bm{w}})=(\partial^{n}f_{j}({\bm{w}})[{\bm{e}}_{i_{1}},\ldots,{\bm{e}}_{i_{n}}])_{j=1,i_{1},\ldots,i_{n}=1}^{M,P,\ldots,P}\in\mathbb{R}^{M\times P\times\ldots\times P}.

2.5.4 泰勒展开Taylor expansions

借助 Landau 的小 oo 记号,我们已经看到,如果一个函数可微,那么它在 v{\bm{v}} 意义下可由一个线性函数逼近,With Landau’s little oo notation, we have seen that if a function is differentiable, it is approximated by a linear function in v{\bm{v}},

f(w+v)=f(w)+⟨∇f(w),v⟩+o(∥v∥2).f({\bm{w}}+{\bm{v}})=f({\bm{w}})+\langle\nabla f({\bm{w}}),{\bm{v}}\rangle+o(\|{\bm{v}}\|_{2}).

将函数展开到其一阶导数的这种做法,称为 ff 在 w{\bm{w}} 附近的一阶泰勒展开。Such an expansion of the function up to its first derivative is called the first-order Taylor expansion of ff around w{\bm{w}}.

若函数 ff 二次可微,则可在 v{\bm{v}} 中用二次函数近似它,从而得到 ff 在 w{\bm{w}} 附近的二阶泰勒展开,If the function ff is twice differentiable, we can approximate it by a quadratic in v{\bm{v}}, leading to the second-order Taylor expansion of ff around w{\bm{w}},

f(w+v)=f(w)+⟨∇f(w),v⟩+12⟨v,∇2f(w)v⟩+o(∥v∥22).f({\bm{w}}+{\bm{v}})=f({\bm{w}})+\langle\nabla f({\bm{w}}),{\bm{v}}\rangle+\frac{1}{2}\langle{\bm{v}},\nabla^{2}f({\bm{w}}){\bm{v}}\rangle+o(\|{\bm{v}}\|_{2}^{2}).

与一阶泰勒近似相比,它在 w{\bm{w}} 附近自然更为精确,这体现在:对于 ∥v∥2≤1\|{\bm{v}}\|_{2}\leq 1,有 ∥v∥23≤∥v∥22\|{\bm{v}}\|_{2}^{3}\leq\|{\bm{v}}\|_{2}^{2}。Compared to the first-order Taylor approximation, it is naturally more accurate around w{\bm{w}}, as reflected by the fact that ∥v∥23≤∥v∥22\|{\bm{v}}\|_{2}^{3}\leq\|{\bm{v}}\|_{2}^{2} for ∥v∥2≤1\|{\bm{v}}\|_{2}\leq 1.

更一般地,我们可以通过下式构造 nn 次可微函数 f ⁣:RP→RMf\colon\mathbb{R}^{P}\to\mathbb{R}^{M} 在 w∈RP{\bm{w}}\in\mathbb{R}^{P} 附近的第 n{\bm{n}} 阶泰勒展开More generally, we can build the n{\bm{n}}th order Taylor expansion of a nn times differentiable function f ⁣:RP→RMf\colon\mathbb{R}^{P}\to\mathbb{R}^{M} around w∈RP{\bm{w}}\in\mathbb{R}^{P} by

f(w+v)\displaystyle f({\bm{w}}+{\bm{v}}) =f(w)+∂f(w)[v]+12∂2f(w)[v,v]+…\displaystyle=f({\bm{w}})+\partial f({\bm{w}})[{\bm{v}}]+\frac{1}{2}\partial^{2}f({\bm{w}})[{\bm{v}},{\bm{v}}]+\ldots
+1n!∂nf(w)[v,…,v⏟n times]+o(∥v∥2n).\displaystyle+\frac{1}{n!}\partial^{n}f({\bm{w}})[\underbrace{{\bm{v}},\ldots,{\bm{v}}}_{n\,\mathrm{times}}]+o(\|{\bm{v}}\|_{2}^{n}).

注意,利用变量替换 w′=w+v  ⟺  v=w′−w{\bm{w}}^{\prime}={\bm{w}}+{\bm{v}}\iff{\bm{v}}={\bm{w}}^{\prime}-{\bm{w}},通常可以方便地把 f(w′)f({\bm{w}}^{\prime}) 在 w{\bm{w}} 附近的第 nn 阶泰勒展开写成Note that, using the change of variable w′=w+v  ⟺  v=w′−w{\bm{w}}^{\prime}={\bm{w}}+{\bm{v}}\iff{\bm{v}}={\bm{w}}^{\prime}-{\bm{w}}, it is often convenient to write the nnth Taylor expansion of f(w′)f({\bm{w}}^{\prime}) around w{\bm{w}} as

f(w′)=f(w)+∑j=1n1j!∂jf(w)[w′−w,…,w′−w⏟j times]+o(∥w′−w∥2n).f({\bm{w}}^{\prime})=f({\bm{w}})+\sum_{j=1}^{n}\frac{1}{j!}\partial^{j}f({\bm{w}})[\underbrace{{\bm{w}}^{\prime}-{\bm{w}},\ldots,{\bm{w}}^{\prime}-{\bm{w}}}_{j\,\mathrm{times}}]+o(\|{\bm{w}}^{\prime}-{\bm{w}}\|_{2}^{n}).

泰勒展开在第 7 章中对于通过有限差分计算导数将很有用。Taylor expansions will prove useful in Chapter 7 for computing derivatives by finite differences.

2.6 微分几何2.6 Differential geometry

在本章中,我们逐步把导数的概念从实数推广到向量,以及推广到位于线性空间(又称向量空间)中的变量,这些空间既可以是有限维的,也可以是无穷维的。我们还可以通过引入一种局部线性性的概念来进一步推广这些概念。微分几何中的光滑流形对之进行了形式化,其术语常被自动微分文献和软件采用。本节简要概述光滑流形(简称流形)上的导数,完整介绍请参见 [32]。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 [32] for a complete introduction.

2.6.1 流形上的可微性Differentiability on manifolds

本质上,流形是一个可以被欧几里得空间局部近似的集合。最常见的例子是像地球这样的球面。从月球上看,地球并不是一个平面,但在人类尺度上局部来看,它可以被视为一个平坦的表面。欧几里得空间也是流形的平凡例子。例 2.6.7 给出了球面作为流形的形式化刻画。目前,我们可以把“流形”视为包含在某个环境欧几里得空间中的某个集合(例如球面);但请注意,流形一般也可以在不包含于欧几里得空间的情况下定义[32,第 8 章]。流形上的可微性直接继承自环境欧几里得空间中的可微性概念。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.6.7. 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 [32, Chapter 8]. Differentiability in manifolds is simply inherited from the notion of differentiability in the ambient Euclidean space.

定义 2.6.1(受限函数的可微性)。Definition 2.6.1 (Differentiability of restricted functions).
设 M\mathcal{M} 与 N\mathcal{N} 为流形。设 E\mathcal{E} 与 F\mathcal{F} 为欧几里得空间,则由 M⊆E\mathcal{M}\subseteq\mathcal{E} 到 N⊆F\mathcal{N}\subseteq\mathcal{F} 的函数 f:M→Nf:\mathcal{M}\rightarrow\mathcal{N},若 ff 是某个可微函数 fˉ:E→F\bar{f}:\mathcal{E}\rightarrow\mathcal{F} 的限制,使 ff 在 M\mathcal{M} 上与 fˉ\bar{f} 一致,则是可微的。Let M\mathcal{M} and N\mathcal{N} be manifolds. A function f:M→Nf:\mathcal{M}\rightarrow\mathcal{N} defined from M⊆E\mathcal{M}\subseteq\mathcal{E} to N⊆F\mathcal{N}\subseteq\mathcal{F}, with E\mathcal{E} and F\mathcal{F} Euclidean spaces, is differentiable if ff is the restriction of a differentiable function fˉ:E→F\bar{f}:\mathcal{E}\rightarrow\mathcal{F}, so that ff coincides with fˉ\bar{f} on M\mathcal{M}.

我们的目标是为定义在流形上的函数形式化地给出方向导数与梯度。这一形式化将引出切空间与余切空间的定义,并引出 JVP 与 VJP 算子分别作为前推算子与拉回算子的相应推广。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

为推广函数 ff 的方向导数这一概念,我们希望保留的性质是链式法则。我们不再从 ff 在给定点沿某一方向的变化入手,而是从 ff 沿曲线的变化入手。也就是说,对于像 RP\mathbb{R}^{P} 中的球面 SP\mathcal{S}^{P} 这样的流形,可以考察在时刻 00 经过 w∈SP{\bm{w}}\in\mathcal{S}^{P} 的曲线 α:R→SP\alpha:\mathbb{R}\rightarrow\mathcal{S}^{P},即 α(0)=w\alpha(0)={\bm{w}}。对于像 α\alpha 这样的单输入函数,为简便起见我们记作 α′(0):=(α1′(0),…,αP′(0))\alpha^{\prime}(0)\coloneqq(\alpha_{1}^{\prime}(0),\ldots,\alpha_{P}^{\prime}(0))。函数 ff 的方向导数通常须用来定义 f∘αf\circ\alpha 在 00 处的导数,使得 (f∘α)′(0)=∂f(w)[α′(0)](f\circ\alpha)^{\prime}(0)=\partial f({\bm{w}})[\alpha^{\prime}(0)]。在球面的情形中,如图 2.8 所示,经过点 w{\bm{w}} 的曲线 α\alpha 的导数 α′(0)\alpha^{\prime}(0) 总在 w{\bm{w}} 处与球面相切。于是,球面在 w{\bm{w}} 处的切平面便涵盖了传入我们正在构建的 JVP 的所有可能的相关向量。因此,为定义流形上函数 ff 的方向导数,我们将讨论范围限定在定义于切空间 TwM\mathcal{T}_{\bm{w}}\mathcal{M} 上的算子;下文中该定义已按我们的需要作了简化。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\mathcal{S}^{P} in RP\mathbb{R}^{P}, we can look at curves α:R→SP\alpha:\mathbb{R}\rightarrow\mathcal{S}^{P} passing through w∈SP{\bm{w}}\in\mathcal{S}^{P} at time 00, that is, α(0)=w\alpha(0)={\bm{w}}. For single-input functions like α\alpha, we denote for simplicity α′(0):=(α1′(0),…,αP′(0))\alpha^{\prime}(0)\coloneqq(\alpha_{1}^{\prime}(0),\ldots,\alpha_{P}^{\prime}(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)^{\prime}(0)=\partial f({\bm{w}})[\alpha^{\prime}(0)]. In the case of the sphere, as illustrated in Fig. 2.8, the derivative α′(0)\alpha^{\prime}(0) of a curve α\alpha passing through a point w{\bm{w}} is always tangent to the sphere at w{\bm{w}}. The tangent plane to the sphere at w{\bm{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\mathcal{T}_{\bm{w}}\mathcal{M}, whose definition below is simplified for our purposes.

定义 2.6.2(切空间)。Definition 2.6.2 (Tangent space).
流形 M\mathcal{M} 在 w∈M{\bm{w}}\in\mathcal{M} 处的切空间定义为The tangent space of a manifold M\mathcal{M} at w∈M{\bm{w}}\in\mathcal{M} is defined as TwM:={v=α′(0) for any α:R→M differentiable s.t. α(0)=w}.\mathcal{T}_{\bm{w}}\mathcal{M}\coloneqq\{{\bm{v}}=\alpha^{\prime}(0)\ \text{for any}\ \alpha:\mathbb{R}\rightarrow\mathcal{M}\ \text{differentiable s.t.}\ \alpha(0)={\bm{w}}\}.

就图 2.8 中的球面而言,其切空间是一个平面,也就是一个欧几里得空间。这一性质具有普遍性:切空间都是欧几里得空间,因而我们可以把方向导数定义为线性映射。若 ff 可微,并将流形 M\mathcal{M} 映射到流形 N\mathcal{N},则 f∘αf\circ\alpha 便是 N\mathcal{N} 中的一条可微曲线。因此,(f∘α)′(0)(f\circ\alpha)^{\prime}(0) 是经过 f(w)f({\bm{w}}) 的曲线在 00 处的导数,且与 N\mathcal{N} 在 f(w)f({\bm{w}}) 处相切。于是,f:M→Nf:\mathcal{M}\rightarrow\mathcal{N} 在 w{\bm{w}} 处的方向导数可定义为从 M\mathcal{M} 在 w{\bm{w}} 处的切空间 TwM\mathcal{T}_{\bm{w}}\mathcal{M} 映入 N\mathcal{N} 在 f(w)f({\bm{w}}) 处的切空间 Tf(w)N\mathcal{T}_{f({\bm{w}})}\mathcal{N} 的函数。概而言之,我们考察 ff 与任意曲线 α\alpha 的组合如何将 α\alpha 的导数前推为 f∘αf\circ\alpha 的导数,由此构造出方向导数(JVP)。这样得到的 JVP 在微分几何中称为前推算子。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\mathcal{M} to a manifold N\mathcal{N}, then f∘αf\circ\alpha is a differentiable curve in N\mathcal{N}. Therefore, (f∘α)′(0)(f\circ\alpha)^{\prime}(0) is the derivative of a curve passing through f(w)f({\bm{w}}) at 00 and is tangent to N\mathcal{N} at f(w)f({\bm{w}}). Hence, the directional derivative of f:M→Nf:\mathcal{M}\rightarrow\mathcal{N} at w{\bm{w}} can be defined as a function from the tangent space TwM\mathcal{T}_{\bm{w}}\mathcal{M} of M\mathcal{M} at w{\bm{w}} onto the tangent space Tf(w)N\mathcal{T}_{f({\bm{w}})}\mathcal{N} of N\mathcal{N} at f(w)f({\bm{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.

定义 2.6.3(前推算子)。Definition 2.6.3 (Pushforward operator).
给定两个流形 M\mathcal{M} 与 N\mathcal{N},可微函数 f:M→Nf:\mathcal{M}\rightarrow\mathcal{N} 在 w∈M{\bm{w}}\in\mathcal{M} 处的前推算子,是指由下式定义的线性映射 ∂f(w):TwM→Tf(w)N\partial f({\bm{w}}):\mathcal{T}_{\bm{w}}\mathcal{M}\rightarrow\mathcal{T}_{f({\bm{w}})}\mathcal{N}:Given two manifolds M\mathcal{M} and N\mathcal{N}, the pushforward operator of a differentiable function f:M→Nf:\mathcal{M}\rightarrow\mathcal{N} at w∈M{\bm{w}}\in\mathcal{M} is the linear map ∂f(w):TwM→Tf(w)N\partial f({\bm{w}}):\mathcal{T}_{\bm{w}}\mathcal{M}\rightarrow\mathcal{T}_{f({\bm{w}})}\mathcal{N} defined by ∂f(w)[v]:=(f∘α)′(0),\partial f({\bm{w}})[{\bm{v}}]\coloneqq(f\circ\alpha)^{\prime}(0), 其中 v∈TwM{\bm{v}}\in\mathcal{T}_{\bm{w}}\mathcal{M} 为任意满足 v=α′(0){\bm{v}}=\alpha^{\prime}(0) 的向量,α:R→M\alpha:\mathbb{R}\rightarrow\mathcal{M} 是在 00 处经过 w{\bm{w}} 的可微曲线,即 α(0)=w\alpha(0)={\bm{w}}。for any v∈TwM{\bm{v}}\in\mathcal{T}_{\bm{w}}\mathcal{M} such that v=α′(0){\bm{v}}=\alpha^{\prime}(0), where α:R→M\alpha:\mathbb{R}\rightarrow\mathcal{M} is a differentiable curve passing through w{\bm{w}} at 00, i.e., α(0)=w\alpha(0)={\bm{w}}.
图 2.8:由球面 M\mathcal{M} 映到球面 N\mathcal{N} 的可微函数 ff 定义了一个前推算子;该算子把切空间 TwM\mathcal{T}_{{\bm{w}}}\mathcal{M} 中的切向量(球面上过 w{\bm{w}} 的函数的导数)映射为 N\mathcal{N} 在 f(w)f({\bm{w}}) 处的切空间 Tf(w)N\mathcal{T}_{f({\bm{w}})}\mathcal{N} 中的切向量。Figure 2.8: A differentiable function ff defined from a sphere M\mathcal{M} to a sphere N\mathcal{N} defines a push-forward operator that maps tangent vectors (derivatives of functions on the sphere passing through w{\bm{w}}) in the tangent space TwM\mathcal{T}_{{\bm{w}}}\mathcal{M} to tangent vectors of N\mathcal{N} at f(w)f({\bm{w}}) in the tangent space Tf(w)N\mathcal{T}_{f({\bm{w}})}\mathcal{N}.

2.6.3 余切空间与拉回算子Cotangent spaces and pullback operators

为推广 JVP,我们将 f:M→Nf:\mathcal{M}\rightarrow\mathcal{N} 与任意在流形上取值的单输入函数 α:R→M\alpha:\mathbb{R}\rightarrow\mathcal{M} 复合。于是,任何这样的 α\alpha 的导数随后从 TwM\mathcal{T}_{\bm{w}}\mathcal{M} 被前推到 Tf(w)N\mathcal{T}_{f({\bm{w}})}\mathcal{N},这是通过 ff 的作用实现的。为定义 VJP,我们采用对称的方法。我们考虑所有单输出可微函数 β:N→R\beta:\mathcal{N}\rightarrow\mathbb{R},它们定义在 y∈N{\bm{y}}\in\mathcal{N} 上,并且具有如下性质:y=f(w){\bm{y}}=f({\bm{w}}) 对某个 w∈M{\bm{w}}\in\mathcal{M} 成立。然后,我们想要在将 β\beta 用 ff 预复合时,拉回其导数。因此,VJP 作用的空间是任意 β:N→R\beta:\mathcal{N}\rightarrow\mathbb{R} 在 y{\bm{y}} 处的方向导数构成的空间,这定义了余切空间。To generalize the JVP, we composed f:M→Nf:\mathcal{M}\rightarrow\mathcal{N} with any single-input function α:R→M\alpha:\mathbb{R}\rightarrow\mathcal{M} giving values on the manifold. The derivative of any such α\alpha is then pushed forward from TwM\mathcal{T}_{\bm{w}}\mathcal{M} to Tf(w)N\mathcal{T}_{f({\bm{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:\mathcal{N}\rightarrow\mathbb{R} defined on y∈N{\bm{y}}\in\mathcal{N} with y=f(w){\bm{y}}=f({\bm{w}}) for some w∈M{\bm{w}}\in\mathcal{M}. 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:\mathcal{N}\rightarrow\mathbb{R} at y{\bm{y}}, defining the cotangent space.

定义 2.6.4(余切空间)。Definition 2.6.4 (Cotangent space).
流形 N\mathcal{N} 在 y∈N{\bm{y}}\in\mathcal{N} 处的余切空间定义为The cotangent space of a manifold N\mathcal{N} at y∈N{\bm{y}}\in\mathcal{N} is defined as Ty∗N\displaystyle\mathcal{T}_{\bm{y}}^{*}\mathcal{N} ={u=∂β(y) for any β:N→R differentiable}\displaystyle=\{u=\partial\beta({\bm{y}})\ \text{for any}\ \beta:\mathcal{N}\rightarrow\mathbb{R}\ \text{differentiable}\} ={u:TyN→R for any linear map u},\displaystyle=\{u:\mathcal{T}_{\bm{y}}\mathcal{N}\rightarrow\mathbb{R}\ \text{for any linear map}\ u\},

注意,余切空间的元素是线性映射,而非向量。这一区分之所以重要,是因为在测度论中,拉回算子正是被定义为作用于函数的算子。从线性代数的角度看,余切空间恰是 TyN\mathcal{T}_{\bm{y}}\mathcal{N} 的对偶空间,即由 TyN\mathcal{T}_{\bm{y}}\mathcal{N} 到 R\mathbb{R} 的线性映射所构成的集合,其中的元素称为线性泛函。由于 TyN\mathcal{T}_{\bm{y}}\mathcal{N} 是欧几里得空间,其对偶空间 Ty∗N\mathcal{T}_{\bm{y}}^{*}\mathcal{N} 也是欧几里得空间。于是,拉回算子便定义为这样的算子:给定 β\beta 在 f(w)f({\bm{w}}) 处的方向导数,它便能给出 β∘f\beta\circ f 的方向导数。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\mathcal{T}_{\bm{y}}\mathcal{N}, that is, the set of linear maps from TyN\mathcal{T}_{\bm{y}}\mathcal{N} to R\mathbb{R}, called linear forms. As TyN\mathcal{T}_{\bm{y}}\mathcal{N} is a Euclidean space, its dual space Ty∗N\mathcal{T}_{\bm{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({\bm{w}}).

定义 2.6.5(拉回算子)。Definition 2.6.5 (Pullback operator).
给定两个流形 M\mathcal{M} 与 N\mathcal{N},可微函数 f:M→Nf:\mathcal{M}\rightarrow\mathcal{N} 在 w∈M{\bm{w}}\in\mathcal{M} 处的拉回算子,是指由下式定义的线性映射 ∂f(w)⋆:Tf(w)∗N→Tw∗M\partial f({\bm{w}})^{\star}:\mathcal{T}_{f({\bm{w}})}^{*}\mathcal{N}\rightarrow\mathcal{T}_{{\bm{w}}}^{*}\mathcal{M}:Given two manifolds M\mathcal{M} and N\mathcal{N}, the pullback operator of a differentiable function f:M→Nf:\mathcal{M}\rightarrow\mathcal{N} at w∈M{\bm{w}}\in\mathcal{M} is the linear map ∂f(w)⋆:Tf(w)∗N→Tw∗M\partial f({\bm{w}})^{\star}:\mathcal{T}_{f({\bm{w}})}^{*}\mathcal{N}\rightarrow\mathcal{T}_{{\bm{w}}}^{*}\mathcal{M} defined by ∂f(w)⋆u:=∂(β∘f)(w),\partial f({\bm{w}})^{\star}u\coloneqq\partial(\beta\circ f)({\bm{w}}), 其中 u∈Tf(w)N∗u\in\mathcal{T}_{f({\bm{w}})}\mathcal{N}^{*} 为任意满足 ∂β(f(w))=u\partial\beta(f({\bm{w}}))=u 的向量,β:N→R\beta:\mathcal{N}\rightarrow\mathbb{R} 为可微函数。for any u∈Tf(w)N∗u\in\mathcal{T}_{f({\bm{w}})}\mathcal{N}^{*} such that ∂β(f(w))=u\partial\beta(f({\bm{w}}))=u, for a differentiable function β:N→R\beta:\mathcal{N}\rightarrow\mathbb{R}.

与前推算子作用于向量不同,拉回算子作用于线性泛函。因此,∂f(w)⋆\partial f({\bm{w}})^{\star} 与 ∂f(w)∗\partial f({\bm{w}})^{*}——即 ∂f(w)\partial f({\bm{w}}) 的伴随算子——在记号上略有差异。为恰当地定义伴随算子 ∂f(w)∗\partial f({\bm{w}})^{*},我们需要引入内积的概念。由于切空间都是欧几里得空间,我们可以对每个 TwM\mathcal{T}_{\bm{w}}\mathcal{M} 和 w∈M{\bm{w}}\in\mathcal{M} 定义内积 ⟨⋅,⋅⟩w\langle\cdot,\cdot\rangle_{\bm{w}},从而使 M\mathcal{M} 成为黎曼流形。有了这些内积,余切空间便可与切空间等同起来,我们也就可以定义梯度了。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({\bm{w}})^{\star} and ∂f(w)∗\partial f({\bm{w}})^{*}, the adjoint operator of ∂f(w)\partial f({\bm{w}}). To properly define the adjoint operator ∂f(w)∗\partial f({\bm{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_{\bm{w}} for each TwM\mathcal{T}_{\bm{w}}\mathcal{M} and w∈M{\bm{w}}\in\mathcal{M}, making M\mathcal{M} a Riemannian manifold. Equipped with these inner products, the cotangent space can be identified with the tangent space, and we can define gradients.

定义 2.6.6(黎曼流形中的梯度)。Definition 2.6.6 (Gradients in Riemannian manifolds).
设 M\mathcal{M} 为赋以内积 ⟨⋅,⋅⟩w\langle\cdot,\cdot\rangle_{\bm{w}} 的黎曼流形。对任意余切向量 u∈Tw∗Mu\in\mathcal{T}_{\bm{w}}^{*}\mathcal{M}(其中 w∈M{\bm{w}}\in\mathcal{M}),存在唯一的切向量 u∈TwM{\bm{u}}\in\mathcal{T}_{\bm{w}}\mathcal{M},使得Let M\mathcal{M} be a Riemannian manifold equipped with inner products ⟨⋅,⋅⟩w\langle\cdot,\cdot\rangle_{\bm{w}}. For any cotangent vector u∈Tw∗Mu\in\mathcal{T}_{\bm{w}}^{*}\mathcal{M}, with w∈M{\bm{w}}\in\mathcal{M}, there exists a unique tangent vector u∈TwM{\bm{u}}\in\mathcal{T}_{\bm{w}}\mathcal{M} such that ∀v∈TwM, u[v]=⟨u,v⟩w.\forall{\bm{v}}\in\mathcal{T}_{\bm{w}}\mathcal{M},\ u[{\bm{v}}]=\langle{\bm{u}},{\bm{v}}\rangle_{\bm{w}}. 特别地,对任意可微函数 f:M→Rf:\mathcal{M}\rightarrow\mathbb{R},可将 ff 的梯度定义为满足下式的唯一切向量 ∇f(w)∈TwM\nabla f({\bm{w}})\in\mathcal{T}_{\bm{w}}\mathcal{M}:In particular for any differentiable function f:M→Rf:\mathcal{M}\rightarrow\mathbb{R}, we can define the gradient of ff as the unique tangent vector ∇f(w)∈TwM\nabla f({\bm{w}})\in\mathcal{T}_{\bm{w}}\mathcal{M} such that ∀v∈TwM, ∂f(w)[v]=⟨∇f(w),v⟩.\forall{\bm{v}}\in\mathcal{T}_{\bm{w}}\mathcal{M},\ \partial f({\bm{w}})[{\bm{v}}]=\langle\nabla f({\bm{w}}),{\bm{v}}\rangle.

因此,与其对方向导数作拉回,不如对梯度作拉回。相应的算子自然就是前推算子的伴随 ∂f(w)∗\partial f({\bm{w}})^{*}。具体而言,给定两个黎曼流形 M\mathcal{M} 与 N\mathcal{N},以及一个可微函数 f:M→Nf:\mathcal{M}\rightarrow\mathcal{N},我们有Therefore, rather than pulling back directional derivatives, we can pull back gradients. The corresponding operator is then naturally the adjoint ∂f(w)∗\partial f({\bm{w}})^{*} of the pushforward operator. Namely, given two Riemannian manifolds M\mathcal{M} and N\mathcal{N}, 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({\bm{w}})^{\star}u\right)[{\bm{v}}]=\langle\partial f({\bm{w}})^{*}[{\bm{u}}],{\bm{v}}\rangle\ \text{for any}\ {\bm{v}}\in\mathcal{T}_{\bm{w}}\mathcal{M}

其中 u=⟨⋅,u⟩∈Tf(w)∗Nu=\langle\cdot,{\bm{u}}\rangle\in\mathcal{T}_{f({\bm{w}})}^{*}\mathcal{N} 由 u∈Tf(w)N{\bm{u}}\in\mathcal{T}_{f({\bm{w}})}\mathcal{N} 表示。for u=⟨⋅,u⟩∈Tf(w)∗Nu=\langle\cdot,{\bm{u}}\rangle\in\mathcal{T}_{f({\bm{w}})}^{*}\mathcal{N} represented by u∈Tf(w)N{\bm{u}}\in\mathcal{T}_{f({\bm{w}})}\mathcal{N}.

函数Function ff M→N\mathcal{M}\rightarrow\mathcal{N}
前推Push-forward ∂f(w)\partial f({\bm{w}}) TwM→Tf(w)N\mathcal{T}_{\bm{w}}\mathcal{M}\rightarrow\mathcal{T}_{f({\bm{w}})}\mathcal{N}
拉回Pullback ∂f(w)⋆\partial f({\bm{w}})^{\star} Tf(w)∗N→Tw∗M\mathcal{T}_{f({\bm{w}})}^{*}\mathcal{N}\rightarrow\mathcal{T}_{\bm{w}}^{*}\mathcal{M}
前推算子的伴随Adjoint of pushforward ∂f(w)∗\partial f({\bm{w}})^{*} Tf(w)N→TwM\mathcal{T}_{f({\bm{w}})}\mathcal{N}\rightarrow\mathcal{T}_{\bm{w}}\mathcal{M}
表 2.1:对于定义在流形 M\mathcal{M} 上、取值于流形 N\mathcal{N} 的可微函数 ff,JVP 通过前推 ∂f(w)\partial f({\bm{w}}) 的概念得到推广。前推的对应物是作用于切空间中线性泛函的拉回运算 ∂f(w)⋆\partial f({\bm{w}})^{\star}。对于黎曼流形,由于任一线性泛函都可由一个向量表示,拉回运算可等同于前推算子的伴随算子 ∂f(w)∗\partial f({\bm{w}})^{*}。Table 2.1: For a differentiable function ff defined from a manifold M\mathcal{M} onto a manifold N\mathcal{N}, the JVP is generalized with the notion of pushforward ∂f(w)\partial f({\bm{w}}). The counterpart of the pushforward is the pullback operation ∂f(w)⋆\partial f({\bm{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({\bm{w}})^{*} of the pushforward operator as any linear form is represented by a vector.
例 2.6.7(球面作为流形)。Example 2.6.7 (The sphere as a manifold).
RP\mathbb{R}^{P} 中的球面 SP\mathcal{S}^{P} 定义为满足 c(w):=⟨w,w⟩−1=0c({\bm{w}})\coloneqq\langle{\bm{w}},{\bm{w}}\rangle-1=0 的点 w∈RP{\bm{w}}\in\mathbb{R}^{P} 所构成的集合,其雅可比—向量乘积(JVP)为 ∂c(w)[v]=2⟨w,v⟩\partial c({\bm{w}})[{\bm{v}}]=2\langle{\bm{w}},{\bm{v}}\rangle。The sphere SP\mathcal{S}^{P} in RP\mathbb{R}^{P} is defined as the set of points w∈RP{\bm{w}}\in\mathbb{R}^{P}, satisfying c(w):=⟨w,w⟩−1=0c({\bm{w}})\coloneqq\langle{\bm{w}},{\bm{w}}\rangle-1=0, with JVP ∂c(w)[v]=2⟨w,v⟩\partial c({\bm{w}})[{\bm{v}}]=2\langle{\bm{w}},{\bm{v}}\rangle. 我们首先来看,为何可以局部地将球面 SP\mathcal{S}^{P} 视为一个 P−1P-1 维空间。取半球 H={w∈SP:wP≥0}\mathcal{H}=\{{\bm{w}}\in\mathcal{S}^{P}:w_{P}\geq 0\},它完全可以由每个点的前 P−1P-1 个坐标描述,而最后一个坐标直接由 wP=1−∑i=1P−1wi2w_{P}=\sqrt{1-\sum_{i=1}^{P-1}w_{i}^{2}} 给出。形式上,对 P−1P-1 个坐标构成的任意向量 v=(v1,…,vP−1){\bm{v}}=(v_{1},\ldots,v_{P-1}),只要满足 ∑i=1P−1vi2≤1\sum_{i=1}^{P-1}v_{i}^{2}\leq 1,便可定义 ψ1(v):=1−∑i=1P−1vi2\psi_{1}({\bm{v}})\coloneqq\sqrt{1-\sum_{i=1}^{P-1}v_{i}^{2}} 与 ψ(v):=(v1,…,vP−1,ψ1(v)){\bm{\psi}}({\bm{v}})\coloneqq(v_{1},\ldots,v_{P-1},\psi_{1}({\bm{v}}))。于是有 ⟨ψ(v),ψ(v)⟩=1\langle{\bm{\psi}}({\bm{v}}),{\bm{\psi}}({\bm{v}})\rangle=1,即 c(ψ(v))=0c({\bm{\psi}}({\bm{v}}))=0。由此,我们可以将半球 H\mathcal{H} 描述为 P−1P-1 维单位球 B={v=(v1,…,vP−1):∑i=1P−1vi2≤1}B=\{{\bm{v}}=(v_{1},\ldots,v_{P-1}):\sum_{i=1}^{P-1}v_{i}^{2}\leq 1\} 在映射 ψ\psi 下的像,即 H=ψ(B)\mathcal{H}=\psi(B)。特别地,半球上的任意开集都可描述为 RP−1\mathbb{R}^{P-1} 中某个开集在映射下的像。这一论证适用于所有半球,只是所用的映射不同,因此该视角具有局部性。Let us first understand why we may locally see the sphere SP\mathcal{S}^{P} as a space of dimension P−1P-1. Take the hemisphere H={w∈SP:wP≥0}\mathcal{H}=\{{\bm{w}}\in\mathcal{S}^{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){\bm{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}({\bm{v}})\coloneqq\sqrt{1-\sum_{i=1}^{P-1}v_{i}^{2}} and ψ(v):=(v1,…,vP−1,ψ1(v)){\bm{\psi}}({\bm{v}})\coloneqq(v_{1},\ldots,v_{P-1},\psi_{1}({\bm{v}})). We have then ⟨ψ(v),ψ(v)⟩=1\langle{\bm{\psi}}({\bm{v}}),{\bm{\psi}}({\bm{v}})\rangle=1, that is c(ψ(v))=0c({\bm{\psi}}({\bm{v}}))=0. We can then describe the hemisphere H\mathcal{H} as the image of the unit ball B={v=(v1,…,vP−1):∑i=1P−1vi2≤1}B=\{{\bm{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)\mathcal{H}=\psi(B). In particular, any open set on the hemisphere can be described as the mapping of an open set in RP−1\mathbb{R}^{P-1}. The reasoning applies to all hemispheres with different mappings, hence the local aspect of this viewpoint. 切空间可以自然地借助约束函数 cc 来刻画。也就是说,满足 α(0)=w\alpha(0)={\bm{w}} 的曲线 α:R→SP\alpha:\mathbb{R}\rightarrow\mathcal{S}^{P} 对任意 δ∈R\delta\in\mathbb{R} 都满足 c(α(δ))=0c(\alpha(\delta))={\mathbf{0}}。因此,对该隐式方程求导,可得The tangent space can be naturally characterized in terms of the constraining function cc. Namely, the curve α:R→SP\alpha:\mathbb{R}\rightarrow\mathcal{S}^{P} such that α(0)=w\alpha(0)={\bm{w}} satisfies for any δ∈R\delta\in\mathbb{R}, c(α(δ))=0c(\alpha(\delta))={\mathbf{0}}. Hence, differentiating the implicit equation, we have (c∘α)′(0)=∂c(w)[α′(0)].(c\circ\alpha)^{\prime}(0)=\partial c({\bm{w}})[\alpha^{\prime}(0)]. 即 α′(0)\alpha^{\prime}(0) 位于 ∂c(w)\partial c({\bm{w}}) 的零空间中,记作That is, α′(0)\alpha^{\prime}(0) is in the null space of ∂c(w)\partial c({\bm{w}}), denoted Null(∂c(w)):={v∈RP:∂c(w)[v]=0}.\mathrm{Null}(\partial c({\bm{w}}))\coloneqq\{{\bm{v}}\in\mathbb{R}^{P}:\partial c({\bm{w}})[{\bm{v}}]=0\}. SP\mathcal{S}^{P} 在 w{\bm{w}} 处的切空间即为The tangent space of SP\mathcal{S}^{P} at w{\bm{w}} is then TwM\displaystyle\mathcal{T}_{\bm{w}}\mathcal{M} =Null(2⟨w,⋅⟩)\displaystyle=\mathrm{Null}(2\langle{\bm{w}},\cdot\rangle) ={v∈RP:⟨w,v⟩=0}\displaystyle=\{{\bm{v}}\in\mathbb{R}^{P}:\langle{\bm{w}},{\bm{v}}\rangle=0\} 由此自然地得到,该切空间是一个 P−1P-1 维欧几里得空间,定义为与 w{\bm{w}} 正交的点所构成的集合。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{\bm{w}}.

2.7 广义导数2.7 Generalized derivatives

本书主要关注可微函数,但刻画不可微函数同样重要。这里我们区分两种情形:连续函数与非连续函数。对于前者,存在方向导数、梯度与雅可比矩阵等概念的推广,下文将逐一介绍。对于非连续函数,即便导数几乎处处存在,它们也可能不提供有用信息。例如,控制流(第 5 章)中遇到的分段常值函数几乎处处可微,但导数几乎处处为零。在此类情形下,可以定义替代函数以确保程序的可微性(第四部分)。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 定理Rademacher’s theorem

我们首先回顾(局部)Lipschitz 连续函数的定义。We first recall the definition of (locally) Lipschitz continuous function.

定义 2.7.1((局部)Lipschitz 连续函数)。Definition 2.7.1 ((Locally) Lipschitz continuous function).
函数 f:E→Ff:\mathcal{E}\rightarrow\mathcal{F} 是Lipschitz 连续的,若存在 C≥0C\geq 0,使得对任意 x,y∈E{\bm{x}},{\bm{y}}\in\mathcal{E},A function f:E→Ff:\mathcal{E}\rightarrow\mathcal{F}, is Lipschitz continuous if there exists C≥0C\geq 0 such that for any x,y∈E{\bm{x}},{\bm{y}}\in\mathcal{E}, ∥f(x)−f(y)∥≤C∥x−y∥.\|f({\bm{x}})-f({\bm{y}})\|\leq C\|{\bm{x}}-{\bm{y}}\|. 函数 f:E→Ff:\mathcal{E}\rightarrow\mathcal{F} 是局部Lipschitz 连续的,若对任意 x∈E{\bm{x}}\in\mathcal{E},都存在 x{\bm{x}} 的一个邻域 U\mathcal{U},使得 ff 限制在 U\mathcal{U} 上是Lipschitz 连续的。A function f:E→Ff:\mathcal{E}\rightarrow\mathcal{F} is locally Lipschitz continuous if for any x∈E{\bm{x}}\in\mathcal{E}, there exists a neighborhood U\mathcal{U} of x{\bm{x}} such that ff restricted to U\mathcal{U} is Lipschitz continuous.

Rademacher 定理 [163] 随后保证 ff 几乎处处可微。Rademacher’s theorem [163] then ensures that ff is differentiable almost everywhere.

命题 2.7.2(Rademacher 定理)。Proposition 2.7.2 (Rademacher’s theorem).
设 E\mathcal{E} 与 F\mathcal{F} 为欧几里得空间。若 f:E→Ff:\mathcal{E}\rightarrow\mathcal{F} 是局部Lipschitz 连续的,则 ff 几乎处处可微,也就是说,E\mathcal{E} 中使 ff 不可微的点集具有(勒贝格)测度零。Let E\mathcal{E} and F\mathcal{F} denote Euclidean spaces. If f:E→Ff:\mathcal{E}\rightarrow\mathcal{F} is locally Lipschitz-continuous, then ff is almost everywhere differentiable, that is, the set of points in E\mathcal{E} at which ff is not differentiable is of (Lebesgue) measure zero.

标准证明亦可参见 [139]。See also [139] for a standard proof.

2.7.2 Clarke 导数Clarke derivatives

Rademacher 定理暗示,方向导数、梯度与雅可比矩阵的定义可以推广到局部 Lipschitz 连续函数。这正是 [46] 在其开创性工作中所做的,该工作奠定了非光滑分析的基础。第一个基本构件是广义方向导数的概念。Rademacher’s theorem hints that the definitions of directional derivatives, gradients and Jacobians may be generalized to locally Lipschitz continuous functions. This is what [46] did in his seminal work, which laid the foundation of nonsmooth analysis. The first building block is a notion of generalized directional derivative.

定义 2.7.3(Clarke 广义方向导数)。Definition 2.7.3 (Clarke generalized directional derivative).
局部Lipschitz 连续函数 f:E→Rf:\mathcal{E}\rightarrow\mathbb{R} 在 w∈E{\bm{w}}\in\mathcal{E} 处沿方向 v∈E{\bm{v}}\in\mathcal{E} 的 Clarke 广义方向导数为The Clarke generalized directional derivative of a locally Lipschitz continuous function f:E→Rf:\mathcal{E}\rightarrow\mathbb{R} at w∈E{\bm{w}}\in\mathcal{E} in the direction v∈E{\bm{v}}\in\mathcal{E} is ∂Cf(w)[v]:=lim sup⁡u→wδ↘0f(u+δv)−f(u)δ,\partial_{C}f({\bm{w}})[{\bm{v}}]\coloneqq\limsup_{\begin{subarray}{c}{\bm{u}}\rightarrow{\bm{w}}\\ \delta\searrow 0\end{subarray}}\frac{f({\bm{u}}+\delta{\bm{v}})-f({\bm{u}})}{\delta}, 其中极限存在,δ↘0\delta\searrow 0 表示 δ\delta 以非负值趋近于 00,而上极限定义为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_{{\bm{x}}\rightarrow{\bm{a}}}f({\bm{x}})\coloneqq\lim_{\varepsilon\rightarrow 0}\sup\{f({\bm{x}}):{\bm{x}}\in B({\bm{a}},\varepsilon)\setminus\{{\bm{a}}\}\} 其中 B(a,ε):={x∈E:∥x−a∥≤ε}B({\bm{a}},\varepsilon)\coloneqq\{{\bm{x}}\in\mathcal{E}:\|{\bm{x}}-{\bm{a}}\|\leq\varepsilon\} 表示以 a{\bm{a}} 为中心、半径为 ε\varepsilon 的球。for B(a,ε):={x∈E:∥x−a∥≤ε}B({\bm{a}},\varepsilon)\coloneqq\{{\bm{x}}\in\mathcal{E}:\|{\bm{x}}-{\bm{a}}\|\leq\varepsilon\} the ball centered at a{\bm{a}} of radius ε\varepsilon.

与方向导数的通常定义相比,此定义有两点不同:(i) 我们考察的是该点邻域内函数的斜率,而非给定点处的斜率;(ii) 我们取上极限,而非通常的极限。第一点从 Rademacher 定理来看颇为自然:既然函数可微之处的变化能够被恰当刻画,我们便可把这些斜率的极限当作所关注点斜率的候选。第二点更具技术性,却也必不可少:它使我们得以将方向导数刻画为某些线性泛函的上确界 [45];而这些线性泛函反过来又定义了一组广义梯度 [45,第 2 章]。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 [45]. These linear forms in turn define a set of generalized gradients [45, Chapter 2].

定义 2.7.4(Clarke 广义梯度)。Definition 2.7.4 (Clarke generalized gradient).
局部Lipschitz 函数 f:E→Rf:\mathcal{E}\rightarrow\mathbb{R} 在 w∈E{\bm{w}}\in\mathcal{E} 处的 Clarke 广义梯度是一个点 g∈E{\bm{g}}\in\mathcal{E},使得 ∀v∈E\forall{\bm{v}}\in\mathcal{E}A Clarke generalized gradient of a locally Lipschitz function f:E→Rf:\mathcal{E}\rightarrow\mathbb{R} at w∈E{\bm{w}}\in\mathcal{E} is a point g∈E{\bm{g}}\in\mathcal{E} such that ∀v∈E\forall{\bm{v}}\in\mathcal{E} ∂Cf(w)[v]≥⟨g,v⟩.\partial_{C}f({\bm{w}})[{\bm{v}}]\geq\langle{\bm{g}},{\bm{v}}\rangle. Clarke 广义梯度的集合称为 ff 在 w{\bm{w}} 处的 Clarke 次微分。The set of Clarke generalized gradients is called the Clarke subdifferential of ff at w{\bm{w}}.

定义 2.7.3 和定义 2.7.4 适用于非欧几里得空间,例如巴拿赫空间或希尔伯特空间 [45]。在欧几里得空间中,借助 Rademacher 定理,Clarke 广义梯度可以得到更简洁的刻画 [45,定理 8.1]。也就是说,如下文所示,它们可定义为 ff 的梯度在一列收敛于 w{\bm{w}} 的序列 E∖Ω\mathcal{E}\setminus\Omega 上取值的极限的凸组合。Definition 2.7.3 and Definition 2.7.4 can be used in non-Euclidean spaces, such as Banach or Hilbert spaces [45]. In Euclidean spaces, the Clarke generalized gradients can be characterized more simply thanks to Rademacher’s theorem [45, 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∖Ω\mathcal{E}\setminus\Omega that converges to w{\bm{w}}.

命题 2.7.5(Clarke 广义梯度的刻画)。Proposition 2.7.5 (Characterization of Clarke generalized gradients).
设 f:E→Rf:\mathcal{E}\rightarrow\mathbb{R} 为局部Lipschitz 连续函数,并记 Ω\Omega 为 ff 不可微的点集(命题 2.7.2)。元素 g∈E{\bm{g}}\in\mathcal{E} 是 ff 在 w∈E{\bm{w}}\in\mathcal{E} 处的 Clarke 广义梯度,当且仅当Let f:E→Rf:\mathcal{E}\rightarrow\mathbb{R} be a locally Lipschitz continuous and denote Ω\Omega the set of points at which ff is not differentiable (Proposition 2.7.2). An element g∈E{\bm{g}}\in\mathcal{E} is a Clarke generalized gradient of ff at w∈E{\bm{w}}\in\mathcal{E} if and only if g\displaystyle{\bm{g}} ∈conv⁡({lim⁡n→+∞∇f(vn) ⁣:(vn)n=1+∞ s.t. vn∈E∖Ω, vn→n→+∞w}).\displaystyle\in\conv\left(\left\{\lim_{n\rightarrow+\infty}\nabla f({\bm{v}}_{n})\colon({\bm{v}}_{n})_{n=1}^{+\infty}\ \text{s.t.}\ {\bm{v}}_{n}\in\mathcal{E}\setminus\Omega,\ {\bm{v}}_{n}\underset{n\rightarrow+\infty}{\rightarrow}{\bm{w}}\right\}\right).

上文中,集合 S⊆ES\subseteq\mathcal{E} 的凸包——即由 SS 中元素的凸组合构成的集合——记作In the above, the convex hull of a set S⊆ES\subseteq\mathcal{E}, 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}{\bm{s}}_{1}+\ldots+\lambda_{m}{\bm{s}}_{m}\ \colon m\in\mathbb{N},\ \lambda_{i}\geq 0,\sum_{i=1}^{m}\lambda_{i}=1,{\bm{s}}_{i}\in S\}.

两个欧几里得空间之间的函数 f:E→Ff:\mathcal{E}\rightarrow\mathcal{F} 的雅可比矩阵也可类似地推广 [45,第 3.3 节]。The Jacobian of a function f:E→Ff:\mathcal{E}\rightarrow\mathcal{F} between two Euclidean spaces can be generalized similarly [45, Section 3.3].

定义 2.7.6(Clarke 广义雅可比矩阵)。Definition 2.7.6 (Clarke generalized Jacobian).
设 f:E→Ff:\mathcal{E}\rightarrow\mathcal{F} 为局部Lipschitz 连续函数,并记 Ω\Omega 为 ff 不可微的点集(命题 2.7.2)。ff 在 w∈E{\bm{w}}\in\mathcal{E} 处的 Clarke 广义雅可比矩阵是 J\mathbf{J} 中的一个元素,其中Let f:E→Ff:\mathcal{E}\rightarrow\mathcal{F} be locally Lipschitz continuous and denote Ω\Omega the set of points at which ff is not differentiable (Proposition 2.7.2). A Clarke generalized Jacobian of ff at w∈E{\bm{w}}\in\mathcal{E} 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}\bm{\partial}f({\bm{v}}_{n})\colon({\bm{v}}_{n})_{n=1}^{+\infty}\ \text{s.t.}\ {\bm{v}}_{n}\in\mathcal{E}\setminus\Omega,\ {\bm{v}}_{n}\underset{n\rightarrow+\infty}{\rightarrow}{\bm{w}}\right\}\right).

对于连续可微函数 f:E→Ff:\mathcal{E}\rightarrow\mathcal{F} 或 f:E→Rf:\mathcal{E}\rightarrow\mathbb{R} ,广义梯度唯一存在,并还原出通常的梯度 [45,命题 3.1,第 78 页]。链式法则也可推广至这些对象 [45]。近来,[30, 29] 通过定义保守梯度,进一步推广了 Clarke 梯度,从而为非光滑函数构造出自动微分方案。For a continuously differentiable function f:E→Ff:\mathcal{E}\rightarrow\mathcal{F} or f:E→Rf:\mathcal{E}\rightarrow\mathbb{R}, there is a unique generalized gradient, recovering the usual gradient [45, Proposition 3.1, page 78]. The chain rule can be generalized to these objects [45]. Recently, [30, 29] further generalized Clarke gradients through the definition of conservative gradients to define automatic differentiation schemes for nonsmooth functions.

2.8 小结2.8 Summary

  • •

    实值一元函数导数的通常定义,经由在 w∈RP{\bm{w}}\in\mathbb{R}^{P} 处沿方向 v∈RP{\bm{v}}\in\mathbb{R}^{P} 的方向导数 ∂f(w)[v]\partial f({\bm{w}})[{\bm{v}}] 这一概念,推广到了多元函数 f ⁣:RP→Rf\colon\mathbb{R}^{P}\to\mathbb{R} 。The usual definition of derivatives of real-valued univariate functions extends to multivariate functions f ⁣:RP→Rf\colon\mathbb{R}^{P}\to\mathbb{R} through the notion of directional derivative ∂f(w)[v]\partial f({\bm{w}})[{\bm{v}}] at w∈RP{\bm{w}}\in\mathbb{R}^{P} in the direction v∈RP{\bm{v}}\in\mathbb{R}^{P}.

  • •

    为了利用标准基 {e1,…,eP}\{{\bm{e}}_{1},\dots,{\bm{e}}_{P}\} 来表示 w=∑j=1Pwjej{\bm{w}}=\sum_{j=1}^{P}w_{j}{\bm{e}}_{j} ,可微函数的定义要求方向导数关于方向 v{\bm{v}} 具有线性性。To take advantage of the representation of w=∑j=1Pwjej{\bm{w}}=\sum_{j=1}^{P}w_{j}{\bm{e}}_{j} using the canonical bases {e1,…,eP}\{{\bm{e}}_{1},\dots,{\bm{e}}_{P}\}, the definition of differentiable functions requires the linearity of the directional derivative w.r.t. the direction v{\bm{v}}.

  • •

    这一要求引出了梯度 ∇f(w)∈RP\nabla f({\bm{w}})\in\mathbb{R}^{P} 的概念:它汇集了各偏导数,并进一步确定了在 w{\bm{w}} 处的最陡上升方向。This requirement gives rise to the notion of gradient ∇f(w)∈RP\nabla f({\bm{w}})\in\mathbb{R}^{P}, the vector that gathers the partial derivatives and further defines the steepest ascent direction at w{\bm{w}}.

  • •

    对于向量输入、向量输出的函数 f ⁣:RP→RMf\colon\mathbb{R}^{P}\to\mathbb{R}^{M},方向导数引出了雅可比矩阵 ∂f(w)∈RM×P\bm{\partial}f({\bm{w}})\in\mathbb{R}^{M\times P} 的定义,该矩阵汇集了所有偏导数(注意我们使用粗体 ∂\bm{\partial})。于是,链式法则即为雅可比矩阵的乘积。For vector-input vector-output functions f ⁣:RP→RMf\colon\mathbb{R}^{P}\to\mathbb{R}^{M}, the directional derivative leads to the definition of Jacobian matrix ∂f(w)∈RM×P\bm{\partial}f({\bm{w}})\in\mathbb{R}^{M\times P}, the matrix which gathers all partial derivatives (notice that we use bold ∂\bm{\partial}). The chain rule is then the product of Jacobian matrices.

  • •

    这些概念可推广到一般的欧几里得空间,例如矩阵空间或张量空间。对于形如 f ⁣:E→Rf\colon\mathcal{E}\to\mathbb{R} 的函数,其梯度为 ∇f(w)∈E\nabla f({\bm{w}})\in\mathcal{E}。更一般地,对于形如 f ⁣:E→Ff\colon\mathcal{E}\to\mathcal{F} 的函数,雅可比矩阵 ∂f(w)\partial f({\bm{w}}) 可视为一个线性映射(注意此处 ∂\partial 未加粗)。在 w∈E{\bm{w}}\in\mathcal{E} 处的方向导数自然地定义了一个线性映射 l[v]=∂f(w)[v]l[{\bm{v}}]=\partial f({\bm{w}})[{\bm{v}}],其中 ∂f(w) ⁣:E→F\partial f({\bm{w}})\colon\mathcal{E}\to\mathcal{F} 称为雅可比—向量乘积(JVP),它刻画了在 w∈E{\bm{w}}\in\mathcal{E} 处沿输入方向 v∈E{\bm{v}}\in\mathcal{E} 的无穷小变化。These notions can be extended to general Euclidean spaces, such as the spaces of matrices or tensors. For functions of the form f ⁣:E→Rf\colon\mathcal{E}\to\mathbb{R}, the gradient is ∇f(w)∈E\nabla f({\bm{w}})\in\mathcal{E}. More generally, for functions of the form f ⁣:E→Ff\colon\mathcal{E}\to\mathcal{F}, the Jacobian ∂f(w)\partial f({\bm{w}}) can be seen as a linear map (notice the non-bold ∂\partial). The directional derivative at w∈E{\bm{w}}\in\mathcal{E} naturally defines a linear map l[v]=∂f(w)[v]l[{\bm{v}}]=\partial f({\bm{w}})[{\bm{v}}], where ∂f(w) ⁣:E→F\partial f({\bm{w}})\colon\mathcal{E}\to\mathcal{F} is called the Jacobian vector product (JVP) and captures the infinitesimal variation at w∈E{\bm{w}}\in\mathcal{E} along the input direction v∈E{\bm{v}}\in\mathcal{E}.

  • •

    其伴随 ∂f(w)∗ ⁣:F→E\partial f({\bm{w}})^{*}\colon\mathcal{F}\to\mathcal{E} 定义了另一个线性映射 l[u]=∂f(w)∗[u]l[{\bm{u}}]=\partial f({\bm{w}})^{*}[{\bm{u}}],称为向量—雅可比乘积(VJP),它刻画了在 w∈E{\bm{w}}\in\mathcal{E} 处沿输出方向 u∈F{\bm{u}}\in\mathcal{F} 的无穷小变化。于是,链式法则即为这些线性映射的组合。Its adjoint ∂f(w)∗ ⁣:F→E\partial f({\bm{w}})^{*}\colon\mathcal{F}\to\mathcal{E} defines another linear map l[u]=∂f(w)∗[u]l[{\bm{u}}]=\partial f({\bm{w}})^{*}[{\bm{u}}] called the vector Jacobian product (VJP) and captures the infinitesimal variation at w∈E{\bm{w}}\in\mathcal{E} along the output direction u∈F{\bm{u}}\in\mathcal{F}. The chain rule is then the composition of these linear maps.

  • •

    对于将标量值函数 ℓ\ell(例如损失函数)与向量值函数 ff(例如网络函数)进行组合的特殊情形,梯度由 ∇(ℓ∘f)(w)=∂f(w)∗∇ℓ(f(w))\nabla(\ell\circ f)({\bm{w}})=\partial f({\bm{w}})^{*}\nabla\ell(f({\bm{w}})) 给出。正因如此,能够将伴随作用于梯度——正如我们将看到的,这可以通过反向模式自动微分来实现——在机器学习中才如此普遍。For the particular case when we compose a scalar-valued function ℓ\ell (such as a loss function) with a vector-valued function ff (such as a network function), the gradient is given by ∇(ℓ∘f)(w)=∂f(w)∗∇ℓ(f(w))\nabla(\ell\circ f)({\bm{w}})=\partial f({\bm{w}})^{*}\nabla\ell(f({\bm{w}})). This is why being able to apply the adjoint to a gradient, which as we shall see can be done with reverse-mode autodiff, is so pervasive in machine learning.

  • •

    JVP 与 VJP 算子的定义还可以在微分几何的语境中进一步推广。在该框架下,JVP 相当于作用于切向量的前推算子,VJP 则相当于作用于余切向量的拉回算子。The definitions of JVP and VJP operators can further be generalized in the context of differentiable geometry. In that framework, the JVP amounts to the pushforward operator that acts on tangent vectors. The VJP amounts to the pullback operator that acts on cotangent vectors.

  • •

    我们还看到,从 RP\mathbb{R}^{P} 到 R\mathbb{R} 的函数 f(w)f({\bm{w}}) 的 Hessian 矩阵记为 ∇2f(w)∈RP×P\nabla^{2}f({\bm{w}})\in\mathbb{R}^{P\times P}。若二阶偏导数连续,则该矩阵是对称的。将其视为线性映射,Hessian 矩阵引出了 Hessian—向量乘积(HVP)的概念;我们看到,它可以归结为 ∇f(w)\nabla f({\bm{w}}) 的 JVP 或 VJP。We also saw that the Hessian matrix of a function f(w)f({\bm{w}}) from RP\mathbb{R}^{P} to R\mathbb{R} is denoted ∇2f(w)∈RP×P\nabla^{2}f({\bm{w}})\in\mathbb{R}^{P\times P}. It is symmetric if the second partial derivatives are continuous. Seen as linear map, the Hessian leads to the notion of Hessian-vector product (HVP), which we saw can be reduced to the JVP or the VJP of ∇f(w)\nabla f({\bm{w}}).

  • •

    本章的主要要点是:计算函数组合的方向导数或梯度,并不需要计算中间雅可比矩阵,而只需计算与这些中间函数相关联的线性映射(JVP 或 VJP)。第 8 章介绍自动微分,其目标正是为计算链、或更一般地为计算图,提供这些映射的高效实现。The main take-away message of this chapter is that computing the directional derivative or the gradient of compositions of functions does not require computing intermediate Jacobians but only to evaluate linear maps (JVPs or VJPs) associated with these intermediate functions. The goal of automatic differentiation, presented in Chapter 8, is precisely to provide an efficient implementation of these maps for computation chains or more generally for computation graphs.