第 16 章 · 归一化流
Understanding Deep Learning — Normalizing flows
Chapter 15 introduced generative adversarial networks (GANs). These are generative models that pass a latent variable through a deep network to create a new sample. GANs are trained using the principle that the samples should be indistinguishable from real data. However, they don’t define a distribution over data examples. Hence, assessing the probability that a new example belongs to the same dataset isn’t straightforward.
In this chapter, we describe normalizing flows. These learn a probability model by transforming a simple distribution into a more complicated one using a deep network. Normalizing flows can both sample from this distribution and evaluate the probability of new examples. However, they require specialized architecture: each layer must be invertible. In other words, it must be able to transform data in both directions.
第 15 章介绍了生成对抗网络(GAN)。这些是生成模型,它们将隐变量通过深度网络以生成新样本。 GAN 的训练原则是样本应与真实数据无法区分。然而,它们并未定义数据样本上的分布。因此,评估新样本属于同一数据集的概率并不简单。
在本章中,我们介绍归一化流。它们通过使用深度网络将简单分布转换为更复杂的分布来学习概率模型。归一化流既可以从该分布采样,也可以评估新样本的概率。然而,它们需要专门的架构:每一层都必须可逆。换句话说,它必须能够双向转换数据。
Normalizing flows are probabilistic generative models: they fit a probability distribution to training data (figure 14.2b). Consider modeling a 1D distribution Pr(x). Normalizing flows start with a simple tractable base distribution Pr(z) over a latent variable z and apply a function x= f[z, ϕ], where the parameters ϕ are chosen so that Pr(x) has the desired distribution (figure 16.1). Generating a new example x∗ is easy; we draw z∗ from the base density and pass this through the function so that x∗= f[z∗, ϕ].
归一化流是概率生成模型:它们将概率分布拟合到训练数据(图 14.2b)。考虑对一维分布 Pr (x) 进行建模。归一化流从一个简单可处理的基础分布 Pr (z) 开始,该分布定义在隐变量 z 上,并应用函数 x= f[z, ϕ],其中参数 ϕ 的选择使得 Pr (x) 具有所需分布(图 16.1)。生成新样本 x∗ 很容易;我们从基础密度中抽取 z∗,并将其通过函数,使
Measuring the probability of a data point x is more challenging. Consider applying a function f[z, ϕ] to random variable z with known density Pr(z). The probability density will decrease in areas that are stretched by the function and increase in areas that are compressed so that the area under the new distribution remains one. The degree to which a function f[z, ϕ] stretches or compresses its input depends on the magnitude of its gradient. If a small change to the input causes a larger change in the output, it
测量数据点 x 的概率更具挑战性。考虑将函数 f[z, ϕ] 应用于具有已知密度 Pr (z) 的随机变量 z。概率密度会在被函数拉伸的区域减小,并在被函数压缩的区域增大,从而使新分布下的面积保持为 1。函数 f[z, ϕ] 拉伸或压缩其输入的程度取决于其梯度的大小。如果输入的微小变化导致输出发生更大的变化,它
![图 16.1 概率分布的变换。 a) 基础密度是定义在隐变量 z 上的标准正态分布。 b) 该变量通过函数 x = f[z, ϕ] 变换为新变量 x, c) 从](/learning/udl/ch16/figures/ch16_p02_f0.webp)
stretches the function. If a small change to the input causes a smaller change in the output, it compresses the function (figure 16.2).
More precisely, the probability of data x under the transformed distribution is: where z= f −1[x, ϕ] is the latent variable that created x. The term Pr(z) is the original probability of this latent variable under the base density. This is moderated according to the magnitude of the derivative of the function. If this is greater than one, then the
拉伸函数。如果输入的微小变化导致输出的变化更小,则会压缩函数(图 16.2)。
更准确地说,变换后分布下数据 x 的概率为:其中, z= f −1[x, ϕ] 是生成 x 的隐变量。项 Pr (z) 是原始在基准密度下,该隐变量的概率。该概率会根据函数导数的大小进行调整。如果该值大

probability decreases. If it is smaller, the probability increases.
于一,则概率减小;如果较小,则概率增大。
To draw samples from the distribution, we need the forward mapping x = f[z, ϕ], but to measure the likelihood, we need to compute the inverse z= f −1[x, ϕ]. Hence, we need to choose f[z, ϕ] judiciously so that it is invertible.
要从该分布中采样,我们需要前向映射 x = f[z, ϕ],但要度量似然,我们需要计算逆映射 z= f −1[x, ϕ]。因此,我们需要谨慎地选择 f[z, ϕ],使其可逆。
The forward mapping is sometimes termed the generative direction. The base density is usually chosen to be a standard normal distribution. Hence, the inverse mapping is termed the normalizingdirection since this takes the complex distribution over x and turns it into a normal distribution over z (figure 16.3).
前向映射有时被称为生成方向。基础密度通常选择为标准正态分布。因此,逆映射被称为归一化方向,因为它将关于 x 的复杂分布转换为关于 z 的正态分布(图 16.3)。

To learn the distribution, we find parameters ϕ that maximize the likelihood of the training data{xi}I
为了学习该分布,我们寻找参数 ϕ,使其最大化训练数据 {xi}I
i=1 or equivalently minimize the negative log-likelihood:
where we have assumed that the data are independent and identically distributed in the first line and used the likelihood definition from equation 16.1 in the third line.
i=1 的似然,或等价地最小化负对数似然: w 这里我们假设数据独立同分布,在第一
行中,并在第三行中使用了式 16.1 的似然定义。
The previous section developed a simple 1D example that modeled a probability distribution Pr(x) by transforming a simpler base density Pr(z). We now extend this to multivariate distributions Pr( x ) and Pr( z ) and add the complication that the transformation is defined by a deep neural network. Consider applying a function x = f[ z , ϕ] to a random variable z ∈RD with base
f[ z , ϕ] is a deep network. The resulting variable x ∈RD has a density Pr( z ), where new distribution. A new sample x ∗ can be drawn from this distribution by (i) drawing a sample z ∗ from the base density and (ii) passing this through the neural network so that x ∗= f[ z ∗, ϕ].
By analogy with equation 16.1, the likelihood of a sample under this distribution is:
上一节给出了一个简单的一维示例,该示例通过变换一个更简单的基础密度 Pr (z) 对概率分布 Pr (x) 进行建模。我们现在将其扩展到多元分布 Pr (x) 和 Pr (z),并增加了一个复杂之处:该变换由深层神经网络定义。 ∈RD,其基础密度为 Pr (z),其中考虑将函数 x = f[ z ϕ] 应用于随机变量 z f[ z ϕ] 是一个深层网络。所得变量 x ∈RD 具有新的分布。可以通过以下方式从该分布中抽取新样本 x ∗: (i) 从基础密度中抽取一个样本 z ∗,以及 (ii) 将其通过神经网络,与式 16.1 类比,该分布下样本的似然为
:
where z = f −1[ x , ϕ] is the latent variable z that created x. The first term is the inverse of the determinant of the D× D Jacobian matrix ∂ f[ z , ϕ]/∂ z, which contains elements ∂ f j[ z , ϕ]/∂zi at position(i, j). Just as the absolute derivative measured the change of area at a point on a 1D function when the function was applied, the absolute determinant measures the change in volume at a point in the multivariate function. The
元素 ∂ f j[ z ϕ]/∂zi 位于位置 (i, j)。正如绝对导数度量了在一维函数被应用时某一点处面积的变化,绝对行列式度量了在多元函数中某一点处体积的变化。第二项是隐变量在基础密度下的概率。
second term is the probability of the latent variable under the base density.
![图 16.4 深层神经网络的正向与逆向映射。基础密度(左图)通过网络层 f 1[•, ϕ1], f 2[•, ϕ2], …… 逐步变换,从而形成模型密度。每一层](/learning/udl/ch16/figures/ch16_p05_f0.webp)
In practice, the forward mapping f[ z , ϕ] is usually defined by a neural network, consisting of a series of layers f k[•, ϕk] with parameters ϕk, which are composed together as: The inverse mapping (normalizing direction) is defined by the composition of the inverse of each layer f −1
实际上,前向映射 f[ z ϕ] 通常由神经网络定义,该神经网络由一系列层 f k[•, ϕk] 组成,这些层带有参数 ϕk,并组合在一起如下:逆映射(归一化方向)由每一层的逆 f −1
k[•, ϕk] 按相反顺序复合而成:
k[•, ϕk] applied in the opposite order: The base density Pr( z ) is usually defined as a multivariate standard normal (i.e., with mean zero and identity covariance). Hence, the effect of each subsequent inverse layer is to gradually move or “flow” the data density toward this normal distribution (figure 16.4). This gives rise to the name “normalizing flows.”
The Jacobian of the forward mapping can be expressed as:
基础密度 Pr (z) 通常定义为多元标准正态分布(即均值为零、协方差为单位矩阵)。因此,每个后续逆层的作用是将数据密度逐步移动或“流动”到该正态分布(图 16.4)。由此得名“归一化流”。
前向映射的雅可比矩阵可以表示为:
where we have overloaded the notation to make f k the output of the function f k[•, ϕk]. The absolute determinant of this Jacobian can be computed by taking the product of the individual absolute determinants: The absolute determinant of the Jacobian of the inverse mapping is found by applying
这里我们重载了符号,使 f k 成为函数 f 的输出 k[•, ϕk]。该雅可比矩阵的绝对行列式可以通过各个绝对行列式的乘积来计算:逆映射的雅可比矩阵的绝对行列式可通过应用
the same rule to equation 16.5. It is the reciprocal of the absolute determinant in the forward mapping.
将相同的规则应用于公式 16.5。它是正向映射中行列式绝对值的倒数。
We train normalizing flows with a dataset{ x i} of I training examples using the negative log-likelihood criterion:
where z i= f −1[ x i, ϕ], Pr( z i) is measured under the base distribution, and the absolute determinant |∂ f[ z i, ϕ]/∂ z i| is given by equation 16.7.
我们使用负对数似然准则,用数据集 { x i} 中的 I 个训练样本训练归一化流:
其中, z i= f −1[ x i, ϕ], Pr (z i) 在基础分布下度量,且绝对行列式 |∂ f[ z i, ϕ]/∂ z i| 由公式 16.7 给出。
The theory of normalizing flows is straightforward. However, for this to be practical, we need neural network layers f k that have four properties. 1. Collectively, the set of network layers must be sufficiently expressive to map a
multivariate standard normal distribution to an arbitrary density. 2. The network layers must be invertible; each must define a unique one-to-one map- ping from any input point to an output point (a bijection). If multiple inputs were
归一化流的理论简单明了。然而,为了使其具有实用性,我们需要具有四个性质的神经网络层 f k。 1. 总体上,网络层集合必须足够具有表达力,以将多元标准正态分布映射到任意密度。 2. 网络层必须可逆;每个网络层必须定义从任意输入点到输出点的唯一一对一映射(双射)。如果多个输入被映射到同一输出,逆映射就会存在歧义。 3. 必须能够计算每个网络层的逆,且要高效。每次评估似然时都需要这样做。这在训练期间会反复发生,因此逆映射必须有闭式解或快速算法。 4. 还必须能够评估雅可比矩阵的行列式,并且对正向或逆向
mapped to the same output, the inverse would be ambiguous.
映射都要高效。
3. It must be possible to compute the inverse of each layer efficiently. We need to do this every time we evaluate the likelihood. This happens repeatedly during
training, so there must be a closed-form solution or a fast algorithm for the inverse. 4. It also must be possible to evaluate the determinant of the Jacobian efficiently for
either the forward or inverse mapping.
We now describe different invertible network layers or flows for use in these models. We start with linear and elementwise flows. These are easy to invert, and it’s possible to compute the determinant of their Jacobians, but neither is sufficiently expressive to describe arbitrary transformations of the base density. However, they form the building blocks of coupling, autoregressive, and residual flows, which are all more expressive.
我们现在描述用于这些模型的不同可逆网络层或流。我们从线性流和逐元素流开始。这些流易于求逆,并且可以计算其雅可比行列式,但二者都不具有足够的表达力来描述基密度的任意变换。然而,它们构成了耦合流、自回归流和残差流的构建模块,这些流都更具表达力。
A linear flow has the form f[ h ]=β+ Ωh. If the matrix Ω is invertible, the linear transform is invertible. For Ω ∈RD×D, the computation of the inverse is O[D3]. The determinant of the Jacobian is just the determinant of Ω, which can also be computed
in O[D3]. This means that linear flows become expensive as the dimension D increases. If the matrix Ω takes a special form, then inversion and computation of the deter- minant can become more efficient, but the transformation becomes less general. For example, diagonal matrices require only O[D] computation for the inversion and determinant, but the elements of h don’t interact. Orthogonal matrices are also more efficient
如果矩阵 Ω 具有特殊形式,则求逆和行列式的计算 -行列式计算可以更加高效,但变换的通用性会降低。例如,对角矩阵的求逆和行列式计算只需要 O[D] 的计算量,但 h 的各元素之间不会相互作用。正交矩阵也更加高效
to invert, and their determinant is fixed, but they do not allow scaling of the individual dimensions. Triangular matrices are more practical; they are invertible using a process known as back-substitution, which is O[D2], and the determinant is just the product of the diagonal values.
One way to make a linear flow that is general, efficient to invert, and for which the Jacobian can be computed efficiently is to parameterize it directly in terms of the LU decomposition. In other words, we use:
求逆,且它们的行列式固定,但它们不允许对各个维度进行缩放。三角矩阵更实用;它们是可逆的,可通过一种称为回代的过程实现,其复杂度为 O[D2],且行列式只是对角线元素的乘积。
使线性流既通用、易于求逆,又能高效计算雅可比行列式的一种方法,是直接使用 LU 分解对其进行参数化。换言之,我们使用:
where P is a predetermined permutation matrix, L is a lower triangular matrix, U is an upper triangular matrix with zeros on the diagonal, and D is a diagonal matrix that supplies those missing diagonal elements. This can be inverted in O[D2], and the log determinant is just the sum of the log of the absolute values on the diagonals of L and D.
Unfortunately, linear flows are not sufficiently expressive. When a linear function f[ h ]=β+ Ωh is applied to normally distributed input Norm h[µ, Σ ], then the result is also normally distributed with mean and variance, β + Ω µ and ΩΣΩ T, respec-
其中 P 是一个预先设定的置换矩阵, L 是一个下三角矩阵, U 是一个对角线元素为零的上三角矩阵, D 是一个提供那些缺失对角线元素的对角矩阵。这可以在 O[D2] 内完成求逆,行列式的对数只是 L 和 D 对角线上绝对值的对数之和。遗憾的是,线性流的表达能力不足。当线性函数 f[ h ]=β+ Ωh 应用于正态分布输入 Norm h[µ, Σ ] 时,结果也是正态分布,其均值和方差为 β + Ω µ 和 Ω Σ Ω T,分别
tively. Hence, it is not possible to map a normal distribution to an arbitrary density using linear flows alone.
而言。因此,仅使用线性流无法将正态分布映射到任意密度。
Since linear flows are not sufficiently expressive, we must turn to nonlinear flows. The simplest of these are elementwise flows, which apply a pointwise nonlinear function f[•, ϕ] with parameters ϕ to each element of the input so that:
The Jacobian ∂ f[ h ]/∂ h is diagonal since the dth input to f[ h ] only affects the dth output. Its determinant is the product of the entries on the diagonal, so: The function f[•, ϕ] could be a fixed invertible nonlinearity like the leaky ReLU (figure 3.13), in which case there are no parameters, or it may be any parameterized
由于线性流的表达能力不足,我们必须转向非线性流。其中最简单的是逐元素流,它将逐点的非线性函数 f[•, ϕ] (参数为 ϕ)应用于输入的每个元素,从而:
雅可比矩阵 ∂ f[ h ]/∂ h 是对角矩阵,因为 dth 输入到 f[ h ] 仅影响 dth 输出。其行列式是对角线上各元素的乘积,因此:函数 f[•, ϕ] 可以是一个固定的可逆非线性函数,例如 leaky ReLU (图 3.13),在这种情况下没有参数,或者它可以是任何参数化的
invertible one-to-one mapping. A simple example is a piecewise linear function with K regions (figure 16.5) which maps[0, 1] to[0, 1] as:
可逆的一一映射。一个简单的例子是具有 K 个分段的分段线性函数(图 16.5),它将 [0, 1] 映射到 [0, 1],如下所示:
![图 16.5 分段线性映射。一个可逆的分段线性映射 h′= f[h, ϕ] 可以通过将输入域 h∈[0, 1] 划分为 K 个大小相同的区域(此处为 K= 5)](/learning/udl/ch16/figures/ch16_p08_f0.webp)
where the parameters ϕ1, ϕ2,..., ϕK are positive and sum to 1, and b = ⌊Kh⌋+ 1 is the index of the bin that contains h. The first term is the sum of all the preceding bins, and the second term represents the proportion of the way through the current bin that h lies. This function is easy to invert, and its gradient can be calculated almost everywhere. There are many similar schemes for creating smooth functions, often using splines with parameters that ensure the function is monotonic and hence invertible.
Elementwise flows are nonlinear but don’t mix input dimensions, so they can’t create correlations between variables. When alternated with linear flows (which do mix dimensions), more complex transformations can be modeled. However, in practice, elementwise flows are used as components of more complex layers like coupling flows.
其中,参数 ϕ1, ϕ2, …, ϕK 均为正且和为 1,且 b = ⌊Kh⌋+ 1 是包含 h 的分箱索引。第一项是所有前序分箱的和,第二项表示 h 在当前分箱内的位置比例。该函数易于求逆,且其梯度几乎处处可计算。
逐元素流是非线性的,但不会混合输入维度,因此无法在变量之间创建相关性。与线性流(确实会混合维度)交替使用时,可以建模更复杂的变换。然而在实践中,逐元素流被用作更复杂层的组件,例如耦合流。
Couplingflows divide the input h into two parts so that h =[ h T flow f[ h , ϕ] as:
2]T,并定义流 f[ h ϕ] 为:耦合流将输入 h 分成两部分,使得 h =[ h T

Here g[•, ϕ] is an elementwise flow (or other invertible layer) with parameters ϕ[ h 1] that are themselves a nonlinear function of the inputs h 1 (figure 16.6). The function ϕ[•] is usually a neural network of some kind and does not have to be invertible. The original variables can be recovered as:
这里 g[•, ϕ] 是一个逐元素流(或其他可逆层),其参数 ϕ[ h 1] 本身是输入 h 1 (图 16.6)的非线性函数。函数 ϕ[•] 通常是某种神经网络,且不必可逆。原始变量可以恢复为:

If the function g[•, ϕ] is an elementwise flow, the Jacobian will be lower triangular with the identity matrix in the top-left quadrant and the derivatives of the elementwise transformation in the bottom-right. Its determinant is the product of these diagonal values. The inverse and Jacobian can be computed efficiently, but this approach only transforms the second half of the parameters in a way that depends on the first half. To make a more general transformation, the elements of h are randomly shuffled using permuta-
tion matrices between layers, so every variable is ultimately transformed by every other.
如果函数 g[•, ϕ] 是逐元素流,则雅可比矩阵将是下三角矩阵,其左上象限为单位矩阵,右下象限为逐元素变换的导数。其行列式是这些对角值的乘积。逆映射和雅可比矩阵可以高效计算,但这种方法仅以依赖于前半部分的方式变换参数的后半部分。为了形成更一般的变换, h 的元素通过置换随机打乱
In practice, these permutation matrices are difficult to learn. Hence, they are initialized randomly and then frozen. For structured data like images, the channels are divided into two halves h 1 and h 2 and permuted between layers using 1 × 1 convolutions.
置换矩阵在层之间,因此每个变量最终都会被其他所有变量所变换。在实践中,这些置换矩阵难以学习。因此,它们被随机初始化,然后冻结。对于图像等结构化数据,通道被分成两半 h 1 和 h 2,并在层之间使用 1 × 1 卷积进行置换。

Autoregressive flows are a generalization of coupling flows that treat each input dimension as a separate “block” (figure 16.7). They compute the dth dimension of the output h ′ based on the first d−1 dimensions of the input h:
自回归流是耦合流的泛化,它将每个输入维度视为单独的“块”(图 16.7)。它们计算输出的第 dth 个维度 h ′,该维度基于输入 h 的前 d−1 个维度:
The function g[•, •] is termed the transformer, 1 and the parameters ϕ, ϕ[h1], ϕ[h1, h2],... are termed conditioners. As for coupling flows, the transformer g[•, ϕ] must be invertible, but the conditioners ϕ[•] can take any form and are usually neural networks. If the transformer and conditioner are sufficiently flexible, autoregressive flows are universal approximators in that they can represent any probability distribution. It’s possible to compute all of the entries of the output h ′ in parallel using a network with appropriate masks so that the parameters ϕ at position d only depend on previous
positions. This is known as a masked autoregressive flow. The principle is very similar to masked self-attention (section 12.7.2); connections that relate inputs to previous outputs are pruned.
函数 g[•, •] 被称为变换器, 1 而参数 ϕ、 ϕ[h1]、 ϕ[h1, h2],... 被称为调节器。与耦合流一样,变换器 g[•, ϕ] 必须可逆,但调节器 ϕ[•] 可以采用任何形式,通常是神经网络。如果变换器和调节器足够灵活,自回归流就是通用近似器,因为它们可以表示任意概率分布。
位置。这被称为掩码自回归流。其原理与掩码自注意力(第 12.7.2 节)非常相似;将输入与先前输出关联的连接会被剪枝。
Inverting the transformation is less efficient. Consider the forward mapping: This can’t be done in parallel as the computation for hd depends on h1:d−1 (i.e., the
对变换求逆的效率较低。考虑前向映射:

这必须采用与耦合流类似的原理按顺序求逆:这无法并行完成,因为 hd 的计算依赖于 h1:d−1 (即,
partial results so far). Hence, inversion is time-consuming when the input is large.
当前的部分结果)。因此,当输入较大时,求逆会很耗时。
Masked autoregressive flows are defined in the normalizing (inverse) direction. This is required to evaluate the likelihood efficiently and hence to learn the model. However, sampling requires the forward direction, in which each variable must be computed sequentially at each layer, which is slow. If we use an autoregressive flow for the forward (generative) transformation, then sampling is efficient, but computing the likelihood (and training) is slow. This is known as an inverse autoregressive flow.
A trick that allows fast learning and also fast (but approximate) sampling is to build a masked autoregressive flow to learn the distribution (the teacher) and then use this to train an inverse autoregressive flow from which we can sample efficiently (the student).
This requires a different formulation of normalizing flows that learns from another function rather than a set of samples (see section 16.5.3).
掩码自回归流是在归一化(反向)方向上定义的。这对于高效评估似然并因此学习模型是必需的。然而,采样需要前向方向,在该方向中,每个变量必须在每一层依次计算,因此速度较慢。如果将自回归流用于前向(生成)变换,则采样高效,但计算似然(以及训练)较慢。这被称为逆自回归流。
一种既能快速学习又能快速(但近似)采样的技巧是:构建一个掩码自回归流来学习分布(教师),然后使用它来训练一个逆自回归流,从中我们可以高效采样(学生)。这需要一种不同的归一化流表述,它从另一个函数中学习,而不是一组样本(见第 16.5.3 节)。
Residual flows take their inspiration from residual networks. They divide the input into two parts h = [ h T
2]T (与耦合流类似),残差流的设计灵感来自残差网络。它们将输入分成两部分: h = [ h T 并定义输出为:


where f 1[•, ϕ1] and f 2[•, ϕ2] are two functions that do not necessarily have to be invertible (figure 16.8). The inverse can be computed by reversing the order of computation:
其中 f 1[•, ϕ1] 和 f 2[•, ϕ2] 是两个不一定可逆的函数(图 16.8)。可以通过颠倒计算顺序来计算逆运算:
As for coupling flows, the division into blocks restricts the family of transformations that can be represented. Hence, the inputs are permuted between layers so that the variables can mix in arbitrary ways.
This formulation can be inverted easily, but for general functions f 1[•, ϕ1] and f 2[•, ϕ2], there is no efficient way to compute the Jacobian. This formulation is sometimes used to save memory when training residual networks; because the network is invertible, storing the activations at each layer in the forward pass is unnecessary.
与耦合流类似,按块划分限制了可表示的变换族。因此,各层之间会对输入进行置换,以便变量能够以任意方式混合。
A different approach to exploiting residual networks is to utilize the Banach fixed point theorem or contraction mapping theorem, which states that every contraction mapping has a fixed point. A contraction mapping f[•] has the property that: dist where dist[•, •] is a distance function and 0 < β< 1. When a function with this property is iterated (i.e., the output is repeatedly passed back in as an input), the result converges to a fixed point where f[z]=z (figure 16.9). To understand this, consider applying the
利用残差网络的另一种方法是使用巴拿赫不动点定理或压缩映射定理,该定理指出每个压缩映射都有一个不动点。压缩映射 f[•] 具有如下性质: dist到一个不动点,其中 f[z]=z (图 16.9)。要理解这一点,可以考虑将该函数同时作用

function to both the fixed point and the current position; the fixed point remains static, but the distance between the two must become smaller, so the current position must get closer to the fixed point.
This theorem can be exploited to invert an equation of the form:
于不动点和当前位置;不动点保持静止,但两者之间的距离必须变小,因此当前位置必须越来越靠近不动点。
该定理可用于反解如下形式的方程:该利普希茨常数必须小于一。假设激活函数的斜率函数不大于 1,这等价于确保最大的奇异值
if f[z] is a contraction mapping. In other words, it can be used to find the z∗ that maps to a given value, y∗. This can be done by starting with any point z0 and iterating zk+1= y∗− f[zk]. This has a fixed point at z + f[z] = y∗ (figure 16.9b). The same principle can be used to invert residual network layers of the form h ′= h + f[ h , ϕ] if we ensure that f[ h , ϕ] is a contraction mapping. In practice, this means that the Lipschitz constant must be less than one. Assuming that the slope of the activation functions is not greater than one, this is equivalent to ensuring the largest singular value
of each weight matrix Ω must be less than one. A crude way to do this is to ensure that the absolute magnitudes of the weights Ω are small by clipping them.
每个权重矩阵 Ω 必须小于 1。一种粗略的做法是通过裁剪来确保权重 Ω 的绝对值较小。
![图 16.9 压缩映射。如果函数在每一点的绝对斜率都小于 1,则迭代该函数会收敛到一个不动点 f[z] = z。 a) 从 z0 开始,计算 z1= f[z](/learning/udl/ch16/figures/ch16_p13_f0.webp)
The Jacobian determinant cannot be computed easily, but its logarithm can be approximated using a series of tricks.
雅可比行列式不易计算,但它的对数可以通过一系列技巧进行近似。
where we have used the identity log[| A |]= trace[log[ A ]] in the first line and expanded this into a power series in the second line. Even when we truncate this series, it’s still computationally expensive to compute the trace of the constituent terms. Hence, we approximate this using Hutchinson’s trace
其中我们在第一行使用了恒等式 log[| A |]= 迹[ log[ A ]],并在第二行将其展开为幂级数。即使我们截断该级数,计算它仍然代价高昂
estimator. Consider a normal random variable ϵ with mean 0 and variance I. The trace of a matrix A can be estimated as: trace[ A ]
构成项的迹。因此,我们使用 Hutchinson 迹估计量进行近似。考虑一个正态随机变量 ϵ,其均值为 0,方差为 I。矩阵 A 的迹可以估计为: trace[ A ]
where the first line is true because E[ϵϵT] = I. The second line derives from the properties of the expectation operator. The third line comes from the linearity of the trace operator. The fourth line is due to the invariance of the trace to cyclic permutation. The final line is true because the argument in the fourth line is now a scalar. We estimate the trace by drawing samples ϵi from Pr(ϵ): trace[ A ]
In this way, we can approximate the trace of the powers of the Taylor expansion (equation 16.22) and evaluate the log probability.
其中第一行成立是因为 E[ϵϵT] = I。第二行源于期望算子的性质。第三行来自迹算子的线性。第四行是由于迹对循环置换的不变性。最后一行成立是因为第四行中的参数现在是一个标量。我们通过从 Pr (ϵ) 中抽取样本 ϵi 来估计迹: trace[ A ]
这样,我们就可以近似泰勒展开式(公式 16.22)中各幂次项的迹,并计算对数概率。
In normalizing flows, the latent space z must be the same size as the data space x, but we know that natural datasets can often be described by fewer underlying variables. At
some point, we have to introduce all of these variables, but it is inefficient to pass them through the entire network. This leads to the idea of multi-scale flows (figure 16.10).
在归一化流中,潜在空间 z 必须与数据空间 x 具有相同的维度,但我们知道,自然数据集通常可以由更少的底层变量来描述。在

In the generative direction, multi-scale flows partition the latent vector into z = [ z 1, z 2,..., z N]. The first partition z 1 is processed by a series of reversible layers with the same dimension as z 1 until, at some point, z 2 is appended and combined with the first partition. This continues until the network is the same size as the data x. In the normalizing direction, the network starts at the full dimension of x, but when it reaches the point where z n was added, this is assessed against the base distribution.
在生成方向上,多尺度流将潜在向量划分为 z z 1 由一系列与 z 1 维度相同的可逆层处理,直到在某个点, z 2 被追加并与第一个划分部分合并。这一过程持续进行,直到网络与数据 x 具有相同维度。在归一化方向上,网络从 x 的完整维度开始,但当它到达 z n 被添加的位置时,会根据基础分布对其进行评估。
We now describe three applications of normalizing flows. First, we consider modeling probability densities. Second, we consider the GLOW model for synthesizing images. Finally, we discuss using normalizing flows to approximate other distributions.
我们现在介绍归一化流的三个应用。首先,我们讨论概率密度建模。其次,我们考虑用于图像合成的 GLOW 模型。最后,我们讨论使用归一化流来近似其他分布。
Of the four generative models discussed in this book, normalizing flows is the only model that can compute the exact log-likelihood of a new sample. Generative adversarial
networks are not probabilistic, and both variational autoencoders and diffusion models can only return a lower bound on the likelihood. 2 Figure 16.11 depicts the estimated probability distributions in two toy problems using i-ResNet. One application of density estimation is anomaly detection; the data distribution of a clean dataset is described using a normalizing flow model.
New examples with low probability are flagged as outliers. However, caution must be used as there may exist outliers with high probability that don’t fall in the typical set (see figure 8.13).
本书讨论的四种生成模型中,归一化流是唯一能够计算新样本精确对数似然的模型。生成对抗
网络不是概率模型,而变分自编码器和扩散模型都只能回报似然度的下界。 2 图 16.11 展示了使用 i-ResNet 在两个玩具问题中估计的概率分布。密度估计的一个应用是异常检测;干净数据集的数据分布由归一化流模型描述。低概率的新样本会被标记为异常值。但必须谨慎,因为可能存在高概率但不属于典型集的异常值(见图 8.13)。

Generative flows, or GLOW, is a normalizing flow model that can create high-fidelity images (figure 16.12) and uses many of the ideas from this chapter. It is easiest understood in the normalizing direction. GLOW starts with a 256 × 256 × 3 tensor containing an RGB image. It uses coupling layers, in which the channels are partitioned into two halves. The second half is subject to a different affine transform at each spatial position, where the parameters of the affine transformation are computed by a 2D convolutional neural network run on the other half of the channels. The coupling layers are alternated with 1 × 1 convolutions, parameterized as LU decompositions which mix the channels.
生成流,即 GLOW,是一种归一化流模型,能够生成高保真图像(图 16.12),并使用了本章中的许多思想。它最容易从归一化方向理解。 GLOW 以一个包含 RGB 图像的 256 × 256 × 3 张量开始。它使用耦合层,将通道划分为两半。后一半在每个空间位置都经过不同的仿射变换,其中仿射变换的参数由运行在通道另一半上的二维卷积神经网络计算得到。耦合层与 1 × 1 卷积交替使用,这些卷积被参数化为 LU 分解,从而混合通道。
Periodically, the resolution is halved by combining each 2 × 2 patch into one position with four times as many channels. GLOW is a multi-scale flow, and some of the channels are periodically removed to become part of the latent vector z. Images are discrete (due to the quantization of RGB values), so noise is added to the inputs to prevent the training likelihood increasing without bound. This is known as dequantization.
To sample more realistic images, the GLOW model samples from the base density raised to a positive power. This chooses examples that are closer to the center of the density rather than from the tails. This is similar to the truncation trick in GANs
周期性地,通过将每个 2 × 2 图像块合并到一个位置,并将通道数增加到原来的四倍,分辨率会减半。 GLOW 是一种多尺度流,并且会周期性地移除一些通道,使其成为潜变量向量 z 的一部分。图像是离散的(由于 RGB 值被量化),因此会向输入添加噪声,以防止训练似然无限增大。这称为反量化。为了采样出更真实的图像, GLOW 模型会从基础密度的正幂次分布中采样。这会选出更靠近密度中心而非尾部的样本。这类似于 GAN 中的截断技巧

(figure 15.10). Notably, the samples are not as good as those from GANs or diffusion models. It is unknown whether this is due to a fundamental restriction associated with invertible layers or merely because less research effort has been invested in this goal.
Figure 16.13 shows an example of interpolation using GLOW. Two latent vectors are computed by transforming two real images in the normalizing direction. Intermediate points between these latent vectors are computed by linear interpolation, and these are projected back to image space using the network in the generative direction. The result is a set of images that interpolate realistically between the two real ones.
(图 15.10)。值得注意的是,这些样本的质量不如 GAN 或扩散模型生成的样本。目前尚不清楚这是由于与可逆层相关的基本限制,还是仅仅因为对该目标投入的研究较少。
图 16.13 展示了使用 GLOW 进行插值的一个示例。通过将两张真实图像沿归一化方向进行变换,计算出两个隐向量。然后对这些隐向量之间的中间点进行线性插值计算,并使用沿生成方向的网络将其投影回图像空间。结果是一组在两张真实图像之间逼真插值的图像。

Normalizing flows can also learn to generate samples that approximate an existing density which is easy to evaluate but difficult to sample from. In this context, we denote the normalizing flow Pr( x |ϕ) as the student and the target density q( x ) as the teacher.
To make progress, we generate samples x i= f[ z i, ϕ] from the student. Since we generated these samples ourselves, we know their corresponding latent variables z i, and we can calculate their likelihood in the student model without inversion. Thus, we can use a model like a masked-autoregressive flow where inversion is slow. We define a loss function based on the reverse KL divergence that encourages the student and teacher
归一化流也可以学习生成近似现有密度的样本,该密度易于评估但难以采样。在此上下文中,我们将归一化流 Pr (x |ϕ) 称为学生,并将目标密度 q (x) 称为教师。为了取得进展,我们从学生模型生成样本 x i= f[ z i, ϕ]。由于这些样本是我们自己生成的,我们知道它们对应的隐变量 z i,并且可以在不进行求逆的情况下计算它们在学生模型中的似然。因此,我们可以使用像掩码自回归流这样的模型,其中求逆较慢。我们定义一个基于反向 KL 散度的损失函数,以促使学生和教师的似然相同,并使用它来
likelihood to be identical and use this to train the student model (figure 16.14):
This approach contrasts with the typical use of normalizing flows to build a probability model Pr( x i, ϕ) of data that came from an unknown distribution with samples x i using maximum likelihood, which relies on the cross-entropy term from the forward KL divergence (section 5.7):
训练学生模型(图 16.14):
这种方法与归一化流的典型用法形成对比:后者使用极大似然来构建来自未知分布的数据的概率模型 Pr (x i, ϕ),其中使用样本 x i,而极大似然依赖于前向 KL 散度(第 5.7 节)中的交叉熵项:
Normalizing flows can model the posterior in VAEs using this trick (see chapter 17).
归一化流可以使用这一技巧对 VAE 中的后验进行建模(见第 17 章)。

Normalizing flows transform a base distribution (usually a normal distribution) to create a new density.
They have the advantage that they can both evaluate the likelihood of samples exactly and generate new samples.
However, they have the architectural constraint that each layer must be invertible; we need the forward transformation to generate samples and the backward transformation to evaluate the likelihoods. It’s also important that the Jacobian can be estimated efficiently to evaluate the likelihood; this must be done repeatedly to learn the density. However, invertible layers
归一化流对基础分布(通常是正态分布)进行变换,以创建新的密度。其优势在于既能精确评估样本的似然,又能生成新样本。然而,它们存在架构约束:每一层都必须是可逆的;我们需要正向变换来生成样本,并需要反向变换来评估似然。雅可比矩阵能够被高效估计以便计算似然函数也很重要;在训练密度模型时,必须反复进行这一操作。然而,可逆层
are still useful in their own right even when the Jacobian cannot be estimated efficiently; they reduce the memory requirements of training a K -layer network from O[K] to O[1].
This chapter reviewed invertible network layers or flows. We considered linear flows and elementwise flows, which are simple but insufficiently expressive. Then we described more complex flows, such as coupling, autoregressive, and residual flows. Finally, we showed how normalizing flows can be used to estimate likelihoods, generate and interpolate between images, and approximate other distributions.
本章回顾了可逆网络层或流。我们讨论了线性流和逐元素流,它们很简单,但表达能力不足。随后,我们描述了更复杂的流,例如耦合流、自回归流和残差流。最后,我们展示了如何使用归一化流来估计似然、生成图像并在图像之间进行插值,以及近似其他分布。

Invertible network layers:
Radial and planar flows:
Applications:
Linear flows:
Elementwise flows and coupling functions:
Coupling flows:
Autoregressive flows:
Residual flows:
Infinitesimal flows:
Universality:
Other work: