第 23 章 · 数学
Understanding Deep Learning — Mathematics
This appendix reviews mathematical concepts that are used in the main text.
本附录回顾了正文中使用的数学概念。
A function defines a mapping from a set X (e.g., the set of real numbers) to another set Y. An injection is a one-to-one function where every element in the first set maps to a unique position in the second set (but there may be elements of the second set that are not mapped to). A surjection is a function where every element in the second set receives a mapping from the first (but there may be multiple elements of the first set that are mapped to the same element of the second set). A bijection or bijective mapping is a function that is both injective and surjective. It provides a one-to-one correspondence between all members of the two sets. A diffeomorphism is a special case of a bijection where both the forward and reverse mapping are differentiable.
一个函数定义了从集合 X (例如实数集)到另一个集合 Y 的映射。一个单射是一对一函数,其中第一个集合中的每个元素都映射到第二个集合中的唯一位置(但第二个集合中可能存在未被映射到的元素)。一个满射是这样一个函数:第二个集合中的每个元素都有来自第一个集合的映射(但第一个集合中可能有多个元素映射到第二个集合中的同一个元素)。一个双射或双射映射是既单射又满射的函数。它在两个集合的所有成员之间提供一一对应。一个微分同胚是双射的特殊情况,其中正向映射和反向映射都可微。
A function f[z] is Lipschitzcontinuous if for all z1, z2: (B.1)
where β is known as the Lipschitz constant and determines the maximum gradient of the function (i.e., how fast the function can change) with respect to the distance metric. If the Lipschitz constant is less than one, the function is a contraction mapping, and we can use Banach’s theorem to find the inverse for any point (see figure 16.9).
函数 f[z] 是利普希茨连续的,若对所有 z1, z2:
(B.1) 其中, β 被称为利普希茨常数,它决定了函数相对于距离度量的最大梯度(即函数变化的速度有多快)。如果利普希茨常数小于 1,则该函数是压缩映射,我们可以使用巴拿赫定理为任意点求逆(见图 16.9)。

Composing two functions with Lipschitz constants β1 and β2 creates a new Lipschitz continuous function with a constant that is less than or equal to β1β2.
Adding two functions with Lipschitz constants β1 and β2 creates a new Lipschitz continuous function with a constant that is less than or equal to β1+β2. The Lipschitz constant of a linear transformation f[ z ] = Az + b with respect to a Euclidean distance measure is the maximum eigenvalue of A.
将两个分别具有利普希茨常数 β1 和 β2 的函数复合,会得到一个新的利普希茨连续函数,其利普希茨常数小于或等于 β1β2。将两个分别具有利普希茨常数 β1 和 β2 的函数相加,会得到一个新的利普希茨连续函数,其利普希茨常数小于或等于 β1+β2。线性变换 f[ z ] = Az + b 相对于欧几里得距离度量的利普希茨常数是 A 的最大特征值。
A function is convex if we can draw a straight line between any two points on the function, and this line always lies above the function. Similarly, a function is concave if a straight line between any two points always lies below the function. By definition, convex (concave) functions have at most one minimum (maximum).
A region of RD is convex if we can draw a straight line between any two points on the boundary of the region without intersecting the boundary in another place. Gradient descent guarantees to find the global minimum of any function that is both convex and defined on a convex region.
如果可以在函数上任意两点之间画一条直线,且这条直线始终位于函数上方,则该函数是凸函数。类似地,如果任意两点之间的直线始终位于函数下方,则该函数是凹函数。根据定义,凸(凹)函数至多有一个最小值(最大值)。
RD 中的一个区域是凸的,如果可以在该区域边界上的任意两点之间画一条直线,而不在其他地方与边界相交。梯度下降保证能够找到任何既是凸函数又定义在凸区域上的函数的全局最小值。
The following functions are used in the main text: • The exponentialfunctiony= exp[x] (figure B.1a) maps a real variable x ∈R to a
non-negative number y∈R+ as y= ex. • The logarithmx=log[y] (figure B.1b) is the inverse of the exponential function and maps a non-negative number y∈R+ to a real variable x∈R. Note that all
logarithms in this book are natural (i.e., in base e).
正文中使用了以下函数:
• 指数函数 y= exp[x] (图 B.1a)将实变量 x ∈R 映射为非负数 y∈R+,即 y= ex。 • 对数 x= log[y] (图 B.1b)是指数函数的逆函数,将非负数 y∈R+ 映射为实
变量 x∈R。请注意,本书中所有对数均为自然对数(即以 e 为底)。
• The gammafunctionΓ[x] (figure B.1c) is defined as: (B.2)
• 伽马函数 Γ[x] (图 B.1c)定义为: (B.2)
• The Dirac delta function δ[ z ] has a total area of one, all of which is at position z = 0. A dataset with N elements can be thought of as a probability distribution consisting
of a sum of N delta functions centered at each data point x i and scaled by 1/N.
这将阶乘函数推广到连续值,使得 Γ[x] = (x −1)! 对 x ∈{1, 2,...} 成立。 • 狄拉克 delta 函数 δ[ z ] 的总面积为 1,且全部集中于位置 z = 0。包含 N 个元素的数据集可视为一个概率分布,该分布由 N 个 delta 函数之和构成,这些 delta函数以每个数据点 x i 为中心,并按 1/N 缩放。 delta 函数通常绘制为箭头(例如,
The delta function is usually drawn as an arrow (e.g., figure 5.12). The delta
function has the key property that: (B.3)
图 5.12)。 delta 函数具有如下关键性质: (B.3)
Stirling’s formula (figure B.2) approximates the factorial function (and hence the Gamma function) using the formula: (B.4)
斯特林公式(图 B.2)用以下公式近似阶乘函数(从而近似伽马函数): (B.4)


![图 B.2 斯特林公式。阶乘函数 x!可由斯特林公式Stir[x] 近似,该公式对所有实数值都有定义。](/learning/udl/ch23/figures/ch23_p03_f1.webp)
Binomial coefficients are written as \binom{n}{k} and pronounced as “n choose k.” They are positive integers that represent the number of ways of choosing an unordered subset of k items from a set of n items without replacement. Binomial coefficients can be computed using the simple formula: (B.5)
二项式系数写作 \binom{n}{k},读作“n 选 k”。它们是正整数,表示从 n 个元素组成的集合中不放回地选择 k 个元素构成的无序子集的方法数。二项式系数可用简单公式计算: (B.5)
The autocorrelation r[τ] of a continuous function f[z] is defined as: (B.6)
where τ is the time lag. Sometimes, this is normalized by r[0] so that the autocorrelation at time lag zero is one. The autocorrelation function is a measure of the correlation of the function with itself as a function of an offset (i.e., the time lag). If a function changes slowly and predictably, then the autocorrelation function will decrease slowly as the time lag increases from zero. If the function changes fast and unpredictably, then it will decrease quickly to zero.
连续函数 f[z] 的自相关 r[τ] 定义为: (B.6)
其中 τ 是时间滞后。有时,会由 r[0] 对其进行归一化,使得在时间滞后为零时的自相关值为 1。自相关函数衡量的是函数与其自身之间的相关性随偏移量(即时间滞后)变化的情况。如果一个函数变化缓慢且可预测,那么随着时间滞后从零开始增大,自相关函数会缓慢下降。如果函数变化快速且不可预测,那么它会迅速下降至零。
In machine learning, a vector x ∈RD is a one-dimensional array of D numbers, which we will assume are organized in a column. Similarly, a matrix Y ∈RD1×D2 is a twodimensional array of numbers with D1 rows and D2 columns. A tensor z ∈RD1×D2...×DN is an N-dimensional array of numbers. Confusingly, all three of these quantities are stored in objects known as “tensors” in deep learning APIs such as PyTorch and TensorFlow.
在机器学习中,向量 x ∈RD 是一个一维数组,其中包含 D 个数字,我们假设这些数字按列排列。类似地,矩阵 Y ∈RD1×D2 是一个二维数字数组,有 D1 行和 D2 列。张量 z ∈RD1×D2...×DN 是一个 N 维数字数组。令人困惑的是,在 PyTorch 和 TensorFlow 等深度学习 API 中,这三种量都存储在称为“张量”的对象中。
The transpose A T∈RD2×D1 of a matrix A ∈RD1×D2 is formed by reflecting it around the principal diagonal so that the kth column becomes the kth row and vice-versa. If we take the transpose of a matrix product AB, then we take the transpose of the original matrices but reverse the order so that (B.7)
The transpose of a column vector a is a row vector a T and vice-versa.
转置 A T∈RD2×D1 是通过绕主对角线反射矩阵 A ∈RD1×D2 形成的,使得 kth 列变为 kth 行,反之亦然。如果我们取矩阵乘积 AB 的转置,那么我们对原矩阵取转置,但颠倒顺序,使得(B.7)
列向量 a 的转置是行向量 a T,反之亦然。
For a vector z, the ℓp norm is defined as: (B.8)
对于向量 z, ℓp 范数定义为: (B.8)
for real-valued p>1.
When p=2, this returns the length of the vector, and this is known as the Euclidean norm. It is this case that is most commonly used in deep learning, and often the exponent p is omitted, and the Euclidean norm is just written as || z ||. When p = ∞, the operator returns the maximum absolute value in the vector.
Norms can be computed in a similar way for matrices. For example, the ℓ2 norm of a matrix Z (known as the Frobeniusnorm) is calculated as: (B.9)
对于实值 p>1。当 p=2 时,这会返回向量的长度,这称为欧几里得范数。在深度学习中,最常使用的就是这种情况,并且通常会省略指数 p,将欧几里得范数直接写为 || z ||。当 p = ∞ 时,该算子返回向量中的最大绝对值。
对于矩阵,范数也可以以类似方式计算。例如,矩阵 Z 的 ℓ2 范数(称为 Frobenius 范数)计算如下: (B.9)
The product C = AB of two matrices A ∈RD1×D2 and B ∈RD2×D3 is a third matrix C ∈RD1×D3 where: (B.10)
(B.10)
The dotproduct a T b of two vectors a ∈RD and b ∈RD is a scalar and is defined as: (B.11)
两个向量 a ∈RD 和 b ∈RD 的点积 a T b 是一个标量,定义为: (B.11)
It can be shown that the dot product is proportional to the Euclidean norm of the first vector times the Euclidean norm of the second vector times the angle θ between them: (B.12)
可以证明,点积与第一个向量的欧几里得范数、第二个向量的欧几里得范数以及它们之间夹角 θ 的乘积成正比: (B.12)
A square matrix A may or may not have an inverse A −1 such that A −1 A = AA −1= I. If a matrix does not have an inverse, it is called singular.
方阵 A 可能有逆矩阵 A −1,也可能没有;若有,则满足 A −1 A = AA −1= I。如果矩阵没有逆矩阵,则称为奇异。若对矩阵乘积 AB 求逆,其中 A 和 B 为方阵且可逆,则等价地可对每个矩阵分别求逆并交换乘法顺序。
If we take the inverse of a matrix product AB where A and B are square and invertible, then we can equivalently take the inverse of each matrix individually and reverse the order of multiplication. (B.13)
In general, it takes O[D3] operations to invert a D×D matrix. However, inversion is more efficient for special types of matrices, including diagonal, orthogonal, and triangular matrices (see section B.4).
(B.13) 通常,需要 O[D3] 次运算才能对 D×D 矩阵求逆。然而,对于特殊类型的矩阵,求逆更为高效,包括对角矩阵、正交矩阵和三角矩阵(见 B.4 节)。
Consider a matrix A ∈RD1×D2. If the number of columns D2 of the matrix is fewer than the number of rows D1 (i.e., the matrix is “portrait”), the product Ax cannot reach all
考虑一个矩阵 A ∈RD1×D2。如果该矩阵的列数 D2 少于行数 D1 (即该矩阵为“竖版”),则乘积 Ax 无法到达所有

possible positions in the D1 -dimensional output space. This product consists of the D2 columns of A weighted by the D2 elements of x and can only reach the linearsubspace that is spanned by these columns. This is known as the column space of the matrix. Conversely, for a landscape matrix A, the part of the input space that maps to zero (i.e., those x where Ax = 0) is termed the nullspace of the matrix.
在 D1 维输出空间中的可能位置。该乘积由 A 的 D2 列以 x 的 D2 个元素为权重加权而成,且只能到达这些列张成的线性子空间。这被称为该矩阵的列空间。相反,对于横版矩阵 A,输入空间中映射为零的部分(即那些 x 满足 Ax = 0)被称为该矩阵的零空间。
If we multiply the set of 2D points on a unit circle by a 2 × 2 matrix A, they map to an ellipse (figure B.3). The radii of the major and minor axes of this ellipse (i.e., the longest and shortest directions) correspond to the magnitude of the singularvaluesλ1 and λ2 of the matrix. The same idea applies in higher dimensions. A D− dimensional spheroid is mapped by a D× D matrix A to a D -dimensional ellipsoid. The radii of the D principal axes of this ellipsoid determine the magnitude of the singular values. For symmetric square matrices, the same information is captured by the eigenvalues which are here the same as the singular values.
The spectral norm of a square matrix is the largest absolute eigenvalue. It captures the largest possible change in magnitude when the matrix is applied to a vector of unit length. As such, it tells us about the Lipschitz constant of the transformation. The set of eigenvalues is sometimes called the eigenspectrum and tells us about the magnitude of the scaling applied by the matrix across all directions. This information can be summarized using the determinant and trace of the matrix.
如果我们用 2 × 2 矩阵 A 乘以单位圆上的二维点集,它们会映射为一个椭圆(图 B.3)。该椭圆长轴和短轴的半径(即最长和最短方向)对应于矩阵的奇异值 λ1 和 λ2 的大小。同样的思想适用于更高维度。 D− 维球体经 D× D 矩阵 A 映射为 D 维椭球。该椭球 D 条主轴的半径决定了奇异值的大小。对于对称方阵,同样的信息可由特征值刻画,这里的特征值与奇异值相同。
一个方阵的谱范数是其绝对值最大的特征值。它刻画了将矩阵作用于单位长度向量时,向量模长的最大可能变化。因此,它告诉我们该变换的 Lipschitz 常数。特征值集合有时称为特征谱,它告诉我们矩阵在所有方向上施加的缩放幅度。这些信息可以用矩阵的行列式和迹来概括。
Every square matrix A has a scalar associated with it called the determinant and denoted by | A | or det[ A ], which is the product of the eigenvalues. It is hence related to the average scaling applied by the matrix for different inputs. Matrices with small absolute determinants tend to decrease the norm of vectors upon multiplication. Matrices with
large absolute determinants tend to increase the norm.
If a matrix is singular, the determinant will be zero, and there will be at least one direction in space that is mapped to the origin when the matrix is applied. Determinants of matrix expressions obey the following rules: (B.14)
每个方阵 A 都有一个与之关联的标量,称为行列式,记作 | A | 或 det[ A ],,它等于特征值的乘积。因此,它与矩阵对不同输入施加的平均缩放有关。行列式绝对值较小的矩阵在乘法运算中往往会缩小向量的范数。具有
绝对值较大的行列式往往会增大范数。如果矩阵是奇异的,行列式将为零,并且在空间中存在至少一个方向,在应用该矩阵时会被映射到原点。矩阵表达式的行列式遵循以下规则: (B.14)
The trace of a square matrix is the sum of the diagonal values (the matrix itself need not be diagonal) or the sum of the eigenvalues. Traces obey these rules: trace[ A T ] trace[ A ] trace[ AB ] trace[ BA ] trace[ A + B ] trace[ A ] + trace[ B ] trace[ ABC ] trace[ BCA ] = trace[ CAB ], (B.15) where in the last relation, the trace is invariant for cyclic permutations only, so in general, trace[ ABC ] ̸= trace[ BAC ].
方阵的迹是其对角线元素之和(矩阵本身不必为对角矩阵),或其特征值之和。迹满足以下规则: trace[ A ] trace[ BA ] trace[ A ] + trace[ B ] trace[ BCA ] = trace[ CAB ], (B.15)
Calculating the inverse of a square matrix A ∈RD×D has a complexity of O[D3], as does the computation of the determinant. However, for some matrices with special properties, these computations can be more efficient.
计算方阵 A ∈RD×D 的逆矩阵的复杂度为 O[D3],计算行列式的复杂度也是如此。然而,对于某些具有特殊性质的矩阵,这些计算可以更加高效。
A diagonal matrix has zeros everywhere except on the principal diagonal. If these diagonal entries are all non-zero, the inverse is also a diagonal matrix, with each diagonal entry dii replaced by 1/dii. The determinant is the product of the values on the diagonal. A special case of this is the identity matrix, which has ones on the diagonal. Consequently, its inverse is also the identity matrix, and its determinant is one.
一个对角矩阵除主对角线外所有元素均为零。如果这些对角线元素均非零,则其逆矩阵也是对角矩阵,且每个对角线元素 dii 被替换为 1/dii。行列式等于对角线上各值的乘积。其特例是单位矩阵,其对角线元素均为 1。因此,它的逆矩阵也是单位矩阵,行列式为 1。
A lower triangular matrix has all of its non-zero values on the principal diagonal and/or the positions below this. An upper triangular matrix has all of its non-zero values on the principal diagonal and/or the positions above this. In both cases, the matrix can
be inverted in O[D2] (see problem 16.4), and the determinant is just the product of the values on the diagonal.
一个下三角矩阵的所有非零元素都位于主对角线上和 / 或其下方。一个上三角矩阵的所有非零元素都位于主对角线上和 / 或其上方。在两种情况下,该矩阵都可
在 O[D2] 中求逆(见习题 16.4),且行列式仅为对角线上元素的乘积。
Orthogonal matrices represent rotations and reflections around the origin, so in figure B.3, the circle would be mapped to another circle of unit radius but rotated and possibly reflected. Accordingly, the eigenvalues must all have magnitude one, and the determinant must be either one or minus one. The inverse of an orthogonal matrix is its transpose,
正交矩阵表示绕原点的旋转和反射,因此在图 B.3 中,圆会被映射为另一个半径为 1 的圆,但会旋转,并可能被反射。相应地,其特征值的模都必须为 1,行列式必须为 1 或 -1。正交矩阵的逆矩阵是其转置,因此 A −1= A T。
A permutation matrix has exactly one non-zero entry in each row and column, and all of these entries take the value one. It is a special case of an orthogonal matrix, so its inverse is its own transpose, and its determinant is always ±1. As the name suggests, it has the effect of permuting the entries of a vector. For example: (B.16)
置换矩阵在每一行和每一列中恰好有一个非零元素,并且所有这些元素的值都是 1。它是正交矩阵的特例,因此其逆矩阵就是其转置,行列式始终为 ±1。顾名思义,它的作用是置换向量中的元素。例如: (B.16)
Linear algebra is the mathematics of linear functions, which have the form: (B.17)
线性代数是关于线性函数的数学,其形式为: (B.17)
where ϕ1,..., ϕD are parameters that define the function.
We often add a constant term ϕ0 to the right-hand side. This is technically an affine function but is commonly referred to as linear in machine learning. We adopt this convention throughout.
其中 ϕ1,..., ϕD 是定义该函数的参数。我们常在右侧添加常数项 ϕ0。这在严格意义上是一个仿射函数,但在机器学习中通常称为线性函数。我们在全文中沿用这一约定。
Consider a collection of linear functions: (B.18)
考虑一组线性函数: (B.18)
These can be written in matrix form as: (B.19)
这些可以写成矩阵形式如下: (B.19)
Most readers of this book will be accustomed to the idea that if we have a function y= f[x], we can compute the derivative ∂y/∂x, and this represents how y changes when we make a small change in x. This idea extends to functions y= f[ x ] mapping a vector x to a scalar y, functions y = f[ x ] mapping a vector x to a vector y, functions y = f[ X ] mapping a matrix X to a vector y, and so on. The rules of matrixcalculus help us compute derivatives of these quantities. The derivatives take the following forms: • For a function y= f[ x ] where y∈R and x ∈RD, the derivative ∂y/∂ x is also a
D -dimensional vector, where the ith element is computed as ∂y/∂xi. • For a function y = f[ x ] where y ∈RDy and x ∈RDx, the derivative ∂ y /∂ x is a Dx× Dy matrix where element(i, j) contains the derivative ∂yj/∂xi. This is
known as a Jacobian and is sometimes written as ∇ x y in other documents. • For a function y = f[ X ] where y ∈RDy and X ∈RD1×D2, the derivative ∂ y /∂ X
本书的大多数读者都会习惯这样一个概念:如果我们有一个函数 y= f[x],就可以计算导数 ∂y/∂x,它表示当我们对 x 做微小改变时 y 如何变化。这一思想可以推广到函数 y= f[ x ],它将向量 x 映射为标量 y;函数 y = f[ x ],它将向量 x 映射为向量 y;函数 y = f[ X ],它将矩阵 X 映射为向量 y,等等。矩阵微积分的规则帮助我们计算这些量的导数。这些导数具有以下形式: • 对于函数 y= f[ x ],其中 y∈R 且 x ∈RD,导数 ∂y/∂ x 也是一个 D 维向量,其
中第 ith 个元素按 ∂y/∂xi 计算。 • 对于函数 y = f[ X ],其中 y ∈RDy 且 X ∈RD1×D2,导数 ∂ y /∂ X 是一个三维张量,
is a 3D tensor containing the derivatives ∂yi/∂xjk.
Often these matrix and vector derivatives have superficially similar forms to the scalar case. For example, we have: (B.20) and (B.21)
包含各个导数 ∂yi/∂xjk。通常,这些矩阵和向量导数的形式与标量
r c 情况。例如,我们有: (B.20) and (B.21)