学习资料 理解深度学习 第 2 章 · 监督学习
学 习 资 料 · 中 英 对 照

第 2 章 · 监督学习

Understanding Deep Learning — Supervised learning

监督学习
Supervised learning

A supervised learning model defines a mapping from one or more inputs to one or more outputs. For example, the input might be the age and mileage of a second-hand Toyota Prius, and the output might be the estimated value of the car in dollars.

一个监督学习模型定义了一个从一个或多个输入到一个或多个输出的映射。例如,输入可能是一辆二手丰田普锐斯的车龄和里程数,输出可能是以美元计的汽车估价。

The model is just a mathematical function (i.e., an equation); when the inputs are passed through this function, it computes the output, and this is termed inference. The model equation also contains parameters. Different parameter values change the outcome of the computation; the model equation describes a family of possible relationships between inputs and outputs, and the parameters specify the particular relationship.

When we train or learn a model, we find parameters that describe the true relationship between inputs and outputs. A learning algorithm takes a training set of input/output pairs and manipulates the parameters until the inputs predict their corresponding outputs as closely as possible. If the model works well for these training pairs, then we hope it will make good predictions for new inputs where the true output is unknown.

The goal of this chapter is to expand on these ideas. First, we describe this framework more formally and introduce some notation. Then we work through a simple example in which we use a straight line to describe the relationship between input and output. This linear model is both familiar and easy to visualize, but nevertheless illustrates all the main ideas of supervised learning.

模型只是一个数学函数(即方程);当输入经过这个函数时,它计算输出,这称为推理。模型方程还包含参数。不同的参数值会改变计算结果;模型方程描述了输入与输出之间可能关系的一族,而参数则指定了具体关系。

当我们训练或学习一个模型时,我们寻找能够描述输入与输出之间真实关系的参数。学习算法接收一个由输入 / 输出对组成的训练集,并调整参数,直到输入能够尽可能准确地预测其对应的输出。如果模型在这些训练对上表现良好,那么我们希望它对真实输出未知的新输入也能做出良好的预测。

本章的目标是进一步阐述这些想法。首先,我们更正式地描述这一框架,并引入一些符号。然后,我们讲解一个简单的例子,其中使用一条直线来描述输入与输出之间的关系。这种线性模型既常见又易于可视化,但仍然能够说明监督学习的所有主要思想。

2.1 监督学习概述
2.1 Supervised learning overview

In supervised learning, we aim to build a model that takes an input x and outputs a prediction y. For simplicity, we assume that both the input x and output y are vectors of a predetermined and fixed size and that the elements of each vector are always ordered in the same way; in the Prius example above, the input x would always contain the age of the car and then the mileage, in that order. This is termed structured or tabular data.

To make the prediction, we need a model f[•] that takes input x and returns y, so:

在监督学习中,我们的目标是构建一个模型,该模型接收输入 x 并输出预测 y。为简单起见,我们假设输入 x 和输出 y 都是预先确定且大小固定的向量,并且每个向量的元素始终以相同顺序排列;在上述普锐斯示例中,输入 x 始终包含汽车车龄和里程数,且按此顺序。这称为结构化或表格化数据。

为了进行预测,我们需要一个模型 f[•],它接受输入 x 并回报 y,因此:

When we compute the prediction y from the input x, we call this inference.

The model is just a mathematical equation with a fixed form. It represents a family of different relations between the input and the output. The model also contains parametersϕ. The choice of parameters determines the particular relation between input and output, so we should really write:

当我们根据输入 x 计算预测 y 时,我们将此称为推理。

模型只是一个具有固定形式的数学方程。它表示输入与输出之间的一组不同关系。模型还包含参数 ϕ。参数的选择决定了输入与输出之间的特定关系,因此我们实际上应该写成:

When we talk about learning or training a model, we mean that we attempt to find parameters ϕ that make sensible output predictions from the input. We learn these parameters using a trainingdataset of I pairs of input and output examples { x i, y i}. We aim to select parameters that map each training input to its associated output as closely as possible. We quantify the degree of mismatch in this mapping with the lossL. This is a scalar value that summarizes how poorly the model predicts the training outputs from their corresponding inputs for parameters ϕ. We can treat the loss as a function L[ϕ] of these parameters. When we train the model, we are seeking parameters ˆϕ that minimize this lossfunction: 1

当我们谈论学习或训练模型时,意思是我们试图找到参数 ϕ,使其能够根据输入做出合理的输出预测。我们使用一个训练数据集来学习这些参数,该数据集包含 I 组输入和输出示例对 { x i, y i}。我们的目标是选择参数,使每个训练输入都能尽可能准确地映射到其对应的输出。我们用损失 L 来量化这种映射中的不匹配程度。这是一个标量值,用于概括在参数 ϕ 下,模型根据对应输入预测训练输出时的糟糕程度。我们可以将损失视为这些参数的函数 L[ϕ]。训练模型时,我们是在寻找使该损失函数最小化的参数 ˆ ϕ: 1

If the loss is small after this minimization, we have found model parameters that accurately predict the training outputs y i from the training inputs x i.

After training a model, we must now assess its performance; we run the model on separate test data to see how well it generalizes to examples that it didn’t observe during training. If the performance is adequate, then we are ready to deploy the model.

训练模型后,我们现在必须评估其性能;我们在单独的测试数据上运行模型,以查看它对训练期间未观察到的样本泛化得如何。如果性能足够好,我们就可以部署该模型。

2.2 线性回归示例
2.2 Linear regression example

Let’s now make these ideas concrete with a simple example. We consider a model y= f[x, ϕ] that predicts a single output y from a single input x. Then we develop a loss function, and finally, we discuss model training.

现在,让我们通过一个简单的例子将这些想法具体化。我们考虑一个模型 y= f[x, ϕ],它预测单个输出 y (来自单个输入 x)。然后我们构建损失函数,最后讨论模型训练。

2.2.1 一维线性回归模型
2.2.1 1D linear regression model

A 1Dlinearregressionmodel describes the relationship between input x and output y as a straight line:

一维线性回归模型将输入 x 与输出 y 之间的关系描述为一条直线:

This model has two parameters ϕ = [ϕ0, ϕ1], where ϕ0 is the y-intercept of the line and ϕ1 is the slope. Different choices for the y-intercept and slope result in different relations between input and output (figure 2.1). Hence, equation 2.4 defines a family of possible input-output relations (all possible lines), and the choice of parameters determines the member of this family (the particular line).

该模型有两个参数 ϕ = [ϕ0, ϕ1],其中 ϕ0 是直线的 y 轴截距, ϕ1 是斜率。不同的 y 轴截距和斜率选择会导致输入与输出之间的不同关系(图 2.1)。因此,公式 2.4 定义了一族可能的输入 / 输出关系(所有可能的直线),参数的选择决定该族中的成员(特定直线)。

2.2.2 损失
2.2.2 Loss

For this model, the training dataset (figure 2.2a) consists of I input/output pairs {xi, yi}. Figures 2.2b–d show three lines defined by three sets of parameters.

The green line in figure 2.2d describes the data more accurately than the other two since it is much closer to the data points. However, we need a principled approach for deciding which parameters ϕ are better than others. To this end, we assign a numerical value to each choice of parameters that quantifies the degree of mismatch between the model and the data. We term this value the loss; a lower loss means a better fit.

对于该模型,训练数据集(图 2.2a)由 I 个输入 / 输出对 {xi, yi} 组成。图 2.2b–d 展示了由三组参数定义的三条直线。图 2.2d 中的绿色直线比其他两条更准确地描述数据,因为它更贴近数据点。然而,我们需要一种原则性的方法来判断哪些参数 ϕ 比其他参数更优。为此,我们为每一种参数选择赋予一个数值,以量化模型与数据之间的不匹配程度。我们将该值称为损失;损失越低,拟合效果越好。

The mismatch is captured by the deviation between the model predictions f[xi, ϕ] (height of the line at xi) and the ground truth outputs yi. These deviations are depicted as orange dashed lines in figures 2.2b–d. We quantify the total mismatch, training error, or loss as the sum of the squares of these deviations for all I training pairs: Since the best parameters minimize this expression, we call this a least-squares loss. The squaring operation means that the direction of the deviation (i.e., whether the line is

这种不匹配由模型预测 f[xi, ϕ] (xi 处直线的高度)与真值输出 yi 之间的偏差来刻画。这些偏差在图 2.2b–d 中以橙色虚线表示。我们将总不匹配、训练误差或损失量化为所有 I 个训练对中这些偏差的平方和:由于最佳参数会使该表达式最小化,我们将其称为最小二乘损失。平方运算意味着偏差的方向(即直线位于数据上方还是下方)并不重要。此外,这一选择也有理论依据,我们在第 5 章会回到这些内容。

图 2.2 线性回归训练数据、模型和损失。 a) 训练数据(橙色点)由 I=12 输入 / 输出对 {xi, yi}。 b–d) 每个子图展示了具有不同参数的线
图 2.2 线性回归训练数据、模型和损失。 a) 训练数据(橙色点)由 I=12 输入 / 输出对 {xi, yi}。 b–d) 每个子图展示了具有不同参数的线性回归模型。根据 y 轴截距和斜率参数 ϕ = [ϕ0, ϕ1] 的选择,模型误差(橙色虚线)可能更大或更小。损失 L 是这些误差的平方和。定义子图 (b) 和 (c) 中直线的参数具有较大的损失 L = 7.07 和 L = 10.28,分别因为模型拟合不佳。损失 L=0.20 在子图 (d) 中较小,因为模型拟合良好;事实上,这是所有可能直线中最小的损失,因此这些是最优参数。(交互图)Figure 2.2 Linear regression training data, model, and loss. a) The training data (orange points) consist of I=12 input/output pairs{xi, yi}. b–d) Each panel shows the linear regression model with different parameters. Depending on the choice of y-intercept and slope parameters ϕ = [ϕ0, ϕ1], the model errors (orange dashed lines) may be larger or smaller. The loss L is the sum of the squares of these errors. The parameters that define the lines in panels (b) and (c) have large losses L = 7.07 and L = 10.28, respectively because the models fit badly. The loss L=0.20 in panel (d) is smaller because the model fits well; in fact, this has the smallest loss of all possible lines, so these are the optimal parameters. (Interactive figure)

above or below the data) is unimportant. There are also theoretical reasons for this choice which we return to in chapter 5. The loss L is a function of the parameters ϕ; it will be larger when the model fit is poor (figure 2.2b,c) and smaller when it is good (figure 2.2d). Considered in this light,

we term L[ϕ] the lossfunction or costfunction. The goal is to find the parameters ˆϕ that minimize this quantity:

损失 L 是关于参数 ϕ 的函数;模型拟合较差时(图 2.2b,c)它会更大,拟合较好时(图 2.2d)更小。据此,我们称 L[ϕ] 为损失函数或代价函数。目标是找到使该量最小化的参数 ϕ:

图 2.3 针对图 2.2a 中数据集的线性回归模型的损失函数。 a) 每个参数组合 ϕ = [ϕ0, ϕ1] 都有相应的损失。由此得到的损失函数 L[ϕ] 可
图 2.3 针对图 2.2a 中数据集的线性回归模型的损失函数。 a) 每个参数组合 ϕ = [ϕ0, ϕ1] 都有相应的损失。由此得到的损失函数 L[ϕ] 可以可视化为一个曲面。三个圆圈表示图 2.2b–d 中的直线。 b) 损失也可以可视化为热力图,其中较亮的区域表示较大的损失;这里我们从上方垂直俯视 (a) 中的曲面,灰色椭圆表示等值线。最佳拟合直线(图 2.2d)具有损失最小的参数(绿色圆圈)。在数据上方或下方)并不重要。此外,这一选择也有理论依据,我们在第 5 章会回到这些内容。Figure 2.3 Loss function for linear regression model with the dataset in figure 2.2a. a) Each combination of parameters ϕ = [ϕ0, ϕ1] has an associated loss. The resulting loss function L[ϕ] can be visualized as a surface. The three circles represent the lines from figure 2.2b–d. b) The loss can also be visualized as a heatmap, where brighter regions represent larger losses; here we are looking straight down at the surface in (a) from above and gray ellipses represent isocontours. The best fitting line (figure 2.2d) has the parameters with the smallest loss (green circle).

There are only two parameters (the y-intercept ϕ0 and slope ϕ1), so we can calculate the loss for every combination of values and visualize the loss function as a surface (figure 2.3). The “best” parameters are at the minimum of this surface.

只有两个参数(y 轴截距 ϕ0 和斜率 ϕ1),因此我们可以计算每组取值的损失,并将损失函数可视化为一个曲面(图 2.3)。“最佳”参数位于该曲面的最小值处。

2.2.3 训练
2.2.3 Training

The process of finding parameters that minimize the loss is termed model fitting, training, or learning. The basic method is to choose the initial parameters randomly and then improve them by “walking down” the loss function until we reach the bottom (figure 2.4). One way to do this is to measure the gradient of the surface at the current position and take a step in the direction that is most steeply downhill. Then we repeat this process until the gradient is flat and we can improve no further. 2

寻找使损失最小化的参数这一过程称为模型拟合、训练或学习。基本方法是随机选择初始参数,然后通过沿损失函数“向下走”来改进这些参数,直到到达底部(图 2.4)。一种做法是测量当前曲面位置的梯度,并沿最陡峭的下坡方向迈出一步。然后重复这一过程,直到梯度平坦且无法进一步改进为止。 2

2.2.4 测试
2.2.4 Testing

Having trained the model, we want to know how it will perform in the real world. We do this by computing the loss on a separate set of test data. The degree to which the prediction accuracy generalizes to the test data depends in part on how representative and complete the training data is. However, it also depends on how expressive the model is. A simple model like a line might not be able to capture the true relationship between input and output. This is known as underfitting. Conversely, a very expressive model may describe statistical peculiarities of the training data that are atypical and lead to unusual predictions. This is known as overfitting.

在训练完模型之后,我们想知道它在现实世界中的表现如何。我们通过在一组单独的测试数据上计算损失来做到这一点。预测准确率泛化到测试数据的程度,部分取决于训练数据的代表性和完整性。然而,它也取决于模型的表达能力。像直线这样的简单模型可能无法捕捉输入与输出之间的真实关系。这被称为欠拟合。相反,表达能力非常强的模型可能会描述训练数据中不典型的统计特殊性,并导致异常预测。这被称为过拟合。

2.3 总结
2.3 Summary

A supervised learning model is a function y = f[ x , ϕ] that relates inputs x to outputs y. The particular relationship is determined by parameters ϕ.

To train the model, we define a loss function L[ϕ] over a training dataset { x i, y i}. This quantifies the mismatch between the model predictions f[ x i, ϕ] and observed outputs y i as a function of the parameters ϕ. Then we search for the parameters that minimize the loss. We evaluate the model on a different set of test data to see how well it generalizes to new inputs.

Chapters 3–9 expand on these ideas. First, we tackle the model itself; 1D linear regression has the obvious drawback that it can only describe the relationship between the input and output as a straight line. Shallow neural networks (chapter 3) are only slightly more complex than linear regression but describe a much larger family of input/output relationships. Deep neural networks (chapter 4) are just as expressive but can describe complex functions with fewer parameters and work better in practice.

监督学习模型是一个函数 y = f[ x ϕ],它将输入 x 与输出 y 关联起来。这种特定关系由参数 ϕ 决定。为了训练模型,我们定义一个损失函数 L[ϕ],它在训练数据集 { x i, y i} 上,将模型预测 f[ x i, ϕ] 与观测输出 y i 之间的不匹配程度量化为参数 ϕ 的函数。然后我们寻找使损失最小化的参数。我们在另一组测试数据上评估模型,以查看它对新输入的泛化能力。

第 3–9 章对这些思想进行了展开。首先,我们处理模型本身;一维线性回归有一个明显的缺点,即它只能将输入与输出之间的关系描述为一条直线。浅层神经网络(第 3 章)只比线性回归稍微复杂一些,但能描述更庞大的输入 / 输出关系族。深层神经网络(第 4 章)同样具有表达能力,但可以用更少的参数描述复杂函数,并在实践中表现更好。

Chapter 5 investigates loss functions for different tasks and reveals the theoretical underpinnings of the least-squares loss. Chapters 6 and 7 discuss the training process. Chapter 8 discusses how to measure model performance. Chapter 9 considers regularization techniques, which aim to improve that performance.

第 5 章研究不同任务的损失函数,并揭示最小二乘损失的理论基础。第 6 章和第 7 章讨论训练过程。第 8 章讨论如何衡量模型性能。第 9 章考虑正则化技术,这些技术旨在提升该性能。

图 2.4 线性回归训练。目标是找到使损失最小的 y 轴截距和斜率参数。 a) 迭代训练算法随机初始化参数,然后通过“下坡”进行改进,直到无法进一步改进。这里,
图 2.4 线性回归训练。目标是找到使损失最小的 y 轴截距和斜率参数。 a) 迭代训练算法随机初始化参数,然后通过“下坡”进行改进,直到无法进一步改进。这里,我们从位置 0 出发,沿下坡方向(垂直于等高线)移动一段距离到位置 1。然后重新计算下坡方向,并移动到位置 2。最终,我们到达函数最小值(位置 4)。 b) 面板 (a) 中的每个位置 0–4 都对应不同的 y 轴截距和斜率,因此代表一条不同的直线。随着损失减小,这些直线与数据的拟合程度更高。(交互图)Figure 2.4 Linear regression training. The goal is to find the y-intercept and slope parameters that correspond to the smallest loss. a) Iterative training algorithms initialize the parameters randomly and then improve them by “walking downhill” until no further improvement can be made. Here, we start at position 0 and move a certain distance downhill (perpendicular to the contours) to position 1. Then we re-calculate the downhill direction and move to position 2. Eventually, we reach the minimum of the function (position 4). b) Each position 0–4 from panel (a) corresponds to a different y-intercept and slope and so represents a different line. As the loss decreases, the lines fit the data more closely. (Interactive figure)
注释
Notes

Loss functions vs. cost functions:

Generative vs. discriminative models:

习题
Problems