第 3 章 · 浅层神经网络
Understanding Deep Learning — Shallow neural networks
Chapter 2 introduced supervised learning using 1D linear regression. However, this model can only describe the input/output relationship as a line. This chapter introduces shallow neural networks. These describe piecewise linear functions and are expressive enough to approximate arbitrarily complex relationships between multi-dimensional inputs and outputs.
第 2 章介绍了使用一维线性回归的监督学习。然而,该模型只能将输入 / 输出关系描述为一条直线。本章介绍浅层神经网络。这些神经网络描述分段线性函数,并且具有足够的表达能力,可以近似多维输入与输出之间任意复杂的关系。
Shallow neural networks are functions y = f[ x , ϕ] with parameters ϕ that map multivariate inputs x to multivariate outputs y. We defer a full definition until section 3.4 and introduce the main ideas using an example network f[x, ϕ] that maps a scalar input x to a scalar output y and has ten parameters ϕ = {ϕ0, ϕ1, ϕ2, ϕ3, θ10, θ11, θ20, θ21, θ30, θ31}:
We can break down this calculation into three parts: first, we compute three linear functions of the input data (θ10 + θ11x, θ20 + θ21x, and θ30 + θ31x). Second, we pass the three results through an activationfunction a[•]. Finally, we weight the three resulting activations with ϕ1, ϕ2, and ϕ3, sum them, and add an offset ϕ0.
浅层神经网络是函数 y = f[ x ϕ],具有参数 ϕ,可将多元输入 x 映射到多元输出 y。我们将完整定义推迟到 3.4 节,并使用一个示例网络 f[x, ϕ] 来介绍主要思想,该网络将标量输
我们可以将此计算拆分为三个部分:首先,计算输入数据的三个线性函数(θ10 + θ11x、 θ20 + θ21x 和 θ30 + θ31x)。其次,将三个结果通过一个激活函数 a[•]。最后,我们使用权重 ϕ1, ϕ2 和 ϕ3 对三个所得激活值进行加权,求和,并加上一个偏置 ϕ0。
To complete the description, we must define the activation function a[•]. There are many possibilities, but the most common choice is the rectified linear unit or ReLU:
This returns the input when it is positive and zero otherwise (figure 3.1). It is probably not obvious which family of input/output relations is represented by equation 3.1. Nonetheless, the ideas from the previous chapter are all applicable. Equation 3.1 represents a family of functions where the particular member of the family
depends on the ten parameters in ϕ. If we know these parameters, we can perform inference (predict y) by evaluating the equation for a given input x. Given a training dataset{xi, yi}I
它会在输入为正时回报输入,否则回报零(图 3.1)。公式 3.1 所表示的是哪一族输入 / 输出关系可能并不显而易见。尽管如此,前一章中的思想都仍然适用。公式 3.1 表示一个函数族,而该函数族中的具体成员取决于 ϕ 中的十个参数。如果已知这些参数,我们可以通过对给定输入 x 计算该方程来执行推断(预测 y)。给定训练数据集 {xi, yi}I

i=1, we can define a least squares loss function L[ϕ] and use this to measure how effectively the model describes this dataset for any given parameter values ϕ. To train the model, we search for the values ˆϕ that minimize this loss.
i=1,我们可以定义最小二乘损失函数 L[ϕ],并使用它来衡量对于任意给定的参数值 ϕ,模型描述该数据集的有效性。 ˆ 为了训练模型,我们搜索使该损失最小化的值 ϕ。
In fact, equation 3.1 represents a family of continuous piecewise linear functions (figure 3.2) with up to four linear regions. We now break down equation 3.1 and show why it describes this family. To make this easier to understand, we split the function into two parts. First, we introduce the intermediate quantities: where we refer to h1, h2, and h3 as hiddenunits. Second, we compute the output by combining these hidden units with a linear function: 1
事实上,公式 3.1 表示一族连续的分段线性函数(图 3.2),最多包含四个线性区域。我们现在拆解公式 3.1,并说明为什么它描述了这一族函数。为了便于理解,我们将函数拆分为两部分。首先,我们引入中间量:其中,我们将 h1、 h2 和 h3 称为隐藏单元。其次,我们通过将这些隐藏单元与一个线性函数相结合来计算输出: 1
Figure 3.3 shows the flow of computation that creates the function in figure 3.2a. Each hidden unit contains a linear function θ•0+ θ•1x of the input, and that line is clipped by the ReLU function a[•] below zero. The positions where the three lines cross zero become the three “joints” in the final output. The three clipped lines are then weighted by ϕ1, ϕ2, and ϕ3, respectively. Finally, the offset ϕ0 is added, which controls the overall height of the final function.
图 3.3 展示了生成图 3.2a 中函数的计算流程。每个隐藏单元包含一个关于输入的线性函数 θ•0+ θ•1x,该直线会被 ReLU 函数 a[•] 在零以下截断。三条直线与零相交的位置成为最终输出中的三个“关节”。这三条截断后的直线随后分别以权重 ϕ1、 ϕ2 和 ϕ3 进行加权。最后加上偏移量 ϕ0,它控制最终函数的整体高度。
Each linear region in figure 3.3j corresponds to a different activation pattern in the hidden units. When a unit is clipped, we refer to it as inactive, and when it is not clipped, we refer to it as active. For example, the shaded region receives contributions from h1 and h3 (which are active) but not from h2 (which is inactive). The slope of each linear region is determined by the original slopes θ•1 of the active inputs for this region and the weights ϕ• that were subsequently applied. For example, the slope in the shaded region (see problem 3.3) is θ11ϕ1 + θ31ϕ3, where the first term is the slope in panel (g) and the second term is the slope in panel (i).
Each hidden unit contributes one “joint” to the function, so with three hidden units, there can be four linear regions. However, only three of the slopes of these regions are independent; the fourth is either zero (if all the hidden units are inactive in this region) or is a sum of slopes from the other regions.
图 3.3j 中的每个线性区域都对应隐藏单元中一种不同的激活模式。当一个单元被截断时,我们称其为不活跃;当它未被截断时,我们称其为活跃。例如,阴影区域接收来自 h1 和 h3 (它们是活跃的)的贡献,但不接收来自 h2 (它是不活跃的)的贡献。每个线性区域的斜率由该区域中活跃输入的原始斜率 θ•1 以及随后施加的权重 ϕ• 决定。例如,阴影区域的斜率(见习题 3.3)为 θ11ϕ1 + θ31ϕ3,其中第一项是面板 (g) 中的斜率,第二项是面板 (i) 中的斜率。每个隐藏单元为该函数贡献一个“关节”,因此有三个隐藏单元时,
或者是其他区域斜率的总和。
We have been discussing a neural network with one input, one output, and three hidden units. We visualize this network in figure 3.4a. The input is on the left, the hidden units are in the middle, and the output is on the right. Each connection represents one of the ten parameters. To simplify this representation, we do not typically draw the intercept parameters, so this network is usually depicted as in figure 3.4b.
我们一直在讨论一个具有一个输入、一个输出和三个隐藏单元的神经网络。我们在图 3.4a 中对该网络进行可视化。输入在左侧,隐藏单元在中间,输出在右侧。每条连接代表十个参数中的一个。为简化表示,我们通常不绘制截距参数,因此该网络通常如图 3.4b 所示。


In the previous section, we introduced an example neural network with one input, one output, ReLU activation functions, and three hidden units. Let’s now generalize this slightly and consider the case with D hidden units where the dth hidden unit is:
and these are combined linearly to create the output:
The number of hidden units in a shallow network is a measure of the network capacity. With ReLU activation functions, the output of a network with D hidden units has at most D joints and so is a piecewise linear function with at most D + 1 linear regions. As we add more hidden units, the model can approximate more complex functions.
Indeed, with enough capacity (hidden units), a shallow network can describe any continuous 1D function defined on a compact subset of the real line to arbitrary precision. To see this, consider that every time we add a hidden unit, we add another linear region to the function. As these regions become more numerous, they represent smaller sections of the function, which are increasingly well approximated by a line (figure 3.5). The universal approximation theorem proves that for any continuous function, there exists a shallow network that can approximate this function to any specified precision.
这些单元通过线性组合生成输出:
事实上,只要具有足够的容量(隐藏单元),浅层网络就能以任意精度描述定义在实数轴紧致子集上的任意连续一维函数。为了理解这一点,可以考虑每增加一个隐藏单元,就会为函数增加另一个线性区域。随着这些区域数量增多,它们表示函数的更小片段,而这些片段越来越能被直线很好地近似(图 3.5)。该通用近似定理证明,对于任意连续函数,都存在一个浅层网络,能够以任意指定精度近似该函数。

In the above example, the network has a single scalar input x and a single scalar output y. However, the universal approximation theorem also holds for the more general case where the network maps multivariate inputs x = [x1, x2,..., xDi]T to multivariate output predictions y =[y1, y2,..., yDo]T. We first explore how to extend the model to predict multivariate outputs. Then we consider multivariate inputs. Finally, in section 3.4, we present a general definition of a shallow neural network.
在上述示例中,网络具有一个标量输入 x 和一个标量输出 y。然而,通用近似定理也适用于更一般的情况,即网络将多元输入 x = [x1, x2,..., xDi]T 映射为多元输出预测 y =[y1, y2,..., yDo]T。我们首先探讨如何将模型扩展为预测多元输出。然后考虑多元输入。最后,在第 3.4 节中,我们给出浅层神经网络的一般定义。
To extend the network to multivariate outputs y, we simply use a different linear function of the hidden units for each output. So, a network with a scalar input x, four hidden units h1, h2, h3, and h4, and a 2D multivariate output y = [y1, y2]T would be defined as: and
and
![图 3.6 具有一个输入、四个隐藏单元和两个输出的网络。 a) 网络结构可视化。 b) 该网络生成两个分段线性函数, y1[x] 和 y2[x]。由于它们共享相](/learning/udl/ch3/figures/ch3_p07_f0.webp)
The two outputs are two different linear functions of the hidden units.
As we saw in figure 3.3, the “joints” in the piecewise functions depend on where the initial linear functions θ•0 + θ•1x are clipped by the ReLU functions a[•] at the hidden units. Since both outputs y1 and y2 are different linear functions of the same four hidden units, the four “joints” in each must be in the same places. However, the slopes of the linear regions and the overall vertical offset can differ (figure 3.6).
这两个输出是隐藏单元的两个不同线性函数。正如我们在图 3.3 中所见,分段函数中的“拐点”取决于初始线性函数 θ•0 + θ•1x 在隐藏单元处被 ReLU 函数 a[•] 截断的位置。由于两个输出 y1 和 y2 都是相同四个隐藏
To cope with multivariate inputs x, we extend the linear relations between the input and the hidden units. So a network with two inputs x = [x1, x2]T and a scalar output y (figure 3.7) might have three hidden units defined by:
为了处理多元输入 x,我们扩展输入与隐藏单元之间的线性关系。因此,具有两个输入 x = [x1, x2]T 和一个标量输出 y (图 3.7)的网络可能有三个隐藏单元,定义如下:
![图 3.8 具有两个输入的神经网络中的处理过程 x =[x1, x2]T,三个隐藏单元 h1, h2, h3,以及一个输出 y。 a–c) 每个隐藏单元的输入是](/learning/udl/ch3/figures/ch3_p07_f1.webp)
where there is now one slope parameter for each input. The hidden units are combined to form the output in the usual way: Figure 3.8 illustrates the processing of this network. Each hidden unit receives a linear combination of the two inputs, which forms an oriented plane in the 3D input/output space. The activation function clips the negative values of these planes to zero. The clipped planes are then recombined in a second linear function (equation 3.10) to create
a continuous piecewise linear surface consisting of convex polygonal regions (figure 3.8j). Each region corresponds to a different activation pattern. For example, in the central
现在每个输入都有一个斜率参数。隐藏单元按常规方式组合形成输出:图 3.8 展示了该网络的处理过程。每个隐藏单元接收一个线性两个输入的组合,这在三维输入 / 输出中形成一个有方向的平面
triangular region, the first and third hidden units are active, and the second is inactive.
When there are more than two inputs to the model, it becomes difficult to visualize. However, the interpretation is similar. The output will be a continuous piecewise linear function of the input, where the linear regions are now convex polytopes in the multidimensional input space. Note that as the input dimensions grow, the number of linear regions increases rapidly (figure 3.9). To get a feeling for how rapidly, consider that each hidden unit defines a hyperplane that delineates the part of space where this unit is active from the part where it is not (cyan lines in 3.8d–f). If we had the same number of hidden units as input dimensions Di, we could align each hyperplane with one of the coordinate axes
当模型有超过两个输入时,可视化变得困难。然而,解释类似。输出将是输入的连续分段线性函数,其中线性区域现在是多维输入空间中的凸多胞形。
(figure 3.10). For two input dimensions, this would divide the space into four quadrants. For three dimensions, this would create eight octants, and for Di dimensions, this would create 2Di orthants. Shallow neural networks usually have more hidden units than input dimensions, so they typically create more than 2Di linear regions.
它不成立的区域(3.8d–f 中的青色线条)。如果隐藏单元数与输入维度数 Di 相同,我们可以让每个超平面与其中一个坐标轴对齐(图 3.10)。对于两个输入维度,这会将空间划分为四个象限。对于三维,这会产生八个八分体,而对于 Di 维,这会产生 2Di 个卦限。浅层神经网络通常拥有比输入维度更多的隐藏单元,因此它们通常会产生多于 2Di 个线性区域。
We have described several example shallow networks to help develop intuition about how they work. We now define a general equation for a shallow neural network y = f[ x , ϕ] that maps a multi-dimensional input x ∈RDi to a multi-dimensional output y ∈RDo using D hidden units. Each hidden unit is computed as:
我们已经描述了几个示例浅层网络,以帮助建立对其工作原理的直觉。我们现在定义一个浅层神经网络的一般方程 y = f[ x ϕ],它将多维输入 x ∈RDi 映射为多维输出 y ∈RDo,其中使用 D 个隐藏单元。每个隐藏单元的计算方式如下:
并且这些隐藏单元经线性组合生成输出:
and these are combined linearly to create the output:


where a[•] is a nonlinear activation function. The model has parameters ϕ = {θ••, ϕ••}. Figure 3.11 shows an example with three inputs, three hidden units, and two outputs.
The activation function permits the model to describe nonlinear relations between input and the output, and as such, it must be nonlinear itself; with no activation function, or a linear activation function, the overall mapping from input to output would be restricted to be linear. Many different activation functions have been tried (see figure 3.13), but the most common choice is the ReLU (figure 3.1), which has the merit of being easily interpretable.
With ReLU activations, the network divides the input space into convex polytopes defined by the intersections of hyperplanes computed by the “joints” in the ReLU functions. Each convex polytope contains a different linear function. The polytopes are the same for each output, but the linear functions they contain can differ.
其中 a[•] 是一个非线性激活函数。模型具有参数 ϕ = {θ••, ϕ••}。图 3.11 显示了一个具有三个输入、三个隐藏单元和两个输出的示例。
激活函数使模型能够描述输入与输出之间的非线性关系,因此它本身必须是非线性的;如果没有激活函数,或者使用线性激活函数,则从输入到输出的整体映射将被限制为线性的。人们尝试了许多不同的激活函数(见图 3.13),但最常见的选择是 ReLU (图 3.1),它具有优点易于解释。采用 ReLU 激活时,网络将输入空间划分为由 ReLU 函数中的“拐点”计算出的超平面交点所定义的凸多面体。每个凸多面体包含一个不同的线性函数。对于每个输出,这些多面体都是相同的,但它们包含的线性函数可以不同。
We conclude this chapter by introducing some terminology. Regrettably, neural networks have a lot of associated jargon. They are often referred to in terms of layers. The left of figure 3.12 is the input layer, the center is the hidden layer, and to the right is the output layer. We would say that the network in figure 3.12 has one hidden layer containing four hidden units. The hidden units themselves are sometimes referred to as neurons. When we pass data through the network, the values of the inputs to the hidden layer (i.e., before the ReLU functions are applied) are termed pre-activations. The values at the hidden layer (i.e., after the ReLU functions) are termed activations.
For historical reasons, any neural network with at least one hidden layer is also called a multi-layer perceptron, or MLP for short. Networks with one hidden layer (as described in this chapter) are sometimes referred to as shallow neural networks. Networks with multiple hidden layers (as described in the next chapter) are referred to as deep neural networks. Neural networks in which the connections form an acyclic graph (i.e., a graph with no loops, as in all the examples in this chapter) are referred to as feed-forward networks. If every element in one layer connects to every element in the next (as in all the examples in this chapter), the network is fully connected. These connections
我们通过引入一些术语来结束本章。遗憾的是,神经网络有很多相关行话。它们通常用层来描述。图 3.12 的左侧是输入层,中间是隐藏层,右侧是输出层。我们会说图 3.12 中的网络有一个包含四个隐藏单元的隐藏层。隐藏单元本身有时被称为神经元。当我们将数据通过网络传递时,输入到隐藏层的值(即在应用 ReLU 函数之前)被称为预激活。隐藏层的值(即在 ReLU 函数之后)被称为激活。由于历史原因,任何至少包含一个隐藏层的神经网络也被称为多层感知机,或简称 MLP。只有一个隐藏层的网络(如本章所述)有时被称为浅层神经网络。具有多个隐藏层的网络(如下一章所述)被称为深层神经网络。连接形成无环图(即没有环路的图,如本章所有示例)的神经网络被称为前馈网络。如果某一层的每个元素都与下一层的每个元素相连(如本章所有示例),则该网络是全连接的。这些连接
represent slope parameters in the underlying equations and are referred to as network weights. The offset parameters (not shown in figure 3.12) are called biases.
表示底层方程中的斜率参数,被称为网络权重。偏移参数(图 3.12 中未显示)称为偏差。

Shallow neural networks have one hidden layer. They (i) compute several linear functions of the input, (ii) pass each result through an activation function, and then (iii) take a linear combination of these activations to form the outputs. Shallow neural networks make predictions y based on inputs x by dividing the input space into a continuous surface of piecewise linear regions. With enough hidden units (neurons), shallow neural networks can approximate any continuous function to arbitrary precision.
Chapter 4 discusses deep neural networks, which extend the models from this chapter by adding more hidden layers. Chapters 5–7 describe how to train these models.
浅层神经网络有一个隐藏层。它们(i)计算输入的若干线性函数,(ii)将每个结果送入激活函数,然后(iii)对这些激活值进行线性组合以形成输出。浅层神经网络生成预测 y,这些预测基于输入 x,方法是将输入空间划分为由分段线性区域构成的连续曲面。当隐藏单元(神经元)数量足够时,浅层神经网络可以以任意精度逼近任何连续函数。
第 4 章讨论深层神经网络,它通过增加更多隐藏层来扩展本章中的模型。第 5–7 章描述如何训练这些模型。
“Neural” networks:


History of neural networks:
Activation functions:
Universal approximation theorem:
Number of linear regions:

Linear, affine, and nonlinear functions:
线性函数、仿射函数和非线性函数:从技术上讲,线性变换 f[•] 是任何遵循叠加原理的函数,因

