目录二阶自动微分

第 9 章 二阶自动微分Chapter 9 Second-order automatic differentiation

本章回顾如何用自动微分计算二阶导数。We review in this chapter how to perform automatic differentiation for second-order derivatives.

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

本节考虑函数 f ⁣:E→Rf\colon\mathcal{E}\to\mathbb{R}。与雅可比矩阵类似,在大多数情况下,我们并不需要访问完整的Hessian 矩阵,而是需要求 ∇2f(w)[v]\nabla^{2}f({\bm{w}})[{\bm{v}}] 在 w∈E{\bm{w}}\in\mathcal{E} 处沿方向 v∈E{\bm{v}}\in\mathcal{E} 的Hessian 矩阵—向量乘积(HVP),其定义见定义 2.4.7。这一乘积可以通过四种不同的方式计算,具体取决于我们如何组合自动微分的两种主要模式。We consider in this section a function f ⁣:E→Rf\colon\mathcal{E}\to\mathbb{R}. Similarly to the Jacobian, for most purposes, we do not need access to the full Hessian but rather to the Hessian-vector product (HVP) ∇2f(w)[v]\nabla^{2}f({\bm{w}})[{\bm{v}}] at w∈E{\bm{w}}\in\mathcal{E}, in a direction v∈E{\bm{v}}\in\mathcal{E}, as defined in Definition 2.4.7. The latter can be computed in four different ways, depending on how we combine the two main modes of autodiff.

9.1.1 四种可能的方法Four possible methods

HVP 可以通过四种不同的方式计算。An HVP can be computed in four different ways.

  1. 1.

    反向之上的反向:Hessian 矩阵可以看作梯度的转置雅可比矩阵,因此 HVP 可以计算为梯度的 VJP,Reverse on reverse: The Hessian can be seen as the transposed Jacobian of the gradient, hence the HVP can be computed as the VJP of the gradient,

    ∇2f(w)[v]=∂(∇f)(w)∗[v].\nabla^{2}f({\bm{w}})[{\bm{v}}]=\partial(\nabla f)({\bm{w}})^{*}[{\bm{v}}].
  2. 2.

    反向之上的前向:由于Hessian 矩阵是对称的(见命题 2.4.6),它也可以看作梯度的雅可比矩阵,因此 HVP 可以计算为梯度的 JVP,Forward on reverse: Owing to its symmetry (see Proposition 2.4.6), the Hessian can also be seen as the Jacobian of the gradient, hence the HVP can be computed as the JVP of the gradient,

    ∇2f(w)[v]=∂(∇f)(w)[v].\nabla^{2}f({\bm{w}})[{\bm{v}}]=\partial(\nabla f)({\bm{w}})[{\bm{v}}].
  3. 3.

    反向之上的前向:回顾对于任意函数 g ⁣:E→Eg\colon\mathcal{E}\to\mathcal{E},VJP 可以等价地定义为沿输出方向 v∈E{\bm{v}}\in\mathcal{E} 的梯度,即Reverse on forward: Recall that for any function g ⁣:E→Eg\colon\mathcal{E}\to\mathcal{E}, the VJP can equivalently be defined as the gradient along an output direction v∈E{\bm{v}}\in\mathcal{E}, that is,

    ∂g(w)∗[v]=∇⟨g,v⟩(w),\partial g({\bm{w}})^{*}[{\bm{v}}]=\nabla\langle g,{\bm{v}}\rangle({\bm{w}}),

    其中沿用简写 ⟨g,v⟩(w):=⟨v,g(w)⟩\langle g,{\bm{v}}\rangle({\bm{w}})\coloneqq\langle{\bm{v}},g({\bm{w}})\rangle,于是 ⟨g,v⟩\langle g,{\bm{v}}\rangle 是 w{\bm{w}} 的函数。因此在本例中,我们可以将“反向之上的反向”方法改写为where we recall the shorthand ⟨g,v⟩(w):=⟨v,g(w)⟩\langle g,{\bm{v}}\rangle({\bm{w}})\coloneqq\langle{\bm{v}},g({\bm{w}})\rangle, so that ⟨g,v⟩\langle g,{\bm{v}}\rangle is a function of w{\bm{w}}. In our case, we can therefore rewrite the reverse-on-reverse approach as

    ∂(∇f)(w)∗[v]=∇⟨∇f,v⟩(w).\partial(\nabla f)({\bm{w}})^{*}[{\bm{v}}]=\nabla\langle\nabla f,{\bm{v}}\rangle({\bm{w}}).

    我们知道 ⟨∇f,v⟩(w)=⟨∇f(w),v⟩=∂f(w)[v]\langle\nabla f,{\bm{v}}\rangle({\bm{w}})=\langle\nabla f({\bm{w}}),{\bm{v}}\rangle=\partial f({\bm{w}})[{\bm{v}}] 是 ff 在 w{\bm{w}} 处沿 v{\bm{v}} 的 JVP。因此,我们也可以将 HVP 计算为 ff 在 w{\bm{w}} 处沿 v{\bm{v}} 的 JVP 的梯度,We know that ⟨∇f,v⟩(w)=⟨∇f(w),v⟩=∂f(w)[v]\langle\nabla f,{\bm{v}}\rangle({\bm{w}})=\langle\nabla f({\bm{w}}),{\bm{v}}\rangle=\partial f({\bm{w}})[{\bm{v}}] is the JVP of ff at w{\bm{w}} along v{\bm{v}}. Therefore, we can also compute the HVP as the gradient of the JVP of ff at w{\bm{w}} along v{\bm{v}},

    ∇2f(w)[v]=∇(∂f(⋅)[v])(w),\nabla^{2}f({\bm{w}})[{\bm{v}}]=\nabla(\partial f(\cdot)[{\bm{v}}])({\bm{w}}),

    其中采用记号 (∂f(⋅)[v])(w):=∂f(w)[v](\partial f(\cdot)[{\bm{v}}])({\bm{w}})\coloneqq\partial f({\bm{w}})[{\bm{v}}],以强调它是 w{\bm{w}} 的函数。where we use the notation (∂f(⋅)[v])(w):=∂f(w)[v](\partial f(\cdot)[{\bm{v}}])({\bm{w}})\coloneqq\partial f({\bm{w}})[{\bm{v}}] to insist on the fact that it is a function of w{\bm{w}}.

  4. 4.

    前向之上的前向:最后,我们可以使用定义 2.4.7 中把 HVP 定义为沿 v{\bm{v}} 以及各标准方向的二阶偏导数向量这一点。也就是说,假设 E=RP\mathcal{E}=\mathbb{R}^{P},我们可以计算 JVP 的 JVP 共 PP 次:Forward on forward: Finally, we can use the definition of the HVP in Definition 2.4.7 as a vector of second partial derivatives along v{\bm{v}} and each canonical direction. That is, assuming E=RP\mathcal{E}=\mathbb{R}^{P}, we can compute the JVP of the JVP PP times:

    ∇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。The four different ways of computing the HVP are summarized in Table 9.1.

方法Method 计算方式Computation
反向之上的反向(梯度的 VJP)Reverse on reverse (VJP of gradient) ∂(∇f)(w)∗[v]\partial(\nabla f)({\bm{w}})^{*}[{\bm{v}}]
反向之上的前向(梯度的 JVP)Forward on reverse (JVP of gradient) ∂(∇f)(w)[v]\partial(\nabla f)({\bm{w}})[{\bm{v}}]
前向之上的反向(JVP 的梯度)Reverse on forward (gradient of JVP) ∇(∂f(⋅)[v])(w)\nabla(\partial f(\cdot)[{\bm{v}}])({\bm{w}})
前向之上的前向(JVP 的 JVP)Forward on forward (JVPs of JVPs) (∂2f(w)[v,ei])i=1P(\partial^{2}f({\bm{w}})[{\bm{v}},{\bm{e}}_{i}])_{i=1}^{P}
表 9.1:计算 HVP ∇2f(w)[v]\nabla^{2}f({\bm{w}})[{\bm{v}}] 的四种不同方法。Table 9.1: Four different ways of computing the HVP ∇2f(w)[v]\nabla^{2}f({\bm{w}})[{\bm{v}}].

9.1.2 复杂度Complexity

为直观了解这四种方法的计算复杂度与内存复杂度,我们沿用 8.1 节的做法,考虑一个函数链 f:=fK∘⋯∘f1f\coloneqq f_{K}\circ\dots\circ f_{1}。为简化分析,我们假设对 k∈{1,…,K−1}k\in\{1,\dots,K-1\},有 fk ⁣:RP→RPf_{k}\colon\mathbb{R}^{P}\to\mathbb{R}^{P},且 fK ⁣:RP→Rf_{K}\colon\mathbb{R}^{P}\to\mathbb{R}。To get a sense of the computational and memory complexity of the four approaches, we consider a chain of functions f:=fK∘⋯∘f1f\coloneqq f_{K}\circ\dots\circ f_{1} as done in Section 8.1. To simplify our analysis, we assume fk ⁣:RP→RPf_{k}\colon\mathbb{R}^{P}\to\mathbb{R}^{P} for k∈{1,…,K−1}k\in\{1,\dots,K-1\} and fK ⁣:RP→Rf_{K}\colon\mathbb{R}^{P}\to\mathbb{R}.

反向模式的计算图如图 9.1 所示。f=fK∘⋯∘f1f=f_{K}\circ\dots\circ f_{1} 可以用一条简单的链表示,但 ∇f\nabla f 的计算图不再是一条链,而是一个 DAG。这是因为在 ∂fk(sk−1)∗[rk]\partial f_{k}({\bm{s}}_{k-1})^{*}[{\bm{r}}_{k}] 的计算中,sk−1{\bm{s}}_{k-1} 与 rk{\bm{r}}_{k} 都依赖于 s0{\bm{s}}_{0}。The computation graph of the reverse mode is illustrated in Fig. 9.1. While f=fK∘⋯∘f1f=f_{K}\circ\dots\circ f_{1} would be represented by a simple chain, the computational graph of ∇f\nabla f is no longer a chain: it is a DAG. This is due to the computations of ∂fk(sk−1)∗[rk]\partial f_{k}({\bm{s}}_{k-1})^{*}[{\bm{r}}_{k}], where both sk−1{\bm{s}}_{k-1} and rk{\bm{r}}_{k} depend on s0{\bm{s}}_{0}.

图 9.2 与图 9.3 分别展示了反向之上的反向、反向之上的前向的计算图。在反向模式之上再施加反向模式时,每个扇入运算 sk−1,rk↦∂fk(sk−1)∗[rk]{\bm{s}}_{k-1},{\bm{r}}_{k}\mapsto\partial f_{k}({\bm{s}}_{k-1})^{*}[{\bm{r}}_{k}] 处,∇f\nabla f 上的反向模式都会分岔为两条路径,随后由一次求和合并。而在反向模式之上叠加前向模式时,计算流只是沿着 ∇f\nabla f 的流程推进。We illustrate the computation graphs of reverse-on-reverse and forward-on-reverse in Fig. 9.2 and Fig. 9.3 respectively. By applying reverse mode on reverse mode, at each fan-in operation sk−1,rk↦∂fk(sk−1)∗[rk]{\bm{s}}_{k-1},{\bm{r}}_{k}\mapsto\partial f_{k}({\bm{s}}_{k-1})^{*}[{\bm{r}}_{k}], the reverse mode on ∇f\nabla f branches out into two paths that are later merged by a sum. By applying forward mode on top of reverse mode, the flow of computations simply follows that of ∇f\nabla f.

图 9.1:计算 f=fK∘…∘f1f=f_{K}\circ\ldots\circ f_{1} 的梯度时,反向模式自动微分所对应的计算图。ff 是一条简单的链,而 ∇f\nabla f 是一个 DAG。Figure 9.1: Computation graph corresponding to reverse mode autodiff for evaluating the gradient of f=fK∘…∘f1f=f_{K}\circ\ldots\circ f_{1}. While ff is a simple chain, ∇f\nabla f is a DAG.
图 9.2:在反向模式之上再施加反向模式来计算 HVP ∇2f(w)[v]\nabla^{2}f({\bm{w}})[{\bm{v}}] 的计算图。由于 ∇f\nabla f 的计算图引入了扇入运算 sk−1,rk↦∂fk(sk−1)∗[rk]{\bm{s}}_{k-1},{\bm{r}}_{k}\mapsto\partial f_{k}({\bm{s}}_{k-1})^{*}[{\bm{r}}_{k}],施加于 ∇f\nabla f 的反向模式会在每个这类节点处使计算分岔。Figure 9.2: Computation graph for computing the HVP ∇2f(w)[v]\nabla^{2}f({\bm{w}})[{\bm{v}}] by using reverse mode on top of reverse mode. As the computation graph of ∇f\nabla f induces fan-in operations sk−1,rk↦∂fk(sk−1)∗[rk]{\bm{s}}_{k-1},{\bm{r}}_{k}\mapsto\partial f_{k}({\bm{s}}_{k-1})^{*}[{\bm{r}}_{k}], the reverse mode applied on ∇f\nabla f induces branching of the computations at each such node.
图 9.3:在反向模式之上叠加前向模式来计算 HVP ∇2f(w)[v]\nabla^{2}f({\bm{w}})[{\bm{v}}] 的计算图。前向模式沿着为梯度所做的计算自然推进,只是还会经过各中间运算的导数。Figure 9.3: Computation graph for computing the HVP ∇2f(w)[v]\nabla^{2}f({\bm{w}})[{\bm{v}}] by using forward mode on top of reverse mode. The forward mode naturally follows the computations done for the gradient, except that it passes through the derivatives of the intermediate operations.

据此,仿照表 8.1 的计算,可得如下结果。我们假设每个 ∂fk(sk−1)\partial f_{k}({\bm{s}}_{k-1}) 都是稠密线性算子,因而其应用代价与矩阵—向量乘积相同。至于内存复杂度,我们考虑每个运算的输入都会被保存下来,以便在反向传播中计算所需的导数。With this in mind, following a similar calculation as for Table 8.1, we obtain the following results. We assume that each ∂fk(sk−1)\partial f_{k}({\bm{s}}_{k-1}) is a dense linear operator, so that its application has the same cost as a matrix-vector multiplication. For the memory complexity, we consider that the inputs of each operation are saved to compute the required derivatives in the backward passes.

  1. 1.

    反向之上的反向:时间为 O(KP2)O(KP^{2}),空间为 O(KP)O(KP)。Reverse on reverse: O(KP2)O(KP^{2}) time and O(KP)O(KP) space.

  2. 2.

    前向模式叠加于反向模式之上:时间复杂度为 O(KP2)O(KP^{2}),空间复杂度为 O(KP)O(KP)。Forward on reverse: O(KP2)O(KP^{2}) time and O(KP)O(KP) space.

  3. 3.

    反向模式叠加于前向模式之上:时间复杂度为 O(KP2)O(KP^{2}),空间复杂度为 O(KP)O(KP)。Reverse on forward: O(KP2)O(KP^{2}) time and O(KP)O(KP) space.

  4. 4.

    前向模式叠加于前向模式之上:对 PP 个 JVP,时间复杂度为 O(KP3)O(KP^{3}),空间复杂度为 O(3P)O(3P),其中 e1,…,eP{\bm{e}}_{1},\dots,{\bm{e}}_{P}。Forward on forward: O(KP3)O(KP^{3}) time and O(3P)O(3P) space for the PP JVPs with e1,…,eP{\bm{e}}_{1},\dots,{\bm{e}}_{P}.

可以看到,对于由函数构成的链,“反向叠加反向”“前向叠加反向”与“反向叠加前向”三者的时间复杂度至多相差若干常数因子,因而大体相当。在反向模式之上再叠加反向模式,需要保存反向传播的信息,即 rk{\bm{r}}_{k}(相应地,保存前向传播的信息,即图 8.1 中的 tk{\bm{t}}_{k}),以便完成最后一次反向传播。若在反向模式之上改用前向模式,便不会产生这项额外开销,内存代价因而略低。此外,反向模式叠加反向模式还会因图 9.2 所示的分支与合并运算而多出若干次求和;反向模式叠加于前向模式之上时同样如此,因为我们无法回避扇入运算(此处其形式为 sk−1,tk−1↦∂fk(sk−1)[tk−1]{\bm{s}}_{k-1},{\bm{t}}_{k-1}\mapsto\partial f_{k}({\bm{s}}_{k-1})[{\bm{t}}_{k-1}])。遗憾的是,“前向叠加前向”的代价高昂得令人望而却步。We see that, for chains of functions, “reverse on reverse”, “forward on reverse” and “reverse on forward” all have similar time complexities up to some constant factors. Using reverse mode on top of reverse mode requires storing the information backpropagated, i.e., the rk{\bm{r}}_{k} (resp. the information forwarded, i.e., the tk{\bm{t}}_{k} in Fig. 8.1), to perform the final reverse pass. By using forward mode on top of reverse mode, this additional cost is not incurred, making it slightly less memory expensive. In addition, reverse mode on top of reverse mode induces a few additional summations due to the branching and merge operations depicted in Fig. 9.2. The same holds when using reverse on top of forward as we cannot avoid fan-in operations (this time of the form sk−1,tk−1↦∂fk(sk−1)[tk−1]{\bm{s}}_{k-1},{\bm{t}}_{k-1}\mapsto\partial f_{k}({\bm{s}}_{k-1})[{\bm{t}}_{k-1}]). Unfortunately, “forward on forward” is prohibitively expensive.

总之,在计算 HVP 的四种方法中,就计算复杂度与内存复杂度而言,前向叠加反向模式是先验上最可取的一种。不过需要注意的是,高阶导数的计算可以受益于专门设计的自动微分实现,例如泰勒模式自动微分,它们并非只是把前向模式与反向模式简单组合起来。对于一般的函数 ff,合理的做法是对前三种方法逐一做基准测试,以确定哪一种最适合当前的函数。To summarize, among the four approaches presented to compute HVPs, the forward-on-reverse mode is a priori the most preferable in terms of computational and memory complexities. Note, however, that computations of higher derivatives can benefit from dedicated autodiff implementations such as Taylor mode autodiff, that do not merely compose forward and reverse modes. For general functions ff, it is reasonable to benchmark the first three methods to determine which method is the best for the function at hand.

9.2 Gauss-Newton 矩阵9.2 Gauss-Newton matrix

9.2.1 Hessian 矩阵的一种近似An approximation of the Hessian

函数 L ⁣:W→RL\colon\mathcal{W}\to\mathbb{R} 的 Hessian 矩阵 ∇2L(w)\nabla^{2}L({\bm{w}}) 常被用来构造 L(w)L({\bm{w}}) 的二次近似,The Hessian matrix ∇2L(w)\nabla^{2}L({\bm{w}}) of a function L ⁣:W→RL\colon\mathcal{W}\to\mathbb{R} is often used to construct a quadratic approximation of L(w)L({\bm{w}}),

L(w+v)≈L(w)+⟨∇L(w),v⟩+12⟨v,∇2L(w)[v]⟩.L({\bm{w}}+{\bm{v}})\approx L({\bm{w}})+\langle\nabla L({\bm{w}}),{\bm{v}}\rangle+\frac{1}{2}\langle{\bm{v}},\nabla^{2}L({\bm{w}})[{\bm{v}}]\rangle.

遗憾的是,当 LL 非凸时,∇2L(w)\nabla^{2}L({\bm{w}}) 通常是不定矩阵,也就是说,上述近似是关于 v{\bm{v}} 的非凸二次函数。例如,若 L=ℓ∘fL=\ell\circ f 而 ℓ\ell 为凸,则当 ff 为线性时 LL 是凸的;但当 ff 非线性时,它通常是非凸的。(广义)Gauss-Newton 矩阵是 Hessian 矩阵的一种有原则的替代,它针对 L:=ℓ∘fL\coloneqq\ell\circ f 定义。Unfortunately, when LL is nonconvex, ∇2L(w)\nabla^{2}L({\bm{w}}) is typically an indefinite matrix, which means that the above approximation is a nonconvex quadratic w.r.t. v{\bm{v}}. For instance, if L=ℓ∘fL=\ell\circ f with ℓ\ell convex, then LL is convex if ff is linear, but it is typically nonconvex if ff is nonlinear. The (generalized) Gauss-Newton matrix is a principled alternative to the Hessian, which is defined for L:=ℓ∘fL\coloneqq\ell\circ f.

定义 9.2.1(Gauss-Newton 矩阵)。Definition 9.2.1 (Gauss-Newton matrix).
给定可微函数 f:W→Mf:\mathcal{W}\rightarrow\mathcal{M} 与二次可微函数 ℓ:M→R\ell:\mathcal{M}\rightarrow\mathbb{R},复合函数 L=ℓ∘fL=\ell\circ f 在点 w∈W{\bm{w}}\in\mathcal{W} 处的(广义)Gauss-Newton 矩阵定义为Given a differentiable function f:W→Mf:\mathcal{W}\rightarrow\mathcal{M} and a twice differentiable function ℓ:M→R\ell:\mathcal{M}\rightarrow\mathbb{R}, the (generalized) Gauss-Newton matrix of the composition L=ℓ∘fL=\ell\circ f evaluated at a point w∈W{\bm{w}}\in\mathcal{W} is defined as ∇GN2(ℓ∘f)(w):=∂f(w)∗∇2ℓ(f(w))∂f(w).\nabla^{2}_{\mathrm{GN}}(\ell\circ f)({\bm{w}})\coloneqq\partial f({\bm{w}})^{*}\nabla^{2}\ell(f({\bm{w}}))\partial f({\bm{w}}).

正如 17.2 节所讨论的,Gauss-Newton 矩阵是 Gauss-Newton 方法的关键要素。其优点之一在于:只要 ℓ\ell 为凸,它便是半正定的。As studied in Section 17.2, the Gauss-Newton matrix is a key ingredient of the Gauss-Newton method. An advantage of the Gauss-Newton matrix is its positive semi-definiteness provided that ℓ\ell is convex.

命题 9.2.2(Gauss-Newton 矩阵的半正定性)。Proposition 9.2.2 (Positive semi-definiteness of the GN matrix).
若 ℓ\ell 为凸函数,则对任意 ff,∇GN2(ℓ∘f)(w)\nabla^{2}_{\mathrm{GN}}(\ell\circ f)({\bm{w}}) 都是半正定的。If ℓ\ell is convex, then ∇GN2(ℓ∘f)(w)\nabla^{2}_{\mathrm{GN}}(\ell\circ f)({\bm{w}}) is positive semi-definite for all ff.

这意味着该近似This means that the approximation

L(w+v)≈L(w)+⟨∇L(w),v⟩+12⟨v,∇GN2L(w)[v]⟩L({\bm{w}}+{\bm{v}})\approx L({\bm{w}})+\langle\nabla L({\bm{w}}),{\bm{v}}\rangle+\frac{1}{2}\langle{\bm{v}},\nabla^{2}_{\mathrm{GN}}L({\bm{w}})[{\bm{v}}]\rangle

是关于 v{\bm{v}} 的凸二次函数。is a convex quadratic w.r.t. v{\bm{v}}.

利用链式法则可知,L=ℓ∘fL=\ell\circ f 的Hessian 矩阵可分解为两项之和(另见命题 9.5.1)。Using the chain rule, we find that the Hessian of L=ℓ∘fL=\ell\circ f decomposes into the sum of two terms (see also Proposition 9.5.1).

命题 9.2.3(Hessian 矩阵的近似)。Proposition 9.2.3 (Approximation of the Hessian).
对于二次可微的 ff 与 ℓ\ell,有For ff and ℓ\ell twice differentiable, we have ∇2(ℓ∘f)(w)\displaystyle\nabla^{2}(\ell\circ f)({\bm{w}}) =∂f(w)∗∇2ℓ(f(w))∂f(w)+∂2f(w)∗[∇ℓ(f(w))]\displaystyle=\partial f({\bm{w}})^{*}\nabla^{2}\ell(f({\bm{w}}))\partial f({\bm{w}})+\partial^{2}f({\bm{w}})^{*}[\nabla\ell(f({\bm{w}}))] =∇GN2(ℓ∘f)(w)+∑j=1M∇jℓ(f(w))∇2fj(w).\displaystyle=\nabla^{2}_{\mathrm{GN}}(\ell\circ f)({\bm{w}})+\sum_{j=1}^{M}\nabla_{j}\ell(f({\bm{w}}))\nabla^{2}f_{j}({\bm{w}}). 若 ff 是线性的,则 Hessian 矩阵与Gauss-Newton 矩阵重合,If ff is linear, then the Hessian and Gauss-Newton matrices coincide, ∇2(ℓ∘f)(w)=∇GN2(ℓ∘f)(w).\nabla^{2}(\ell\circ f)({\bm{w}})=\nabla^{2}_{\mathrm{GN}}(\ell\circ f)({\bm{w}}).

因此,Gauss-Newton 算子 ∇GN2(ℓ∘f)\nabla^{2}_{\mathrm{GN}}(\ell\circ f) 可视为 Hessian 矩阵 ∇2(ℓ∘f)\nabla^{2}(\ell\circ f) 的一种近似;当 ff 为线性时,二者相等。The Gauss-Newton operator ∇GN2(ℓ∘f)\nabla^{2}_{\mathrm{GN}}(\ell\circ f) can therefore be seen as an approximation of the Hessian ∇2(ℓ∘f)\nabla^{2}(\ell\circ f), with equality if ff is linear.

9.2.2 Gauss-Newton 链式法则Gauss-Newton chain rule

命题 9.5.1 给出了计算两个函数复合的 Hessian 矩阵的链式法则,但由于交叉项的存在,该公式相对复杂;相比之下,Gauss-Newton 链式法则十分简洁。A chain rule for computing the Hessian of a composition of two functions is presented in Proposition 9.5.1, but the formula is relatively complicated, due to the cross-terms. In contrast, a Gauss-Newton chain rule is straightforward.

命题 9.2.4(Gauss-Newton 链式法则)。Proposition 9.2.4 (Gauss-Newton chain rule).
∇GN2(ℓ∘f∘g)(w)=∂g(w)∗∇GN2(ℓ∘f)(g(w))∂g(w).\displaystyle\nabla^{2}_{\mathrm{GN}}(\ell\circ f\circ g)({\bm{w}})=\partial g({\bm{w}})^{*}\nabla^{2}_{\mathrm{GN}}(\ell\circ f)(g({\bm{w}}))\partial g({\bm{w}}).

9.2.3 Gauss-Newton 向量乘积Gauss-Newton vector product

与 Hessian 矩阵类似,我们很少需要在内存中具体化完整的 Gauss-Newton 矩阵。事实上,我们可以针对方向 v∈W{\bm{v}}\in\mathcal{W} 定义 Gauss-Newton 向量乘积(GNVP),即一个线性映射:As for the Hessian, we rarely need to materialize the full Gauss-Newton matrix in memory. Indeed, we can define the Gauss-Newton vector product (GNVP), a linear map for a direction v∈W{\bm{v}}\in\mathcal{W}, as

∇GN2(ℓ∘f)(w)[v]:=∂f(w)∗[∇2ℓ(f(w))[∂f(w)[v]]],\nabla^{2}_{\mathrm{GN}}(\ell\circ f)({\bm{w}})[{\bm{v}}]\coloneqq\partial f({\bm{w}})^{*}[\nabla^{2}\ell(f({\bm{w}}))[\partial f({\bm{w}})[{\bm{v}}]]], (9.1)

其中 ∇2ℓ(f(w))[u]\nabla^{2}\ell(f({\bm{w}}))[{\bm{u}}] 是 ℓ\ell 的 HVP,即从 M\mathcal{M} 到 M\mathcal{M} 的线性映射。GNVP 可以利用 ff 的 JVP、ℓ\ell 的 HVP 以及 ff 的 VJP 来计算。具体化 VJP 需要对 ff 进行 11 次前向传播,由此可同时得到值 f(w)f({\bm{w}}) 和伴随线性映射 u↦∂f(w)∗[u]{\bm{u}}\mapsto\partial f({\bm{w}})^{*}[{\bm{u}}];求值 VJP 需要对 ff 进行 11 次反向传播;求值 JVP 则需要对 ff 进行 11 次前向传播。因此,求值 v↦∇GN2(ℓ∘f)(w)[v]{\bm{v}}\mapsto\nabla^{2}_{\mathrm{GN}}(\ell\circ f)({\bm{w}})[{\bm{v}}] 总共需要对 ff 进行 22 次前向传播和 11 次反向传播。where ∇2ℓ(f(w))[u]\nabla^{2}\ell(f({\bm{w}}))[{\bm{u}}] is the HVP of ℓ\ell, a linear map from M\mathcal{M} to M\mathcal{M}. The GNVP can be computed using the JVP of ff, the HVP of ℓ\ell and the VJP of ff. Instantiating the VJP requires 11 forward pass through ff, from which we get both the value f(w)f({\bm{w}}) and the adjoint linear map u↦∂f(w)∗[u]{\bm{u}}\mapsto\partial f({\bm{w}})^{*}[{\bm{u}}]. Evaluating the VJP requires 11 backward pass through ff. Evaluating the JVP requires 11 forward pass through ff. In total, evaluating v↦∇GN2(ℓ∘f)(w)[v]{\bm{v}}\mapsto\nabla^{2}_{\mathrm{GN}}(\ell\circ f)({\bm{w}})[{\bm{v}}] therefore requires 22 forward passes and 11 backward pass through ff.

9.2.4 Gauss-Newton 矩阵分解Gauss-Newton matrix factorization

在本节中,我们假定 W⊆RP\mathcal{W}\subseteq\mathbb{R}^{P} 且 M⊆RM\mathcal{M}\subseteq\mathbb{R}^{M}。当 ℓ\ell 为凸函数时,Gauss-Newton 矩阵是半正定的,因此可分解为 ∇GN2(ℓ∘f)(w)=VV⊤\nabla^{2}_{\mathrm{GN}}(\ell\circ f)({\bm{w}})=VV^{\top},其中 V∈RP×RV\in\mathbb{R}^{P\times R},R≤min⁡{P,M}R\leq\min\{P,M\} 为该矩阵的秩。这一分解实际上可以很方便地由 ℓ\ell 的 Hessian 矩阵的因子分解算出。例如,设已知 ℓ\ell 的 Hessian 矩阵的特征分解 ∇2ℓ(f(w))=∑i=1Mλiuiui⊤\nabla^{2}\ell(f({\bm{w}}))=\sum_{i=1}^{M}\lambda_{i}{\bm{u}}_{i}{\bm{u}}_{i}^{\top},其中 ui{\bm{u}}_{i} 为特征向量,λi≥0\lambda_{i}\geq 0 为特征值(由半正定性可知这些特征值非负)。于是,Gauss-Newton 矩阵可分解为In this section, we assume W⊆RP\mathcal{W}\subseteq\mathbb{R}^{P} and M⊆RM\mathcal{M}\subseteq\mathbb{R}^{M}. When ℓ\ell is convex, we know that the Gauss-Newton matrix is positive semi-definite and therefore it can be factorized into ∇GN2(ℓ∘f)(w)=VV⊤\nabla^{2}_{\mathrm{GN}}(\ell\circ f)({\bm{w}})=VV^{\top} for some V∈RP×RV\in\mathbb{R}^{P\times R}, where R≤min⁡{P,M}R\leq\min\{P,M\} is the rank of the matrix. Such a decomposition can actually be computed easily from a factorization of the Hessian of ℓ\ell. For instance, suppose we know the eigendecomposition of the Hessian of ℓ\ell, ∇2ℓ(f(w))=∑i=1Mλiuiui⊤\nabla^{2}\ell(f({\bm{w}}))=\sum_{i=1}^{M}\lambda_{i}{\bm{u}}_{i}{\bm{u}}_{i}^{\top}, where the ui{\bm{u}}_{i} are the eigenvectors and the λi≥0\lambda_{i}\geq 0 are the eigenvalues (which we know are non-negative due to positive semidefiniteness). Then, the Gauss-Newton matrix can be decomposed as

∇GN2(ℓ∘f)(w)\displaystyle\nabla^{2}_{\mathrm{GN}}(\ell\circ f)({\bm{w}}) =∑i=1Mλi∂f(w)∗[ui]ui⊤∂f(w)\displaystyle=\sum_{i=1}^{M}\lambda_{i}\partial f({\bm{w}})^{*}[{\bm{u}}_{i}]{\bm{u}}_{i}^{\top}\partial f({\bm{w}})
=∑i=1M(λi∂f(w)∗[ui])(λi∂f(w)∗[ui])⊤\displaystyle=\sum_{i=1}^{M}\left(\sqrt{\lambda_{i}}\partial f({\bm{w}})^{*}[{\bm{u}}_{i}]\right)\left(\sqrt{\lambda_{i}}\partial f({\bm{w}})^{*}[{\bm{u}}_{i}]\right)^{\top}
=∑i=1Mvivi⊤where vi:=λi∂f(w)∗[ui].\displaystyle=\sum_{i=1}^{M}{\bm{v}}_{i}{\bm{v}}_{i}^{\top}\quad\text{where}\ {\bm{v}}_{i}\coloneqq\sqrt{\lambda_{i}}\partial f({\bm{w}})^{*}[{\bm{u}}_{i}].

将向量 vi{\bm{v}}_{i} 按列堆叠成矩阵 V=(v1,…,vM)V=({\bm{v}}_{1},\ldots,{\bm{v}}_{M}),即得到分解 ∇GN2(ℓ∘f)(w)=VV⊤\nabla^{2}_{\mathrm{GN}}(\ell\circ f)({\bm{w}})=VV^{\top}。为构造该分解,需要对 ∇2ℓ(f(w))∈RM×M\nabla^{2}\ell(f({\bm{w}}))\in\mathbb{R}^{M\times M} 做特征分解,耗时 O(M3)O(M^{3});还需要在 w{\bm{w}} 处调用 ff 的 VJP 共 MM 次。与式 9.1 中的直接实现相比,该分解一经算出,便可按 ∇GN2(ℓ∘f)(w)[v]=VV⊤v\nabla^{2}_{\mathrm{GN}}(\ell\circ f)({\bm{w}})[{\bm{v}}]=VV^{\top}{\bm{v}} 计算 Gauss-Newton 向量乘积(GNVP)。该分解仅需 P×MP\times M 的内存,而式 9.1 的直接实现则需保存 ff 的中间计算。可见,计算与内存之间的权衡取决于所考虑的具体函数。Stacking the vectors vi{\bm{v}}_{i} into a matrix V=(v1,…,vM)V=({\bm{v}}_{1},\ldots,{\bm{v}}_{M}), we recover the factorization ∇GN2(ℓ∘f)(w)=VV⊤\nabla^{2}_{\mathrm{GN}}(\ell\circ f)({\bm{w}})=VV^{\top}. To form this decomposition, we need to perform the eigendecomposition of ∇2ℓ(f(w))∈RM×M\nabla^{2}\ell(f({\bm{w}}))\in\mathbb{R}^{M\times M}, which takes O(M3)O(M^{3}) time. We also need MM calls to the VJP of ff at w{\bm{w}}. Compared to the direct implementation in Eq. 9.1, the factorization, once computed, allows us to compute the Gauss-Newton vector product (GNVP) as ∇GN2(ℓ∘f)(w)[v]=VV⊤v\nabla^{2}_{\mathrm{GN}}(\ell\circ f)({\bm{w}})[{\bm{v}}]=VV^{\top}{\bm{v}}. The factorization only requires P×MP\times M memory, while the direct implementation in Eq. 9.1 requires us to maintain the intermediate computations of ff. The computation-memory trade-offs therefore depend on the function considered.

9.2.5 随机情形Stochastic setting

假设目标函数具有如下形式Suppose the objective function is of the form

L(w;x,y):=ℓ(f(w;x);y).L({\bm{w}};{\bm{x}},{\bm{y}})\coloneqq\ell(f({\bm{w}};{\bm{x}});{\bm{y}}).

略微滥用记号,与 (x,y)({\bm{x}},{\bm{y}}) 对所对应的 Gauss-Newton 矩阵为With some slight abuse of notation, we then have that the Gauss-Newton matrix associated with a pair (x,y)({\bm{x}},{\bm{y}}) is

∇GN2L(w;x,y):=∂f(w;x)∗∇2ℓ(θ;y)∂f(w;x),\nabla^{2}_{\mathrm{GN}}L({\bm{w}};{\bm{x}},{\bm{y}})\coloneqq\partial f({\bm{w}};{\bm{x}})^{*}\nabla^{2}\ell({\bm{\theta}};{\bm{y}})\partial f({\bm{w}};{\bm{x}}),

其中 θ:=f(w;x){\bm{\theta}}\coloneqq f({\bm{w}};{\bm{x}})。给定 (x,y)({\bm{x}},{\bm{y}}) 对上的分布 ρ\rho,则与平均损失where θ:=f(w;x){\bm{\theta}}\coloneqq f({\bm{w}};{\bm{x}}). Given a distribution ρ\rho over (x,y)({\bm{x}},{\bm{y}}) pairs, the Gauss-Newton matrix associated with the averaged loss

L(w):=EX,Y∼ρ[L(w;X,Y)]L({\bm{w}})\coloneqq\mathbb{E}_{X,Y\sim\rho}\left[L({\bm{w}};X,Y)\right]

相对应的 Gauss-Newton 矩阵为is then

∇GN2L(w)=EX,Y∼ρ[∇GN2L(w;X,Y)].\nabla^{2}_{\mathrm{GN}}L({\bm{w}})=\mathbb{E}_{X,Y\sim\rho}\left[\nabla^{2}_{\mathrm{GN}}L({\bm{w}};X,Y)\right].

9.3 Fisher 信息矩阵9.3 Fisher information matrix

9.3.1 基于得分函数的定义Definition using the score function

Fisher 信息是度量随机变量 SS 所含信息量的一种方式。The Fisher information is a way to measure the amount of information in a random variable SS.

定义 9.3.1(Fisher 信息矩阵)。Definition 9.3.1 (Fisher information matrix).
与概率分布 qwq_{\bm{w}}(参数为 w{\bm{w}})的负对数似然 L(w;S)=−log⁡qw(S)L({\bm{w}};S)=-\log q_{{\bm{w}}}(S) 相关联的 Fisher 信息矩阵(简称 Fisher),是 LL 的梯度在 w{\bm{w}} 处的协方差,其中 SS 服从 qwq_{\bm{w}} 分布,The Fisher information matrix, or Fisher for short, associated with the negative log-likelihood L(w;S)=−log⁡qw(S)L({\bm{w}};S)=-\log q_{{\bm{w}}}(S) of a probability distribution qwq_{\bm{w}} with parameters w{\bm{w}} is the covariance of the gradients of LL at w{\bm{w}} for SS distributed according to qwq_{\bm{w}}, ∇F2L(w)\displaystyle\nabla^{2}_{\mathrm{F}}L({\bm{w}}) :=ES∼qw[∇L(w;S)⊗∇L(w;S)]\displaystyle\coloneqq\mathbb{E}_{S\sim q_{{\bm{w}}}}[\nabla L({\bm{w}};S)\otimes\nabla L({\bm{w}};S)] =ES∼qw[∇wlog⁡qw(S)⊗∇wlog⁡qw(S)].\displaystyle=\mathbb{E}_{S\sim q_{{\bm{w}}}}[\nabla_{\bm{w}}\log q_{{\bm{w}}}(S)\otimes\nabla_{\bm{w}}\log q_{{\bm{w}}}(S)]. 梯度 ∇wlog⁡qw(S)\nabla_{\bm{w}}\log q_{{\bm{w}}}(S) 称为得分函数。The gradient ∇wlog⁡qw(S)\nabla_{\bm{w}}\log q_{{\bm{w}}}(S) is known as the score function.

如 17.3 节所述,Fisher 信息矩阵是自然梯度下降法的关键要素。As studied in Section 17.3, the Fisher information matrix is a key ingredient of the natural gradient descent method.

9.3.2 与 Hessian 矩阵的联系Link with the Hessian

只要该概率分布关于 w{\bm{w}} 二次可微,且其二阶导数可积,Fisher 信息矩阵也可表示为负对数似然的Hessian 矩阵 [5, 127]。Provided that the probability distribution is twice differentiable w.r.t. w{\bm{w}} with integrable second derivatives, the Fisher information matrix can also be expressed as the Hessian of the negative log-likelihood [5, 127].

命题 9.3.2(与 Hessian 矩阵的联系)。Proposition 9.3.2 (Connection with the Hessian).
负对数似然 L(w;S)=−log⁡qw(S)L({\bm{w}};S)=-\log q_{{\bm{w}}}(S) 的 Fisher 信息矩阵满足The Fisher information matrix of the negative log-likelihood L(w;S)=−log⁡qw(S)L({\bm{w}};S)=-\log q_{{\bm{w}}}(S) satisfies ∇F2L(w)\displaystyle\nabla^{2}_{\mathrm{F}}L({\bm{w}}) =ES∼qw[∇2L(w;S)]=ES∼qw[−∇w2log⁡qw(S)].\displaystyle=\mathbb{E}_{S\sim q_{{\bm{w}}}}[\nabla^{2}L({\bm{w}};S)]=\mathbb{E}_{S\sim q_{{\bm{w}}}}[-\nabla_{{\bm{w}}}^{2}\log q_{{\bm{w}}}(S)].
注 9.3.3(经验 Fisher)。Remark 9.3.3 (Empirical Fisher).
我们强调,在上述定义中,SS 采样自模型分布 qwq_{\bm{w}},而非数据分布 ρ\rho。也就是说,有We emphasize that in the above definitions, SS is sampled from the model distribution qwq_{\bm{w}}, not from the data distribution ρ\rho. That is, we have ∇F2L(w)\displaystyle\nabla^{2}_{\mathrm{F}}L({\bm{w}}) =ES∼qw[∇wlog⁡qw(S)∇wlog⁡qw(S)⊤]\displaystyle=\mathbb{E}_{S\sim q_{{\bm{w}}}}[\nabla_{\bm{w}}\log q_{{\bm{w}}}(S)\nabla_{\bm{w}}\log q_{{\bm{w}}}(S)^{\top}] ≠ES∼ρ[∇wlog⁡qw(S)∇wlog⁡qw(S)⊤]\displaystyle\neq\mathbb{E}_{S\sim\rho}[\nabla_{\bm{w}}\log q_{{\bm{w}}}(S)\nabla_{\bm{w}}\log q_{{\bm{w}}}(S)^{\top}] 后者有时被含糊地称为“经验”Fisher,尽管这一名称已引起混淆 [113]。The latter is sometimes called ambiguously the “empirical” Fisher, though this name has generated confusion [113].

9.3.3 与 Gauss-Newton 矩阵的等价性Equivalence with the Gauss-Newton matrix

迄今为止,我们讨论的都是关于一般随机变量 S∼qwS\sim q_{\bm{w}} 的 Fisher 信息。下面转向监督概率学习的情形:此时 S=(X,Y)S=(X,Y),并可按概率的乘积法则定义概率密度函数 qw(X,Y):=ρX(X)pθ(Y)q_{\bm{w}}(X,Y)\coloneqq\rho_{X}(X)p_{\bm{\theta}}(Y),简写作 θ:=f(w;X){\bm{\theta}}\coloneqq f({\bm{w}};X)。So far, we discussed the Fisher information for a generic random variable S∼qwS\sim q_{\bm{w}}. We now discuss the supervised probabilistic learning setting where S=(X,Y)S=(X,Y) and where, using the product rule of probability, we define the PDF qw(X,Y):=ρX(X)pθ(Y)q_{\bm{w}}(X,Y)\coloneqq\rho_{X}(X)p_{\bm{\theta}}(Y), with the shorthand θ:=f(w;X){\bm{\theta}}\coloneqq f({\bm{w}};X).

命题 9.3.4(监督情形下的 Fisher 矩阵)。Proposition 9.3.4 (Fisher matrix in supervised setting).
设 (X,Y)∼qw(X,Y)\sim q_{\bm{w}},其中 qwq_{\bm{w}} 的概率密度函数为 qw(X,Y):=ρX(X)pθ(Y)q_{\bm{w}}(X,Y)\coloneqq\rho_{X}(X)p_{\bm{\theta}}(Y)。此时,负对数似然 L(w;x,y)=−log⁡qw(x,y)L({\bm{w}};{\bm{x}},{\bm{y}})=-\log q_{{\bm{w}}}({\bm{x}},{\bm{y}}) 的 Fisher 信息矩阵可分解为:Suppose (X,Y)∼qw(X,Y)\sim q_{\bm{w}} where the PDF of qwq_{\bm{w}} is qw(X,Y):=ρX(X)pθ(Y)q_{\bm{w}}(X,Y)\coloneqq\rho_{X}(X)p_{\bm{\theta}}(Y). In that case, the Fisher information matrix of the negative log-likelihood L(w;x,y)=−log⁡qw(x,y)L({\bm{w}};{\bm{x}},{\bm{y}})=-\log q_{{\bm{w}}}({\bm{x}},{\bm{y}}) decomposes as: ∇F2L(w)\displaystyle\nabla^{2}_{\mathrm{F}}L({\bm{w}}) =E(X,Y)∼qw[∇wlog⁡qw(X,Y)⊗∇wlog⁡qw(X,Y)]\displaystyle=\mathbb{E}_{(X,Y)\sim q_{\bm{w}}}[\nabla_{\bm{w}}\log q_{\bm{w}}(X,Y)\otimes\nabla_{\bm{w}}\log q_{\bm{w}}(X,Y)] =EX∼ρX[EY∼pθ[∇wlog⁡pθ(Y)⊗∇wlog⁡pθ(Y)]]\displaystyle=\mathbb{E}_{X\sim\rho_{X}}\left[\mathbb{E}_{Y\sim p_{{\bm{\theta}}}}\left[\nabla_{\bm{w}}\log p_{{\bm{\theta}}}(Y)\otimes\nabla_{\bm{w}}\log p_{{\bm{\theta}}}(Y)\right]\right] =EX∼ρX[∂f(w;X)∗∇F2ℓ(θ)∂f(w;X)],\displaystyle=\mathbb{E}_{X\sim\rho_{X}}\left[\partial f({\bm{w}};X)^{*}\nabla^{2}_{\mathrm{F}}\ell({\bm{\theta}})\partial f({\bm{w}};X)\right], 其中简写 θ:=f(w;X){\bm{\theta}}\coloneqq f({\bm{w}};X) 已由我们定义,负对数似然损失 ℓ(θ;Y):=−log⁡pθ(Y)\ell({\bm{\theta}};Y)\coloneqq-\log p_{\bm{\theta}}(Y) 亦已定义。where we defined the shorthand θ:=f(w;X){\bm{\theta}}\coloneqq f({\bm{w}};X) and where we defined the negative log-likelihood loss ℓ(θ;Y):=−log⁡pθ(Y)\ell({\bm{\theta}};Y)\coloneqq-\log p_{\bm{\theta}}(Y).

当 pθp_{\bm{\theta}} 为指数族分布时,可以证明 Fisher 信息矩阵与 Gauss-Newton 矩阵彼此等价。When pθp_{\bm{\theta}} is an exponential family distribution, we can show that the Fisher information matrix and the Gauss-Newton matrix are equivalent.

命题 9.3.5(Fisher 矩阵与 Gauss-Newton 矩阵的等价性)。Proposition 9.3.5 (Equivalence between Fisher and Gauss-Newton).
若 pθp_{\bm{\theta}} 属于指数族分布,则If pθp_{\bm{\theta}} is an exponential family distribution, then ∇F2L(w)\displaystyle\nabla^{2}_{\mathrm{F}}L({\bm{w}}) =EX∼ρXEY∼pθ[∇L(w;X,Y)⊗∇L(w;X,Y)]\displaystyle=\mathbb{E}_{X\sim\rho_{X}}\mathbb{E}_{Y\sim p_{\bm{\theta}}}[\nabla L({\bm{w}};X,Y)\otimes\nabla L({\bm{w}};X,Y)] =EX∼ρXEY∼pθ[∂f(w;X)∗(∇ℓ(θ;Y)⊗∇ℓ(θ;Y))∂f(w;X)]\displaystyle=\mathbb{E}_{X\sim\rho_{X}}\mathbb{E}_{Y\sim p_{\bm{\theta}}}[\partial f({\bm{w}};X)^{*}(\nabla\ell({\bm{\theta}};Y)\otimes\nabla\ell({\bm{\theta}};Y))\partial f({\bm{w}};X)] =EX∼ρXEY∼pθ[∂f(w;X)∗∇2ℓ(θ;Y)∂f(w;X)]\displaystyle=\mathbb{E}_{X\sim\rho_{X}}\mathbb{E}_{Y\sim p_{\bm{\theta}}}[\partial f({\bm{w}};X)^{*}\nabla^{2}\ell({\bm{\theta}};Y)\partial f({\bm{w}};X)] =EX,Y∼ρ[∇GN2L(w;X,Y)],\displaystyle=\mathbb{E}_{X,Y\sim\rho}\left[\nabla^{2}_{\mathrm{GN}}L({\bm{w}};X,Y)\right], 其中 ρX(x):=∫ρ(x,y)dy\rho_{X}({\bm{x}})\coloneqq\int\rho({\bm{x}},{\bm{y}})d{\bm{y}}。where ρX(x):=∫ρ(x,y)dy\rho_{X}({\bm{x}})\coloneqq\int\rho({\bm{x}},{\bm{y}})d{\bm{y}}.
证明 9.3.6。Proof 9.3.6.

由命题 3.4.5 可知,若 pθp_{\bm{\theta}} 为指数族分布,则 ∇2ℓ(θ;y)\nabla^{2}\ell({\bm{\theta}};{\bm{y}}) 实际上与 y{\bm{y}} 无关。再借助 Bartlett 第二恒等式(式 12.3),即可得到From Proposition 3.4.5, if pθp_{\bm{\theta}} is an exponential family distribution, ∇2ℓ(θ;y)\nabla^{2}\ell({\bm{\theta}};{\bm{y}}) is actually independent of y{\bm{y}}. Using Bartlett’s second identity Eq. 12.3, we then obtain

∇2ℓ(θ;⋅)\displaystyle\nabla^{2}\ell({\bm{\theta}};\cdot) =EY∼pθ[∇2ℓ(θ;Y)]\displaystyle=\mathbb{E}_{Y\sim p_{\bm{\theta}}}[\nabla^{2}\ell({\bm{\theta}};Y)]
=EY∼pθ[−∇θ2log⁡pθ(Y)]\displaystyle=\mathbb{E}_{Y\sim p_{\bm{\theta}}}[-\nabla^{2}_{\bm{\theta}}\log p_{\bm{\theta}}(Y)]
=EY∼pθ[∇θlog⁡pθ(Y)⊗∇θlog⁡pθ(Y)]\displaystyle=\mathbb{E}_{Y\sim p_{\bm{\theta}}}[\nabla_{\bm{\theta}}\log p_{\bm{\theta}}(Y)\otimes\nabla_{\bm{\theta}}\log p_{\bm{\theta}}(Y)]
=EY∼pθ[∇ℓ(θ;Y)⊗∇ℓ(θ;Y)],\displaystyle=\mathbb{E}_{Y\sim p_{\bm{\theta}}}[\nabla\ell({\bm{\theta}};Y)\otimes\nabla\ell({\bm{\theta}};Y)],

这里用 ⋅\cdot 表示该结果对一切 y{\bm{y}} 都成立。将此结果代回 Fisher 信息矩阵,证明即告完成。where we used ⋅\cdot to indicate that the result holds for all y{\bm{y}}. Plugging the result back in the Fisher information matrix concludes the proof.

9.4 逆 Hessian—向量乘积9.4 Inverse-Hessian vector product

9.4.1 作为线性映射的定义Definition as a linear map

我们将在 17.1 节看到,牛顿法的迭代格式为We will see in Section 17.1 that Newton’s method uses iterations as

wt+1\displaystyle{\bm{w}}^{t+1} =wt−∇2L(wt)−1∇L(wt).\displaystyle={\bm{w}}^{t}-\nabla^{2}L({\bm{w}}^{t})^{-1}\nabla L({\bm{w}}^{t}).

例如,当 LL 严格凸时,其逆是良定义的;否则可引入额外的正则化。因此,牛顿法需要用到下面定义的逆Hessian 矩阵—向量乘积(IHVP)。The inverse is well-defined if for example LL is strictly convex. Otherwise, some additional regularization can be added. Newton’s method therefore requires access to inverse-Hessian vector products (IHVPs), as defined below.

定义 9.4.1(逆 Hessian—向量乘积)。Definition 9.4.1 (Inverse-Hessian vector product).
对于二次可微函数 L:RP→RL:\mathbb{R}^{P}\rightarrow\mathbb{R},我们把 LL 在 w∈RP{\bm{w}}\in\mathbb{R}^{P} 处的逆 Hessian-向量乘积(IHVP)定义为如下线性映射For a twice differentiable function L:RP→RL:\mathbb{R}^{P}\rightarrow\mathbb{R}, we define the inverse-Hessian Vector Product (IHVP) of LL at w∈RP{\bm{w}}\in\mathbb{R}^{P} as the linear map u↦∇2L(w)−1u,{\bm{u}}\mapsto\nabla^{2}L({\bm{w}})^{-1}{\bm{u}}, 前提是该映射存在。换言之,它是这样一个线性映射:将 u{\bm{u}} 映为 v{\bm{v}},且满足 ∇2L(w)v=u\nabla^{2}L({\bm{w}}){\bm{v}}={\bm{u}}。provided that it exists. In other words, it is the linear map which to u{\bm{u}} associates v{\bm{v}} such that ∇2L(w)v=u\nabla^{2}L({\bm{w}}){\bm{v}}={\bm{u}}.

9.4.2 用无矩阵线性求解器实现Implementation with matrix-free linear solvers

计算矩阵之逆有许多直接方法,如 Cholesky 分解、QR 分解与高斯消元法。然而这些算法都需要访问矩阵的具体元素,而自动微分框架通过 HVP 提供对Hessian 矩阵的访问。所幸存在所谓的无矩阵算法,它们无需访问矩阵元素即可求解线性方程组Numerous direct methods exist to compute the inverse of a matrix, such as the Cholesky decomposition, QR decomposition and Gaussian elimination. However, these algorithms require accessing elementary entries of the matrix, while an autodiff framework gives access to the Hessian through HVPs. Fortunately, there exist so-called matrix-free algorithms, that can solve a linear system of equations

H[v]=uH[{\bm{v}}]={\bm{u}}

并且只需能够对任意 v{\bm{v}} 求取线性映射 v↦H[v]{\bm{v}}\mapsto H[{\bm{v}}]。此类算法中,共轭梯度(CG)法适用于 HH 为正定的情形,即对一切 v≠0{\bm{v}}\neq 0 都有 ⟨v,H[v]⟩>0\langle{\bm{v}},H[{\bm{v}}]\rangle>0;广义最小残差(GMRES)法则适用于任意可逆的 HH。更完整的求解器名录可见于 SciPy [203] 等公开软件。因此,对于严格凸函数(其 Hessian 矩阵必为正定),只需将 CG 作用于 HVP,便可算得相应的 IHVP,by only accessing the linear map v↦H[v]{\bm{v}}\mapsto H[{\bm{v}}] for any v{\bm{v}}. Among such algorithms, we have the conjugate gradient (CG) method, that applies for HH positive-definite, i.e., such that ⟨v,H[v]⟩>0\langle{\bm{v}},H[{\bm{v}}]\rangle>0 for all v≠0{\bm{v}}\neq 0, or the generalized minimal residual (GMRES) method, that applies for any invertible HH. A longer list of solvers can be found in public software such as SciPy [203]. The IHVP of a strictly convex function (ensuring that the Hessian is positive definite) can therefore be computed by instantiating CG on the HVP,

∇2L(w)−1u≈CG(∇2L(w)[⋅],u).\nabla^{2}L({\bm{w}})^{-1}{\bm{u}}\approx\mathrm{CG}(\nabla^{2}L({\bm{w}})[\cdot],{\bm{u}}).

Hessian 矩阵的正定性对严格凸函数而言确实是有保证的;而对于一般非凸函数,这种性质或许只在极小值点附近成立,一般而言并不成立。算法 1 以最简单的形式回顾了共轭梯度法。理论上,经过至多 T=PT=P 次 CG 迭代即可求得线性系统的精确解;不过在实际中,数值误差可能使人无法得到精确解。Positive-definiteness of the Hessian is indeed guaranteed for strictly convex functions for example, while for generic nonconvex functions, such a property may be verified around a minimizer but not in general. The conjugate gradient method is recalled in Algorithm 1 in its simplest form. In theory, the exact solution of the linear system is found after at most T=PT=P iterations of CG, though in practice numerical errors may prevent one from getting an exact solution.

算法 1 共轭梯度法Algorithm 1 Conjugate gradient method
1: Inputs: linear map H[⋅]:RP→RPH[\cdot]:\mathbb{R}^{P}\rightarrow\mathbb{R}^{P}, target u∈RP{\bm{u}}\in\mathbb{R}^{P}, initialization v0{\bm{v}}_{0} (default 0{\mathbf{0}}), number of iterations TT (default PP), target accuracy ε\varepsilon (default machine precision)
2: r0=u−H[v0]{\bm{r}}_{0}={\bm{u}}-H[{\bm{v}}_{0}]
3: p0=r0{\bm{p}}_{0}={\bm{r}}_{0}
4: for t=0,…,T−1t=0,\ldots,T-1 do
5:   αt=⟨rt,rt⟩⟨pt,H[pt]⟩\alpha_{t}=\frac{\langle{\bm{r}}_{t},{\bm{r}}_{t}\rangle}{\langle{\bm{p}}_{t},H[{\bm{p}}_{t}]\rangle}
6:   vt+1=vt+αtpt{\bm{v}}_{t+1}={\bm{v}}_{t}+\alpha_{t}{\bm{p}}_{t}
7:   rt+1=rt−αtH[pt]{\bm{r}}_{t+1}={\bm{r}}_{t}-\alpha_{t}H[{\bm{p}}_{t}]
8:   if ⟨rt+1,rt+1⟩≤ε\langle{\bm{r}}_{t+1},{\bm{r}}_{t+1}\rangle\leq\varepsilon then break
9:   βt=⟨rt+1,rt+1⟩⟨rt,rt⟩\beta_{t}=\frac{\langle{\bm{r}}_{t+1},{\bm{r}}_{t+1}\rangle}{\langle{\bm{r}}_{t},{\bm{r}}_{t}\rangle}
10:   pt+1=rt+1+βtpt{\bm{p}}_{t+1}={\bm{r}}_{t+1}+\beta_{t}{\bm{p}}_{t}
11: Output: vT{\bm{v}}_{T}, such that H[vT]≈uH[{\bm{v}}_{T}]\approx{\bm{u}}

9.4.3 复杂度Complexity

对于给定的矩阵 H∈RP×PH\in\mathbb{R}^{P\times P},求解 Hv=uH{\bm{v}}={\bm{u}} 可通过分解方法(LU、QR、Cholesky)在 O(P3)O(P^{3}) 时间内完成。对于 CG 或 GMRES 这类无矩阵方法,每次迭代的代价为 O(P2)O(P^{2})。理论上,它们至多经过 O(P)O(P) 次迭代即可求解该线性系统,因此获得精确解的理论代价与之相同,也是 O(P3)O(P^{3})。For a given matrix H∈RP×PH\in\mathbb{R}^{P\times P}, solving Hv=uH{\bm{v}}={\bm{u}} can be done with decomposition methods (LU, QR, Cholesky) in O(P3)O(P^{3}) time. For matrix-free methods such as CG or GMRES, the cost per iteration is O(P2)O(P^{2}). Since they theoretically solve the linear system in O(P)O(P) iterations, the cost to obtain an exact solution is theoretically the same, O(P3)O(P^{3}).

然而,CG 或 GMRES 与分解方法的区别在于,前者是迭代方法,这意味着每进行一次迭代,它们都会更接近解。与分解方法不同,这意味着我们可以在找到精确解之前停止迭代。在实际中,找到一个良好近似解所需的迭代次数取决于矩阵本身。良态的矩阵只需少量迭代。病态的矩阵会使 CG 出现一些数值不稳定,因此可能需要超过 PP 次迭代才能得到良好的解。相比之下,分解方法分两步进行:首先以 O(P3)O(P^{3}) 的代价构建 HH 的一个分解,然后利用该结构以 O(P2)O(P^{2}) 的代价求解一个线性系统。众所周知,LU 与 QR 分解通常更为稳定,因此当我们能够无代价地访问 HH 的元素时,实际中往往更倾向于使用它们。However, CG or GMRES differ from decomposition methods in that they are iterative methods, meaning that, at each iteration, they get closer to a solution. Unlike decomposition methods, this means that we can stop them before an exact solution is found. In practice, the number of iterations required to find a good approximate solution depends on the matrix. Well conditioned matrices require only few iterations. Badly conditioned matrices lead to some numerical instabilities for CG, so that more than PP iterations may be needed to get a good solution. In contrast, decomposition methods proceed in two steps: first they build a decomposition of HH at a cost of O(P3)O(P^{3}), and second they solve a linear system at a cost of O(P2)O(P^{2}), by leveraging the structure. LU and QR decompositions are known to be generally more stable and are therefore often preferred in practice, when we can access entries of HH at no cost.

如果我们无法访问Hessian 矩阵 HH,而只能访问其 HVP,那么访问 HH 的元素将付出高昂的代价。确实,HH 的元素仍可由 HVP 恢复,因为 ei⊤Hej=Hi,j{\bm{e}}_{i}^{\top}H{\bm{e}}_{j}=H_{i,j},但访问 HH 的每一行或每一列都需要一次 HVP(矩阵—向量乘积)。因此,为了获得使用分解方法所需的信息,我们需要进行 PP 次 HVP 调用,之后才能真正计算出解。而在相同调用次数下,CG 或 GMRES 早已找到一个近似解。此外,CG 方法不需要将矩阵存储在内存中。If we do not have access to the Hessian HH, but only to its HVP, accessing entries of HH comes at a prohibitive cost. Indeed, entries of HH can still be recovered from HVPs, since ei⊤Hej=Hi,j{\bm{e}}_{i}^{\top}H{\bm{e}}_{j}=H_{i,j}, but accessing each row or column of HH costs one HVP (matrix-vector product). To access the information necessary to use a decomposition method, we therefore need PP calls to HVPs before being able to actually compute the solution. For the same number of calls, CG or GMRES will already have found an approximate solution. In addition, a CG method does not require storing the matrix in memory.

9.5 二阶反向传播9.5 Second-order backpropagation

9.5.1 二阶雅可比矩阵的链式法则Second-order Jacobian chain rule

发展前向模式与反向模式自动微分的核心要素,依赖于复合函数的链式法则,h=g∘fh=g\circ f。对于二阶导数,也可以得到类似的法则。为此,我们略微滥用记号,记The essential ingredient to develop forward-mode and reverse-mode autodiff hinged upon the chain rule for composed functions, h=g∘fh=g\circ f. For second derivatives, a similar rule can be obtained. To do so, we slightly abuse notation and denote

∂2h(w)∗[u]:=∇2⟨h,u⟩(w)∈RP×P,\partial^{2}h({\bm{w}})^{*}[{\bm{u}}]\coloneqq\nabla^{2}\langle h,{\bm{u}}\rangle({\bm{w}})\in\mathbb{R}^{P\times P},

其中 h:RP→RQh:\mathbb{R}^{P}\rightarrow\mathbb{R}^{Q}、w∈RP{\bm{w}}\in\mathbb{R}^{P}、u∈RQ{\bm{u}}\in\mathbb{R}^{Q},此处我们回顾简写记号 ⟨u,h⟩(w):=⟨u,h(w)⟩\langle{\bm{u}},h\rangle({\bm{w}})\coloneqq\langle{\bm{u}},h({\bm{w}})\rangle。此外,我们把上述量视为一个线性映射。严格来说,上标 ∗ 已不再是线性伴随,因为 v1,v2↦∂2h(w)[v1,v2]{\bm{v}}_{1},{\bm{v}}_{2}\mapsto\partial^{2}h({\bm{w}})[{\bm{v}}_{1},{\bm{v}}_{2}] 不再是线性的,而是双线性的。不过,这个上标所起的作用与 VJP 相同:它接收一个输出向量,并返回与该输出向量方向上的无穷小变化相对应的输入导数。where h:RP→RQh:\mathbb{R}^{P}\rightarrow\mathbb{R}^{Q}, w∈RP{\bm{w}}\in\mathbb{R}^{P}, u∈RQ{\bm{u}}\in\mathbb{R}^{Q}, and where we recall the shorthand notation ⟨u,h⟩(w):=⟨u,h(w)⟩\langle{\bm{u}},h\rangle({\bm{w}})\coloneqq\langle{\bm{u}},h({\bm{w}})\rangle. Moreover, we view the above quantity as a linear map. Strictly speaking, the superscript ∗ is not a linear adjoint anymore, since v1,v2↦∂2h(w)[v1,v2]{\bm{v}}_{1},{\bm{v}}_{2}\mapsto\partial^{2}h({\bm{w}})[{\bm{v}}_{1},{\bm{v}}_{2}] is no longer linear but bilinear. However, this superscript plays the same role as the VJP, since it takes an output vector and returns the input derivatives that correspond to infinitesimal variations along that output vector.

命题 9.5.1(Hessian 矩阵链式法则)。Proposition 9.5.1 (Hessian chain rule).
对于两个二次可微函数 f:RP→RMf:\mathbb{R}^{P}\rightarrow\mathbb{R}^{M} 与 g:RM→RQg:\mathbb{R}^{M}\rightarrow\mathbb{R}^{Q},复合函数 g∘fg\circ f 沿输入方向 v1,v2∈RP{\bm{v}}_{1},{\bm{v}}_{2}\in\mathbb{R}^{P} 的二阶方向导数是从 RP×RP\mathbb{R}^{P}\times\mathbb{R}^{P} 到 RQ\mathbb{R}^{Q} 的双线性映射,其形式为For two twice differentiable functions f:RP→RMf:\mathbb{R}^{P}\rightarrow\mathbb{R}^{M} and g:RM→RQg:\mathbb{R}^{M}\rightarrow\mathbb{R}^{Q}, the second directional derivative of the composition g∘fg\circ f is a bilinear map from RP×RP\mathbb{R}^{P}\times\mathbb{R}^{P} to RQ\mathbb{R}^{Q} along input directions v1,v2∈RP{\bm{v}}_{1},{\bm{v}}_{2}\in\mathbb{R}^{P} of the form ∂2(g∘f)(w)[v1,v2]\displaystyle\partial^{2}(g\circ f)({\bm{w}})[{\bm{v}}_{1},{\bm{v}}_{2}] =∂g(f(w))[∂2f(w)[v1,v2]]\displaystyle=\partial g(f({\bm{w}}))[\partial^{2}f({\bm{w}})[{\bm{v}}_{1},{\bm{v}}_{2}]] +∂2g(f(w))[∂f(w)[v1],∂f(w)[v2]].\displaystyle\quad+\partial^{2}g(f({\bm{w}}))[\partial f({\bm{w}})[{\bm{v}}_{1}],\partial f({\bm{w}})[{\bm{v}}_{2}]]. 复合函数 g∘fg\circ f 沿输出方向 u∈RQ{\bm{u}}\in\mathbb{R}^{Q} 的 Hessian 矩阵,视为线性映射时具有如下形式The Hessian of the composition g∘fg\circ f along an output direction u∈RQ{\bm{u}}\in\mathbb{R}^{Q} is, seen as a linear map, ∂2(g∘f)(w)∗[u]\displaystyle\partial^{2}(g\circ f)({\bm{w}})^{*}[{\bm{u}}] =∂2f(w)∗[∂g(f(w))∗[u]]\displaystyle=\partial^{2}f({\bm{w}})^{*}[\partial g(f({\bm{w}}))^{*}[{\bm{u}}]] (9.2) +∂f(w)∗∂2g(f(w))∗[u]∂f(w).\displaystyle\quad+\partial f({\bm{w}})^{*}\partial^{2}g(f({\bm{w}}))^{*}[{\bm{u}}]\partial f({\bm{w}}). 将 f ⁣:RP→RMf\colon\mathbb{R}^{P}\to\mathbb{R}^{M} 与标量值函数 ℓ ⁣:RM→R\ell\colon\mathbb{R}^{M}\to\mathbb{R} 复合,其矩阵形式为For the composition of f ⁣:RP→RMf\colon\mathbb{R}^{P}\to\mathbb{R}^{M} with a scalar-valued function ℓ ⁣:RM→R\ell\colon\mathbb{R}^{M}\to\mathbb{R}, we have in matrix form ∇2(ℓ∘f)(w)\displaystyle\nabla^{2}(\ell\circ f)({\bm{w}}) =∑j=1M(∇ℓ(f(w)))j∇2fj(w)\displaystyle=\sum_{j=1}^{M}(\nabla\ell(f({\bm{w}})))_{j}\nabla^{2}f_{j}({\bm{w}}) +∂f(w)⊤∇2ℓ(f(w))∂f(w).\displaystyle\quad+\bm{\partial}f({\bm{w}})^{\top}\nabla^{2}\ell(f({\bm{w}}))\bm{\partial}f({\bm{w}}).

请注意,Hessian 矩阵通常针对标量值函数 h ⁣:RP→Rh\colon\mathbb{R}^{P}\to\mathbb{R} 定义,而上述定义针对的是一种广义的 Hessian 概念,它适用于任意函数 h ⁣:RP→RQh\colon\mathbb{R}^{P}\to\mathbb{R}^{Q}。Note that, while the Hessian is usually defined for scalar-valued functions h ⁣:RP→Rh\colon\mathbb{R}^{P}\to\mathbb{R}, the above definition is for a generalized notion of Hessian that works for any function h ⁣:RP→RQh\colon\mathbb{R}^{P}\to\mathbb{R}^{Q}.

式 9.2 中的 Hessian 反向传播法则包含两项。第一项 ∂2f(w)∗[∂g(f(w))∗[u]]\partial^{2}f({\bm{w}})^{*}[\partial g(f({\bm{w}}))^{*}[{\bm{u}}]] 沿输出方向计算中间函数的 Hessian 矩阵,而该方向通常由 VJP 反向传播。第二项 ∂f(w)∗∂2g(f(w))∗[u]∂f(w)\partial f({\bm{w}})^{*}\partial^{2}g(f({\bm{w}}))^{*}[{\bm{u}}]\partial f({\bm{w}}) 则刻画了中间的一阶变化如何影响输出的二阶导数。The Hessian backpropagation rule in Eq. 9.2 reveals two terms. The first one ∂2f(w)∗[∂g(f(w))∗[u]]\partial^{2}f({\bm{w}})^{*}[\partial g(f({\bm{w}}))^{*}[{\bm{u}}]] simply computes the Hessian of the intermediate function along the output direction normally backpropagated by a VJP. The second term ∂f(w)∗∂2g(f(w))∗[u]∂f(w)\partial f({\bm{w}})^{*}\partial^{2}g(f({\bm{w}}))^{*}[{\bm{u}}]\partial f({\bm{w}}) shows how intermediate first-order variations influence second-order derivatives of the output.

例 9.5.2(与逐元素非线性函数的复合)。Example 9.5.2 (Composition with an elementwise nonlinear function).
考虑二次可微标量值函数 f(x)=(f(xi))i=1Mf({\bm{x}})=(f(x_{i}))_{i=1}^{M} 的逐元素应用,随后再作用某个二次可微函数 ℓ\ell。注意 ∇2fi(x)=f′′(xi)eiei⊤\nabla^{2}f_{i}({\bm{x}})=f^{\prime\prime}(x_{i}){\bm{e}}_{i}{\bm{e}}_{i}^{\top}。因此,复合函数的 Hessian 矩阵为Consider the element-wise application of a twice differentiable scalar-valued function f(x)=(f(xi))i=1Mf({\bm{x}})=(f(x_{i}))_{i=1}^{M} followed by some twice differentiable function ℓ\ell. Note that ∇2fi(x)=f′′(xi)eiei⊤\nabla^{2}f_{i}({\bm{x}})=f^{\prime\prime}(x_{i}){\bm{e}}_{i}{\bm{e}}_{i}^{\top}. Hence, the Hessian of the composition reads ∇2(ℓ∘f)(x)\displaystyle\nabla^{2}(\ell\circ f)({\bm{x}}) =∑i=1M(∇ℓ(f(x)))if′′(xi)eiei⊤\displaystyle=\sum_{i=1}^{M}(\nabla\ell(f({\bm{x}})))_{i}f^{\prime\prime}(x_{i}){\bm{e}}_{i}{\bm{e}}_{i}^{\top} +Diag⁡(f′(x))∇2ℓ(f(x))Diag⁡(f′(x))\displaystyle\quad+\operatorname{\bf Diag}(f^{\prime}({\bm{x}}))\nabla^{2}\ell(f({\bm{x}}))\operatorname{\bf Diag}(f^{\prime}({\bm{x}})) =Diag⁡(∇ℓ(f(x))⊙f′′(x))\displaystyle=\operatorname{\bf Diag}(\nabla\ell(f({\bm{x}}))\odot f^{\prime\prime}({\bm{x}})) +∇2ℓ(f(x))⊙(f′(x)f′(x)⊤),\displaystyle\quad+\nabla^{2}\ell(f({\bm{x}}))\odot(f^{\prime}({\bm{x}})f^{\prime}({\bm{x}})^{\top}), 其中 f′(x):=(f′(xi))i=1Mf^{\prime}({\bm{x}})\coloneqq(f^{\prime}(x_{i}))_{i=1}^{M},f′′(x):=(f′′(xi))i=1Mf^{\prime\prime}({\bm{x}})\coloneqq(f^{\prime\prime}(x_{i}))_{i=1}^{M}。where f′(x):=(f′(xi))i=1Mf^{\prime}({\bm{x}})\coloneqq(f^{\prime}(x_{i}))_{i=1}^{M} and f′′(x):=(f′′(xi))i=1Mf^{\prime\prime}({\bm{x}})\coloneqq(f^{\prime\prime}(x_{i}))_{i=1}^{M}.
例 9.5.3(与线性函数复合后的 Hessian 矩阵)。Example 9.5.3 (Hessian of the composition with a linear function).
考虑线性函数 f(W)=Wxf({\bm{W}})={\bm{W}}{\bm{x}}(其中 W∈RM×D{\bm{W}}\in\mathbb{R}^{M\times D}),它与某个二次可微函数 ℓ:RM→R\ell:\mathbb{R}^{M}\rightarrow\mathbb{R} 复合。由命题 9.5.1,以线性映射表示可得Consider a linear function f(W)=Wxf({\bm{W}})={\bm{W}}{\bm{x}}, for W∈RM×D{\bm{W}}\in\mathbb{R}^{M\times D}, composed with some twice differentiable function ℓ:RM→R\ell:\mathbb{R}^{M}\rightarrow\mathbb{R}. From Proposition 9.5.1, we get, in terms of linear maps, ∇2(ℓ∘f)(W)=∂f(W)∗∇2ℓ(f(W))∂f(W).\displaystyle\nabla^{2}(\ell\circ f)({\bm{W}})=\partial f({\bm{W}})^{*}\nabla^{2}\ell(f({\bm{W}}))\partial f({\bm{W}}). 正如第 2.3 节已指出的,∂f(W)[V]=Vx\partial f({\bm{W}})[{\bm{V}}]={\bm{V}}{\bm{x}},且 ∂f(W)∗[u]=ux⊤\partial f({\bm{W}})^{*}[{\bm{u}}]={\bm{u}}{\bm{x}}^{\top}。因此,视为线性映射的 Hessian 矩阵为As already noted in Section 2.3, we have that ∂f(W)[V]=Vx\partial f({\bm{W}})[{\bm{V}}]={\bm{V}}{\bm{x}} and ∂f(W)∗[u]=ux⊤\partial f({\bm{W}})^{*}[{\bm{u}}]={\bm{u}}{\bm{x}}^{\top}. Hence, the Hessian seen as a linear map reads ∇2(ℓ∘f)(W)[V]\displaystyle\nabla^{2}(\ell\circ f)({\bm{W}})[{\bm{V}}] =∂f(W)∗[∇2ℓ(f(W))[∂f(W)[V]]]\displaystyle=\partial f({\bm{W}})^{*}[\nabla^{2}\ell(f({\bm{W}}))[\partial f({\bm{W}})[{\bm{V}}]]] =HVxx⊤,\displaystyle={\bm{H}}{\bm{V}}{\bm{x}}{\bm{x}}^{\top}, 其中 H:=∇2ℓ(f(W)){\bm{H}}\coloneqq\nabla^{2}\ell(f({\bm{W}}))。where H:=∇2ℓ(f(W)){\bm{H}}\coloneqq\nabla^{2}\ell(f({\bm{W}})).

9.5.2 计算链Computation chains

对于 8.1 节中那样的简单计算链 f=fK∘…∘f1f=f_{K}\circ\ldots\circ f_{1},命题 9.5.1 推导出的公式已足以给出一种反向传播 Hessian 矩阵的算法,如算法 2 所示。与算法 2 相比,我们只是借助中间的一阶与二阶导数,同时反向传播向量 rk{\bm{r}}_{k} 与矩阵 Rk\bm{R}_{k}。For a simple computation chain f=fK∘…∘f1f=f_{K}\circ\ldots\circ f_{1} as in Section 8.1, the formula derived in Proposition 9.5.1 suffices to develop an algorithm that backpropagates the Hessian, as shown in Algorithm 2. Compared to Algorithm 2, we simply backpropagate both the vectors rk{\bm{r}}_{k} and the matrices Rk\bm{R}_{k} using intermediate first and second derivatives.

算法 2 计算链的 Hessian 反向传播Algorithm 2 Hessian backprop for computation chains
1: Functions: ℓ∘f\ell\circ f with f:=fK∘…∘f1f\coloneqq f_{K}\circ\ldots\circ f_{1},
2: Inputs: input x{\bm{x}}
3: Initialize and store s0:=x{\bm{s}}_{0}\coloneqq{\bm{x}} ▹\triangleright Forward pass
4: for k:=1,…,Kk\coloneqq 1,\ldots,K do
5:   Compute and store sk:=fk(sk−1){\bm{s}}_{k}\coloneqq f_{k}({\bm{s}}_{k-1})
6: Initialize rK:=∇ℓ(sK), RK:=∇2ℓ(sK){\bm{r}}_{K}\coloneqq\nabla\ell({\bm{s}}_{K}),\ \bm{R}_{K}\coloneqq\nabla^{2}\ell({\bm{s}}_{K}) ▹\triangleright Backward pass
7: for k:=K,…,1k\coloneqq K,\ldots,1 do
8:   Compute rk−1:=∂fk(sk−1)∗[rk]{\bm{r}}_{k-1}\coloneqq\partial f_{k}({\bm{s}}_{k-1})^{*}[{\bm{r}}_{k}]
9:   Compute Rk−1:=∂2fk(sk−1)∗[rk]+∂fk(sk−1)∗Rk∂fk(sk−1)\bm{R}_{k-1}\coloneqq\partial^{2}f_{k}({\bm{s}}_{k-1})^{*}[{\bm{r}}_{k}]+\partial f_{k}({\bm{s}}_{k-1})^{*}\bm{R}_{k}\partial f_{k}({\bm{s}}_{k-1})
10:   Release sk−1{\bm{s}}_{k-1} from memory
11: Outputs: ℓ(f(x))=ℓ(sK)\ell(f({\bm{x}}))=\ell({\bm{s}}_{K}), ∇(ℓ∘f)(x)=r0\nabla(\ell\circ f)({\bm{x}})={\bm{r}}_{0}, ∇2(ℓ∘f)(x)=R0\nabla^{2}(\ell\circ f)({\bm{x}})=\bm{R}_{0}

9.5.3 扇入与扇出Fan-in and fan-out

对于一般计算图(见 8.3 节),我们已经看到多输入函数(扇入)至关重要。因此,要在计算图中进行Hessian 矩阵反向传播,我们也需要推导出类似的公式。For generic computation graphs (see Section 8.3), we saw that multi-input functions (fan-in) were crucial. For Hessian backpropagation in computation graphs, we therefore need to develop a similar formula.

命题 9.5.4(扇入的Hessian 矩阵链式法则)。Proposition 9.5.4 (Hessian chain rule for fan-in).
考虑 n+1n+1 个二次可微函数 f1,…,fnf_{1},\ldots,f_{n} 与 gg,满足 fi:RP→RMif_{i}:\mathbb{R}^{P}\rightarrow\mathbb{R}^{M_{i}} 与 g:RM1×…×RMn→RQg:\mathbb{R}^{M_{1}}\times\ldots\times\mathbb{R}^{M_{n}}\rightarrow\mathbb{R}^{Q}。g∘fg\circ f 在 f(w)=(f1(w),…,fn(w))f({\bm{w}})=(f_{1}({\bm{w}}),\ldots,f_{n}({\bm{w}})) 处沿输出方向 u∈RQ{\bm{u}}\in\mathbb{R}^{Q} 的 Hessian 矩阵由下式给出Consider n+1n+1 twice differentiable functions f1,…,fnf_{1},\ldots,f_{n} and gg with fi:RP→RMif_{i}:\mathbb{R}^{P}\rightarrow\mathbb{R}^{M_{i}} and g:RM1×…×RMn→RQg:\mathbb{R}^{M_{1}}\times\ldots\times\mathbb{R}^{M_{n}}\rightarrow\mathbb{R}^{Q}. The Hessian of g∘fg\circ f for f(w)=(f1(w),…,fn(w))f({\bm{w}})=(f_{1}({\bm{w}}),\ldots,f_{n}({\bm{w}})) along an output direction u∈RQ{\bm{u}}\in\mathbb{R}^{Q} is given by ∂2(g∘f)(w)∗[u]\displaystyle\partial^{2}(g\circ f)({\bm{w}})^{*}[{\bm{u}}] =∑i=1n∂2fi(w)∗[∂ig(f(w))∗[u]]\displaystyle=\sum_{i=1}^{n}\partial^{2}f_{i}({\bm{w}})^{*}[\partial_{i}g(f({\bm{w}}))^{*}[{\bm{u}}]] +∑i,j=1n∂fi(w)∗∂i,j2g(f(w))∗[u]∂fj(w).\displaystyle\quad+\sum_{i,j=1}^{n}\partial f_{i}({\bm{w}})^{*}\partial^{2}_{i,j}g(f({\bm{w}}))^{*}[{\bm{u}}]\partial f_{j}({\bm{w}}).

扇入的梯度反向传播表达式之所以简单,是因为各函数 fif_{i} 之间没有任何路径相连。相比之下,Hessian 矩阵反向传播则涉及 i≠ji\neq j 的交叉乘积项 ∂fi(w)∗∂i,j2g(f(w))∗[u]∂fj(w)\partial f_{i}({\bm{w}})^{*}\partial^{2}_{i,j}g(f({\bm{w}}))^{*}[{\bm{u}}]\partial f_{j}({\bm{w}}),与 fif_{i} 计算相关的节点再也无法被独立处理。The gradient backpropagation expression for fan-in is simple because the functions fif_{i} are not linked by any path. In contrast, the Hessian backpropagation involves cross-product terms
∂fi(w)∗∂i,j2g(f(w))∗[u]∂fj(w)\partial f_{i}({\bm{w}})^{*}\partial^{2}_{i,j}g(f({\bm{w}}))^{*}[{\bm{u}}]\partial f_{j}({\bm{w}}) for i≠ji\neq j. The nodes associated with the fif_{i} computations cannot be treated independently anymore.

另一方面,为扇出推导反向传播法则毫无困难,因为每个输出函数都可以独立处理。On the other hand, developing a backpropagation rule for fan-out does not pose any issue, since each output function can be treated independently.

命题 9.5.5(扇出的Hessian 矩阵链式法则)。Proposition 9.5.5 (Hessian chain rule for fan-out).
考虑 n+1n+1 个二次可微函数 g1,…,gng_{1},\ldots,g_{n} 与 ff,其中 gi:RM→RQig_{i}:\mathbb{R}^{M}\rightarrow\mathbb{R}^{Q_{i}},f:RP→RMf:\mathbb{R}^{P}\rightarrow\mathbb{R}^{M}。则 g∘fg\circ f 在 g(y)=(g1(y),…,gn(y))g({\bm{y}})=(g_{1}({\bm{y}}),\ldots,g_{n}({\bm{y}})) 处沿方向 u=(u1,…,un)∈RQ1×…×RQn{\bm{u}}=({\bm{u}}_{1},\ldots,{\bm{u}}_{n})\in\mathbb{R}^{Q_{1}}\times\ldots\times\mathbb{R}^{Q_{n}} 的 Hessian 矩阵为Consider n+1n+1 twice differentiable functions g1,…,gng_{1},\ldots,g_{n} and ff with gi:RM→RQig_{i}:\mathbb{R}^{M}\rightarrow\mathbb{R}^{Q_{i}} and f:RP→RMf:\mathbb{R}^{P}\rightarrow\mathbb{R}^{M}. The Hessian of g∘fg\circ f for g(y)=(g1(y),…,gn(y))g({\bm{y}})=(g_{1}({\bm{y}}),\ldots,g_{n}({\bm{y}})) along a direction u=(u1,…,un)∈RQ1×…×RQn{\bm{u}}=({\bm{u}}_{1},\ldots,{\bm{u}}_{n})\in\mathbb{R}^{Q_{1}}\times\ldots\times\mathbb{R}^{Q_{n}} is given by ∂2(g∘f)(w)∗[u]\displaystyle\partial^{2}(g\circ f)({\bm{w}})^{*}[{\bm{u}}] =∑i=1n∂2f(w)∗[∂gi(f(w))∗[ui]]\displaystyle=\sum_{i=1}^{n}\partial^{2}f({\bm{w}})^{*}[\partial g_{i}(f({\bm{w}}))^{*}[{\bm{u}}_{i}]] +∑i=1n∂f(w)∗∂2gi(f(w))∗[ui]∂f(w).\displaystyle\quad+\sum_{i=1}^{n}\partial f({\bm{w}})^{*}\partial^{2}g_{i}(f({\bm{w}}))^{*}[{\bm{u}}_{i}]\partial f({\bm{w}}).

9.6 块对角近似9.6 Block diagonal approximations

与其计算完整的Hessian 矩阵或 Gauss-Newton 矩阵,我们可以考虑计算块对角近似或对角近似,它们更容易求逆。本节介绍的近似法则建立在 9.5 节所研究的Hessian 矩阵链式法则之上。Rather than computing the whole Hessian or Gauss-Newton matrices, we can consider computing block-diagonal or diagonal approximations, which are easier to invert. The approximation rules we present in this section build upon the Hessian chain rule studied in Section 9.5.

9.6.1 前馈网络Feedforward networks

回顾前馈网络的定义:Recall the definition of a feedforward network:

s0\displaystyle{\bm{s}}_{0} :=x\displaystyle\coloneqq{\bm{x}}
sk\displaystyle{\bm{s}}_{k} :=fk(sk−1,wk) ∀k∈{1,…,K}\displaystyle\coloneqq f_{k}({\bm{s}}_{k-1},{\bm{w}}_{k}) \ \forall k\in\{1,\ldots,K\}
f(x,w)\displaystyle f({\bm{x}},{\bm{w}}) :=sK,\displaystyle\coloneqq{\bm{s}}_{K},

其中w:=(w1,…,wK){\bm{w}}\coloneqq({\bm{w}}_{1},\dots,{\bm{w}}_{K})。我们可以不计算ℓ∘f\ell\circ f关于w{\bm{w}}的完整Hessian 矩阵,而分别计算关于各组参数wk{\bm{w}}_{k}的Hessian 矩阵。对于计算链的情形,我们在算法 2 中使用的Hessian 矩阵反向传播递推式为where w:=(w1,…,wK){\bm{w}}\coloneqq({\bm{w}}_{1},\dots,{\bm{w}}_{K}). Rather than computing the entire Hessian of ℓ∘f\ell\circ f w.r.t. w{\bm{w}}, we can compute the Hessians w.r.t. each set of parameters wk{\bm{w}}_{k}. For the case of computation chains, the Hessian backpropagation recursion we used in Algorithm 2 was

Rk−1:=∂2fk(sk−1)∗[rk]+∂fk(sk−1)∗Rk∂fk(sk−1).\bm{R}_{k-1}\coloneqq\partial^{2}f_{k}({\bm{s}}_{k-1})^{*}[{\bm{r}}_{k}]+\partial f_{k}({\bm{s}}_{k-1})^{*}\bm{R}_{k}\partial f_{k}({\bm{s}}_{k-1}).

将该递推式推广到前馈网络的情形,我们从rK:=∇ℓ(sK){\bm{r}}_{K}\coloneqq\nabla\ell({\bm{s}}_{K})和RK:=∇2ℓ(sK)\bm{R}_{K}\coloneqq\nabla^{2}\ell({\bm{s}}_{K})出发,可得Extending this recursion to the feedforward network case, we obtain, starting from rK:=∇ℓ(sK){\bm{r}}_{K}\coloneqq\nabla\ell({\bm{s}}_{K}) and RK:=∇2ℓ(sK)\bm{R}_{K}\coloneqq\nabla^{2}\ell({\bm{s}}_{K}),

rk−1\displaystyle{\bm{r}}_{k-1} :=∂fk(sk−1,wk)∗[rk]\displaystyle\coloneqq\partial f_{k}({\bm{s}}_{k-1},{\bm{w}}_{k})^{*}[{\bm{r}}_{k}]
(Rk−1∼∼Hk)\displaystyle\begin{pmatrix}\bm{R}_{k-1}&\sim\\ \sim&\bm{H}_{k}\end{pmatrix} :=∂2fk(sk−1,wk)∗[rk]\displaystyle\coloneqq\partial^{2}f_{k}({\bm{s}}_{k-1},{\bm{w}}_{k})^{*}[{\bm{r}}_{k}]
+∂fk(sk−1,wk)∗Rk∂fk(sk−1,wk),\displaystyle\quad+\partial f_{k}({\bm{s}}_{k-1},{\bm{w}}_{k})^{*}\bm{R}_{k}\partial f_{k}({\bm{s}}_{k-1},{\bm{w}}_{k}),

其中我们用∼\sim表示这些分块未被使用。于是,关于各组参数的 Hessian 矩阵即为where we used ∼\sim to indicate that these blocks are not used. The Hessians w.r.t. each set of parameters are then

R0\displaystyle\bm{R}_{0} =∇xx2(ℓ∘f)(x,w)\displaystyle=\nabla_{{\bm{x}}{\bm{x}}}^{2}(\ell\circ f)({\bm{x}},{\bm{w}})
H1\displaystyle{\bm{H}}_{1} =∇w1w12(ℓ∘f)(x,w)\displaystyle=\nabla_{{\bm{w}}_{1}{\bm{w}}_{1}}^{2}(\ell\circ f)({\bm{x}},{\bm{w}})
HK\displaystyle{\bm{H}}_{K} =∇wKwK2(ℓ∘f)(x,w).\displaystyle=\nabla_{{\bm{w}}_{K}{\bm{w}}_{K}}^{2}(\ell\circ f)({\bm{x}},{\bm{w}}).

这一结果之所以成立,是因为可以将关于wk{\bm{w}}_{k}的 Hessian 矩阵视为计算下式关于wk{\bm{w}}_{k}的 Hessian 矩阵:The validity of this result stems from the fact that we can view the Hessian w.r.t. wk{\bm{w}}_{k} as computing the Hessian w.r.t. wk{\bm{w}}_{k} of

f~K∘…∘f~k+1∘fk(sk−1,wk)\tilde{f}_{K}\circ\ldots\circ\tilde{f}_{k+1}\circ f_{k}({\bm{s}}_{k-1},{\bm{w}}_{k})

其中f~i:=fi(⋅,wi)\tilde{f}_{i}\coloneqq f_{i}(\cdot,{\bm{w}}_{i}),对i∈{k+1,…,K}i\in\{k+1,\dots,K\}。由于各分块Hessian 矩阵的计算共享了大部分计算量,因此它们可以像梯度一样,在一次反向传播中一并求值。where f~i:=fi(⋅,wi)\tilde{f}_{i}\coloneqq f_{i}(\cdot,{\bm{w}}_{i}), for i∈{k+1,…,K}i\in\{k+1,\dots,K\}. As the computations of the block-wise Hessians share most of the computations, they can be evaluated in a single backward pass just as the gradients.

例 9.6.1(Gauss-Newton 矩阵的分块计算)。Example 9.6.1 (Block-wise computation of the Gauss-Newton matrix).
我们的分块反向传播方案可直接调整用于Gauss-Newton 矩阵,即Our blockwise backpropagation scheme can readily be adapted for the Gauss-Newton matrix as (Rk−1∼∼Gk)\displaystyle\begin{pmatrix}\bm{R}_{k-1}&\sim\\ \sim&{\bm{G}}_{k}\end{pmatrix} :=∂fk(sk−1,wk)∗Rk∂fk(sk−1,wk),\displaystyle\coloneqq\partial f_{k}({\bm{s}}_{k-1},{\bm{w}}_{k})^{*}\bm{R}_{k}\partial f_{k}({\bm{s}}_{k-1},{\bm{w}}_{k}), 从 RK:=∇2ℓ(sK)\bm{R}_{K}\coloneqq\nabla^{2}\ell({\bm{s}}_{K}) 出发。所得输出 R0,G1,…,GK\bm{R}_{0},{\bm{G}}_{1},\ldots,{\bm{G}}_{K} 给出Gauss-Newton 矩阵的分块近似。starting from RK:=∇2ℓ(sK)\bm{R}_{K}\coloneqq\nabla^{2}\ell({\bm{s}}_{K}). The outputs R0,G1,…,GK\bm{R}_{0},{\bm{G}}_{1},\ldots,{\bm{G}}_{K} give a block-wise approximation of the Gauss-Newton matrix. 下面考虑一个简单的多层感知机,使得Now, consider a simple multilayer perceptron such that fk(sk−1,wk):=a(Wksk−1)withwk:=vec⁡(Wk)f_{k}({\bm{s}}_{k-1},{\bm{w}}_{k})\coloneqq{\bm{a}}({\bm{W}}_{k}{\bm{s}}_{k-1})\quad\text{with}\quad{\bm{w}}_{k}\coloneqq\operatorname{vec}({\bm{W}}_{k}) 借助适配于Gauss-Newton 矩阵的例 9.5.3 与例 9.5.2,可将Gauss-Newton 矩阵的分块分解计算为:对 k=K,…,1k=K,\ldots,1,有Using Example 9.5.3 and Example 9.5.2 adapted to the Gauss-Newton matrix, we can compute the block-wise decomposition of the Gauss-Newton matrix as, for k=K,…,1k=K,\ldots,1, Rk−1\displaystyle\bm{R}_{k-1} :=Wk⊤JkWk\displaystyle\coloneqq{\bm{W}}_{k}^{\top}{\bm{J}}_{k}{\bm{W}}_{k} Jk\displaystyle{\bm{J}}_{k} :=Rk⊙(a′(Wksk−1)a′(Wksk−1)⊤)\displaystyle\coloneqq\bm{R}_{k}\odot({\bm{a}}^{\prime}({\bm{W}}_{k}{\bm{s}}_{k-1}){\bm{a}}^{\prime}({\bm{W}}_{k}{\bm{s}}_{k-1})^{\top}) Gk\displaystyle{\bm{G}}_{k} :=Jk⊗sk−1sk−1⊤\displaystyle\coloneqq{\bm{J}}_{k}\otimes{\bm{s}}_{k-1}{\bm{s}}_{k-1}^{\top} 从 RK:=∇2ℓ(sK)\bm{R}_{K}\coloneqq\nabla^{2}\ell({\bm{s}}_{K}) 出发。输出 G1,…,GK{\bm{G}}_{1},\ldots,{\bm{G}}_{K} 对应于 ff 关于向量化权重 w1,…,wK{\bm{w}}_{1},\ldots,{\bm{w}}_{K} 的Gauss-Newton 矩阵的各分块元素。KFRA [31] 与 BackPack [50] 中也进行了类似计算。starting from RK:=∇2ℓ(sK)\bm{R}_{K}\coloneqq\nabla^{2}\ell({\bm{s}}_{K}). The outputs G1,…,GK{\bm{G}}_{1},\ldots,{\bm{G}}_{K} correspond to the block-wise elements of the Gauss-Newton matrix of ff for the vectorized weights w1,…,wK{\bm{w}}_{1},\ldots,{\bm{w}}_{K}. Similar computations were done in KFRA [31] and BackPack [50].

9.6.2 计算图Computation graphs

对于一般的计算图,考虑由下式定义的函数f(x,w)f({\bm{x}},{\bm{w}}),记i1,…,ipk:=pa⁡(k)i_{1},\ldots,i_{p_{k}}\coloneqq{\operatorname{pa}}(k),For generic computation graphs, consider a function f(x,w)f({\bm{x}},{\bm{w}}) defined by, denoting i1,…,ipk:=pa⁡(k)i_{1},\ldots,i_{p_{k}}\coloneqq{\operatorname{pa}}(k),

sk:=fk(si1,…,sipk)∀k∈{1,…,K}{\bm{s}}_{k}\coloneqq f_{k}({\bm{s}}_{i_{1}},\ldots,{\bm{s}}_{i_{p_{k}}})\quad\forall k\in\{1,\ldots,K\}

使得f(x,w)=sKf({\bm{x}},{\bm{w}})={\bm{s}}_{K},且kk依图的拓扑序排列(见 8.3 节)。对k=K,…,1k=K,\ldots,1和j∈pa⁡(k)j\in{\operatorname{pa}}(k),我们可以考虑如下反向传播方案:such that f(x,w)=sKf({\bm{x}},{\bm{w}})={\bm{s}}_{K}, and kk is following a topological ordering of the graph (see Section 8.3). We can consider the following backpropagation scheme, for k=K,…,1k=K,\ldots,1 and j∈pa⁡(k)j\in{\operatorname{pa}}(k)

rij\displaystyle{\bm{r}}_{i_{j}} ←rij+∂jfk(si1,…,sipk)∗[rk]\displaystyle\leftarrow{\bm{r}}_{i_{j}}+\partial_{j}f_{k}({\bm{s}}_{i_{1}},\ldots,{\bm{s}}_{i_{p_{k}}})^{*}[{\bm{r}}_{k}] (9.3)
Rij\displaystyle\bm{R}_{i_{j}} ←Rij+∂jj2fk(si1,…,sipk)∗[rk]\displaystyle\leftarrow\bm{R}_{i_{j}}+\partial^{2}_{jj}f_{k}({\bm{s}}_{i_{1}},\ldots,{\bm{s}}_{i_{p_{k}}})^{*}[{\bm{r}}_{k}]
+∂jfk(si1,…,sipk)∗Rk∂jfk(si1,…,sipk),\displaystyle\quad+\partial_{j}f_{k}({\bm{s}}_{i_{1}},\ldots,{\bm{s}}_{i_{p_{k}}})^{*}\bm{R}_{k}\partial_{j}f_{k}({\bm{s}}_{i_{1}},\ldots,{\bm{s}}_{i_{p_{k}}}), (9.4)

从RK:=∇2ℓ(sK)\bm{R}_{K}\coloneqq\nabla^{2}\ell({\bm{s}}_{K})和rK:=∇ℓ(sK){\bm{r}}_{K}\coloneqq\nabla\ell({\bm{s}}_{K})出发。回顾可知,对于多输入的情形,命题 9.5.4 中给出的链式法则涉及交叉导数。正因如此,式 9.3 中的反向传播方案只计算出一个近似结果。例如,可以验证,用式 9.3 计算ℓ(f1(w),f2(w))\ell(f_{1}({\bm{w}}),f_{2}({\bm{w}}))的Hessian 矩阵,并不能得到ff的Hessian 矩阵的精确表达式。该方案易于实现,并且可以为Hessian 矩阵提供一个有参考价值的替代量。starting from RK:=∇2ℓ(sK)\bm{R}_{K}\coloneqq\nabla^{2}\ell({\bm{s}}_{K}) and rK:=∇ℓ(sK){\bm{r}}_{K}\coloneqq\nabla\ell({\bm{s}}_{K}). Recall that for multiple inputs, the chain rule presented in Proposition 9.5.4 involves the cross-derivatives. For this reason the backpropagation scheme in Eq. 9.3 only computes an approximation. For example, one can verify that using Eq. 9.3 to compute the Hessian of ℓ(f1(w),f2(w))\ell(f_{1}({\bm{w}}),f_{2}({\bm{w}})) does not provide an exact expression for the Hessian of ff. This scheme is easy to implement and may provide a relevant proxy for the Hessian.

9.7 对角近似9.7 Diagonal approximations

与设计近似Hessian 矩阵各分块的反向传播方案这一思路类似,我们也可以设计一种近似Hessian 矩阵对角线的反向传播方案。该方法最初由 [20] 针对前馈网络提出,但据我们所知,我们的阐述是新的,其优点在于能自然地推广到计算图,下文即可看到。Similarly to the idea of designing a backpropagation scheme that approximates blocks of the Hessian, we can design a backpropagation scheme that approximates the diagonal of the Hessian. The approach was originally proposed by [20] for feedforward networks, but our exposition, new to our knowledge, has the benefit that it naturally extends to computational graphs, as we shall see.

9.7.1 计算链Computation chains

这一思路源于对命题 9.5.1 中 Hessian 反向传播法则的修改:只保留 Hessian 矩阵的对角线。形式上,给定矩阵 M∈RD×D{\bm{M}}\in\mathbb{R}^{D\times D},用 diag⁡(M)=(Mii)i=1D∈RD\operatorname{\bf diag}({\bm{M}})=({\bm{M}}_{ii})_{i=1}^{D}\in\mathbb{R}^{D} 表示由 M{\bm{M}} 的对角元素组成的向量;对于向量 m∈RD{\bm{m}}\in\mathbb{R}^{D},用 Diag⁡(m)=∑i=1Dmieiei⊤\operatorname{\bf Diag}({\bm{m}})=\sum_{i=1}^{D}m_{i}{\bm{e}}_{i}{\bm{e}}_{i}^{\top} 表示以 mim_{i} 为元素的对角矩阵。对于 ℓ∘fK∘…∘f1\ell\circ f_{K}\circ\ldots\circ f_{1} 的 Hessian 矩阵的反向传播,由算法 2 可知,diag⁡(Hk−1)\operatorname{\bf diag}({\bm{H}}_{k-1}) 可由 Hk{\bm{H}}_{k} 表示为The idea stems from modifying the Hessian backpropagation rule in Proposition 9.5.1 to only keep the diagonal of the Hessian. Formally, given a matrix M∈RD×D{\bm{M}}\in\mathbb{R}^{D\times D}, we denote by diag⁡(M)=(Mii)i=1D∈RD\operatorname{\bf diag}({\bm{M}})=({\bm{M}}_{ii})_{i=1}^{D}\in\mathbb{R}^{D} the vector of diagonal entries of M{\bm{M}}, and for a vector m∈RD{\bm{m}}\in\mathbb{R}^{D}, we denote Diag⁡(m)=∑i=1Dmieiei⊤\operatorname{\bf Diag}({\bm{m}})=\sum_{i=1}^{D}m_{i}{\bm{e}}_{i}{\bm{e}}_{i}^{\top} the diagonal matrix with entries mim_{i}. For the backpropagation of the Hessian of ℓ∘fK∘…∘f1\ell\circ f_{K}\circ\ldots\circ f_{1}, we see from Algorithm 2 that diag⁡(Hk−1)\operatorname{\bf diag}({\bm{H}}_{k-1}) can be expressed in terms of Hk{\bm{H}}_{k} as

diag⁡(Hk−1)=\displaystyle\operatorname{\bf diag}({\bm{H}}_{k-1})= diag⁡(∂2fk(sk−1)∗rk)\displaystyle\operatorname{\bf diag}(\partial^{2}f_{k}({\bm{s}}_{k-1})^{*}{\bm{r}}_{k})
+diag⁡(∂fk(sk−1)∗Hk∂fk(sk−1)).\displaystyle+\operatorname{\bf diag}(\bm{\partial}f_{k}({\bm{s}}_{k-1})^{*}{\bm{H}}_{k}\bm{\partial}f_{k}({\bm{s}}_{k-1})).

遗憾的是,这一递推式需要访问整个Hessian 矩阵 Hk{\bm{H}}_{k},代价因而过高。一个自然的想法是改造该递推式,通过反向传播向量来近似 diag⁡(Hk)\operatorname{\bf diag}({\bm{H}}_{k}):Unfortunately, that recursion needs access to the whole Hessian Hk{\bm{H}}_{k}, and would therefore be too expensive. A natural idea is to modify the recursion to approximate diag⁡(Hk)\operatorname{\bf diag}({\bm{H}}_{k}) by backpropagating vectors:

dk−1:=\displaystyle{\bm{d}}_{k-1}\coloneqq diag⁡(∂2fk(sk−1)∗rk)\displaystyle\operatorname{\bf diag}(\partial^{2}f_{k}({\bm{s}}_{k-1})^{*}{\bm{r}}_{k})
+diag⁡(∂fk(sk−1)∗Diag⁡(dk)∂fk(sk−1)).\displaystyle+\operatorname{\bf diag}(\bm{\partial}f_{k}({\bm{s}}_{k-1})^{*}\operatorname{\bf Diag}({\bm{d}}_{k})\bm{\partial}f_{k}({\bm{s}}_{k-1})).

对角矩阵 Diag⁡(dk)\operatorname{\bf Diag}({\bm{d}}_{k}) 充当 Hk{\bm{H}}_{k} 的替代量。该递推式的每次迭代都可在输出维度 DkD_{k} 上以线性时间完成计算,因为The diagonal matrix Diag⁡(dk)\operatorname{\bf Diag}({\bm{d}}_{k}) serves as a surrogate for Hk{\bm{H}}_{k}. Each iteration of this recursion can be computed in linear time in the output dimension DkD_{k} since

dk−1,i=\displaystyle d_{k-1,i}= ∑j=1Dkrk,j⋅∂i,i2fk,j(sk−1)+∑j=1Dkdk,j(∂ifk,j(sk−1))2.\displaystyle\sum_{j=1}^{D_{k}}r_{k,j}\cdot\partial_{i,i}^{2}f_{k,j}({\bm{s}}_{k-1})+\sum_{j=1}^{D_{k}}d_{k,j}(\partial_{i}f_{k,j}({\bm{s}}_{k-1}))^{2}.

为启动该递推,可令 dK:=diag⁡(∇2ℓ(sK)){\bm{d}}_{K}\coloneqq\operatorname{\bf diag}(\nabla^{2}\ell({\bm{s}}_{K}))。另一种做法由 [62] 提出:若 HK{\bm{H}}_{K} 具有简单形式,则可在第一次迭代时用 ∇2ℓ(sK)\nabla^{2}\ell({\bm{s}}_{K}) 代替 Diag⁡(dK)\operatorname{\bf Diag}({\bm{d}}_{K})。例如,当 fKf_{K} 为交叉熵损失时便是如此。递推反复进行,直至得到近似的对角 Hessian 矩阵 d0≈diag⁡(∇2(ℓ∘f)(x)){\bm{d}}_{0}\approx\operatorname{\bf diag}(\nabla^{2}(\ell\circ f)({\bm{x}}))。计算 dk{\bm{d}}_{k} 所需的梯度 rk{\bm{r}}_{k} 会在此过程中一并算出,因此该算法亦可返回 r0=∇(ℓ∘f)(x){\bm{r}}_{0}=\nabla(\ell\circ f)({\bm{x}})。To initialize the recursion, we can set dK:=diag⁡(∇2ℓ(sK)){\bm{d}}_{K}\coloneqq\operatorname{\bf diag}(\nabla^{2}\ell({\bm{s}}_{K})). As an alternative, as proposed by [62], if HK{\bm{H}}_{K} has a simple form, we can use ∇2ℓ(sK)\nabla^{2}\ell({\bm{s}}_{K}) instead of Diag⁡(dK)\operatorname{\bf Diag}({\bm{d}}_{K}) at the first iteration. This is the case for instance if fKf_{K} is a cross-entropy loss. The recursion is repeated until we obtain the approximate diagonal Hessian d0≈diag⁡(∇2(ℓ∘f)(x)){\bm{d}}_{0}\approx\operatorname{\bf diag}(\nabla^{2}(\ell\circ f)({\bm{x}})). The gradients rk{\bm{r}}_{k}, needed to compute dk{\bm{d}}_{k}, are computed along the way and the algorithm can therefore also return r0=∇(ℓ∘f)(x){\bm{r}}_{0}=\nabla(\ell\circ f)({\bm{x}}).

9.7.2 计算图Computation graphs

这一对角近似最初虽由 [20] 针对前馈网络导出,但推广到计算图并不困难。也就是说,对于沿计算图分解的函数 f(x,w)f({\bm{x}},{\bm{w}}),可按逆拓扑序反向传播对角近似,如下:Although this diagonal approximation was originally derived for feedforward networks by [20], it is straightforward to generalize it to computation graphs. Namely, for a function f(x,w)f({\bm{x}},{\bm{w}}) decomposed along a computation graph, we can backpropagate a diagonal approximation in reverse topological order as

rij\displaystyle{\bm{r}}_{i_{j}} ←rij+∂jfk(si1,…,sipk)∗[rk]\displaystyle\leftarrow{\bm{r}}_{i_{j}}+\partial_{j}f_{k}({\bm{s}}_{i_{1}},\ldots,{\bm{s}}_{i_{p_{k}}})^{*}[{\bm{r}}_{k}]
dij\displaystyle{\bm{d}}_{i_{j}} ←dij+diag⁡(∂jj2fk(si1,…,sipk)∗[rk])\displaystyle\leftarrow{\bm{d}}_{i_{j}}+\operatorname{\bf diag}(\partial^{2}_{jj}f_{k}({\bm{s}}_{i_{1}},\ldots,{\bm{s}}_{i_{p_{k}}})^{*}[{\bm{r}}_{k}])
+diag⁡(∂jfk(si1,…,sipk)∗Diag⁡(dk)∂jfk(si1,…,sipk)),\displaystyle\quad+\operatorname{\bf diag}(\partial_{j}f_{k}({\bm{s}}_{i_{1}},\ldots,{\bm{s}}_{i_{p_{k}}})^{*}\operatorname{\bf Diag}({\bm{d}}_{k})\partial_{j}f_{k}({\bm{s}}_{i_{1}},\ldots,{\bm{s}}_{i_{p_{k}}})), (9.5)

其中 j∈pa⁡(k)j\in{\operatorname{pa}}(k),起点为 rK=∇ℓ(sK){\bm{r}}_{K}=\nabla\ell({\bm{s}}_{K}) 与 dK=diag⁡(∇2ℓ(sK)){\bm{d}}_{K}=\operatorname{\bf diag}(\nabla^{2}\ell({\bm{s}}_{K})),或 Diag⁡(dK)=∇2ℓ(sK)\operatorname{\bf Diag}({\bm{d}}_{K})=\nabla^{2}\ell({\bm{s}}_{K})。要实现这样的算法,计算图中的每个基本函数都需配上一个 oracle,以便根据先前的结果计算当前函数的 Hessian 对角近似。例 9.7.1 给出了一个 MLP 的示例。for j∈pa⁡(k)j\in{\operatorname{pa}}(k), starting from rK=∇ℓ(sK){\bm{r}}_{K}=\nabla\ell({\bm{s}}_{K}) and dK=diag⁡(∇2ℓ(sK)){\bm{d}}_{K}=\operatorname{\bf diag}(\nabla^{2}\ell({\bm{s}}_{K})) or Diag⁡(dK)=∇2ℓ(sK)\operatorname{\bf Diag}({\bm{d}}_{K})=\nabla^{2}\ell({\bm{s}}_{K}). To implement such an algorithm, each elementary function in the computational graph needs to be augmented with an oracle that computes the Hessian diagonal approximation of the current function, given the previous ones. An example with MLPs is presented in Example 9.7.1.

例 9.7.1(MLP 的 Hessian 对角近似)。Example 9.7.1 (Hessian diagonal approximation for MLPs ).
考虑一个多层感知机Consider a multilayer perceptron sk\displaystyle{\bm{s}}_{k} :=ak(Wksk−1)∀k∈{1,…,K}\displaystyle\coloneqq{\bm{a}}_{k}({\bm{W}}_{k}{\bm{s}}_{k-1})\quad\forall k\in\{1,\dots,K\} f(w,x)\displaystyle f({\bm{w}},{\bm{x}}) :=sK\displaystyle\coloneqq{\bm{s}}_{K} 从 s0=x{\bm{s}}_{0}={\bm{x}} 出发。这里 ak{\bm{a}}_{k} 为逐元素激活函数(也可为恒等函数),w{\bm{w}} 则封装了权重矩阵 W1,…,WK{\bm{W}}_{1},\ldots,{\bm{W}}_{K}。我们考虑关于展平后矩阵的导数,因此关于这些展平量的梯度与对角近似均为向量。于是,记 tk=Wksk−1{\bm{t}}_{k}={\bm{W}}_{k}{\bm{s}}_{k-1},反向传播方案 (9.5) 归结为starting from s0=x{\bm{s}}_{0}={\bm{x}}. Here ak{\bm{a}}_{k} is the element-wise activation function (potentially the identity) and w{\bm{w}} encapsulates the weight matrices W1,…,WK{\bm{W}}_{1},\ldots,{\bm{W}}_{K}. We consider the derivatives w.r.t. the flattened matrices, so that gradients and diagonal approximations w.r.t. these flattened quantities are vectors. The backpropagation scheme (9.5) then reduces to, denoting tk=Wksk−1{\bm{t}}_{k}={\bm{W}}_{k}{\bm{s}}_{k-1}, rk−1\displaystyle{\bm{r}}_{k-1} :=Wk⊤(a′(tk)⊙rk)\displaystyle\coloneqq{\bm{W}}_{k}^{\top}({\bm{a}}^{\prime}({\bm{t}}_{k})\odot{\bm{r}}_{k}) gk\displaystyle{\bm{g}}_{k} :=vec⁡((a′(tk)⊙rk)sk−1⊤)\displaystyle\coloneqq\operatorname{vec}(({\bm{a}}^{\prime}({\bm{t}}_{k})\odot{\bm{r}}_{k}){\bm{s}}_{k-1}^{\top}) δk\displaystyle{\bm{\delta}}_{k} :=rk⊙a′′(tk)+dk⊙a′(tk)2\displaystyle\coloneqq{\bm{r}}_{k}\odot{\bm{a}}^{\prime\prime}({\bm{t}}_{k})+{\bm{d}}_{k}\odot{\bm{a}}^{\prime}({\bm{t}}_{k})^{2} dk−1\displaystyle{\bm{d}}_{k-1} :=(∑j=1DkWk,ji2δk,j)i=1Dk−1\displaystyle\coloneqq\left(\sum_{j=1}^{D_{k}}{\bm{W}}_{k,ji}^{2}\delta_{k,j}\right)_{i=1}^{D_{k-1}} hk\displaystyle{\bm{h}}_{k} :=vec⁡(δk(sk−12)⊤)\displaystyle\coloneqq\operatorname{vec}({\bm{\delta}}_{k}({\bm{s}}_{k-1}^{2})^{\top}) 从 rK=∇ℓ(sK){\bm{r}}_{K}=\nabla\ell({\bm{s}}_{K}) 出发,并例如取 dK=diag⁡(∇2ℓ(sK)){\bm{d}}_{K}=\operatorname{\bf diag}(\nabla^{2}\ell({\bm{s}}_{K}))。该算法返回 g1,…,gK{\bm{g}}_{1},\ldots,{\bm{g}}_{K},作为 ff 关于 w1,…,wK{\bm{w}}_{1},\ldots,{\bm{w}}_{K} 的梯度,其中 wi=vec⁡(Wi){\bm{w}}_{i}=\operatorname{vec}({\bm{W}}_{i});并返回 h1,…,hK{\bm{h}}_{1},\ldots,{\bm{h}}_{K},作为 Hessian 关于 w1,…,wK{\bm{w}}_{1},\ldots,{\bm{w}}_{K} 的对角近似。starting from rK=∇ℓ(sK){\bm{r}}_{K}=\nabla\ell({\bm{s}}_{K}) and, e.g., dK=diag⁡(∇2ℓ(sK)){\bm{d}}_{K}=\operatorname{\bf diag}(\nabla^{2}\ell({\bm{s}}_{K})). The algorithm returns g1,…,gK{\bm{g}}_{1},\ldots,{\bm{g}}_{K} as the gradients of ff w.r.t. w1,…,wK{\bm{w}}_{1},\ldots,{\bm{w}}_{K}, with wi=vec⁡(Wi){\bm{w}}_{i}=\operatorname{vec}({\bm{W}}_{i}), and h1,…,hK{\bm{h}}_{1},\ldots,{\bm{h}}_{K} as the diagonal approximations of the Hessian w.r.t. w1,…,wK{\bm{w}}_{1},\ldots,{\bm{w}}_{K}.

9.8 随机估计量9.8 Randomized estimators

本节介绍 Hessian 矩阵或 Gauss-Newton 矩阵对角线的随机估计量。In this section, we describe randomized estimators of the diagonal of the Hessian or Gauss-Newton matrices.

9.8.1 Girard-Hutchinson 估计量Girard-Hutchinson estimator

我们首先介绍一个通用估计量,它最初由 [79] 提出用于迹估计,并由 [97] 加以扩展。设 A∈RP×P{\bm{A}}\in\mathbb{R}^{P\times P} 为任意方阵,其矩阵—向量乘积(matvec)可用。假设 ω∈RP{\bm{\omega}}\in\mathbb{R}^{P} 是各向同性随机向量,即满足 Eω∼p[ωω⊤]=I\mathbb{E}_{{\bm{\omega}}\sim p}[{\bm{\omega}}{\bm{\omega}}^{\top}]=I。例如,两种常见选择是 Rademacher 分布 p=Uniform({−1,1}P)p=\mathrm{Uniform}(\{-1,1\}^{P}) 和标准正态分布 p=Normal(0,IP)p=\mathrm{Normal}({\mathbf{0}},I_{P})。于是,我们有We begin with a generic estimator, originally proposed for trace estimation by [79] and extended by [97]. Let A∈RP×P{\bm{A}}\in\mathbb{R}^{P\times P} be an arbitrary square matrix, whose matrix-vector product (matvec) is available. Suppose ω∈RP{\bm{\omega}}\in\mathbb{R}^{P} is an isotropic random vector, i.e., such that Eω∼p[ωω⊤]=I\mathbb{E}_{{\bm{\omega}}\sim p}[{\bm{\omega}}{\bm{\omega}}^{\top}]=I. For example, two common choices are the Rademacher distribution p=Uniform({−1,1}P)p=\mathrm{Uniform}(\{-1,1\}^{P}) and the standard normal distribution p=Normal(0,IP)p=\mathrm{Normal}({\mathbf{0}},I_{P}). Then, we have

Eω∼p[⟨ω,Aω⟩]=tr⁡(A).\mathbb{E}_{{\bm{\omega}}\sim p}[\langle{\bm{\omega}},{\bm{A}}{\bm{\omega}}\rangle]=\operatorname{tr}({\bm{A}}).

应用包括广义交叉验证、计算两个高斯分布之间的 Kullback-Leibler 散度,以及计算对数行列式的导数。Applications include generalized cross-validation, computing the Kullback-Leibler divergence between two Gaussians, and computing the derivatives of the log-determinant.

该方法可以推广 [21, 12, 90],从而得到 A{\bm{A}} 的对角线的估计量,The approach can be extended [21, 12, 90] to obtain an estimator of the diagonal of A{\bm{A}},

Eω∼p[ω⊙Aω]=diag⁡(A),\mathbb{E}_{{\bm{\omega}}\sim p}[{\bm{\omega}}\odot{\bm{A}}{\bm{\omega}}]=\operatorname{\bf diag}({\bm{A}}),

其中 ⊙\odot 表示 Hadamard 乘积(逐元素相乘)。这表明我们可以用蒙特卡洛方法估计 A{\bm{A}} 的对角线,where ⊙\odot denotes the Hadamard product (element-wise multiplication). This suggests that we can use the Monte-Carlo method to estimate the diagonal of A{\bm{A}},

diag⁡(A)≈1S∑i=1Sωi⊙Aωi,\operatorname{\bf diag}({\bm{A}})\approx\frac{1}{S}\sum_{i=1}^{S}{\bm{\omega}}_{i}\odot{\bm{A}}{\bm{\omega}}_{i},

当 S→∞S\to\infty 时取等号,因为该估计量是无偏的。正如 9.1 节和 9.2 节所回顾的,我们已知如何高效地与 Hessian 矩阵和 Gauss-Newton 矩阵相乘,因此可以将该技术应用于这些矩阵。方差由抽取的样本数 SS 决定,因而由所执行的矩阵—向量乘积次数决定。已有更精细的方法被提出,以进一步减小方差 [134, 63]。with equality as S→∞S\to\infty, since the estimator is unbiased. Since, as reviewed in Section 9.1 and Section 9.2, we know how to multiply efficiently with the Hessian and the Gauss-Newton matrices, we can apply the technique with these matrices. The variance is determined by the number SS of samples drawn and therefore by the number of matvecs performed. More elaborate approaches have been proposed to further reduce the variance [134, 63].

9.8.2 用于分解的 Bartlett 估计量Bartlett estimator for the factorization

假设目标函数具有形式 L(w;x,y):=ℓ(f(w;x);y)L({\bm{w}};{\bm{x}},{\bm{y}})\coloneqq\ell(f({\bm{w}};{\bm{x}});{\bm{y}}),其中 ℓ\ell 是指数族分布的负对数似然 ℓ(θ;y):=−log⁡pθ(y)\ell({\bm{\theta}};{\bm{y}})\coloneqq-\log p_{\bm{\theta}}({\bm{y}}),且 θ:=f(w;x){\bm{\theta}}\coloneqq f({\bm{w}};{\bm{x}}),其中 ff 为某个网络。由命题 9.3.5 中 Fisher 矩阵与 Gauss-Newton 矩阵之间的等价性(它可由 Bartlett 恒等式推出)可知,Suppose the objective function is of the form L(w;x,y):=ℓ(f(w;x);y)L({\bm{w}};{\bm{x}},{\bm{y}})\coloneqq\ell(f({\bm{w}};{\bm{x}});{\bm{y}}) where ℓ\ell is the negative log-likelihood ℓ(θ;y):=−log⁡pθ(y)\ell({\bm{\theta}};{\bm{y}})\coloneqq-\log p_{\bm{\theta}}({\bm{y}}) of an exponential family distribution, and θ:=f(w;x){\bm{\theta}}\coloneqq f({\bm{w}};{\bm{x}}), for some network ff. We saw from the equivalence between the Fisher and Gauss-Newton matrices in Proposition 9.3.5 (which follows from the Bartlett identity) that

∇GN2L(w;x,⋅)\displaystyle\nabla^{2}_{\mathrm{GN}}L({\bm{w}};{\bm{x}},\cdot) =EY∼pθ[∂f(w;x)∗∇ℓ(θ;Y)⊗∇ℓ(θ;Y)∂f(w;x)]\displaystyle=\mathbb{E}_{Y\sim p_{\bm{\theta}}}[\partial f({\bm{w}};{\bm{x}})^{*}\nabla\ell({\bm{\theta}};Y)\otimes\nabla\ell({\bm{\theta}};Y)\partial f({\bm{w}};{\bm{x}})]
=EY∼pθ[∇L(w;x,Y)⊗∇L(w;x,Y)],\displaystyle=\mathbb{E}_{Y\sim p_{\bm{\theta}}}[\nabla L({\bm{w}};{\bm{x}},Y)\otimes\nabla L({\bm{w}};{\bm{x}},Y)],

其中 ⋅\cdot 表示该结果对第二个自变量的任意取值都成立。这提示了一种蒙特卡洛方案:where ⋅\cdot indicates that the result holds for any value of the second argument. This suggests a Monte-Carlo scheme

∇GN2L(w;x,⋅)≈1S∑j=1S[∇L(w;x,yij)⊗∇L(w;x,yij)]\displaystyle\nabla^{2}_{\mathrm{GN}}L({\bm{w}};{\bm{x}},\cdot)\approx\frac{1}{S}\sum_{j=1}^{S}[\nabla L({\bm{w}};{\bm{x}},{\bm{y}}_{i_{j}})\otimes\nabla L({\bm{w}};{\bm{x}},{\bm{y}}_{i_{j}})]

其中 yi1,…,yiS∼pθ{\bm{y}}_{i_{1}},\dots,{\bm{y}}_{i_{S}}\sim p_{\bm{\theta}},θ=f(w,x){\bm{\theta}}=f({\bm{w}},{\bm{x}})。换言之,我们可以用 SS 次梯度计算来近似 Gauss-Newton 矩阵。该分解还可用于近似式 9.1 中的 GNVP。where yi1,…,yiS∼pθ{\bm{y}}_{i_{1}},\dots,{\bm{y}}_{i_{S}}\sim p_{\bm{\theta}} and θ=f(w,x){\bm{\theta}}=f({\bm{w}},{\bm{x}}). In words, we can approximate the Gauss-Newton matrix with SS gradient computations. This factorization can also be used to approximate the GNVP in Eq. 9.1.

9.8.3 用于对角线的 Bartlett 估计量Bartlett estimator for the diagonal

采用类似的方法,我们得到Following a similar approach, we obtain

diag⁡(∇GN2L(w;x,⋅))=EY∼pθ[∇L(w;x,Y)⊙∇L(w;x,Y)],\operatorname{\bf diag}(\nabla^{2}_{\mathrm{GN}}L({\bm{w}};{\bm{x}},\cdot))=\mathbb{E}_{Y\sim p_{\bm{\theta}}}[\nabla L({\bm{w}};{\bm{x}},Y)\odot\nabla L({\bm{w}};{\bm{x}},Y)],

其中 ⊙\odot 表示逐元素(Hadamard)乘积。采用蒙特卡洛方案,从 pθp_{\bm{\theta}} 中采样 yi1,…,yiS{\bm{y}}_{i_{1}},\dots,{\bm{y}}_{i_{S}},我们因此得到where ⊙\odot indicates the element-wise (Hadamard) product. Using a Monte-Carlo scheme, sampling yi1,…,yiS{\bm{y}}_{i_{1}},\dots,{\bm{y}}_{i_{S}} from pθp_{\bm{\theta}}, we therefore obtain

diag⁡(∇GN2L(w;x,⋅))≈1S∑j=1S∇L(w;x,yij)⊙∇L(w;x,yij),\operatorname{\bf diag}(\nabla^{2}_{\mathrm{GN}}L({\bm{w}};{\bm{x}},\cdot))\approx\frac{1}{S}\sum_{j=1}^{S}\nabla L({\bm{w}};{\bm{x}},{\bm{y}}_{i_{j}})\odot\nabla L({\bm{w}};{\bm{x}},{\bm{y}}_{i_{j}}),

当 pθp_{\bm{\theta}} 的支撑集中的所有标签均被采样时取等号。该估计量例如用于 [208, 附录 C.1.],它需要访问在采样标签处求值的单个梯度。另一种可能的对角线估计量由下式给出:with equality when all labels in the support of pθp_{\bm{\theta}} have been sampled. That estimator, used for instance in [208, Appendix C.1.], requires access to individual gradients evaluated at the sampled labels. Another possible estimator of the diagonal is given by

1Sdiag⁡(∇GN2L(w;x,⋅))\displaystyle\frac{1}{S}\operatorname{\bf diag}(\nabla^{2}_{\mathrm{GN}}L({\bm{w}};{\bm{x}},\cdot))
=\displaystyle= EY1,…,YS∼pθ[∇1S∑i=1SL(w;x,Yi)⊙∇1S∑i=1SL(w;x,Yi)].\displaystyle\mathbb{E}_{Y_{1},\dots,Y_{S}\sim p_{\bm{\theta}}}\left[\nabla\frac{1}{S}\sum_{i=1}^{S}L({\bm{w}};{\bm{x}},Y_{i})\odot\nabla\frac{1}{S}\sum_{i=1}^{S}L({\bm{w}};{\bm{x}},Y_{i})\right].

令 γi:=∇L(w;x,Yi)\gamma_{i}\coloneqq\nabla L({\bm{w}};{\bm{x}},Y_{i}),这可由下式推出:Letting γi:=∇L(w;x,Yi)\gamma_{i}\coloneqq\nabla L({\bm{w}};{\bm{x}},Y_{i}), this follows from

E[(∑iγi)⊙(∑jγj)]\displaystyle\mathbb{E}\left[\left(\sum_{i}\gamma_{i}\right)\odot\left(\sum_{j}\gamma_{j}\right)\right] =E[∑iγi⊙γi+∑i≠jγi⊙γj]\displaystyle=\mathbb{E}\left[\sum_{i}\gamma_{i}\odot\gamma_{i}+\sum_{i\neq j}\gamma_{i}\odot\gamma_{j}\right]
=E[∑iγi⊙γi]\displaystyle=\mathbb{E}\left[\sum_{i}\gamma_{i}\odot\gamma_{i}\right]

此处我们使用了 E[γi⊙γj]=E[γi]⊙E[γj]=0\mathbb{E}[\gamma_{i}\odot\gamma_{j}]=\mathbb{E}[\gamma_{i}]\odot\mathbb{E}[\gamma_{j}]={\mathbf{0}}:这是因为对于 i≠ji\neq j,γi\gamma_{i} 与 γj\gamma_{j} 是相互独立的变量且均值为零;这来自 Bartlett 第一恒等式(式 12.2)。于是可以用蒙特卡洛方法得到where we used that E[γi⊙γj]=E[γi]⊙E[γj]=0\mathbb{E}[\gamma_{i}\odot\gamma_{j}]=\mathbb{E}[\gamma_{i}]\odot\mathbb{E}[\gamma_{j}]={\mathbf{0}} since γi\gamma_{i} and γj\gamma_{j} are independent variables for i≠ji\neq j and have zero mean, from Bartlett’s first identity Eq. 12.2. We can then use the Monte-Carlo method to obtain

1Sdiag⁡(∇GN2L(w;x,⋅))≈(∇1S∑j=1SL(w;x,yij))⊙(∇1S∑j=1SL(w;x,yij)),\displaystyle\frac{1}{S}\operatorname{\bf diag}(\nabla^{2}_{\mathrm{GN}}L({\bm{w}};{\bm{x}},\cdot))\approx\left(\nabla\frac{1}{S}\sum_{j=1}^{S}L({\bm{w}};{\bm{x}},{\bm{y}}_{i_{j}})\right)\odot\left(\nabla\frac{1}{S}\sum_{j=1}^{S}L({\bm{w}};{\bm{x}},{\bm{y}}_{i_{j}})\right),

当 pθp_{\bm{\theta}} 的支撑集中所有标签均被采样时取等号。该估计量实现起来可能更为方便,因为它只需访问平均损失的梯度。不过,它的方差可能偏大。[120] 使用了它的一个特例,即对每个 x{\bm{x}} 只抽取一个 y{\bm{y}} 。with equality when all labels in the support of pθp_{\bm{\theta}} have been sampled. This estimator can be more convenient to implement, since it only needs access to the gradient of the averaged losses. However, it may suffer from higher variance. A special case of this estimator is used by [120], where they draw only one y{\bm{y}} for each x{\bm{x}}.

9.9 小结9.9 Summary

  • •

    借助 Hessian 链式法则,可以构造出“Hessian 反向传播”。它在计算链上相当简单,但在计算图上却因扇入时出现交叉乘积项而变得计算上难以承受。By using a Hessian chain rule, we can develop a “Hessian backpropagation”. While it is reasonably simple for computation chains, it becomes computationally prohibitive for computation graphs, due to the cross-product terms occurring with fan-in.

  • •

    更好的办法是使用Hessian 矩阵—向量乘积(HVP)。前文已指出,计算 HVP 共有四种可能的方法,而前向—反向方法先验地最为高效。与计算梯度类似,计算 HVP 的代价仅为求函数本身值的常数倍。A better approach is to use Hessian-vector products (HVPs). We saw that there are four possible methods to compute HVPs, but the forward-on-reverse method is a priori the most efficient. Similarly to computing gradients, computing HVPs is only a constant times more expensive than evaluating the function itself.

  • •

    与组合 ℓ∘f\ell\circ f 相关联的 Gauss-Newton 矩阵可视为 Hessian 矩阵的一种近似。若 ℓ\ell 为凸,则它是半正定矩阵,并可用于构造函数的具有原则性的二次近似;在指数族情形下,它等价于 Fisher 信息矩阵。Gauss-Newton—向量乘积亦可像 HVP 一样高效计算。The Gauss-Newton matrix associated with the composition ℓ∘f\ell\circ f can be seen as an approximation of the Hessian. It is a positive semidefinite matrix if ℓ\ell is convex, and can be used to build a principled quadratic approximation of a function. It is equivalent to the Fisher information matrix in the case of exponential families. Gauss-Newton-vector products can be computed efficiently, like HVPs.

  • •

    我们还介绍了其他近似,例如(块)对角近似与随机估计量。We also described other approximations, such as (block) diagonal approximations, and randomized estimators.