本章回顾如何用自动微分计算二阶导数。We review in this chapter how to perform automatic differentiation for second-order derivatives.
本节考虑函数 。与雅可比矩阵类似,在大多数情况下,我们并不需要访问完整的Hessian 矩阵,而是需要求 在 处沿方向 的Hessian 矩阵—向量乘积(HVP),其定义见定义 2.4.7。这一乘积可以通过四种不同的方式计算,具体取决于我们如何组合自动微分的两种主要模式。We consider in this section a function . Similarly to the Jacobian, for most purposes, we do not need access to the full Hessian but rather to the Hessian-vector product (HVP) at , in a direction , 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.
HVP 可以通过四种不同的方式计算。An HVP can be computed in four different ways.
反向之上的反向: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,
反向之上的前向:由于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,
反向之上的前向:回顾对于任意函数 ,VJP 可以等价地定义为沿输出方向 的梯度,即Reverse on forward: Recall that for any function , the VJP can equivalently be defined as the gradient along an output direction , that is,
其中沿用简写 ,于是 是 的函数。因此在本例中,我们可以将“反向之上的反向”方法改写为where we recall the shorthand , so that is a function of . In our case, we can therefore rewrite the reverse-on-reverse approach as
我们知道 是 在 处沿 的 JVP。因此,我们也可以将 HVP 计算为 在 处沿 的 JVP 的梯度,We know that is the JVP of at along . Therefore, we can also compute the HVP as the gradient of the JVP of at along ,
其中采用记号 ,以强调它是 的函数。where we use the notation to insist on the fact that it is a function of .
前向之上的前向:最后,我们可以使用定义 2.4.7 中把 HVP 定义为沿 以及各标准方向的二阶偏导数向量这一点。也就是说,假设 ,我们可以计算 JVP 的 JVP 共 次: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 and each canonical direction. That is, assuming , we can compute the JVP of the JVP times:
计算 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) | |
| 反向之上的前向(梯度的 JVP)Forward on reverse (JVP of gradient) | |
| 前向之上的反向(JVP 的梯度)Reverse on forward (gradient of JVP) | |
| 前向之上的前向(JVP 的 JVP)Forward on forward (JVPs of JVPs) |
为直观了解这四种方法的计算复杂度与内存复杂度,我们沿用 8.1 节的做法,考虑一个函数链 。为简化分析,我们假设对 ,有 ,且 。To get a sense of the computational and memory complexity of the four approaches, we consider a chain of functions as done in Section 8.1. To simplify our analysis, we assume for and .
反向模式的计算图如图 9.1 所示。 可以用一条简单的链表示,但 的计算图不再是一条链,而是一个 DAG。这是因为在 的计算中, 与 都依赖于 。The computation graph of the reverse mode is illustrated in Fig. 9.1. While would be represented by a simple chain, the computational graph of is no longer a chain: it is a DAG. This is due to the computations of , where both and depend on .
图 9.2 与图 9.3 分别展示了反向之上的反向、反向之上的前向的计算图。在反向模式之上再施加反向模式时,每个扇入运算 处, 上的反向模式都会分岔为两条路径,随后由一次求和合并。而在反向模式之上叠加前向模式时,计算流只是沿着 的流程推进。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 , the reverse mode on 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 .
据此,仿照表 8.1 的计算,可得如下结果。我们假设每个 都是稠密线性算子,因而其应用代价与矩阵—向量乘积相同。至于内存复杂度,我们考虑每个运算的输入都会被保存下来,以便在反向传播中计算所需的导数。With this in mind, following a similar calculation as for Table 8.1, we obtain the following results. We assume that each 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.
反向之上的反向:时间为 ,空间为 。Reverse on reverse: time and space.
前向模式叠加于反向模式之上:时间复杂度为 ,空间复杂度为 。Forward on reverse: time and space.
反向模式叠加于前向模式之上:时间复杂度为 ,空间复杂度为 。Reverse on forward: time and space.
前向模式叠加于前向模式之上:对 个 JVP,时间复杂度为 ,空间复杂度为 ,其中 。Forward on forward: time and space for the JVPs with .
可以看到,对于由函数构成的链,“反向叠加反向”“前向叠加反向”与“反向叠加前向”三者的时间复杂度至多相差若干常数因子,因而大体相当。在反向模式之上再叠加反向模式,需要保存反向传播的信息,即 (相应地,保存前向传播的信息,即图 8.1 中的 ),以便完成最后一次反向传播。若在反向模式之上改用前向模式,便不会产生这项额外开销,内存代价因而略低。此外,反向模式叠加反向模式还会因图 9.2 所示的分支与合并运算而多出若干次求和;反向模式叠加于前向模式之上时同样如此,因为我们无法回避扇入运算(此处其形式为 )。遗憾的是,“前向叠加前向”的代价高昂得令人望而却步。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 (resp. the information forwarded, i.e., the 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 ). Unfortunately, “forward on forward” is prohibitively expensive.
总之,在计算 HVP 的四种方法中,就计算复杂度与内存复杂度而言,前向叠加反向模式是先验上最可取的一种。不过需要注意的是,高阶导数的计算可以受益于专门设计的自动微分实现,例如泰勒模式自动微分,它们并非只是把前向模式与反向模式简单组合起来。对于一般的函数 ,合理的做法是对前三种方法逐一做基准测试,以确定哪一种最适合当前的函数。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 , it is reasonable to benchmark the first three methods to determine which method is the best for the function at hand.
函数 的 Hessian 矩阵 常被用来构造 的二次近似,The Hessian matrix of a function is often used to construct a quadratic approximation of ,
遗憾的是,当 非凸时, 通常是不定矩阵,也就是说,上述近似是关于 的非凸二次函数。例如,若 而 为凸,则当 为线性时 是凸的;但当 非线性时,它通常是非凸的。(广义)Gauss-Newton 矩阵是 Hessian 矩阵的一种有原则的替代,它针对 定义。Unfortunately, when is nonconvex, is typically an indefinite matrix, which means that the above approximation is a nonconvex quadratic w.r.t. . For instance, if with convex, then is convex if is linear, but it is typically nonconvex if is nonlinear. The (generalized) Gauss-Newton matrix is a principled alternative to the Hessian, which is defined for .
正如 17.2 节所讨论的,Gauss-Newton 矩阵是 Gauss-Newton 方法的关键要素。其优点之一在于:只要 为凸,它便是半正定的。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 is convex.
这意味着该近似This means that the approximation
是关于 的凸二次函数。is a convex quadratic w.r.t. .
利用链式法则可知, 的Hessian 矩阵可分解为两项之和(另见命题 9.5.1)。Using the chain rule, we find that the Hessian of decomposes into the sum of two terms (see also Proposition 9.5.1).
因此,Gauss-Newton 算子 可视为 Hessian 矩阵 的一种近似;当 为线性时,二者相等。The Gauss-Newton operator can therefore be seen as an approximation of the Hessian , with equality if is linear.
命题 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.
与 Hessian 矩阵类似,我们很少需要在内存中具体化完整的 Gauss-Newton 矩阵。事实上,我们可以针对方向 定义 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 , as
| (9.1) |
其中 是 的 HVP,即从 到 的线性映射。GNVP 可以利用 的 JVP、 的 HVP 以及 的 VJP 来计算。具体化 VJP 需要对 进行 次前向传播,由此可同时得到值 和伴随线性映射 ;求值 VJP 需要对 进行 次反向传播;求值 JVP 则需要对 进行 次前向传播。因此,求值 总共需要对 进行 次前向传播和 次反向传播。where is the HVP of , a linear map from to . The GNVP can be computed using the JVP of , the HVP of and the VJP of . Instantiating the VJP requires forward pass through , from which we get both the value and the adjoint linear map . Evaluating the VJP requires backward pass through . Evaluating the JVP requires forward pass through . In total, evaluating therefore requires forward passes and backward pass through .
在本节中,我们假定 且 。当 为凸函数时,Gauss-Newton 矩阵是半正定的,因此可分解为 ,其中 , 为该矩阵的秩。这一分解实际上可以很方便地由 的 Hessian 矩阵的因子分解算出。例如,设已知 的 Hessian 矩阵的特征分解 ,其中 为特征向量, 为特征值(由半正定性可知这些特征值非负)。于是,Gauss-Newton 矩阵可分解为In this section, we assume and . When is convex, we know that the Gauss-Newton matrix is positive semi-definite and therefore it can be factorized into for some , where is the rank of the matrix. Such a decomposition can actually be computed easily from a factorization of the Hessian of . For instance, suppose we know the eigendecomposition of the Hessian of , , where the are the eigenvectors and the are the eigenvalues (which we know are non-negative due to positive semidefiniteness). Then, the Gauss-Newton matrix can be decomposed as
将向量 按列堆叠成矩阵 ,即得到分解 。为构造该分解,需要对 做特征分解,耗时 ;还需要在 处调用 的 VJP 共 次。与式 9.1 中的直接实现相比,该分解一经算出,便可按 计算 Gauss-Newton 向量乘积(GNVP)。该分解仅需 的内存,而式 9.1 的直接实现则需保存 的中间计算。可见,计算与内存之间的权衡取决于所考虑的具体函数。Stacking the vectors into a matrix , we recover the factorization . To form this decomposition, we need to perform the eigendecomposition of , which takes time. We also need calls to the VJP of at . Compared to the direct implementation in Eq. 9.1, the factorization, once computed, allows us to compute the Gauss-Newton vector product (GNVP) as . The factorization only requires memory, while the direct implementation in Eq. 9.1 requires us to maintain the intermediate computations of . The computation-memory trade-offs therefore depend on the function considered.
假设目标函数具有如下形式Suppose the objective function is of the form
略微滥用记号,与 对所对应的 Gauss-Newton 矩阵为With some slight abuse of notation, we then have that the Gauss-Newton matrix associated with a pair is
其中 。给定 对上的分布 ,则与平均损失where . Given a distribution over pairs, the Gauss-Newton matrix associated with the averaged loss
相对应的 Gauss-Newton 矩阵为is then
Fisher 信息是度量随机变量 所含信息量的一种方式。The Fisher information is a way to measure the amount of information in a random variable .
如 17.3 节所述,Fisher 信息矩阵是自然梯度下降法的关键要素。As studied in Section 17.3, the Fisher information matrix is a key ingredient of the natural gradient descent method.
只要该概率分布关于 二次可微,且其二阶导数可积,Fisher 信息矩阵也可表示为负对数似然的Hessian 矩阵 [5, 127]。Provided that the probability distribution is twice differentiable w.r.t. with integrable second derivatives, the Fisher information matrix can also be expressed as the Hessian of the negative log-likelihood [5, 127].
迄今为止,我们讨论的都是关于一般随机变量 的 Fisher 信息。下面转向监督概率学习的情形:此时 ,并可按概率的乘积法则定义概率密度函数 ,简写作 。So far, we discussed the Fisher information for a generic random variable . We now discuss the supervised probabilistic learning setting where and where, using the product rule of probability, we define the PDF , with the shorthand .
当 为指数族分布时,可以证明 Fisher 信息矩阵与 Gauss-Newton 矩阵彼此等价。When is an exponential family distribution, we can show that the Fisher information matrix and the Gauss-Newton matrix are equivalent.
由命题 3.4.5 可知,若 为指数族分布,则 实际上与 无关。再借助 Bartlett 第二恒等式(式 12.3),即可得到From Proposition 3.4.5, if is an exponential family distribution, is actually independent of . Using Bartlett’s second identity Eq. 12.3, we then obtain
这里用 表示该结果对一切 都成立。将此结果代回 Fisher 信息矩阵,证明即告完成。where we used to indicate that the result holds for all . Plugging the result back in the Fisher information matrix concludes the proof.
我们将在 17.1 节看到,牛顿法的迭代格式为We will see in Section 17.1 that Newton’s method uses iterations as
例如,当 严格凸时,其逆是良定义的;否则可引入额外的正则化。因此,牛顿法需要用到下面定义的逆Hessian 矩阵—向量乘积(IHVP)。The inverse is well-defined if for example 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.
计算矩阵之逆有许多直接方法,如 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
并且只需能够对任意 求取线性映射 。此类算法中,共轭梯度(CG)法适用于 为正定的情形,即对一切 都有 ;广义最小残差(GMRES)法则适用于任意可逆的 。更完整的求解器名录可见于 SciPy [203] 等公开软件。因此,对于严格凸函数(其 Hessian 矩阵必为正定),只需将 CG 作用于 HVP,便可算得相应的 IHVP,by only accessing the linear map for any . Among such algorithms, we have the conjugate gradient (CG) method, that applies for positive-definite, i.e., such that for all , or the generalized minimal residual (GMRES) method, that applies for any invertible . 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,
Hessian 矩阵的正定性对严格凸函数而言确实是有保证的;而对于一般非凸函数,这种性质或许只在极小值点附近成立,一般而言并不成立。算法 1 以最简单的形式回顾了共轭梯度法。理论上,经过至多 次 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 iterations of CG, though in practice numerical errors may prevent one from getting an exact solution.
对于给定的矩阵 ,求解 可通过分解方法(LU、QR、Cholesky)在 时间内完成。对于 CG 或 GMRES 这类无矩阵方法,每次迭代的代价为 。理论上,它们至多经过 次迭代即可求解该线性系统,因此获得精确解的理论代价与之相同,也是 。For a given matrix , solving can be done with decomposition methods (LU, QR, Cholesky) in time. For matrix-free methods such as CG or GMRES, the cost per iteration is . Since they theoretically solve the linear system in iterations, the cost to obtain an exact solution is theoretically the same, .
然而,CG 或 GMRES 与分解方法的区别在于,前者是迭代方法,这意味着每进行一次迭代,它们都会更接近解。与分解方法不同,这意味着我们可以在找到精确解之前停止迭代。在实际中,找到一个良好近似解所需的迭代次数取决于矩阵本身。良态的矩阵只需少量迭代。病态的矩阵会使 CG 出现一些数值不稳定,因此可能需要超过 次迭代才能得到良好的解。相比之下,分解方法分两步进行:首先以 的代价构建 的一个分解,然后利用该结构以 的代价求解一个线性系统。众所周知,LU 与 QR 分解通常更为稳定,因此当我们能够无代价地访问 的元素时,实际中往往更倾向于使用它们。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 iterations may be needed to get a good solution. In contrast, decomposition methods proceed in two steps: first they build a decomposition of at a cost of , and second they solve a linear system at a cost of , 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 at no cost.
如果我们无法访问Hessian 矩阵 ,而只能访问其 HVP,那么访问 的元素将付出高昂的代价。确实, 的元素仍可由 HVP 恢复,因为 ,但访问 的每一行或每一列都需要一次 HVP(矩阵—向量乘积)。因此,为了获得使用分解方法所需的信息,我们需要进行 次 HVP 调用,之后才能真正计算出解。而在相同调用次数下,CG 或 GMRES 早已找到一个近似解。此外,CG 方法不需要将矩阵存储在内存中。If we do not have access to the Hessian , but only to its HVP, accessing entries of comes at a prohibitive cost. Indeed, entries of can still be recovered from HVPs, since , but accessing each row or column of costs one HVP (matrix-vector product). To access the information necessary to use a decomposition method, we therefore need 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.
发展前向模式与反向模式自动微分的核心要素,依赖于复合函数的链式法则,。对于二阶导数,也可以得到类似的法则。为此,我们略微滥用记号,记The essential ingredient to develop forward-mode and reverse-mode autodiff hinged upon the chain rule for composed functions, . For second derivatives, a similar rule can be obtained. To do so, we slightly abuse notation and denote
其中 、、,此处我们回顾简写记号 。此外,我们把上述量视为一个线性映射。严格来说,上标 ∗ 已不再是线性伴随,因为 不再是线性的,而是双线性的。不过,这个上标所起的作用与 VJP 相同:它接收一个输出向量,并返回与该输出向量方向上的无穷小变化相对应的输入导数。where , , , and where we recall the shorthand notation . Moreover, we view the above quantity as a linear map. Strictly speaking, the superscript ∗ is not a linear adjoint anymore, since 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.
请注意,Hessian 矩阵通常针对标量值函数 定义,而上述定义针对的是一种广义的 Hessian 概念,它适用于任意函数 。Note that, while the Hessian is usually defined for scalar-valued functions , the above definition is for a generalized notion of Hessian that works for any function .
式 9.2 中的 Hessian 反向传播法则包含两项。第一项 沿输出方向计算中间函数的 Hessian 矩阵,而该方向通常由 VJP 反向传播。第二项 则刻画了中间的一阶变化如何影响输出的二阶导数。The Hessian backpropagation rule in Eq. 9.2 reveals two terms. The first one simply computes the Hessian of the intermediate function along the output direction normally backpropagated by a VJP. The second term shows how intermediate first-order variations influence second-order derivatives of the output.
对于 8.1 节中那样的简单计算链 ,命题 9.5.1 推导出的公式已足以给出一种反向传播 Hessian 矩阵的算法,如算法 2 所示。与算法 2 相比,我们只是借助中间的一阶与二阶导数,同时反向传播向量 与矩阵 。For a simple computation chain 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 and the matrices using intermediate first and second derivatives.
对于一般计算图(见 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.
扇入的梯度反向传播表达式之所以简单,是因为各函数 之间没有任何路径相连。相比之下,Hessian 矩阵反向传播则涉及 的交叉乘积项 ,与 计算相关的节点再也无法被独立处理。The gradient backpropagation expression for fan-in is simple
because the functions are not linked by any path.
In contrast, the Hessian backpropagation involves
cross-product terms
for .
The nodes associated with the 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.
与其计算完整的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.
回顾前馈网络的定义:Recall the definition of a feedforward network:
其中。我们可以不计算关于的完整Hessian 矩阵,而分别计算关于各组参数的Hessian 矩阵。对于计算链的情形,我们在算法 2 中使用的Hessian 矩阵反向传播递推式为where . Rather than computing the entire Hessian of w.r.t. , we can compute the Hessians w.r.t. each set of parameters . For the case of computation chains, the Hessian backpropagation recursion we used in Algorithm 2 was
将该递推式推广到前馈网络的情形,我们从和出发,可得Extending this recursion to the feedforward network case, we obtain, starting from and ,
其中我们用表示这些分块未被使用。于是,关于各组参数的 Hessian 矩阵即为where we used to indicate that these blocks are not used. The Hessians w.r.t. each set of parameters are then
这一结果之所以成立,是因为可以将关于的 Hessian 矩阵视为计算下式关于的 Hessian 矩阵:The validity of this result stems from the fact that we can view the Hessian w.r.t. as computing the Hessian w.r.t. of
其中,对。由于各分块Hessian 矩阵的计算共享了大部分计算量,因此它们可以像梯度一样,在一次反向传播中一并求值。where , for . 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.
对于一般的计算图,考虑由下式定义的函数,记,For generic computation graphs, consider a function defined by, denoting ,
使得,且依图的拓扑序排列(见 8.3 节)。对和,我们可以考虑如下反向传播方案:such that , and is following a topological ordering of the graph (see Section 8.3). We can consider the following backpropagation scheme, for and
| (9.3) | ||
| (9.4) |
从和出发。回顾可知,对于多输入的情形,命题 9.5.4 中给出的链式法则涉及交叉导数。正因如此,式 9.3 中的反向传播方案只计算出一个近似结果。例如,可以验证,用式 9.3 计算的Hessian 矩阵,并不能得到的Hessian 矩阵的精确表达式。该方案易于实现,并且可以为Hessian 矩阵提供一个有参考价值的替代量。starting from and . 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 does not provide an exact expression for the Hessian of . This scheme is easy to implement and may provide a relevant proxy for the Hessian.
与设计近似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.5.1 中 Hessian 反向传播法则的修改:只保留 Hessian 矩阵的对角线。形式上,给定矩阵 ,用 表示由 的对角元素组成的向量;对于向量 ,用 表示以 为元素的对角矩阵。对于 的 Hessian 矩阵的反向传播,由算法 2 可知, 可由 表示为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 , we denote by the vector of diagonal entries of , and for a vector , we denote the diagonal matrix with entries . For the backpropagation of the Hessian of , we see from Algorithm 2 that can be expressed in terms of as
遗憾的是,这一递推式需要访问整个Hessian 矩阵 ,代价因而过高。一个自然的想法是改造该递推式,通过反向传播向量来近似 :Unfortunately, that recursion needs access to the whole Hessian , and would therefore be too expensive. A natural idea is to modify the recursion to approximate by backpropagating vectors:
对角矩阵 充当 的替代量。该递推式的每次迭代都可在输出维度 上以线性时间完成计算,因为The diagonal matrix serves as a surrogate for . Each iteration of this recursion can be computed in linear time in the output dimension since
为启动该递推,可令 。另一种做法由 [62] 提出:若 具有简单形式,则可在第一次迭代时用 代替 。例如,当 为交叉熵损失时便是如此。递推反复进行,直至得到近似的对角 Hessian 矩阵 。计算 所需的梯度 会在此过程中一并算出,因此该算法亦可返回 。To initialize the recursion, we can set . As an alternative, as proposed by [62], if has a simple form, we can use instead of at the first iteration. This is the case for instance if is a cross-entropy loss. The recursion is repeated until we obtain the approximate diagonal Hessian . The gradients , needed to compute , are computed along the way and the algorithm can therefore also return .
这一对角近似最初虽由 [20] 针对前馈网络导出,但推广到计算图并不困难。也就是说,对于沿计算图分解的函数 ,可按逆拓扑序反向传播对角近似,如下: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 decomposed along a computation graph, we can backpropagate a diagonal approximation in reverse topological order as
| (9.5) |
其中 ,起点为 与 ,或 。要实现这样的算法,计算图中的每个基本函数都需配上一个 oracle,以便根据先前的结果计算当前函数的 Hessian 对角近似。例 9.7.1 给出了一个 MLP 的示例。for , starting from and or . 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.
本节介绍 Hessian 矩阵或 Gauss-Newton 矩阵对角线的随机估计量。In this section, we describe randomized estimators of the diagonal of the Hessian or Gauss-Newton matrices.
我们首先介绍一个通用估计量,它最初由 [79] 提出用于迹估计,并由 [97] 加以扩展。设 为任意方阵,其矩阵—向量乘积(matvec)可用。假设 是各向同性随机向量,即满足 。例如,两种常见选择是 Rademacher 分布 和标准正态分布 。于是,我们有We begin with a generic estimator, originally proposed for trace estimation by [79] and extended by [97]. Let be an arbitrary square matrix, whose matrix-vector product (matvec) is available. Suppose is an isotropic random vector, i.e., such that . For example, two common choices are the Rademacher distribution and the standard normal distribution . Then, we have
应用包括广义交叉验证、计算两个高斯分布之间的 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],从而得到 的对角线的估计量,The approach can be extended [21, 12, 90] to obtain an estimator of the diagonal of ,
其中 表示 Hadamard 乘积(逐元素相乘)。这表明我们可以用蒙特卡洛方法估计 的对角线,where denotes the Hadamard product (element-wise multiplication). This suggests that we can use the Monte-Carlo method to estimate the diagonal of ,
当 时取等号,因为该估计量是无偏的。正如 9.1 节和 9.2 节所回顾的,我们已知如何高效地与 Hessian 矩阵和 Gauss-Newton 矩阵相乘,因此可以将该技术应用于这些矩阵。方差由抽取的样本数 决定,因而由所执行的矩阵—向量乘积次数决定。已有更精细的方法被提出,以进一步减小方差 [134, 63]。with equality as , 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 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.3.5 中 Fisher 矩阵与 Gauss-Newton 矩阵之间的等价性(它可由 Bartlett 恒等式推出)可知,Suppose the objective function is of the form where is the negative log-likelihood of an exponential family distribution, and , for some network . We saw from the equivalence between the Fisher and Gauss-Newton matrices in Proposition 9.3.5 (which follows from the Bartlett identity) that
其中 表示该结果对第二个自变量的任意取值都成立。这提示了一种蒙特卡洛方案:where indicates that the result holds for any value of the second argument. This suggests a Monte-Carlo scheme
其中 ,。换言之,我们可以用 次梯度计算来近似 Gauss-Newton 矩阵。该分解还可用于近似式 9.1 中的 GNVP。where and . In words, we can approximate the Gauss-Newton matrix with gradient computations. This factorization can also be used to approximate the GNVP in Eq. 9.1.
采用类似的方法,我们得到Following a similar approach, we obtain
其中 表示逐元素(Hadamard)乘积。采用蒙特卡洛方案,从 中采样 ,我们因此得到where indicates the element-wise (Hadamard) product. Using a Monte-Carlo scheme, sampling from , we therefore obtain
当 的支撑集中的所有标签均被采样时取等号。该估计量例如用于 [208, 附录 C.1.],它需要访问在采样标签处求值的单个梯度。另一种可能的对角线估计量由下式给出:with equality when all labels in the support of 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
令 ,这可由下式推出:Letting , this follows from
此处我们使用了 :这是因为对于 , 与 是相互独立的变量且均值为零;这来自 Bartlett 第一恒等式(式 12.2)。于是可以用蒙特卡洛方法得到where we used that since and are independent variables for and have zero mean, from Bartlett’s first identity Eq. 12.2. We can then use the Monte-Carlo method to obtain
当 的支撑集中所有标签均被采样时取等号。该估计量实现起来可能更为方便,因为它只需访问平均损失的梯度。不过,它的方差可能偏大。[120] 使用了它的一个特例,即对每个 只抽取一个 。with equality when all labels in the support of 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 for each .
借助 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.
与组合 相关联的 Gauss-Newton 矩阵可视为 Hessian 矩阵的一种近似。若 为凸,则它是半正定矩阵,并可用于构造函数的具有原则性的二次近似;在指数族情形下,它等价于 Fisher 信息矩阵。Gauss-Newton—向量乘积亦可像 HVP 一样高效计算。The Gauss-Newton matrix associated with the composition can be seen as an approximation of the Hessian. It is a positive semidefinite matrix if 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.