数值计算导数最简单的方法之一是用有限差分,它近似于导数的无穷小定义。有限差分只需函数求值,因此可以处理黑箱函数:它忽略函数的复合结构。不失一般性,我们的论述聚焦于计算函数 在 处沿方向 的方向导数 。One of the simplest ways to numerically compute derivatives is to use finite differences, which approximate the infinitesimal definition of derivatives. Finite differences only require function evaluations, and can therefore work with blackbox functions: they ignore the compositional structure of functions. Without loss of generality, our exposition focuses on computing directional derivatives , for a function , evaluated at , in the direction .
根据定义 2.2.1 与定义 2.3.6,方向导数乃至更一般的 JVP 均以极限形式定义,From Definition 2.2.1 and Definition 2.3.6, the directional derivative and more generally the JVP are defined as a limit,
这表明我们可以用下式近似方向导数与 JVP:This suggests that we can approximate the directional derivative and the JVP using
其中 为某个取值。该公式称为前向差分。for some . This formula is called a forward difference.
为度量这一近似所引入的误差,我们使用 Landau 的 记号。To measure the error incurred by this approximation, we use Landau’s notation.
由 2.5.4 节的泰勒展开,我们确实有From the Taylor expansion in Section 2.5.4, we indeed have
从而so that
在前向差分公式中选择有限的而非无穷小的 所引入的误差称为截断误差。上述泰勒近似表明,该误差为 阶。The error incurred by choosing a finite rather than infinitesimal in the forward difference formula is called the truncation error. The Taylor approximation above shows that this error is of the order of .
然而, 不能取得过小,因为在计算机上对函数 求值会把 的值舍入到机器精度。在数学上,标量值函数 在计算机上求值后变为一个函数 ,使得 ,其中 表示 的最接近整数, 为机器精度,即机器所能编码的最小非零实数。这意味着在计算机上计算差值 容易产生 阶的舍入误差。图 7.1 展示了截断误差与舍入误差之间的权衡。However, we cannot choose a too small value of , because the evaluation of the function on a computer rounds the value of to machine precision. Mathematically, a scalar-valued function evaluated on a computer becomes a function such that , where denotes the closest integer of and is the machine precision, i.e., the smallest non-zero real number encoded by the machine. This means that the difference evaluated on a computer is prone to round-off error of the order of . We illustrate the trade-off between truncation and round-off errors in Fig. 7.1.
作为另一种选择,我们可以用下式近似方向导数与 JVP:As an alternative, we can approximate the directional derivative and the JVP by
其中 为某个取值。该公式称为后向差分。由 2.5.4 节的泰勒展开,我们容易验证 ,因此其截断误差与前向差分相同。for some . This formula is called a backward difference. From the Taylor expansion in Section 2.5.4, we easily verify that , so that the truncation error is the same as for the forward difference.
与前向差分和后向差分这类非对称公式不同,我们可以使用对称公式来近似导数:Rather than using an asymmetric formula to approximate the derivative, as in forward and backward differences, we can use a symmetric formula
其中 为某个取值。该公式称为中心差分。由 2.5.4 节的泰勒展开,我们有for some . This formula is called a central difference. From the Taylor expansion in Section 2.5.4, we have
从而so that
可见,偶数阶导数对应的项相互抵消,因而该公式的截断误差达到 阶。对任意 ,中心差分的截断误差都远小于前向差分或后向差分的截断误差,图 7.1 的经验结果也印证了这一点。We see that the terms corresponding to derivatives of even order canceled out, allowing the formula to achieve truncation error. For any , the truncation error of the central difference is much smaller than the one of the forward or backward differences as confirmed empirically in Fig. 7.1.
若利用更多的函数取值,还可进一步降低截断误差。前向差分格式可推广为如下形式:The truncation error can be further reduced by making use of additional function evaluations. One can generalize the forward difference scheme by a formula of the form
该式需进行 次求值。为选取 并使截断误差达到 阶,可对求和中的每一项作泰勒展开,得到requiring evaluations. To select the and reach a truncation error of order , we can use a Taylor expansion on each term of the sum to get
按导数的各阶对求和项分组,便得到 个系数 所需满足的 个方程,即By grouping the terms in the sum for each order of derivative, we obtain a set of equations to be satisfied by the coefficients , that is,
该方程组可解析求解,从而定出各系数。后向差分同样可用 推广。类似地,中心差分格式可通过This system of equations can be solved analytically to derive the coefficients. Backward differences can be generalized similarly by using . Similarly, the central difference scheme can be generalized by using
推广,使截断误差达到 阶。按上述方法解出系数 ,可得 。因此,只需进行 次求值。to reach a truncation error of order . Solving for the coefficients as above reveals that . Therefore, only evaluations are necessary.
要近似高阶导数,可循类似思路。也就是说,前向差分格式可推广为用下式近似 阶导数:To approximate higher order derivatives, we can follow a similar reasoning. Namely, we can generalize the forward difference scheme to approximate the derivative of order by
与之前一样,可将求和中的各项展开。为使近似只捕捉 阶导数,此时要求系数 满足As before, we can expand the terms in the sum. For the approximation to capture only the th derivative, we now require the coefficients to satisfy
由此得到的系数可使截断误差达到 阶,而所需求值次数为 。例如,当 时,可将二阶导数近似为With the resulting coefficients, we obtain a truncation error of order , while making evaluations. For example, for , we can approximate the second-order derivative as
其截断误差为 阶。with a truncation error of order .
中心差分格式可类似地推广为The central difference scheme can be generalized similarly by
使截断误差达到 阶。例如,当 、 时,可得二阶中心差分to reach truncation errors of order . For example, for , , we obtain the second-order central difference
由泰勒展开可见,这一次奇数阶导数对应的项相互抵消,截断误差为 ,而所需求值次数为 。By using a Taylor expansion we see that, this time, the terms corresponding to derivatives of odd order cancel out and the truncation error is while requiring evaluations.
设 在 上良定义,后者即 维复数的空间。记虚数单位为 。于是, 的泰勒展开为Suppose is well defined on , the space of -dimensional complex numbers. Let us denote the imaginary unit by . Then, the Taylor expansion of reads
可见,实部对应偶数阶项,虚部对应奇数阶项。由此可得We see that the real part corresponds to even-degree terms and the imaginary part corresponds to odd-degree terms. We therefore obtain
以及and
这表明,我们可以用如下近似来计算方向导数:This suggests that we can compute directional derivatives using the approximation
其中 。这一方法称为复步法导数近似 [186, 130]。for . This is called the complex-step derivative approximation [186, 130].
与前向差分、后向差分和中心差分不同,这里只需调用一次函数。对复数调用一次函数的开销,大约是对实数调用一次函数的两倍。不过,由于不再需要计算函数之差,复步法导数近似通常具有更小的舍入误差,如图 7.1 所示。话虽如此,该方法的一个缺点是:实现函数 的程序中,所有基本操作都必须在复数上良定义,例如借助运算符重载来实现。Contrary to forward, backward and central differences, we see that only a single function call is necessary. A function call on complex numbers may take roughly twice the cost of a function call on real numbers. However, thanks to the fact that a difference of functions is no longer needed, the complex-step derivative approximation usually enjoys smaller round-off error as illustrated in Fig. 7.1. That said, one drawback of the method is that all elementary operations within the program implementing the function must be well-defined on complex numbers, e.g., using overloading.
下面从函数求值次数的角度,讨论有限差分与复步法导数的计算复杂度。具体而言,由于这是机器学习中最常见的情形,我们考虑单个 输出的情况,即要对一个函数 求导。无论采用前向差分、后向差分还是中心差分,计算任意方向 上的方向导数 的计算复杂度都相当于调用 两次。至于梯度 的计算,我们可以利用(见定义 2.2.4)We now discuss the computational complexity in terms of function evaluations of finite differences and complex-step derivatives. For concreteness, as this is the most common use case in machine learning, we discuss the case of a single output, i.e., we want to differentiate a function . Whether we use forward, backward or central differences, the computational complexity of computing the directional derivative in any direction amounts to two calls to . For computing the gradient , we can use (see Definition 2.2.4) that
对于 。因此,若采用前向差分或后向差分,计算梯度需要 次函数调用;若采用中心差分,则需要 次函数调用。对于复步法近似,则需要 次复函数调用。这些复杂度总结于表 7.1。for . For forward and backward differences, we therefore need function calls to compute the gradient, while we need function calls for central differences. For the complex step approximation, we need complex function calls. We summarize the complexities in Table 7.1.
| 方向导数Directional derivative | 梯度Gradient | |
|---|---|---|
| 前向差分Forward difference | ||
| 后向差分Backward difference | ||
| 中心差分Central difference | ||
| 复步法Complex step |
有限差分是一种仅凭函数求值即可数值计算导数的简便方法。Finite differences are a simple way to numerically compute derivatives using only function evaluations.
中心差分的截断误差小于前向差分和后向差分。若愿意付出更多函数求值的代价,还可进一步减小截断误差。Central differences achieve smaller truncation error than forward and backward differences. It is possible to achieve smaller truncation error, at the cost of more function evaluations.
复步法导数的舍入误差小于中心差分,但要求函数本身以及实现该函数的程序在复数上良定义。Complex-step derivatives achieve smaller round-off error than central differences but require the function and the program implementing it to be well-defined on complex numbers.
然而,无论采用哪种方法,有限差分所需的函数调用次数都与维数成正比。因此,在维度动辄数百万乃至数十亿的机器学习中,这类方法鲜有使用。有限差分的主要应用场景因而有两类:i)低维黑箱函数;ii)测试用途(例如检查梯度函数是否实现正确)。However, whatever the method used, finite differences require a number of function calls that is proportional to the number of dimensions. They are therefore seldom used in machine learning, where there can be millions or billions of dimensions. The main use cases of finite differences are therefore i) for blackbox functions of low dimension and ii) for test purposes (e.g., checking that a gradient function is correctly implemented).
在现代机器学习中,主要依靠的是自动微分,因为它能够利用函数的复合结构。这正是下一章要研究的内容。For modern machine learning, the main workhorse is automatic differentiation, as it leverages the compositional structure of functions. This is what we study in the next chapter.