学习资料 理解深度学习 第 9 章 · 正则化
学 习 资 料 · 中 英 对 照

第 9 章 · 正则化

Understanding Deep Learning — Regularization

正则化
Regularization

Chapter 8 described how to measure model performance and identified that there could be a significant performance gap between the training and test data. Possible reasons for this discrepancy include: (i) the model describes statistical peculiarities of the training data that are not representative of the true mapping from input to output (overfitting), and (ii) the model is unconstrained in areas with no training examples, leading to suboptimal predictions.

This chapter discusses regularization techniques. These are a family of methods that reduce the generalization gap between training and test performance. Strictly speaking, regularization involves adding explicit terms to the loss function that favor certain parameter choices. However, in machine learning, this term is commonly used to refer to any strategy that improves generalization.

第 8 章描述了如何衡量模型性能,并指出训练数据和测试数据之间可能存在显著的性能差距。造成这种差异的可能原因包括: (i) 模型描述了训练数据中不代表输入到输出真实映射的统计特性(过拟合),以及 (ii) 模型在没有训练样本的区域不受约束,导致次优预测。

本章讨论正则化技术。这些技术是一类用于缩小训练性能与测试性能之间泛化差距的方法。严格来说,正则化涉及向损失函数添加有利于某些参数选择的显式项。然而,在机器学习中,这个术语通常用于指任何能够改善泛化的策略。

We start by considering regularization in its strictest sense.

Then we show how the stochastic gradient descent algorithm itself favors certain solutions. This is known as implicit regularization. Following this, we consider a set of heuristic methods that improve test performance.

These include early stopping, ensembling, dropout, label smoothing, and transfer learning.

我们首先从最严格的意义上考虑正则化。然后,我们说明随机梯度下降算法本身如何偏向某些解。这被称为隐式正则化。此后,我们考虑一组改善测试性能的启发式方法。这些方法包括提前停止、集成、随机失活、标签平滑和迁移学习。

9.1 显式正则化
9.1 Explicit regularization

Consider fitting a model f[ x , ϕ] with parameters ϕ using a training set{ x i, y i} of in- put/output pairs. We seek the parameters ˆϕ that minimize the loss function L[ϕ]: where the individual terms ℓi[ x i, y i] measure the mismatch between the network predictions f[ x i, ϕ] and output targets y i for each training pair. To bias this minimization

考虑拟合模型 f[ x ϕ],使用参数 ϕ,并利用由输入 / 输出对组成的训练集{ x i, y i}。我们寻找使损失函数 L[ϕ] 最小化的参数 ϕ:其中,各个项 ℓi[ x i, y i] 衡量网络预测 f[ x i, ϕ] 与输出目标 y i 之间在每个训练对上的不匹配。为了对这种最小化施加偏差

图 9.1 显式正则化。 a) Gabor 模型的损失函数(见 6.1.2 节)。青色圆圈表示局部极小值。灰色圆圈表示全局极小值。 b) 正则化项通过随着远离该
图 9.1 显式正则化。 a) Gabor 模型的损失函数(见 6.1.2 节)。青色圆圈表示局部极小值。灰色圆圈表示全局极小值。 b) 正则化项通过随着远离该点而施加逐渐增大的惩罚,来偏好靠近图中心的参数。 c) 最终损失函数是原始损失函数与正则化项之和。该曲面具有更少的局部极小值,并且全局极小值移动到了不同位置(箭头显示变化)。Figure 9.1 Explicit regularization. a) Loss function for Gabor model (see section 6.1.2). Cyan circles represent local minima. Gray circle represents the global minimum. b) The regularization term favors parameters close to the center of the plot by adding an increasing penalty as we move away from this point. c) The final loss function is the sum of the original loss function plus the regularization term. This surface has fewer local minima, and the global minimum has moved to a different position (arrow shows change).

toward certain solutions, we include an additional term:

where g[ϕ] is a function that returns a scalar which takes larger values when the parameters are less preferred. The term λ is a positive scalar that controls the relative contribution of the original loss function and the regularization term. The minima of the regularized loss function usually differ from those in the original, so the training procedure converges to different parameter values (figure 9.1).

偏向某些解,我们加入一个额外项:

其中 g[ϕ] 是一个函数,当参数较不受偏好时,它返回的标量值更大。项 λ 是一个正标量,用于控制原始损失函数和正则化项的相对贡献。正则化损失函数的极小值通常不同于原始损失函数的极小值,因此训练过程会收敛到不同的参数值(图 9.1)。

9.1.1 概率解释
9.1.1 Probabilistic interpretation

Regularization can be viewed from a probabilistic perspective. Section 5.1 shows how loss functions are constructed from the maximum likelihood criterion:

The regularization term can be considered as a priorPr(ϕ) that represents knowledge about the parameters before we observe the data and we now have the maximum a posteriori or MAP criterion:

正则化可以从概率的角度来理解。第 5.1 节展示了如何根据极大似然准则构建损失函数:

正则化项可以被视为一个先验 Pr(ϕ),它代表在观测数据之前关于参数的知识,现在我们得到最大后验或 MAP 准则:

Moving back to the negative log-likelihood loss function by taking the log and multiplying by minus one, we see that λ · g[ϕ] = −log[Pr(ϕ)].

回到负对数似然损失函数,通过取对数并乘以负一,我们发现 λ · g[ϕ] = − log[Pr (ϕ) ]。

9.1.2 L2 正则化
9.1.2 L2 regularization

This discussion has sidestepped the question of which solutions the regularization term should penalize (or equivalently that the prior should favor). Since neural networks are used in an extremely broad range of applications, these can only be very generic preferences. The most commonly used regularization term is the L2 norm, which penalizes the sum of the squares of the parameter values: where j indexes the parameters. This is also referred to as Tikhonovregularization or

这一讨论避开了正则化项应当惩罚哪些解(或等价地说,先验应当偏爱哪些解)的问题。由于神经网络被应用于极其广泛的领域,这些偏好只能是非常通用的。最常用的正则化项是 L2 范数,它惩罚参数值的平方和:其中, j 对参数进行索引。这也称为吉洪诺夫正则化或

ridge regression, or (when applied to matrices) Frobenius norm regularization.

For neural networks, L2 regularization is usually applied to the weights but not the biases and is hence referred to as a weight decay term. The effect is to encourage smaller weights, so the output function is smoother. To see this, consider that the output prediction is a weighted sum of the activations at the last hidden layer. If the weights have a smaller magnitude, the output will vary less. The same logic applies to

岭回归,或(当应用于矩阵时)弗罗贝尼乌斯范数正则化。对于神经网络, L2 正则化通常应用于权重而不应用于偏差,因此被称为权重衰减项。其作用是促使权重更小,因此输出函数更平滑。要理解这一点,可以考虑输出预测是最后一个隐藏层激活值的加权和。如果权重具有较小的幅度,输出变化就会更小。同样的逻辑适用于最后一个隐藏层预激活值

the computation of the pre-activations at the last hidden layer and so on, progressing backward through the network. In the limit, if we forced all the weights to be zero, the network would produce a constant output determined by the final bias parameter.

Figure 9.2 shows the effect of fitting the simplified network from figure 8.4 with weight decay and different values of the regularization coefficient λ. When λ is small, it has little effect. However, as λ increases, the fit to the data becomes less accurate, and the function becomes smoother. This might improve the test performance for two reasons: • If the network is overfitting, then adding the regularization term means that the network must trade off slavish adherence to the data against the desire to be smooth. One way to think about this is that the error due to variance reduces (the model no longer needs to pass through every data point) at the cost of increased

bias (the model can only describe smooth functions). • When the network is over-parameterized, some of the extra model capacity de- scribes areas with no training data. Here, the regularization term will favor func-

的计算,并以此类推,沿网络反向逐层推进。在极限情况下,如果我们强制所有权重为零,网络将产生由最终偏差参数决定的恒定输出。

图 9.2 展示了使用权重衰减以及不同的正则化系数 λ 对图 8.4 中的简化网络进行拟合的效果。当 λ 较小时,其影响很小。然而,随着 λ 增大,对数据的拟合变得不那么准确,函数变得更平滑。这可能出于两个原因改善测试性能: • 如果网络过拟合,那么添加正则化项意味着网络必须在盲目贴合数据与追求平滑之间进行权衡。一种理解方式是,由方差引起的误差会减小(模型不再需要通过每

个数据点),但代价是偏差增加(模型只能描述平滑函数)。 • 当网络过参数化时,额外的模型容量中有一部分会描述没有训练数据的区域。此时,正则化项会偏好能在邻近点之间平滑插值的函数。在不了解真实函数的情况下,

tions that smoothly interpolate between the nearby points. This is reasonable

behavior in the absence of knowledge about the true function.

这是合理的行为。

图 9.2 在含 14 个隐藏单元的简化网络中的 L2 正则化(见图 8.4)。 a–f) 随着正则化系数  λ 增大而拟合出的函数。黑色曲线为真实函数,橙色圆
图 9.2 在含 14 个隐藏单元的简化网络中的 L2 正则化(见图 8.4)。 a–f) 随着正则化系数 λ 增大而拟合出的函数。黑色曲线为真实函数,橙色圆圈为含噪训练数据,青色曲线为拟合模型。对于较小的 λ (子图 a–b),拟合函数恰好穿过数据点。对于中等大小的 λ (子图 c–d),函数更平滑,更接近真值。对于较大的 λ (子图 e–f),正则化项压过似然项,因此拟合函数过于平滑,整体拟合更差。Figure 9.2 L2 regularization in simplified network with 14 hidden units (see figure 8.4). a–f) Fitted functions as we increase the regularization coefficient λ. The black curve is the true function, the orange circles are the noisy training data, and the cyan curve is the fitted model. For small λ (panels a–b), the fitted function passes exactly through the data points. For intermediate λ (panels c–d), the function is smoother and more similar to the ground truth. For large λ (panels e–f), the regularization term overpowers the likelihood term, so the fitted function is too smooth and the overall fit is worse.
9.2 隐式正则化
9.2 Implicit regularization

An intriguing recent finding is that neither gradient descent nor stochastic gradient descent moves neutrally to the minimum of the loss function; each exhibits a preference for some solutions over others. This is known as implicit regularization.

一个引人注目的近期发现是,梯度下降和随机梯度下降都不会中性地走向损失函数的最小值;二者都表现出对某些解的偏好。这被称为隐式正则化。

9.2.1 梯度下降中的隐式正则化
9.2.1 Implicit regularization in gradient descent

Consider a continuous version of gradient descent where the step size is infinitesimal. The change in parameters ϕ will be governed by the differential equation:

考虑梯度下降的一个连续版本,其中步长为无穷小。参数 ϕ 的变化将由微分方程决定:

图 9.3 梯度下降中的隐式正则化。 a) 损失函数在水平线  ϕ1= 0.61 上有一族全局极小值。蓝色虚线显示从左下角开始的连续梯度下降路径。青色轨迹显示步
图 9.3 梯度下降中的隐式正则化。 a) 损失函数在水平线 ϕ1= 0.61 上有一族全局极小值。蓝色虚线显示从左下角开始的连续梯度下降路径。青色轨迹显示步长为 0.1 的离散梯度下降(前几步以箭头明确显示)。有限步长导致两条路径发散,并到达不同的最终位置。 b) 这种差异可以通过在连续梯度下降损失函数中添加一个正则化项来近似,该项惩罚梯度幅度的平方。 c) 添加该项后,连续梯度下降路径收敛到离散梯度下降在原始函数上到达的同一位置。Figure 9.3 Implicit regularization in gradient descent. a) Loss function with family of global minima on horizontal line ϕ1= 0.61. Dashed blue line shows continuous gradient descent path starting in bottom-left. gradient descent with step size 0.1 (first few steps shown explicitly as arrows). The finite step size causes the paths to diverge and reach a different final position. b) This disparity can be approximated by adding a regularization term to the continuous gradient descent loss function that penalizes the squared gradient magnitude. c) After adding this term, the continuous gradient descent path converges to the same place that the discrete one did on the original function.

Gradient descent approximates this process with a series of discrete steps of size α:

The discretization causes a deviation from the continuous path (figure 9.3).

梯度下降用一系列步长为 α 的离散步骤来近似这一过程:

离散化导致偏离连续路径(图 9.3)。

This deviation can be understood by deriving a modified loss term ˜L for the continuous case that arrives at the same place as the discretized version on the original loss L. It can be shown (see notes “Implicit regularization in gradient descent” at end of chapter) that this modified loss is:

In other words, the discrete trajectory is repelled from places where the gradient norm is large (the surface is steep). This doesn’t change the position of the minima where the gradients are zero anyway. However, it changes the effective loss function elsewhere and modifies the optimization trajectory, which potentially converges to a different minimum. Implicit regularization due to gradient descent may be responsible for the observation that full batch gradient descent generalizes better with larger step sizes (figure 9.5a).

˜ 这种偏离可以通过推导一个针对连续情形的修改损失项 L 来理解,该损失项使连续情形在原始损失 L 上到达与离散版本相同的位置。可以证明(见本章末尾的“梯度下降中的隐式正则化”笔记)该修改后的损失为:

换言之,离散轨迹会被梯度范数较大的区域(曲面陡峭处)排斥。这不会改变梯度为零的极小值点的位置。然而,它会改变其他区域的有效损失函数,并修改优化轨迹,从而可能收敛到另一个极小值点。由梯度下降引起的隐式正则化,可能是以下观察结果的原因:全批量梯度下降在较大步长下泛化性能更好(图 9.5a)。

9.2.2 随机梯度下降中的隐式正则化
9.2.2 Implicit regularization in stochastic gradient descent

A similar analysis can be applied to stochastic gradient descent. Now we seek a modified loss function such that the continuous version reaches the same place as the average of the possible random SGD updates. This can be shown to be:

Here, Lb is the loss for the bth of the B batches in an epoch, and both L and Lb now represent the means of the I individual losses in the full dataset and the |B| individual losses in the batch, respectively:

类似的分析也可以应用于随机梯度下降。现在,我们寻找一个修改后的损失函数,使得连续版本到达的位置与可能的随机 SGD 更新的平均位置相同。可以证明,该损失函数为:

这里, Lb 是一个轮次中 B 个批量的 bth 所对应的损失,而 L 和 Lb 现在分别表示完整数据集中 I 个单个损失和该批量中 |B| 个单个损失的均值:

Equation 9.9 reveals an extra regularization term, which corresponds to the variance of the gradients of the batch losses Lb. In other words, SGD implicitly favors places where the gradients are stable (where all the batches agree on the slope). Once more, this modifies the trajectory of the optimization process (figure 9.4) but does not necessarily change the position of the global minimum; if the model is over-parameterized, then it may fit all the training data exactly, so each of these gradient terms will be zero at the global minimum. SGD generalizes better than gradient descent, and smaller batch sizes generally perform better than larger ones (figure 9.5b). One possible explanation is that the inherent randomness allows the algorithm to reach different parts of the loss function. However, it’s also possible that some or all of this performance increase is due to implicit regular- ization; this encourages solutions where all the data fits well (so the batch variance is

公式 9.9 揭示了一个额外的正则化项,它对应于批量损失 Lb 的梯度方差。换言之,随机梯度下降会隐式地偏好梯度稳定的位置(所有批量在斜率上达成一致的位置)。再一次,这改变了优化过程的轨迹(图 9.4),但不一定改变全局最小值的位置;如果模型是过参数化的,那么它可能完全拟合所有训练数据,因此这些梯度项在全局最小值处都将为零。

small) rather than solutions where some of the data fit extremely well and other data less well (perhaps with the same overall loss, but with larger batch variance). The former solutions are likely to generalize better.

这种性能提升中,部分或全部也可能源于隐式正则化;它鼓励模型找到所有数据都拟合良好的解(因此批次方差较小),而不是某些数据拟合极好、其他数据拟合较差的解(可能总体损失相同,但批次方差更大)。前者往往能更好地泛化。

9.3 改进性能的启发式方法
9.3 Heuristics to improve performance

We’ve seen that explicit regularization encourages the training algorithm to find a good solution by adding extra terms to the loss function. This also occurs implicitly as an unintended (but seemingly helpful) byproduct of stochastic gradient descent. This section describes other heuristic methods used to improve generalization.

我们已经看到,显式正则化通过向损失函数添加额外项来促使训练算法找到好的解。这也会作为随机梯度下降的一个非预期(但看似有益)的副产品而隐式出现。本节介绍用于改进泛化的其他启发式方法。

图 9.4 随机梯度下降的隐式正则化。 a) Gabor 模型的原始损失函数(第 6.1.2 节)。蓝色点表示全局最小值。 b) 来自梯度下降的隐式正则化项对梯
图 9.4 随机梯度下降的隐式正则化。 a) Gabor 模型的原始损失函数(第 6.1.2 节)。蓝色点表示全局最小值。 b) 来自梯度下降的隐式正则化项对梯度幅值的平方进行惩罚。 c) 来自随机梯度下降的额外隐式正则化对批量梯度的方差进行惩罚。 d) 修改后的损失函数(原始损失与两个隐式正则化分量之和)。蓝色点表示全局最小值,它现在可能位于与面板 (a) 不同的位置。Figure 9.4 Implicit regularization for stochastic gradient descent. a) Original loss function for Gabor model (section 6.1.2). Blue point represents global minimum. b) Implicit regularization term from gradient descent penalizes the squared gradient magnitude. c) Additional implicit regularization from stochastic gradient descent penalizes the variance of the batch gradients. d) Modified loss function (sum of original loss plus two implicit regularization components). Blue point represents global minimum which may now be in a different place from panel (a).
图 9.5 在具有两个隐藏层的神经网络中,学习率(LR)和批量大小对来自 MNIST-1D (见图 8.1)的 4000 个训练样本和 4000 个测试样本的影
图 9.5 在具有两个隐藏层的神经网络中,学习率(LR)和批量大小对来自 MNIST-1D (见图 8.1)的 4000 个训练样本和 4000 个测试样本的影响。 a) 学习率较大时的性能优于中等或较小的学习率。在每种情况下,迭代次数均为 6000 / LR,因此每个解都有机会移动相同的距离。 b) 批量大小较小时性能更优。在每种情况下,迭代次数的选择使得模型以大致相同的模型容量记忆训练数据。Figure 9.5 Effect of learning rate (LR) and batch size for 4000 training and 4000 test examples from MNIST-1D (see figure 8.1) for a neural network with two hidden layers. a) Performance is better for large learning rates than for intermediate or small ones. In each case, the number of iterations is 6000 / LR, so each solution has the opportunity to move the same distance. b) Performance is superior for smaller batch sizes. In each case, the number of iterations was chosen so that the training data were memorized at roughly the same model capacity.
9.3.1 提前停止
9.3.1 Early stopping

Early stopping refers to stopping the training procedure before it has fully converged. This can reduce overfitting if the model has already captured the coarse shape of the underlying function but has not yet had time to overfit to the noise (figure 9.6). One way of thinking about this is that since the weights are initialized to small values (see section 7.5), they simply don’t have time to become large, so early stopping has a similar effect to explicit L2 regularization. A different view is that early stopping reduces the effective model complexity. Hence, we move back down the bias/variance trade-off curve from the critical region, and performance improves (see figures 8.9 and 8.10).

Early stopping has a single hyperparameter, the number of steps after which learning is terminated. As usual, this is chosen empirically using a validation set (section 8.5). However, for early stopping, the hyperparameter can be selected without the need to train multiple models. The model is trained once, the performance on the validation set is monitored every T iterations, and the associated parameters are stored. The stored parameters where the validation performance was best are selected.

提前停止指在训练过程完全收敛之前停止训练。如果模型已经捕获了底层函数的粗略形状,但还没有时间过拟合到噪声,这可以减少过拟合(图 9.6)。一种理解方式是,由于权重被初始化为较小值(见第 7.5 节),它们根本没有时间变大,因此提前停止具有与显式 L2 正则化类似的效果。另一种观点是,提前停止降低了有效模型复杂度。因此,我们从临界区域沿偏差 / 方差权衡曲线向下移动,性能得到提升(见图 8.9 和 8.10)。

提前停止只有一个超参数,即学习终止前的步数。通常,该超参数通过验证集经验性地选择(第 8.5 节)。然而,对于提前停止,无需训练多个模型即可选择该超参数。模型只训练一次,每 T 次迭代监控验证集上的性能,并存储相应的参数。选择验证性能最佳时存储的参数。

9.3.2 集成
9.3.2 Ensembling

Another approach to reducing the generalization gap between training and test data is to build several models and average their predictions. A group of such models is known

as an ensemble. This technique reliably improves test performance at the cost of training and storing multiple models and performing inference multiple times.

减少训练数据和测试数据之间泛化差距的另一种方法是构建多个模型并对它们的预测结果取平均值。这样的一组模型被称为

集成。该技术可靠地提高了测试性能,但代价是训练和存储多个模型以及多次执行推理。

The models can be combined by taking the mean of the outputs (for regression problems) or the mean of the pre-softmax activations (for classification problems). The assumption is that model errors are independent and will cancel out.

Alternatively, we can take the median of the outputs (for regression problems) or the most frequent predicted class (for classification problems) to make the predictions more robust. One way to train different models is just to use different random initializations. This may help in regions of input space far from the training data. Here, the fitted function is relatively unconstrained, and different models may produce different predictions, so

the average of several models may generalize better than any single model. A second approach is to generate several different datasets by re-sampling the training data with replacement and training a different model from each. This is known as bootstrap aggregating or bagging for short (figure 9.7). It has the effect of smoothing out the data; if a data point is not present in one training set, the model will interpo-

可以通过取输出的均值(对于回归问题)或取 Softmax 前激活值的均值(对于分类问题)来组合这些模型。假设模型误差相互独立并会相互抵消。或者,我们可以取输出的中位数(对于回归问题)或出现频率最高的预测类别(对于分类问题),以使预测更加稳健。另一种方法是有放回地重新采样训练数据,生成多个不同的数据集,并从每个数据集训练一个不同的模型。这被称为自助聚合,或简称袋装法(图 9.7)。其作用是平滑数据;如果某个数据点不在某个训练集中,模型将插 -

图 9.6 提前停止。 a) 具有 14 个线性区域的简化浅层网络模型(图 8.4)被随机初始化(青色曲线),并使用 SGD 进行训练,批量大小为 5,学习率为
图 9.6 提前停止。 a) 具有 14 个线性区域的简化浅层网络模型(图 8.4)被随机初始化(青色曲线),并使用 SGD 进行训练,批量大小为 5,学习率为 0.05。 b–d) 随着训练进行,函数首先捕捉真实函数(黑色曲线)的粗略结构,随后在 e–f) 中过拟合于含噪训练数据(橙色点)。尽管训练损失在整个过程中持续下降,但面板 (c) 和 (d) 中学习到的模型最接近真实底层函数。它们对测试数据的平均泛化能力会优于面板 (e) 或 (f) 中的模型。训练不同模型的一种方法就是仅使用不同的随机初始化。这可能在远离训练数据的输入空间区域中有所助益。在这里,拟合函数相对不受约束,不同模型可能产生不同的预测,因此多个模型的平均可能比任何单个模型泛化得更好。Figure 9.6 Early stopping. a) Simplified shallow network model with 14 linear regions (figure 8.4) is initialized randomly (cyan curve) and trained with SGD using a batch size of five and a learning rate of 0.05. b–d) As training proceeds, the function first captures the coarse structure of the true function (black curve) before e–f) overfitting to the noisy training data (orange points). Although the training loss continues to decrease throughout this process, the learned models in panels (c) and (d) are closest to the true underlying function. They will generalize better on average to test data than those in panels (e) or (f).

late from nearby points; hence, if that point was an outlier, the fitted function will be more moderate in this region. Other approaches include training models with different hyperparameters or training completely different families of models.

值,根据附近的点;因此,如果该点是异常值,拟合函数在该区域会更加平缓。其他方法包括使用不同超参数训练模型,或训练完全不同的模型族。

图 9.7 集成方法。 a) 使用整个数据集(橙色点)拟合单个模型(灰色曲线)。 b–e) 通过有放回地对数据进行四次重采样(bagging)创建四个模型(橙色
图 9.7 集成方法。 a) 使用整个数据集(橙色点)拟合单个模型(灰色曲线)。 b–e) 通过有放回地对数据进行四次重采样(bagging)创建四个模型(橙色点的大小表示该数据点被重采样的次数)。 f) 当我们对该集成的预测取平均时,结果(青色曲线)比面板 (a) 中针对完整数据集的结果(灰色曲线)更平滑,并且可能具有更好的泛化能力。Figure 9.7 Ensemble methods. entire dataset (orange points). b–e) Four models created by re-sampling the data with replacement (bagging) four times (size of orange point indicates number of times the data point was re-sampled). f) When we average the predictions of this ensemble, the result (cyan curve) is smoother than the result from panel (a) for the full dataset (gray curve) and will probably generalize better.
9.3.3 随机失活
9.3.3 Dropout

Dropout clamps a random subset (typically 50%) of hidden units to zero at each iteration of SGD (figure 9.8). This makes the network less dependent on any given hidden unit and encourages the weights to have smaller magnitudes so that the change in the function due to the presence or absence of any specific hidden unit is reduced. This technique has the positive benefit that it can eliminate undesirable “kinks” in the function that are far from the training data and don’t affect the loss. For example, consider three hidden units that become active sequentially as we move along the curve (figure 9.9a). The first hidden unit causes a large increase in the slope. A second hidden

随机失活会在每次 SGD 迭代中将随机选取的(通常为 50%)隐藏单元钳制为零(图 9.8)。这降低了网络对任何特定隐藏单元的依赖,并鼓励权重具有更小的幅度,从而减小因某个特定隐藏单元的存在或缺失所引起的函数变化。这种技术的积极好处在于,它可以消除函数中远离训练数据且不影响损失的不良“折点”。例如,考虑三个隐藏单元,当我们沿曲线移动时,它们会依次激活(图 9.9a)。第一个隐藏单元导致斜率大幅增加。第二个隐藏

图 9.8 随机失活。 a) 原始网络。 b–d) 在每次训练迭代中,随机选择一部分隐藏单元并将其固定为零(灰色节点)。结果是,这些单元的传入和传出权重不再起作
图 9.8 随机失活。 a) 原始网络。 b–d) 在每次训练迭代中,随机选择一部分隐藏单元并将其固定为零(灰色节点)。结果是,这些单元的传入和传出权重不再起作用,因此我们每次都在一个略有不同的网络上进行训练。Figure 9.8 Dropout. a) Original network. random subset of hidden units is clamped to zero (gray nodes). The result is that the incoming and outgoing weights from these units have no effect, so we are training with a slightly different network each time.

unit decreases the slope, so the function goes back down. Finally, the third unit cancels out this decrease and returns the curve to its original trajectory.

These three units conspire to make an undesirable local change in the function. This will not change the training loss but is unlikely to generalize well.

When several units conspire in this way, eliminating one (as would happen in dropout) causes a considerable change to the output function in the half-space where that unit was active (figure 9.9b). A subsequent gradient descent step will attempt to compensate for the change that this induces, and such dependencies will be eliminated over time. The overall effect is that large unnecessary changes between training data points are gradually removed even though they contribute nothing to the loss (figure 9.9).

单元使斜率减小,因此函数重新下降。最后,第三个单元抵消了这种下降,使曲线回到其原始轨迹。这三个单元合谋在函数中造成了不良的局部变化。这不会改变训练损失,但不太可能很好地泛化。

当几个单元以这种方式共谋时,消除其中一个(如随机失活中会发生的情况)会在该单元处于激活状态的半空间中导致输出函数发生显著变化(图 9.9b)。随后的梯度下降步骤将试图补偿由此引起的变化,并且这些依赖关系会随时间被消除。总体效果是,训练数据点之间大的不必要变化会逐渐被移除,即使它们对损失没有任何贡献(图 9.9)。

At test time, we can run the network as usual with all the hidden units active; however, the network now has more hidden units than it was trained with at any given iteration, so we multiply the weights by one minus the dropout probability to compensate. This is known as the weight scaling inference rule. A different approach to inference is to use Monte Carlo dropout, in which we run the network multiple times with different random subsets of units clamped to zero (as in training) and combine the results. This is closely related to ensembling in that every random version of the network is a different model; however, we do not have to train or store multiple networks here.

在测试时,我们可以照常运行网络,使所有隐藏单元都处于激活状态;然而,此时网络中的隐藏单元数量多于训练时任意一次迭代中使用的隐藏单元数量,因此我们将权重乘以 1 减去随机失活概率,以进行补偿。这被称为权重缩放推断规则。另一种推断方法是使用蒙特卡洛随机失活,其中我们多次运行网络,每次将不同的随机单元子集强制置零(与训练时一样),然后合并结果。这与集成方法密切相关,因为网络的每个随机版本都是一个不同的模型;不过,这里我们无需训练或存储多个网络。

图 9.9 随机失活机制。 a) 曲线中一个不理想的折点是由斜率的依次变化造成的:斜率增大、斜率减小(在圈出的连接点处),随后再次增大,以使曲线回到原来的轨迹。
图 9.9 随机失活机制。 a) 曲线中一个不理想的折点是由斜率的依次变化造成的:斜率增大、斜率减小(在圈出的连接点处),随后再次增大,以使曲线回到原来的轨迹。这里我们使用全批量梯度下降,模型(来自图 8.4)已尽可能拟合数据,因此进一步训练不会消除折点。 b) 考虑如果我们移除在面板 (a) 中产生圈出连接点的第八个隐藏单元会发生什么,这在使用随机失活时可能发生。由于没有斜率减小,函数的右侧会沿上升轨迹变化,随后的梯度下降步骤将力求补偿这一变化。 c) 在 2000 次迭代后的曲线,每次迭代 (i) 随机移除导致折点的三个隐藏单元之一,并 (ii) 执行一步梯度下降。折点不影响损失,但通过这种随机失活机制的近似仍被移除。Figure 9.9 Dropout mechanism. a) An undesirable kink in the curve is caused by a sequential increase in the slope, decrease in the slope (at circled joint), and then another increase to return the curve to its original trajectory. Here we are using full-batch gradient descent, and the model (from figure 8.4) fits the data as well as possible, so further training won’t remove the kink. b) Consider what happens if we remove the eighth hidden unit that produced the circled joint in panel (a), as might happen using dropout. Without the decrease in the slope, the right-hand side of the function takes an upwards trajectory, and a subsequent gradient descent step will aim to compensate for this change. c) Curve after 2000 iterations of (i) randomly removing one of the three hidden units that cause the kink and (ii) performing a gradient descent step. The kink does not affect the loss but is nonetheless removed by this approximation of the dropout mechanism.
9.3.4 应用噪声
9.3.4 Applying noise

Dropout can be interpreted as applying multiplicative Bernoulli noise to the network activations. This leads to the idea of applying noise to other parts of the network during training to make the final model more robust.

One option is to add noise to the input data; this smooths out the learned function (figure 9.10). For regression problems, it can be shown to be equivalent to adding a regularizing term that penalizes the derivatives of the network’s output with respect to its input. An extreme variant is adversarial training, in which the optimization algorithm actively searches for small perturbations of the input that cause large changes to the output. These can be thought of as worst-case additive noise vectors.

A second possibility is to add noise to the weights. This encourages the network to make sensible predictions even for small perturbations of the weights. The result is that the training converges to local minima in the middle of wide, flat regions, where changing the individual weights does not matter much.

随机失活可以被解释为对网络激活施加乘性伯努利噪声。这引出了在训练期间对网络其他部分施加噪声以使最终模型更鲁棒的想法。

一种选择是在输入数据中添加噪声;这会使学习到的函数更加平滑(图 9.10)。对于回归问题,可以证明这相当于添加一个正则化项,该正则化项惩罚网络输出相对于输入的导数。一个极端变体是对抗训练,其中优化算法主动寻找会导致输出大幅变化的输入微小扰动。这些扰动可以被视为最坏情况的加性噪声向量。

另一种可能是在权重中添加噪声。这促使网络即使在权重发生微小扰动时也能做出合理的预测。结果是训练收敛到宽阔平坦区域中间的局部极小值,在这些区域中,改变单个权重影响不大。

Finally, we can perturb the labels. The maximum-likelihood criterion for multiclass classification aims to predict the correct class with absolute certainty (equation 5.24). To this end, the final network activations (i.e., before the softmax function) are pushed to very large values for the correct class and very small values for the wrong classes. We could discourage this overconfident behavior by assuming that a proportion ρ of the training labels are incorrect and belong with equal probability to the other classes. This could be done by randomly changing the labels at each training iteration. However, the same end can be achieved by changing the loss function to minimize the crossentropy between the predicted distribution and a distribution where the true label has

最后,我们可以扰动标签。多分类的最大似然准则旨在以绝对确定性预测正确类别(公式 5.24)。为此,网络的最终激活值(即 Softmax 函数之前)会被推至正确类别的极大值和错误类别的极小值。我们可以通过假设比例为 ρ 的训练标签是错误的,并且以相等的概率属于其他类别。这可以通过在每次训练迭代中随机更改标签来实现。然而,也可以通过更改损失函数来实现相同目的,即最小化预测分布与一个分布之间的交叉熵,该分布中真实标签具有

图 9.10 向输入添加噪声。在 SGD 的每一步,向批量数据添加方差为 σ2x 的随机噪声。 a–c) 不同噪声水平下拟合的模型(小点代表十个样本)。增加更多
图 9.10 向输入添加噪声。在 SGD 的每一步,向批量数据添加方差为 σ2x 的随机噪声。 a–c) 不同噪声水平下拟合的模型(小点代表十个样本)。增加更多噪声会使拟合函数(青色线)更加平滑。Figure 9.10 Adding noise to inputs. At each step of SGD, random noise with variance σ2 x is added to the batch data. a–c) Fitted model with different noise levels (small dots represent ten samples). Adding more noise smooths out the fitted function (cyan line).

probability 1 −ρ, and the other classes have equal probability. This is known as label smoothing and improves generalization in diverse scenarios.

概率 1 −ρ,其他类别具有相等的概率。这被称为标签平滑,并在各种场景中提升泛化能力。

9.3.5 贝叶斯推断
9.3.5 Bayesian inference

The maximum likelihood approach is generally overconfident; it selects the most likely parameters during training and uses these to make predictions. However, many parameter values may be broadly compatible with the data and only slightly less likely. The Bayesian approach treats the parameters as unknown variables and computes a distri- bution Pr(ϕ|{ x i, y i}) over these parameters ϕ conditioned on the training data{ x i, y i}

极大似然方法通常会过度自信;它在训练过程中选择最可能的参数,并使用这些参数进行预测。然而,许多参数取值可能与数据大体兼容,只是可能性略低。贝叶斯方法将参数视为未知变量,并计算一个分布

布 Pr(ϕ|{ x i, y i}) 在这些参数上 ϕ,以训练数据 { x i, y i} 为条件,使用贝叶斯法则:

using Bayes’ rule:

where Pr(ϕ) is the prior probability of the parameters, and the denominator is a normalizing term. Hence, every parameter choice is assigned a probability (figure 9.11).

其中 Pr (ϕ) 是参数的先验概率,分母是一个归一化项。因此,每个参数选择都被赋予一个概率(图 9.11)。预测 y 对于新输入 x,是每个参数集预测的无限加权和(即积分),其中权重为相应概率:

The prediction y for new input x is an infinite weighted sum (i.e., an integral) of the predictions for each parameter set, where the weights are the associated probabilities:

This is effectively an infinite weighted ensemble, where the weight depends on (i) the prior probability of the parameters and (ii) their agreement with the data.

这实际上是一个无限加权集成,其中权重取决于(i)参数的先验概率和(ii)它们与数据的一致性。贝叶斯方法优雅,并且可以提供比源自极大似然的方法更稳健的预测。不幸的是,对于神经网络等复杂模型,没有实用方法来表示完整的概率分布

The Bayesian approach is elegant and can provide more robust predictions than those that derive from maximum likelihood.

Unfortunately, for complex models like neural networks, there is no practical way to represent the full probability distribution over the parameters or to integrate over it during the inference phase. Consequently, all current methods of this type make approximations of some kind, and typically these add considerable complexity to learning and inference.

对参数求和,或在推理阶段对其积分。因此,目前所有此类方法都会做出某种近似,而且这些近似通常会显著增加学习和推理的复杂度。

图 9.11 简化网络模型的贝叶斯方法(见图 8.4)。参数被视为不确定量。一组参数的后验概率  Pr (ϕ|{ x i, y i}) 由其与数据 { x i,
图 9.11 简化网络模型的贝叶斯方法(见图 8.4)。参数被视为不确定量。一组参数的后验概率 Pr (ϕ|{ x i, y i}) 由其与数据 { x i, y i} 以及先验分布 Pr (ϕ) 的相容性决定。 a–c) 从后验中采样得到两组参数(青色和灰色曲线),所用先验为正态分布,均值为零,方差有三种取值。当前先验方差 σ2 ϕ 较小时,参数也往往较小,函数也更平滑。 d–f) 推理过程对所有可能的参数取值进行加权求和,权重为后验概率。这同时给出均值的预测(青色曲线)以及相应的不确定性(灰色区域为两个标准差)。Figure 9.11 Bayesian approach for simplified network model (see figure 8.4). The parameters are treated as uncertain. The posterior probability Pr(ϕ|{ x i, y i}) for a set of parameters is determined by their compatibility with the data{ x i, y i} and a prior distribution Pr(ϕ). a–c) Two sets of parameters (cyan and gray curves) sampled from the posterior using normally distributed priors with mean zero and three variances. When the prior variance σ2 also tend to be small, and the functions smoother. d–f) Inference proceeds by taking a weighted sum over all possible parameter values where the weights are the posterior probabilities. This produces both a prediction of the mean (cyan curves) and the associated uncertainty (gray region is two standard deviations).
9.3.6 迁移学习与多任务学习
9.3.6 Transfer learning and multi-task learning

When training data are limited, other datasets can be exploited to improve performance. In transfer learning (figure 9.12a), the network is pre-trained to perform a related sec-

当训练数据有限时,可以利用其他数据集来提高性能。在迁移学习(图 9.12a)中,网络被预训练以执行一个相关的辅

ondary task for which data are more plentiful. The resulting model is then adapted to the original task. This is typically done by removing the last layer and adding one or more layers that produce a suitable output. The main model may be fixed, and the new layers trained for the original task, or we may fine-tune the entire model.

The principle is that the network will build a good internal representation of the data from the secondary task, which can subsequently be exploited for the original task. Equivalently, transfer learning can be viewed as initializing most of the parameters of the final network in a sensible part of the space that is likely to produce a good solution.

Multi-task learning (figure 9.12b) is a related technique in which the network is trained to solve several problems concurrently. For example, the network might take an image and simultaneously learn to segment the scene, estimate the pixel-wise depth, and predict a caption describing the image. All of these tasks require some understanding of the image and, when learned simultaneously, the model performance for each may improve.

助任务,其数据更充足。然后,将得到的模型适配到原始任务。这通常通过移除最后一层并添加一个或多个能够产生合适输出的层来完成。主模型可以保持固定,仅对新层进行训练以完成原始任务;或者我们可以微调整个模型。

其原理是,网络会从辅助任务中建立对数据的良好内部表示,随后可将其用于原始任务。等价地,迁移学习可以被视为在参数空间的一个合理区域中初始化最终网络的大部分参数,该区域很可能产生良好的解。

多任务学习(图 9.12b)是一种相关技术,其中网络被训练以同时解决多个问题。例如,网络可能会接收一张图像,并同时学习分割场景、估计逐像素深度,以及预测描述该图像的标题。所有这些任务都需要对图像有一定理解,并且当同时学习这些任务时,每个任务上的模型性能都可能得到提升。

9.3.7 自监督学习
9.3.7 Self-supervised learning

The above discussion assumes that we have plentiful data for a secondary task or data for multiple tasks to be learned concurrently. If not, we can create large amounts of “free” labeled data using self-supervised learning and use this for transfer learning. There are two families of methods for self-supervised learning: generative and contrastive.

In generative self-supervised learning, part of each data example is masked, and the secondary task is to predict the missing part (figure 9.12c). For example, we might use a corpus of unlabeled images and a secondary task that aims to inpaint (fill in) missing parts of the image (figure 9.12c). Similarly, we might use a large corpus of text and mask some words. We train the network to predict the missing words and then fine-tune it for the actual language task we are interested in (see chapter 12).

In contrastive self-supervised learning, pairs of examples with commonalities are compared to unrelated pairs. For images, the secondary task might be to identify whether a pair of images are transformed versions of one another or are unconnected. For text, the secondary task might be to determine whether two sentences followed one another in the original document. Sometimes, the precise relationship between a connected pair must be identified (e.g., finding the relative position of two patches from the same image).

上述讨论假设我们拥有用于辅助任务的充足数据,或用于同时学习的多个任务的数据。如果没有,我们可以使用自监督学习创建大量“免费”的带标签数据,并将其用于迁移学习。自监督学习有两类方法:生成式和对比式。

在生成式自监督学习中,每个数据样本的一部分被掩码,辅助任务是预测缺失部分(图 9.12c)。例如,我们可以使用一个无标签图像语料库,以及一个旨在修复(填充)图像缺失部分的辅助任务(图 9.12c)。类似地,我们可以使用一个大型文本语料库并掩码一些词语。我们训练网络预测缺失词语,然后针对我们实际感兴趣的语言任务对其进行微调(见第 12 章)。

在对比式自监督学习中,将具有共同点的样本对与无关联的样本对进行比较。对于图像,辅助任务可能是识别一对图像是否是彼此的变换版本,还是彼此没有关联。对于文本,辅助任务可能是判断两个句子在原始文档中是否前后相连。有时,必须识别相连样本对之间的精确关系(例如,找到同一图像中两个图块的相对位置)。

9.3.8 数据增强
9.3.8 Augmentation

Transfer learning improves performance by exploiting a different dataset. Multi-task learning improves performance using additional labels. A third option is to expand the dataset. We can often transform each input data example in such a way that the label stays the same. For example, we might aim to determine if there is a bird in an image (figure 9.13). Here, we could rotate, flip, blur, or manipulate the color balance of the image, and the label “bird” remains valid. Similarly, for tasks where the input is text, we can substitute synonyms or translate to another language and back again. For tasks

迁移学习通过利用不同的数据集来提升性能。多任务学习通过使用额外的标签来提升性能。第三种选择是扩展数据集。我们通常可以变换每个输入数据样本,使标签保持不变。例如,我们可能希望判断图像中是否有鸟(图 9.13)。在这里,我们可以旋转、翻转、模糊或调整图像的色彩平衡,而标签“鸟”仍然有效。类似地,对于输入为文本的任务,

where the input is audio, we can amplify or attenuate different frequency bands.

图 9.12 迁移学习、多任务学习和自监督学习。 a) 当主任务(此处为深度估计)的带标签数据有限,而次任务(此处为分割)的数据充足时,使用迁移学习。我们为次任
图 9.12 迁移学习、多任务学习和自监督学习。 a) 当主任务(此处为深度估计)的带标签数据有限,而次任务(此处为分割)的数据充足时,使用迁移学习。我们为次任务训练一个模型,移除最终层,并用适合主任务的新层替换它们。然后,我们仅训练新层,或对主任务微调整个网络。网络从次任务中学习到一个良好的内部表示,随后被主任务利用。 b) 在多任务学习中,我们训练一个模型同时执行多个任务,期望每个任务的性能都会提升。 c) 在生成式自监督学习中,我们移除部分数据,并训练网络补全缺失的信息。此处,任务是填充(修复)图像中被掩码的部分。这使得在没有标签时也能进行迁移学习。图像来自 Cordts et al. (2016)。Figure 9.12 Transfer, multi-task, and self-supervised learning. a) Transfer learning is used when we have limited labeled data for the primary task (here depth estimation) but plentiful data for a secondary task (here segmentation). We train a model for the secondary task, remove the final layers, and replace them with new layers appropriate to the primary task. We then train only the new layers or fine-tune the entire network for the primary task. The network learns a good internal representation from the secondary task that is then exploited for the primary task. b) In multi-task learning, we train a model to perform multiple tasks simultaneously, hoping that performance on each will improve. c) In generative self-supervised learning, we remove part of the data and train the network to complete the missing information. Here, the task is to fill in (inpaint) a masked portion of the image. This permits transfer learning when no labels are available. Images from Cordts et al. (2016).
图 9.13 数据增强。对于某些问题,每个数据样本都可以被变换以扩充数据集。 a) 原始图像。 b–h) 该图像的各种几何和光度变换。对于图像分类,所有这些图像
图 9.13 数据增强。对于某些问题,每个数据样本都可以被变换以扩充数据集。 a) 原始图像。 b–h) 该图像的各种几何和光度变换。对于图像分类,所有这些图像仍具有相同的标签“鸟”。改编自 Wu et al. (2015a)。以这种方式生成额外训练数据称为数据增强。其目标是使模型对这些无关的数据变换保持不敏感。Figure 9.13 Data augmentation. For some problems, each data example can be transformed to augment the dataset. a) Original image. b–h) Various geometric and photometric transformations of this image. For image classification, all these images still have the same label, “bird.” Adapted from Wu et al. (2015a).

Generating extra training data in this way is known as data augmentation. The aim is to teach the model to be indifferent to these irrelevant data transformations.

9.4 总结
9.4 Summary

Explicit regularization involves adding an extra term to the loss function that changes the position of the minimum. The term can be interpreted as a prior probability over the parameters. Stochastic gradient descent with a finite step size does not neutrally descend to the minimum of the loss function. This bias can be interpreted as adding additional terms to the loss function, and this is known as implicit regularization.

There are also many heuristics for improving generalization, including early stopping, dropout, ensembling, the Bayesian approach, adding noise, transfer learning, multi-task learning, and data augmentation. There are four main principles behind these methods (figure 9.14). We can (i) encourage the function to be smoother (e.g., L2 regularization), (ii) increase the amount of data (e.g., data augmentation), (iii) combine models (e.g., ensembling), or (iv) search for wider minima (e.g., applying noise to network weights).

显式正则化涉及向损失函数中添加一个额外项,该项会改变最小值的位置。这一项可解释为参数上的先验概率。具有有限步长的随机梯度下降并不会中立地下降到损失函数的最小值。这种偏差可解释为向损失函数添加额外项,这称为隐式正则化。

此外,还有许多用于改善泛化的启发式方法,包括提前停止、随机失活、集成、贝叶斯方法、添加噪声、迁移学习、多任务学习和数据增强。这些方法背后有四个主要原则(图 9.14)。我们可以 (i) 鼓励函数更平滑(例如, L2 正则化), (ii) 增加数据量(例如,数据增强), (iii) 组合模型(例如,集成),或 (iv) 寻找更宽的最小值(例如,将噪声应用于网络权重)。

图 9.14 正则化方法。本章讨论的正则化方法旨在通过四种机制之一来提升泛化能力。一些方法旨在使所建模函数更平滑。其他方法增加有效数据量。第三组方法组合多个模型
图 9.14 正则化方法。本章讨论的正则化方法旨在通过四种机制之一来提升泛化能力。一些方法旨在使所建模函数更平滑。其他方法增加有效数据量。第三组方法组合多个模型,从而缓解拟合过程中的不确定性。最后,第四组方法鼓励训练过程收敛到一个宽极小值,其中估计参数的小误差影响较小(另见图 20.11)。Figure 9.14 Regularization methods. The regularization methods discussed in this chapter aim to improve generalization by one of four mechanisms. Some methods aim to make the modeled function smoother. Other methods increase the effective amount of data. The third group of methods combine multiple models and hence mitigate against uncertainty in the fitting process. Finally, the fourth group of methods encourages the training process to converge to a wide minimum where small errors in the estimated parameters are less important (see also figure 20.11).

Another way to improve generalization is to choose the model architecture to suit the task. For example, in image segmentation, we can share parameters within the model, so we don’t need to independently learn what a tree looks like at every image location. Chapters 10–13 consider architectural variations designed for different tasks.

另一种改善泛化的方法是选择适合任务的模型架构。例如,在图像分割中,我们可以在模型内共享参数,因此无需在图像的每个位置独立学习树的外观。第 10–13 章讨论了为不同任务设计的架构变体。

注释
Notes

Regularization:

公式
公式

Implicit regularization in gradient descent:

梯度下降中的隐式正则化:梯度下降步骤为:

公式

Early stopping:

Ensembling:

Dropout:

Adding noise:

Label smoothing:

Finding wider minima:

Bayesian approaches:

Transfer learning:

Self-supervised learning:

Data augmentation:

问题
Problems
公式
公式
公式