第 5 章 · 损失函数
Understanding Deep Learning — Loss functions
The last three chapters described linear regression, shallow neural networks, and deep neural networks. Each represents a family of functions that map input to output, where the particular member of the family is determined by the model parameters ϕ. When we train these models, we seek the parameters that produce the best possible mapping from input to output for the task we are considering. This chapter defines what is meant by the “best possible” mapping.
That definition requires a training dataset{ x i, y i} of input/output pairs.
A loss function or costfunctionL[ϕ] returns a single number that describes the mismatch between the model predictions f[ x i, ϕ] and their corresponding ground-truth outputs y i. During training, we seek parameter values ϕ that minimize the loss and hence map the training inputs to the outputs as closely as possible. We saw one example of a loss function in chapter 2; the least squares loss function is suitable for univariate regression problems for which the target is a real number y∈R. It computes the sum of the squares
前三章介绍了线性回归、浅层神经网络和深层神经网络。每一种都代表一族函数,将输入映射到输出,其中该族中的特定成员由模型参数 ϕ 决定。在训练这些模型时,我们寻找针对所考虑任务能够产生从输入到输出的最佳可能映射的参数。本章定义了“最佳可能”映射的含义。该定义需要一个训练数据集{ x i, y i} 输入 / 输出对。一个损失函数或成本函数 L[ϕ] 返回一个描述模型预测 f[ x i, ϕ] 与其对应真实输出 y i 之间的差异的单个数值。在训练期间,我们寻找参数值 ϕ 以最小化损失,从而尽可能将训练输入映射到输出。我们在第 2 章中看到了一个损失函数的示例;最小二乘损失函数适用于目标为实数 y∈R。它计算平方和
of the deviations between the model predictions f[ x i, ϕ] and the true values yi.
模型预测 f[ x i, ϕ] 与真实值 yi 之间偏差的。
This chapter provides a framework that both justifies the choice of the least squares criterion for real-valued outputs and allows us to build loss functions for other prediction types. We consider binaryclassification, where the prediction y∈{0, 1} is one of two categories, multiclassclassification, where the prediction y∈{1, 2,..., K} is one of K categories, and more complex cases. In the following two chapters, we address model training, where the goal is to find the parameter values that minimize these loss functions.
本章提供了一个框架,既论证了为实值输出选择最小二乘准则的合理性,又允许我们为其他预测类型构建损失函数。我们考虑二分类,其中预测 y∈{0, 1} 是两类中的一类,多分类,其中预测 y∈{1, 2,..., K} 是 K 类中的一类,以及更复杂的情况。在接下来的两章中,我们将讨论模型训练,其目标是找到最小化这些损失函数的参数值。
In this section, we develop a recipe for constructing loss functions. Consider a model f[ x , ϕ] with parameters ϕ that computes an output from input x. Until now, we have implied that the model directly computes a prediction y. We now shift perspective and consider the model as computing a conditional probability distribution Pr( y | x ) over
已经暗示模型直接计算预测 y。我们现在转换视角,将模型视为计算一个条件概率分布
possible outputs y given input x. The loss encourages each training output y i to have a high probability under the distribution Pr( y i| x i) computed from the corresponding input x i (figure 5.1).
Pr( y | x ),该分布覆盖可能的输出 y (在给定输入 x 下)。损失函数鼓励每个训练输出 y i 在分布 Pr( y i| x i) 下具有较高的概率。该分布由对应的输入 x i 计算得到(图 5.1)。

This shift in perspective raises the question of exactly how a model f[ x , ϕ] can be adapted to compute a probability distribution. The solution is simple. First, we choose a parametric distribution Pr( y |θ) defined on the output domain y. Then we use the network to compute one or more of the parameters θ of this distribution.
For example, suppose the prediction domain is the set of real numbers, so y∈R. Here, we might choose the univariate normal distribution, which is defined on R. This distribution is defined by the mean µ and variance σ2, so θ={µ, σ2}. The machine learning model might predict the mean µ, and the variance σ2 could be treated as an unknown constant.
这种视角的转变提出了一个问题:究竟如何调整模型 f[ x ϕ] 以计算概率分布。解决方案很简单。首先,我们在输出域 y 上选择一个参数化分布 Pr (y |θ)。然后,我们使用网络计算该分布的一个或多个参数 θ。
例如,假设预测域为实数集,因此 y∈R。这里,我们可以选择定义在 R 上的一元正态分布。该分布由均值 µ 和方差 σ2 定义,因此 θ={µ, σ2}。机器学习模型可以预测均值 µ,而方差 σ2 可被视为未知常数。
The model now computes different distribution parameters θi= f[ x i, ϕ] for each training input x i.
Each observed training output y i should have high probability under its corresponding distribution Pr( y i|θi). Hence, we choose the model parameters ϕ so that they maximize the combined probability across all I training examples: The combined probability term is the likelihood of the parameters, and hence equation 5.1 is known as the maximum likelihood criterion. 1 Here we are implicitly making two assumptions. First, we assume that the form of the probability distribution over the outputs y i is the same for each data point. Second, we assume that the conditional distributions Pr( y i| x i) of the output given the input are
模型现在为每个训练输入 x i 计算不同的分布参数 θi= f[ x i, ϕ]。每个观测到的训练输出 y i 都应在其对应的分布 Pr (y i|θi) 下具有较高的概率。因此,我们选择模型参数 ϕ,使它们最大化所有 I 个训练样本的联合概率:合并概率项是参数的似然,因此公式 5.1 被称为极大似然准则。 1这里我们隐含地做了两个假设。首先,我们假设输出 y i 上的概率分布的形式对每个数据点都相同。其次,我们假设给定输入时输出的条件分布 Pr( y i| x i) 是独立的,因此
independent, so the total likelihood of the training data decomposes as:
训练数据的总似然可以分解为:
![图 5.2 对数变换。 a) 对数函数是单调递增的。如果 z > z′,则 log[z] > log[z′]。因此,任意函数 g[z] 的最大值将与](/learning/udl/ch5/figures/ch5_p04_f0.webp)
The maximum likelihood criterion (equation 5.1) is not very practical.
Each term Pr( y i| f[ x i, ϕ]) can be small, so the product of many of these terms can be tiny.
It may be difficult to represent this quantity with finite precision arithmetic. Fortunately, we can equivalently maximize the logarithm of the likelihood:
极大似然准则(公式 5.1)不太实用。每个项 Pr (y i| f[ x i, ϕ]) 都可能很小,因此许多这样的项的乘积可能极小。用有限精度算术表示这个量可能很困难。幸运的是,我们可以等价地最大化似然的对数:
This log-likelihood criterion is equivalent because the logarithm is a monotonically increasing function: if z > z′, then log[z] > log[z′] and vice versa (figure 5.2). It follows that when we change the model parameters ϕ to improve the log-likelihood criterion, we also improve the original maximum likelihood criterion. It also follows that the overall maxima of the two criteria must be in the same place, so the best model parameters ˆϕ are the same in both cases. However, the log-likelihood criterion has the practical advantage of using a sum of terms, not a product, so representing it with finite precision isn’t problematic.
这一对数似然准则是等价的,因为对数函数是单调递增函数:如果 z > z′,则 log [z] > log[z′],反之亦然(图 5.2)。由此可知,当我们改变模型参数 ϕ 以提高对数似然准则时,也会提高原来的极大似然准则。同样可知,两个准则的全局最大值必定位于同一位置,因此两种情况下的最佳模型参数 ϕ 相同。然而,对数似然准则具有实际优势:它使用各项之和而非乘积,因此用有限精度表示不会有问题。
Finally, we note that, by convention, model fitting problems are framed in terms of minimizing a loss. To convert the maximum log-likelihood criterion to a minimization problem, we multiply by minus one, which gives us the negative log-likelihood criterion:
which is what forms the final loss function L[ϕ].
最后,我们注意到,按照惯例,模型拟合问题通常以最小化损失的形式表述。为了将最大对数似然准则转换为最小化问题,我们乘以负一,从而得到负对数似然准则:
这构成了最终的损失函数 L[ϕ]。
The network no longer directly predicts the outputs y but instead determines a probability distribution over y. When we perform inference, we often want a point estimate rather than a distribution, so we return the maximum of the distribution:
It is usually possible to find an expression for this in terms of the distribution parameters θ predicted by the model. For example, in the univariate normal distribution, the maximum occurs at the mean µ.
网络不再直接预测输出 y,而是确定关于 y 的概率分布。当我们执行推断时,通常希望得到一个点估计而非分布,因此我们回报该分布的最大值:
通常可以根据模型预测的分布参数 θ 找到相应的表达式。例如,在一元正态分布中,最大值出现在均值 µ 处。
The recipe for constructing loss functions for training data{ x i, y i} using the maximum likelihood approach is hence: 1. Choose a suitable probability distribution Pr( y |θ) defined over the domain of the
predictions y with distribution parameters θ. 2. Set the machine learning model f[ x , ϕ] to predict one or more of these parameters, 4. To perform inference for a new test example x, return either the full distribu-
因此,使用极大似然方法为训练数据 { x i, y i} 构造损失函数的方法如下:
1. 选择一个合适的概率分布 Pr (y |θ),其定义在预测值 y 的域上,分布参数为 θ。 2. 设置机器学习模型 f[ x ϕ] 来预测这些参数中的一个或多个,使得 θ= f[ x ϕ] 以及Pr (y |θ) = Pr (y | f[ x ϕ]) . ˆ 3. 为了训练模型,找到网络参数 ϕ,使其在训练数据集
tion Pr( y | f[ x ,ˆϕ]) or the value where this distribution is maximized.
We devote most of the rest of this chapter to constructing loss functions for common prediction types using this recipe.
对 { x i, y i} 上最小化负对数似然损失函数: 4. 要对新的测试样本 x 执行推断,请返回完整分布 ˆ Pr (y | f[ x ϕ]),或该分布取得最大值
时的值。我们将本章其余部分的大部分内容用于按照这一方法构建常见预测类型的损失函数。

We start by considering univariate regression models. Here the goal is to predict a single scalar output y∈R from input x using a model f[ x , ϕ] with parameters ϕ. Following the recipe, we choose a probability distribution over the output domain y. We select the univariate normal distribution (figure 5.3), which is defined over y∈R. This has two parameters (mean µ and variance σ2) and has a probability density function:
Second, we set the machine learning model f[ x , ϕ] to compute one or more of the parameters of this distribution. Here, we just compute the mean so µ = f[ x , ϕ]:
我们首先考虑一元回归模型。这里的目标是使用参数为 ϕ 的模型 f[ x ϕ],从输入 x 预测单个标量输出 y∈R。按照这一方法,我们在输出域 y 上选择一个概率分布。我们选择一元正态分布(图 5.3),它定义在 y∈R 上。它有两个参数(均值 µ 和方差 σ2),并具有概率密度函数:
其次,我们设置机器学习模型 f[ x ϕ] 来计算该分布的一个或多个参数。这里我们只计算均值,因此 µ = f[ x ϕ]:
We aim to find the parameters ϕ that make the training data{ x i, yi} most probable under this distribution (figure 5.4). To accomplish this, we choose a loss function L[ϕ] based on the negative log-likelihood:
When we train the model, we seek parameters ˆϕ that minimize this loss.
我们旨在找到参数 ϕ,使训练数据 { x i, yi} 在该分布下的概率最大(图 5.4)。为此,我们选择基于负对数似然的损失函数 L[ϕ]:
ˆ 在训练模型时,我们寻找使该损失最小化的参数 ϕ。
Now let’s perform some algebraic manipulations on the loss function. We seek:
现在让我们对损失函数进行一些代数变换。我们寻求:

where we removed the first term between the second and third lines because it doesn’t depend on ϕ. We removed the denominator between the third and fourth lines, as this is just a constant positive scaling factor that does not affect the position of the minimum.
The result of these manipulations is the least squares loss function that we originally introduced when we discussed linear regression in chapter 2: We see that the least squares loss function follows naturally from the assumptions that the predictions are (i) independent and (ii) drawn from a normal distribution with mean µ =
其中,我们在第二行和第三行之间去掉了第一项,因为它不依赖于 ϕ。我们在第三行和第四行之间去掉了分母,因为这只是一个正的常数缩放因子,不会影响最小值的位置。
这些操作的结果就是我们在第 2 章讨论线性回归时最初引入的最小二乘损失函数:我们可以看到,最小二乘损失函数自然源于以下假设,即
f[ x i, ϕ] (figure 5.4).
预测值 (i) 相互独立, (ii) 服从均值为 µ = f[ x i, ϕ] 的正态分布(图 5.4)。
The network no longer directly predicts y but instead predicts the mean µ= f[ x , ϕ] of the normal distribution over y. When we perform inference, we usually want a single “best” point estimate ˆy, so we take the maximum of the predicted distribution:
For the univariate normal distribution, the maximum position is determined by the mean parameter µ (figure 5.3). This is precisely what the model computed, so ˆy= f[ x ,ˆϕ].
网络不再直接预测 y,而是预测正态分布的均值 µ= f[ x ϕ],该正态分布定义在 y 上。在进行推断时,我们通常希望得到一个“最佳”点估计 ˆ y,因此我们取预测分布的最大值:
对于单变量正态分布,最大值位置由均值 ˆ 参数 µ 决定(图 5.3)。这正是模型计算得到的结果,因此 ˆ y= f[ x ϕ]。
To formulate the least squares loss function, we assumed that the network predicted the mean of a normal distribution. The final expression in equation 5.11 (perhaps surpris-
为了构建最小二乘损失函数,我们假设网络预测了正态分布的均值。公式 5.11 中的最终表达式(也许令人惊讶 -
![图 5.4 正态分布下最小二乘损失与极大似然损失的等价性。 a) 考虑图 2.2 中的线性模型。最小二乘准则最小化模型预测 f[xi, ϕ] (绿线)与真实输出](/learning/udl/ch5/figures/ch5_p06_f0.webp)
ingly) does not depend on the variance σ2. However, there is nothing to stop us from treating σ2 as a learned parameter and minimizing equation 5.9 with respect to both the model parameters ϕ and the distribution variance σ2:
In inference, the model predicts the mean µ = f[ x ,ˆϕ] from the input, and we learned the variance ˆσ2 during the training process. The former is the best prediction. The latter tells us about the uncertainty of the prediction.
(不出所料)不依赖于方差 σ2。然而,没有什么能阻止我们将 σ2 视为可学习参数,并同时针对模型参数 ϕ 和分布方差 σ2 最小化公式 5.9:
ˆ 在推理时,模型根据输入预测均值 µ = f[ x ϕ],而我们在训练过程中学习到了方差 ˆ σ2。前者是最佳预测,后者则描述了预测的不确定性。
The model above assumes that the variance of the data is constant everywhere. However, this might be unrealistic. When the uncertainty of the model varies as a function of the input data, we refer to this as heteroscedastic (as opposed to homoscedastic, where the uncertainty is constant).
A simple way to model this is to train a neural network f[ x , ϕ] that computes both the mean and the variance. For example, consider a shallow network with two outputs. We denote the first output as f 1[ x , ϕ] and use this to predict the mean, and we denote the second output as f 2[ x , ϕ] and use it to predict the variance.
There is one complication; the variance must be positive, but we can’t guarantee that the network will always produce a positive output. To ensure that the computed variance is positive, we pass the second network output through a function that maps an arbitrary value to a positive one. A suitable choice is the squaring function, giving:
上述模型假设数据的方差处处恒定。然而,这可能并不现实。当模型的不确定性随输入数据变化时,我们称之为异方差的(与同方差的相反,后者的不确定性恒定)。
一种简单的建模方式是训练一个神经网络 f[ x ϕ],使其同时计算均值和方差。例如,考虑一个具有两个输出的浅层网络。我们将第一个输出记为 f 1[ x ϕ],并用它来预测均值;将第二个输出记为 f 2[ x ϕ],并用它来预测方差。
存在一个复杂之处;方差必须为正,但我们无法保证网络始终输出正值。为确保计算得到的方差为正,我们将网络的第二个输出输入到一个将任意值映射为正值的函数中。一个合适的选择是平方函数,得到:
Homoscedastic and heteroscedastic models are compared in figure 5.5.
由此得到损失函数:同方差和异方差模型的比较见图
5.5。
In binaryclassification, the goal is to assign the data x to one of two discrete classes y∈ {0, 1}. In this context, we refer to y as a label. Examples of binary classification include (i) predicting whether a restaurant review is positive (y=1) or negative (y=0) from text data x and (ii) predicting whether a tumor is present (y=1) or absent (y=0) from an MRI scan x.
在二分类中,目标是将数据 x 分配给两个离散类别之一 y∈{0, 1}。在此语境下,我们将 y 称为标签。二分类的示例包括 (i) 从文本数据 x 预测餐厅评论是正面(y=1)还是负面(y=0),以及 (ii) 从 MRI 扫描 x 预测肿瘤是否存在(y=1)或不存在(y=0)。

Once again, we follow the recipe from section 5.2 to construct the loss function. First, we choose a probability distribution over the output space y∈{0, 1}. A suitable choice is the Bernoulli distribution, which is defined on the domain{0, 1}. This has a single parameter λ ∈[0, 1] that represents the probability that y takes the value one (figure 5.6):
同样,我们遵循第 5.2 节的方法构建损失函数。首先,我们在输出空间 y∈{0, 1} 上选择一个概率分布。合适的选择是伯努利分布,其定义域为 {0, 1}。它只有一个参数 λ ∈[0, 1],表示 y 取值为 1 的概率(图 5.6):

![图 5.7 logistic sigmoid 函数。该函数将实数轴 z∈R 映射到 0 和 1 之间的数,因此 sig[z] ∈[0, 1]。输入 0 映射到](/learning/udl/ch5/figures/ch5_p10_f0.webp)
which can equivalently be written as:
Second, we set the machine learning model f[ x , ϕ] to predict the single distribution parameter λ. However, λ can only take values in the range [0, 1], and we cannot guarantee that the network output will lie in this range. Consequently, we pass the network output through a function that maps the real numbers R to[0, 1]. A suitable function is the logistic sigmoid (figure 5.7):
这也可以等价地写为:
其次,我们设置机器学习模型 f[ x ϕ] 来预测单个分布参数 λ。然而, λ 只能取值于范围 [0, 1],,且我们无法保证网络输出落在该范围内。因此,我们将网络输出通过一个将实数 R 映射到 [0, 1] 的函数。合适的函数是 logisticsigmoid (图 5.7):
This is depicted in figure 5.8 for a shallow neural network model. The loss function is the negative log-likelihood of the training set:
图 5.8 展示了浅层神经网络模型中的这一过程。损失函数是训练集的负对数似然:
For reasons to be explained in section 5.7, this is known as the binary cross-entropy loss. The transformed model output sig[ f[ x , ϕ]] predicts the parameter λ of the Bernoulli
由于将在第 5.7 节中解释的原因,这被称为二元交叉熵损失。变换后的模型输出 sig[ f[ x ϕ]] 预测伯努利分布的参数 λ
distribution. This represents the probability that y=1, and it follows that 1−λ represents the probability that y= 0. When we perform inference, we may want a point
estimate of y, so we set y= 1 if λ > 0.5 and y= 0 otherwise.


The goal of multiclassclassification is to assign an input data example x to one of K> 2 classes, so y∈{1, 2,..., K}. Real-world examples include (i) predicting which of K= 10 digits y is present in an image x of a handwritten number and (ii) predicting which of K possible words y follows an incomplete sentence x.
We once more follow the recipe from section 5.2.
We first choose a distribution over the prediction space y.
多分类的目标是将输入数据示例 x 归入 K> 2 个类别之一,因此 y∈{1, 2,..., K}。现实世界中的示例包括 (i) 预测 K= 10 个数字中 y 出现在手写数字图像 x 中,以及 (ii) 预测 K 个可能词中 y 跟在未完成句子 x 之后。
In this case, we have y∈{1, 2,..., K}, so we choose the categoricaldistribution (figure 5.9), which is defined on this domain. This has K parameters λ1, λ2,..., λK, which determine the probability of each category:
我们再次遵循第 5.2 节的方法。我们首先在预测空间 y 上选择一个分布。在这种情况下,我们有 y∈{1, 2,..., K},因此选择类别分布(图 5.9),它在该定义域上定义。它具有 K 个参数 λ1, λ2,..., λK,它们决定每个类别的概率:
The parameters are constrained to take values between zero and one, and they must collectively sum to one to ensure a valid probability distribution.
Then we use a network f[ x , ϕ] with K outputs to compute these K parameters from the input x. Unfortunately, the network outputs will not necessarily obey the aforementioned constraints. Consequently, we pass the K outputs of the network through a function that ensures these constraints are respected. A suitable choice is the softmax function (figure 5.10). This takes an arbitrary vector of length K and returns a vector of the same length but where the elements are now in the range[0, 1] and sum to one. The kth output of the softmax function is: softmax k[ z ] = where the exponential functions ensure positivity, and the sum in the denominator en-
参数的取值被约束在 0 到 1 之间,并且它们的总和必须为 1,以确保得到有效的概率分布。
然后,我们使用网络 f[ x ϕ],它具有 K 个输出,用于从输入 x 计算这些 K 个参数。遗憾的是,网络输出不一定满足前述约束。因此,我们将网络的 K 个输出输入到一个确保这些约束得到满足的函数中。一个合适的选择是 Softmax 函数(图 5.10)。它接受一个长度为 K 的任意向量,并返回一个长度相同的向量,其中各元素现在位于范围 [0, 1] 内且总和为 1。 Softmax 函数的第 kth 个输出为: softmax k[ z ] =其中指数函数确保为正,并且分母中的和
sures that the K numbers sum to one.
确保这些 K 个数值之和为一。
The likelihood that input x has label y= k (figure 5.10) is hence:
The loss function is the negative log-likelihood of the training data:
输入 x 具有标签 y= k (图 5.10)的似然因此为:
损失函数是训练数据的负对数似然:
For reasons that will be explained in section 5.7, this is known as the multiclass crossentropy loss. The transformed model output represents a categorical distribution over possible classes y∈{1, 2,..., K}. For a point estimate, we take the most probable category ˆy=
i 和 k′th 输出。由于将在第 5.7 节解释的原其中 f yi[ x ϕ] 和 f k′[ x ϕ] 分别表示网络的 yth 因,这被称为多类交叉熵损失。转换后的模型输出表示一个关于可能的类别分布

value of x in figure 5.10.
In this chapter, we have focused on regression and classification because these problems are widespread. However, to make different types of predictions, we simply choose an appropriate distribution over that domain and apply the recipe in section 5.2. Figure 5.11 enumerates a series of probability distributions and their prediction domains. Some of these are explored in the problems at the end of the chapter.
在本章中,我们重点介绍了回归和分类,因为这些问题非常普遍。然而,为了做出不同类型的预测,我们只需在该领域上选择适当的分布,并应用 5.2 节中的方法。图 5.11 列举了一系列概率分布及其预测领域。其中一些
这些内容在章末习题中探讨。
Often, we wish to make more than one prediction with the same model, so the target output y is a vector.
For example, we might want to predict a molecule’s melting and boiling point (a multivariate regression problem, figure 1.2b) or the object class at every point in an image (a multivariate classification problem, figure 1.4a). While it is possible to define multivariate probability distributions and use a neural network to model their parameters as a function of the input, it is more usual to treat each prediction as independent. Independence implies that we treat the probability Pr( y | f[ x , ϕ]) as a product of
通常,我们希望使用同一个模型进行多次预测,因此目标输出 y 是一个向量。例如,我们可能想要预测分子的熔点和沸点(多元回归问题,图 1.2b)或图像中每个点的对象类别(多元分类问题,图 1.4a)。虽然可以定义多元概率分布,并使用神经网络将其参数建模为输入的函数,但更常见的是将每个预测视为独立的。独立性意味着我们将概率 Pr( y | f[ x ϕ]) 视为乘积
univariate terms for each element yd∈ y: where f d[ x , ϕ] is the dth set of network outputs, which describe the parameters of the distribution over yd. For example, to predict multiple continuous variables yd∈R, we use a normal distribution for each yd, and the network outputs f d[ x , ϕ] predict the means of these distributions. To predict multiple discrete variables yd∈{1, 2,..., K}, we use a
每个元素 yd∈ y 的单变量项:其中 f d[ x ϕ] 是 dth 一组网络输出,它们描述 yd 上的分布的参数。例如,要预测多个连续变量 yd∈R,我们对每个 yd 使用正态分布,网络输出 f d[ x ϕ] 预测这些分布的均值。要预测多个离散变量 yd∈{1, 2,..., K},我们使用一个


categorical distribution for each yd. Here, each set of network outputs f d[ x , ϕ] predicts the K values that contribute to the categorical distribution for yd.
When we minimize the negative log probability, this product becomes a sum of terms:
类别分布,每个 yd 一个。在这里,每组网络输出 f d[ x ϕ] 预测 K 值,这些值用于 yd 的类别分布。
当我们最小化负对数概率时,该乘积变为各项之和:
where yid is the dth output from the ith training example. To make two or more prediction types simultaneously, we similarly assume the errors in each are independent. For example, to predict wind direction and strength, we might
其中 yid 是来自 ith 训练样本的 dth 输出。
choose the von Mises distribution (defined on circular domains) for the direction and the exponential distribution (defined on positive real numbers) for the strength. The independence assumption implies that the joint likelihood of the two predictions is the product of individual likelihoods. These terms will become additive when we compute the negative log-likelihood.
对于方向选择冯 · 米塞斯分布(定义在圆形域上),对于强度选择指数分布(定义在正实数上)。独立性假设意味着两个预测的联合似然是各自似然的乘积。当我们计算负对数似然时,这些项会变为相加。

In this chapter, we developed loss functions that minimize negative log-likelihood. However, the term cross-entropy loss is also commonplace. In this section, we describe the cross-entropy loss and show that it is equivalent to using negative log-likelihood.
The cross-entropy loss is based on the idea of finding parameters θ that minimize the distance between the empirical distribution q(y) of the observed data y and a model distribution Pr(y|θ) (figure 5.12). The distance between two probability distributions q(z) and p(z) can be evaluated using the Kullback-Leibler (KL) divergence: Now consider that we observe an empirical data distribution at points{yi}I
在本章中,我们开发了最小化负对数似然的损失函数。然而,交叉熵损失这一术语也很常见。在本节中,我们描述交叉熵损失,并说明它等同于使用负对数似然。交叉熵损失基于这样的思想:寻找参数 θ,使观测数据 y 的经验分布 q (y) 与模型分布 Pr (y|θ) 之间的距离最小化(图 5.12)。两个概率分布 q (z) 之间的距离
与 p(z) 可以使用 Kullback-Leibler (KL) 散度计算:现在考虑我们在点{yi}I
can describe this as a weighted sum of point masses:
i=1 处观测到一个经验数据分布。我们可以将其描述为点质量的加权和:
where δ[•] is the Dirac delta function. We want to minimize the KL divergence between the model distribution Pr(y|θ) and this empirical distribution:
其中 δ[•] 是狄拉克 δ 函数。我们希望最小化 KL 散度,二者之间模型分布 Pr(y|θ) 和这个经验分布:

where the first term disappears, as it has no dependence on θ. The remaining second term is known as the cross-entropy. It can be interpreted as the amount of uncertainty that remains in one distribution after taking into account what we already know from the other. Now, we substitute in the definition of q(y) from equation 5.28:
其中第一项消失,因为它不依赖于 θ。剩下的第二项被称为交叉熵。它可以解释为:在考虑了从另一个分布中已经获得的信息之后,一个分布中仍然保留的不确定性。现在,我们将式 5.28 中 q (y) 的定义代入:
The product of the two terms in the first line corresponds to pointwise multiplying the point masses in figure 5.12a with the logarithm of the distribution in figure 5.12b. We are left with a finite set of weighted probability masses centered on the data points. In the last line, we have eliminated the constant scaling factor 1/I, as this does not affect the position of the minimum.
In machine learning, the distribution parameters θ are computed by the model f[ x i, ϕ], so we have:
第一行中两项的乘积,对应于将图 5.12a 中的点质量与图 5.12b 中分布的对数进行逐点相乘。我们最终得到的是有限的一组以数据点为中心的带权重概率质量。在最后一行中,我们消去了常数缩放因子 1/I,因为它不会影响最小值的位置。
在机器学习中,分布参数 θ 由模型 f[ x i, ϕ] 计算得到,因此我们有:
This is precisely the negative log-likelihood criterion from the recipe in section 5.2.
It follows that the negative log-likelihood criterion (from maximizing the data likelihood) and the cross-entropy criterion (from minimizing the distance between the model and empirical data distributions) are equivalent.
这正是第 5.2 节方法中的负对数似然准则。
由此可知,负对数似然准则(来自最大化数据似然)与交叉熵准则(来自最小化模型分布与经验数据分布之间的距离)是等价的。
We previously considered neural networks as directly predicting outputs y from data x. In this chapter, we shifted perspective to think about neural networks as computing the parameters θ of probability distributions Pr( y |θ) over the output space. This led to a principled approach to building loss functions. We selected model parameters ϕ that maximized the likelihood of the observed data under these distributions. We saw that this is equivalent to minimizing the negative log-likelihood. The least squares criterion for regression is a natural consequence of this approach; it follows from the assumption that y is normally distributed and that we are predicting the mean. We also saw how the regression model could be (i) extended to estimate the uncertainty over the prediction and (ii) extended to make that uncertainty dependent on the input (the heteroscedastic model). We applied the same approach to both binary and multiclass classification and derived loss functions for each. We discussed how to
tackle more complex data types and how to deal with multiple outputs. Finally, we argued that cross-entropy is an equivalent way to think about fitting models.
In previous chapters, we developed neural network models. In this chapter, we developed loss functions for deciding how well a model describes the training data for a given set of parameters. The next chapter considers model training, in which we aim to find the model parameters that minimize this loss.
我们此前将神经网络视为直接从数据 x 预测输出 y。在本章中,我们转变了视角,将神经网络视为计算输出空间上概率分布的参数 θPr (y |θ)。这促成了一种构建损失函数的原则性方法。我们选择了模型参数 ϕ,使其在这些分布下最大化观测数据的似然。我们发现这等价于最小化负对数似然。回归的最小二乘准则是这一方法的自然结果;它源于假设 y 服从正态分布,并且我们预测的是均值。我们还看到了回归模型如何 (i) 扩展以估计预测的不确定性,以及 (ii) 扩展以使该不确定性依赖于输入(异方差模型)。我们将相同的方法应用于二分类和多分类,并为各自推导了损失函数。我们讨论了如何
在前面的章节中,我们构建了神经网络模型。在本章中,我们推导了损失函数,用于判断在给定的一组参数下模型对训练数据的拟合程度。下一章将讨论模型训练,其目标是找到使该损失最小化的模型参数。
Losses based on the normal distribution:
Robust regression:
基于正态分布的损失: Nix 与 Weigend (1994)以及 Williams (1996)研究了异方差非线性
Estimating quantiles:
Class imbalance and focal loss:
Learning to rank:
Other data types:
![图 5.13 von Mises 分布定义在圆形定义域(−π, π])上。它有两个参数。均值 µ 决定峰值的位置。集中参数 κ> 0 的作用类似于方差的倒](/learning/udl/ch5/figures/ch5_p19_f0.webp)
Non-probabilistic approaches:


