学习资料 理解深度学习 第 17 章 · 变分自编码器
学 习 资 料 · 中 英 对 照

第 17 章 · 变分自编码器

Understanding Deep Learning — Variational autoencoders

变分自编码器
Variational autoencoders

Generative adversarial networks learn a mechanism for creating samples that cannot be distinguished from the training examples{ x i}. In contrast, like normalizing flows, variational autoencoders, or VAEs, are probabilistic generative models; they aim to learn a distribution Pr( x ) over the data (see figure 14.2). After training, it is possible to draw (generate) samples from this distribution. However, the properties of the VAE mean that it is unfortunately not possible to evaluate the probability of new examples x ∗ exactly.

It is common to talk about the VAE as if it is the model of Pr( x ), but this is misleading; the VAE is a neural architecture that is designed to help learn the model for Pr( x ). The final model for Pr( x ) contains neither the “variational” nor the “autoencoder” parts and might be better described as a nonlinear latent variable model.

This chapter starts by introducing latent variable models in general and then considers the specific case of the nonlinear latent variable model. It will become clear that maximum likelihood learning of this model is not straightforward. Nevertheless, it is possible to define a lower bound on the likelihood, and the VAE architecture approximates this bound using a Monte Carlo (sampling) method. The chapter concludes by presenting several applications of the VAE.

生成对抗网络学习一种生成样本的机制,其生成的样本无法与训练样本{ x i} 区分。相比之下,像归一化流、变分自编码器或 VAE 这样的模型是概率生成模型;它们旨在学习数据上的分布 Pr (x) (见图 14.2)。训练后,可以从该分布中抽取(生成)样本。然而, VAE 的性质意味着,遗憾的是,无法精确计算新样本 x ∗ 的概率。

人们常常把 VAE 说成好像它是 Pr (x) 的模型,但这是误导性的; VAE 是一种神经网络架构,旨在帮助学习 Pr (x) 的模型。 Pr (x) 的最终模型既不包含“变分”部分,也不包含“自编码器”部分,或许更宜描述为非线性隐变量模型。

本章首先介绍一般的隐变量模型,然后考虑非线性隐变量模型这一具体情形。将会清楚地看到,该模型的极大似然学习并不简单。不过,可以定义似然的下界,而 VAE 架构使用蒙特卡洛(采样)方法来近似该下界。本章最后介绍 VAE 的几个应用。

17.1 隐变量模型
17.1 Latent variable models

Latent variable models take an indirect approach to describing a probability distribution Pr( x ) over a multi-dimensional variable x. Instead of directly writing the expression for Pr( x ), they model a joint distribution Pr( x , z ) of the data x and an unobserved hid- den or latent variable z. They then describe the probability of Pr( x ) as a marginalization

隐变量模型采用一种间接方法来描述多维变量 x 上的概率分布 Pr (x)。它们不直接写出 Pr (x) 的表达式,而是对联合分布 Pr (x , z) 建模,该联合分布描述数据 x 与一个未观测到的隐 -

of this joint probability so that: Typically, the joint probability Pr( x , z ) is broken down using the rules of conditional probability into the likelihood of the data with respect to the latent variables term Pr( x | z )

通常,联合概率 Pr( x , z ) 依据条件概率的规则进行分解将概率转化为关于隐变量的数据似然项 Pr( x | z ) 和先验 Pr( z ):

and the priorPr( z ):

This is a rather indirect approach to describing Pr( x ), but it is useful because relatively simple expressions for Pr( x | z ) and Pr( z ) can define complex distributions Pr( x ).

这是一种描述 Pr (x) 的相当间接的方法,但它很有用,因为 Pr (x | z) 和 Pr (z) 的相对简单的表达式可以定义复杂的分布 Pr (x)。

17.1.1 示例:高斯混合模型
17.1.1 Example: mixture of Gaussians

In a 1D mixture of Gaussians (figure 17.1a), the latent variable z is discrete, and the prior Pr(z) is a categorical distribution (figure 5.9) with one probability λn for each possible value of z. The likelihood Pr(x|z=n) of the data x given that the latent variable z takes value n is normally distributed with mean µn and variance σ2

在一维高斯混合模型(图 17.1a)中,隐变量 z 是离散的,先验 Pr (z) 是类别分布(图 5.9),有一个概率 λn 对应每个z 的可能值。 Pr (x|z=n) 是数据 x 在隐变量 z 取值 n 时的似然,服从均值为 µn、方差为 σ2 的正态分布:

As in equation 17.2, the probability Pr(x) is given by the marginalization over the latent variable z (figure 17.1b). Here, the latent variable is discrete, so we sum over its possible values to marginalize:

From simple expressions for the likelihood and prior, we describe a complex multi-modal probability distribution.

如式 17.2 所示,概率 Pr (x) 由对隐变量 z 的边缘化给出(图 17.1b)。此处隐变量是离散的,因此我们对其可能值求和以进行边缘化:

通过似然和先验的简单表达式,我们描述了一个复杂的多模态概率分布。

17.2 非线性隐变量模型
17.2 Nonlinear latent variable model

In the nonlinear latent variable model, both the data x and the latent variable z are continuous and multivariate. The prior Pr( z ) is a standard multivariate normal:

在非线性隐变量模型中,数据 x 和隐变量 z 都是连续且多元的。先验 Pr( z ) 是标准多元正态分布:

The likelihood Pr( x | z , ϕ) is also normally distributed; its mean is a nonlinear function f[ z , ϕ] of the latent variable, and its covariance σ2 I is spherical:

似然 Pr (x | z ϕ) 也服从正态分布;其均值是隐变量的非线性函数 f[ z ϕ],其协方差 σ2 I 是球形的:

图 17.1 高斯混合模型(MoG)。 a) MoG 将复杂概率分布(青色曲线)描述为高斯分量(虚线曲线)的加权和。 b) 该和是连续观测数据 x 与离散隐变量
图 17.1 高斯混合模型(MoG)。 a) MoG 将复杂概率分布(青色曲线)描述为高斯分量(虚线曲线)的加权和。 b) 该和是连续观测数据 x 与离散隐变量 z 之间的联合密度 Pr (x, z) 的边缘化。Figure 17.1 Mixture of Gaussians (MoG). a) The MoG describes a complex probability distribution (cyan curve) as a weighted sum of Gaussian components (dashed curves). b) This sum is the marginalization of the joint density Pr(x, z) between the continuous observed data x and a discrete latent variable z.

The function f[ z , ϕ] is described by a deep network with parameters ϕ. The latent variable z is lower dimensional than the data x. The model f[ z , ϕ] describes the important aspects of the data, and the remaining unmodeled aspects are ascribed to the noise σ2 I.

The data probability Pr( x |ϕ) is found by marginalizing over the latent variable z:

函数 f[ z ϕ] 由带有参数 ϕ 的深度网络描述。隐变量 z 的维度低于数据 x。模型 f[ z ϕ] 描述了数据的重要方面,而其余未建模的方面则归因于噪声 σ2 I。

数据概率 Pr (x |ϕ) 可通过对隐变量 z 进行边缘化求得:

This can be viewed as an infinite weighted sum (i.e., an infinite mixture) of spherical Gaussians with different means, where the weights are Pr( z ) and the means are the network outputs f[ z , ϕ] (figure 17.2).

这可以看作具有不同均值的球形高斯的无限加权和(即无限混合),其中权重为 Pr (z),均值为网络输出 f[ z ϕ] (图 17.2)。

17.2.1 生成
17.2.1 Generation

A new example x ∗ can be generated using ancestral sampling (figure 17.3). We draw z ∗ from the prior Pr( z ) and pass this through the network f[ z ∗, ϕ] to compute the mean of the likelihood Pr( x | z ∗, ϕ) (equation 17.6), from which we draw x ∗. Both the prior and likelihood are normal distributions, so this is straightforward.

一个新样本 x ∗ 可以使用祖先采样(图 17.3)。我们采样 z ∗ 从先验 Pr (z) 中采样,并将其通过网络 f[ z ∗, ϕ] 以计算似然 Pr (x | z ∗, ϕ) (公式 17.6)的均值,然后从中采样 x ∗。先验和似然均为正态分布,因此这很简单。

图 17.2 非线性隐变量模型。一个复杂的二维密度  Pr (x) (右图)是通过将联合分布 Pr (x z) (左图)对隐变量 z 进行边缘化而得到的;为了得
图 17.2 非线性隐变量模型。一个复杂的二维密度 Pr (x) (右图)是通过将联合分布 Pr (x z) (左图)对隐变量 z 进行边缘化而得到的;为了得到 Pr (x),我们对维度 z 上的三维体积进行积分。对于每个 z, x 上的分布是一个球面高斯分布(显示了两个切片),其均值 f[z, ϕ] 是 z 的非线性函数,并取决于参数 ϕ。分布 Pr (x) 是这些高斯分布的加权和。Figure 17.2 Nonlinear latent variable model. A complex 2D density Pr( x ) (right) is created as the marginalization of the joint distribution Pr( x , z) (left) over the latent variable z; to create Pr( x ), we integrate the 3D volume over the dimension z. For each z, the distribution over x is a spherical Gaussian (two slices shown) with a mean f[z, ϕ] that is a nonlinear function of z and depends on parameters ϕ. The distribution Pr( x ) is a weighted sum of these Gaussians.
图 17.3 非线性隐变量模型的生成。 a) 我们从隐变量的先验概率 Pr (z) 中抽取一个样本 z∗。 b) 然后从 Pr (x |z∗, ϕ) 中抽取一个
图 17.3 非线性隐变量模型的生成。 a) 我们从隐变量的先验概率 Pr (z) 中抽取一个样本 z∗。 b) 然后从 Pr (x |z∗, ϕ) 中抽取一个样本 x ∗。这是一个球面高斯分布,其均值是 z∗ 的非线性函数 f[•, ϕ],方差为固定值 σ2 I。 c) 如果我们重复此过程多次,就能恢复密度 Pr (x |ϕ)。Figure 17.3 Generation from nonlinear latent variable model. a) We draw a sample z∗ from the prior probability Pr(z) over the latent variable. b) A sample x ∗ is then drawn from Pr( x |z∗, ϕ). This is a spherical Gaussian with a mean that is a nonlinear function f[•, ϕ] of z∗ and a fixed variance σ2 I. c) If we repeat this process many times, we recover the density Pr( x |ϕ).
公式
17.3 训练
17.3 Training

To train the model, we maximize the log-likelihood over a training dataset{ x i}I

i=1 with respect to the model parameters. For simplicity, we assume that the variance term σ2 in the likelihood expression is known and concentrate on learning ϕ:

为了训练模型,我们在训练数据集{ x i}I

i=1 上关于模型参数最大化对数似然。为简单起见,我们假设似然表达式中的方差项 σ2 已知,并专注于学习 ϕ:

where:

Unfortunately, this is intractable. There is no closed-form expression for the integral and no easy way to evaluate it for a particular value of x.

其中:

不幸的是,这是难以计算的。该积分没有闭式表达式,也没有简单的方法针对 x 的特定值对其进行求值。

17.3.1 证据下界(ELBO)
17.3.1 Evidence lower bound (ELBO)

To make progress, we define a lower bound on the log-likelihood. This is a function that is always less than or equal to the log-likelihood for a given value of ϕ and will also depend on some other parameters θ. Eventually, we will build a network to compute this lower bound and optimize it. To define this lower bound, we need Jensen’s inequality.

为了推进,我们定义一个对数似然的下界。对于 ϕ 的给定值,该函数始终小于或等于对数似然,并且还会依赖于其他一些参数 θ。最终,我们将构建一个网络来计算这个下界并优化它。为了定义这个下界,我们需要詹森不等式。

17.3.2 詹森不等式
17.3.2 Jensen’s inequality

Jensen’s inequality says that a concave function g[•] of the expectation of data y is greater than or equal to the expectation of the function of the data:

詹森不等式指出,一个凹函数 g[•] 对数据期望的 y 大于或等于该函数对数据的期望:

图 17.4 詹森不等式(离散情形)。对数函数(黑色曲线)是凹函数;你可以在曲线上的任意两点之间画一条直线,这条直线始终位于曲线下方。由此可知,对数函数上六个点
图 17.4 詹森不等式(离散情形)。对数函数(黑色曲线)是凹函数;你可以在曲线上的任意两点之间画一条直线,这条直线始终位于曲线下方。由此可知,对数函数上六个点的任意凸组合(权重为正且总和为一的加权求和)必定位于曲线下方的灰色区域中。这里,我们对这些点赋予相等的权重(即取平均值),得到青色点。由于该点位于曲线下方,log[E[y]] > E[log[y]]。Figure 17.4 Jensen’s inequality (discrete case). The logarithm (black curve) is a concave function; you can draw a straight line between any two points on the curve, and this line will always lie un- derneath it. It follows that any convex combination (weighted sum with posi- tive weights that sum to one) of the six points on the log function must lie in the gray region under the curve. Here, we have weighted the points equally (i.e., taken the mean) to yield the cyan point.
图 17.5 延森不等式(连续情形)。对于凹函数,先计算分布 Pr(y) 的期望,再将该期望代入函数,得到的结果大于或等于先通过函数变换变量 y,再计算新变量的
图 17.5 延森不等式(连续情形)。对于凹函数,先计算分布 Pr(y) 的期望,再将该期望代入函数,得到的结果大于或等于先通过函数变换变量 y,再计算新变量的期望。在对数情形下,有 log[E[y]] ≥ E[log[y]]。图中左侧对应于该不等式的左侧,图中右侧对应于该不等式的右侧。一种理解方式是:对定义在 y ∈ [0, 1] 上的橙色分布中的点取凸组合。根据图 17.4 的逻辑,它必定位于曲线下方。另一种理解是:凹函数会相对于低值压缩 y 的高值,因此当我们先将 y 代入函数时,期望值更低。Figure 17.5 Jensen’s inequality (continuous case). For a concave function, com- puting the expectation of a distribution Pr(y) and passing it through the function gives a result greater than or equal to transforming the variable y by the function and then computing the expectation of the new variable. In the case of the loga- rithm, we have log[E[y]] ≥E[log[y]]. The left-hand side of the figure corresponds to the left-hand side of this inequality and the right-hand side of the figure to the right-hand side. One way of thinking about this is to consider that we are taking a convex combination of the points in the orange distribution defined over y ∈[0, 1]. By the logic of figure 17.4, this must lie under the curve. Alternatively, we can think about the concave function as compressing the high values of y relative to the low values, so the expected value is lower when we pass y through the function first.

:

图 17.6 证据下界(ELBO)。目标是关于参数 ϕ 最大化对数似然 log[Pr (x |ϕ) ] (黑色曲线)。证据下界(ELBO)是一个处处低于对数似然
图 17.6 证据下界(ELBO)。目标是关于参数 ϕ 最大化对数似然 log[Pr (x |ϕ) ] (黑色曲线)。证据下界(ELBO)是一个处处低于对数似然的函数。它同时是 ϕ 和第二组参数 θ 的函数。固定 θ 时,得到 ϕ 的函数(不同 θ 值对应两条彩色曲线)。因此,我们可以通过 a) 针对新参数 θ 改进证据下界(ELBO)(从一条彩色曲线移动到另一条彩色曲线)或 b) 针对原始参数 ϕ 改进证据下界(ELBO)(沿当前彩色曲线移动)来增加对数似然。Figure 17.6 Evidence lower bound (ELBO). The goal is to maximize the loglikelihood log[Pr( x |ϕ)] (black curve) with respect to the parameters ϕ. ELBO is a function that lies everywhere below the log-likelihood. It is a function of both ϕ and a second set of parameters θ. of ϕ (two colored curves for different values of θ). Consequently, we can increase the log-likelihood by either improving the ELBO with respect to a) the new parameters θ (moving from colored curve to colored curve) or b) the original parameters ϕ (moving along the current colored curve).
17.3.3 推导该界
17.3.3 Deriving the bound

We now use Jensen’s inequality to derive the lower bound for the log-likelihood. We start by multiplying and dividing the log-likelihood by an arbitrary probability distribution q( z ) over the latent variables:

We then use Jensen’s inequality for the logarithm (equation 17.12) to find a lower bound:

我们现在使用詹森不等式来推导对数似然的下界。我们首先将对数似然乘以并除以隐变量上的任意概率分布 q (z):

然后,我们利用对数的琴生不等式(公式 17.12)得到一个下界:

where the right-hand side is termed the evidence lower bound or ELBO. It gets this name because Pr( x |ϕ) is called the evidence in the context of Bayes’ rule (equation 17.19).

In practice, the distribution q( z ) has parameters θ, so the ELBO can be written as:

To learn the nonlinear latent variable model, we maximize this quantity as a function of both ϕ and θ. The neural architecture that computes this quantity is the VAE.

在实践中,分布 q (z) 的参数为 θ,因此证据下界(ELBO)可写为:

为了学习非线性隐变量模型,我们将该量作为 ϕ 和 θ 的函数进行最大化。计算该量的神经网络架构是 VAE。

17.4 证据下界(ELBO)的性质
17.4 ELBO properties

When first encountered, the ELBO is a somewhat mysterious object, so we now provide some intuition about its properties. Consider that the original log-likelihood of the data is a function of the parameters ϕ and that we want to find its maximum. For any fixed θ, the ELBO is still a function of the parameters but one that must lie below the original likelihood function. When we change θ, we modify this function, and depending on our choice, the lower bound may move closer or further from the log-likelihood. When we change ϕ, we move along the lower bound function (figure 17.6).

初次接触时,证据下界(ELBO)是一个略显神秘的对象,因此我们现在提供有关其性质的一些直观理解。考虑数据的原始对数似然是参数 ϕ 的函数,并且我们想要找到其最大值。对于任意固定的 θ,证据下界(ELBO)仍然是参数的函数,但必须位于原始似然函数下方。当我们改变 θ 时,我们会修改这个函数;根据选择的不同,下界可能会靠近或远离对数似然。当我们改变 ϕ 时,我们沿着下界函数移动(图 17.6)。

17.4.1 下界的紧致性
17.4.1 Tightness of bound

The ELBO is tight when, for a fixed value of ϕ, the ELBO and the log likelihood function coincide. To find the distribution q( z |θ) that makes the bound tight, we factor the

numerator of the log term in the ELBO using the definition of conditional probability: ELBO[θ, ϕ] Here, the first integral disappears between lines three and four since log[Pr( x |ϕ)] does not depend on z, and the integral of the probability distribution q( z |θ) is one. In the

证据下界(ELBO)中对数项的分子,使用条件概率的定义: ELBO[θ, ϕ]

last line, we have just used the definition of the Kullback-Leibler (KL) divergence.

This equation shows that the ELBO is the original log-likelihood minus the KL divergence D KL [q( z |θ)||Pr( z | x , ϕ)]. The KL divergence measures the “distance” between distributions and can only take non-negative values. It follows the ELBO is a lower bound on log[Pr( x |ϕ)].

The KL distance will be zero, and the bound will be tight when q( z |θ)=Pr( z | x , ϕ). This is the posterior distribution over the latent variables z given observed data x; it indicates which values of the latent variable could have been responsible for the data point (figure 17.7).

最后一行,我们刚刚使用了 Kullback-Leibler (KL) 散度的定义。

该方程表明,证据下界(ELBO)是原始对数似然减去 KL 散度 D KL [q( z |θ)||Pr( z | x ϕ)]。 KL 散度衡量分布之间的“距离”,且只能取非负值。因此,证据下界(ELBO)是 log[Pr( x |ϕ)] 的下界。当 q( z |θ)=Pr( z | x ϕ) 时, KL 距离为零,且该界是紧的。这是隐变量 z 在给定观测数据 x 下的后验分布;它表明哪些隐变量取值可能造成了该数据点(图 17.7)。

图 17.7 隐变量的后验分布。 a) 后验分布 Pr (z| x ∗, ϕ) 是对隐变量 z 的各个取值进行分布,这些取值可能导致了数据点 x ∗。我们通过贝
图 17.7 隐变量的后验分布。 a) 后验分布 Pr (z| x ∗, ϕ) 是对隐变量 z 的各个取值进行分布,这些取值可能导致了数据点 x ∗。我们通过贝叶斯法则计算它: Pr (z| x ∗, ϕ) ∝Pr (x ∗|z, ϕ) Pr (z)。 b) 我们计算右侧第一项(似然),方法是评估 x ∗ 相对于与 z 的每个取值相关联的对称高斯分布的概率。在此例中,它更可能是由 z1 生成的,而不是由 z2 生成的。第二项是隐变量的先验概率 Pr (z)。将这两个因子相结合,并归一化使分布之和为一,即可得到后验 Pr (z| x ∗, ϕ)。Figure 17.7 Posterior distribution over latent variable. a) The posterior distriPr(z| x ∗, ϕ) is the distribution over the values of the latent variable z bution that could be responsible for a data point x ∗. rule Pr(z| x ∗, ϕ) ∝Pr( x ∗|z, ϕ)Pr(z). b) We compute the first term on the righthand side (the likelihood) by assessing the probability of x ∗ against the symmetric Gaussian associated with each value of z. Here, it was more likely to have been created from z1 than z2. The second term is the prior probability Pr(z) over the latent variable. Combining these two factors and normalizing so the distribution sums to one gives us the posterior Pr(z| x ∗, ϕ).
17.4.2 证据下界(ELBO)作为重构损失减去到先验的 KL 距离
17.4.2 ELBO as reconstruction loss minus KL distance to prior

Equations 17.16 and 17.17 are two different ways to express the ELBO. A third way is to consider the bound as reconstruction error minus the distance to the prior: ELBO[θ, ϕ]

式 17.16 和式 17.17 是表达证据下界(ELBO)的两种不同方式。第三种方式是将下界视为重构误差减去与先验的距离: ELBO[θ, ϕ]

where the joint distribution Pr( x , z |ϕ) has been factored into conditional probability Pr( x | z , ϕ)Pr( z ) between the first and second lines, and the definition of KL divergence is used again in the last line.

In this formulation, the first term measures the average agreement Pr( x | z , ϕ) of the latent variable and the data. This measures the reconstruction accuracy. The second term measures the degree to which the auxiliary distribution q( z |θ) matches the prior. This formulation is the one that is used in the variational autoencoder.

其中,联合分布 Pr (x , z |ϕ) 已被分解为条件概率量 Pr( x | z ϕ)Pr( z ) 在第一行和第二行之间,并且最后一行再次使用了 KL 散度的定义。

在这种表述中,第一项衡量隐变量与数据的平均一致性 Pr (x | z ϕ)。这衡量了重构精度。第二项衡量辅助分布 q (z |θ) 与先验分布的匹配程度。这种表述就是变分自编码器中所使用的表述。

17.5 变分近似
17.5 Variational approximation

We saw in equation 17.17 that the ELBO is tight when q( z |θ) is the posterior Pr( z | x , ϕ). In principle, we can compute the posterior using Bayes’ rule:

but in practice, this is intractable because we can’t evaluate the evidence term Pr( x |ϕ) in the denominator (see section 17.3).

我们在式 17.17 中看到,当 q (z |θ) 是后验 Pr (z | x ϕ) 时,证据下界(ELBO)是紧的。原则上,我们可以使用贝叶斯法则计算后验:

但在实践中,这是不可计算的,因为我们无法评估分母中的证据项 Pr (x |ϕ) (见 17.3 节)。多元正态分布的均值为 µ,对角协方差为 Σ。这并不总能很好地匹配后验,但对于某

One solution is to make a variational approximation: we choose a simple parametric form for q( z |θ) and use this to approximate the true posterior. Here, we choose a multivariate normal distribution with mean µ and diagonal covariance Σ. This will not always match the posterior well but will be better for some values of µ and Σ than

others. During training, we will find the normal distribution that is “closest” to the true posterior Pr( z | x ) (figure 17.8). This corresponds to minimizing the KL divergence in equation 17.17 and moving the colored curves in figure 17.6 upwards.

些 µ 和 Σ 的取值而言,会比其他取值更好。在训练期间,我们将找到一个正态分布,它“最接近”真实后验 Pr( z | x ) (图 17.8)。这对应于最小化式 17.17 中的 KL 散度,并将图 17.6 中的彩色曲线上移。

Since the optimal choice for q( z |θ) was the posterior Pr( z | x ), and this depends on the data example x, the variational approximation should do the same, so we choose:

where g[ x , θ] is a second neural network with parameters θ that predicts the mean µ and variance Σ of the normal variational approximation.

由于 q (z |θ) 的最优选择是后验 Pr (z | x),并且这取决于数据样本 x,因此变分近似也应如此,所以我们选择:

其中 g[ x θ] 是带有参数 θ 的第二个神经网络,用于预测正态分布变分近似的均值 µ 和方差 Σ。

17.6 变分自编码器
17.6 The variational autoencoder

Finally, we can describe the VAE. We build a network that computes the ELBO:

where the distribution q( z | x , θ) is the approximation from equation 17.20. The first term still involves an intractable integral, but since it is an expectation with

最后,我们可以描述变分自编码器。我们构建一个计算证据下界(ELBO)的网络:

其中分布 q (z | x θ) 是式 17.20 中的近似。

respect to q( z | x , θ), we can approximate it by sampling. For any function a[•] we have:

第一项仍然涉及一个难以计算的积分,但由于它是一个期望关于相对于 q (z | x θ),我们可以通过采样来近似它。对于任意函数 a[•],我们有:

n is the nth sample from q( z | x , θ). This is known as a MonteCarloestimate. where z ∗ For a very approximate estimate, we can just use a single sample z ∗ from q( z | x , θ): ELBO[θ, ϕ]

n 是来自 q (z | x θ) 的 nth 样本。这被称为蒙特卡洛估计。对于非常粗略的估计,其中 z ∗ 我们可以只使用单个样本 z ∗,它来自 q (z | x θ): ELBO[θ, ϕ]

图 17.8 变分近似。后验 Pr (z | x ∗, ϕ) 无法以闭式计算。变分近似选择一个分布族  q (z | x θ) (此处为高斯分布),并尝试找到该
图 17.8 变分近似。后验 Pr (z | x ∗, ϕ) 无法以闭式计算。变分近似选择一个分布族 q (z | x θ) (此处为高斯分布),并尝试找到该族中与真实后验最接近的成员。 a) 有时,近似(青色曲线)很好,并且接近真实后验(橙色曲线)。 b) 然而,如果后验是多模态的(如图 17.7 所示),那么高斯近似会很差。Figure 17.8 Variational approximation. The posterior Pr( z | x ∗, ϕ) can’t be computed in closed form. The variational approximation chooses a family of distributions q( z | x , θ) (here Gaussians) and tries to find the closest member of this family to the true posterior. a) Sometimes, the approximation (cyan curve) is good and lies close to the true posterior (orange curve). b) However, if the posterior is multi-modal (as in figure 17.7), then the Gaussian approximation will be poor.

The second term is the KL divergence between the variational distribution q( z | x , θ)= Norm z[µ, Σ ] and the prior Pr( z ) = Norm z[ 0 , I ]. The KL divergence between two normal distributions can be calculated in closed form. For the special case where one distribution has parameters µ, Σ and the other is a standard normal, it is given by:

where D z is the dimensionality of the latent space.

第二项是变分分布 q (z | x θ) 与 = 之间的 KL 散度。 Norm z[µ, Σ ] 与先验 Pr( z )= Norm z[ 0 , I ]。两个正态分布之间的 KL 散度可以闭式计算。对于一种特殊情况,其中一个分布的参数为 µ, Σ 而另一个是标准正态分布时,其值为:

其中 D z 是潜在空间的维度。

17.6.1 VAE 算法
17.6.1 VAE algorithm

To summarize, we aim to build a model that computes the evidence lower bound for a point x. Then we use an optimization algorithm to maximize this lower bound over the

总之,我们旨在构建一个模型,用于计算点 x 的证据下界。然后我们使用优化算法最大化该下界于

图 17.9 变分自编码器。编码器 g[ x θ] 接收训练样本 x,并预测变分分布 q (z | x θ) 的参数 µ, Σ。我们从该分布中采样,然后使用解码
图 17.9 变分自编码器。编码器 g[ x θ] 接收训练样本 x,并预测变分分布 q (z | x θ) 的参数 µ, Σ。我们从该分布中采样,然后使用解码器 f[ z ϕ] 预测数据 x。损失函数是负的证据下界(ELBO),取决于该预测的准确程度以及变分分布 q (z | x θ) 与先验 Pr (z) 的相似程度(式 17.21)。Figure 17.9 Variational autoencoder. The encoder g[ x , θ] takes a training example x and predicts the parameters µ, Σ of the variational distribution q( z | x , θ). We sample from this distribution and then use the decoder f[ z , ϕ] to predict the data x. The loss function is the negative ELBO, which depends on how accurate this prediction is and how similar the variational distribution q( z | x , θ) is to the prior Pr( z ) (equation 17.21).

dataset and hence improve the log-likelihood. To compute the ELBO we: • compute the mean µ and variance Σ of the variational posterior distribution q( z |θ, x ) for this data point x using the network g[ x , θ], • draw a sample z ∗ from this distribution, and

• compute the ELBO using equation 17.23.

The associated architecture is shown in figure 17.9. It should now be clear why this is called a variational autoencoder. It is variational because it computes a Gaussian approximation to the posterior distribution. It is an autoencoder because it starts with a data point x, computes a lower-dimensional latent vector z from this, and then uses this vector to recreate the data point x as closely as possible. In this context, the mapping from the data to the latent variable by the network g[ x , θ] is called the encoder, and the mapping from the latent variable to the data by the network f[ z , ϕ] is called the decoder.

数据集上,从而提高对数似然。要计算证据下界(ELBO),我们: • 使用网络 g[ x θ] 计算此数据点 x 的变分后验分布 q (z |θ, x) 的均值 µ 和方差 Σ, • 从该分布中抽取一个样本 z ∗,并且

• 使用式 17.23 计算证据下界(ELBO)。

相关架构如图 17.9 所示。现在应该清楚了,为什么这被称为变分自编码器。它是变分的,因为它计算后验分布的高斯近似。它是一个自编码器,因为它从一个数据点 x 开始,由此计算一个低维隐向量 z,然后使用该向量尽可能准确地重建数据点 x。在此语境中,通过网络 g[ x θ] 从数据到隐变量的映射称为编码器,通过网络 f[ z ϕ] 从隐变量到数据的映射称为解码器。

The VAE computes the ELBO as a function of both ϕ and θ. To maximize this bound, we run mini-batches of samples through the network and update these parameters with an optimization algorithm such as SGD or Adam. The gradients of the ELBO with respect to the parameters are computed as usual using automatic differentiation. During this process, we are both moving between the colored curves (changing θ) and along them (changing ϕ) in figure 17.10. During this process, the parameters ϕ change to assign the data a higher likelihood in the nonlinear latent variable model.

VAE 将证据下界(ELBO)计算为关于 ϕ 和 θ 二者的函数。为了最大化这个下界,我们将小批量样本输入网络,并使用诸如 SGD 或 Adam 的优化算法更新这些参数。通常使用自动微分计算证据下界(ELBO)相对于参数的梯度。在此过程中,我们在图 17.10 中既在彩色曲线之间移动(改变 θ),又沿这些曲线移动(改变 ϕ)。在此过程中,参数 ϕ 发生变化,以在非线性隐变量模型中赋予数据更高的似然。

图 17.10 VAE 在每次迭代时都会更新决定下界的两个因素。解码器的参数 ϕ 和编码器的参数 θ 都会被调整,以增大该下界。
图 17.10 VAE 在每次迭代时都会更新决定下界的两个因素。解码器的参数 ϕ 和编码器的参数 θ 都会被调整,以增大该下界。Figure 17.10 The VAE updates both fac- tors that determine the lower bound at each iteration. Both the parameters ϕ of the decoder and the parameters θ of the encoder are manipulated to increase this lower bound.
图 17.11 重参数化技巧。在原始架构(图 17.9)中,我们无法轻松通过采样步骤进行反向传播。重参数化技巧将采样步骤从主流程中移除;我们从标准正态分布中采样
图 17.11 重参数化技巧。在原始架构(图 17.9)中,我们无法轻松通过采样步骤进行反向传播。重参数化技巧将采样步骤从主流程中移除;我们从标准正态分布中采样,并将其与预测的均值和协方差相结合,从而得到变分分布的一个样本。Figure 17.11 Reparameterization trick. With the original architecture (figure 17.9), we cannot easily backpropagate through the sampling step. The reparameterization trick removes the sampling step from the main pipeline; we draw from a standard normal and combine this with the predicted mean and covariance to get a sample from the variational distribution.
17.7 重参数化技巧
17.7 The reparameterization trick

There is one more complication; the network involves a sampling step, and it is difficult to differentiate through this stochastic component. However, differentiating past this step is necessary to update the parameters θ that precede it in the network.

Fortunately, there is a simple solution; we can move the stochastic part into a branch of the network that draws a sample ϵ∗ from Norm ϵ[ 0 , I ] and then use the relation:

还有一个复杂之处;该网络包含一个采样步骤,难以对这个随机组件进行求导。然而,要更新网络中位于该步骤之前的参数 θ,就必须跨过这一步进行求导。

幸运的是,有一个简单的解决方案;我们可以将随机部分移到一个分支中的网络从正态分布 ϵ[ 0 , I ] 中抽取一个样本 ϵ∗,然后使用以下关系:

to draw from the intended Gaussian.

Now we can compute the derivatives as usual because the backpropagation algorithm does not need to pass down the stochastic branch. This is known as the reparameterization trick (figure 17.11).

以从目标高斯分布中采样。现在我们可以照常计算导数因为反向传播算法不需要沿随机分支向下传递。这被称为重参数化技巧(图 17.11)。

17.8 应用
17.8 Applications

Variational autoencoders have many uses, including denoising, anomaly detection, and compression. This section reviews several applications for image data.

变分自编码器有许多用途,包括去噪、异常检测和压缩。本节回顾了图像数据的若干应用。

17.8.1 近似样本概率
17.8.1 Approximating sample probability

In section 17.3, we argued that it is not possible to evaluate the probability of a sample with the VAE, which describes this probability as:

在第 17.3 节中,我们论证了无法使用 VAE 评估样本的概率, VAE 将该概率描述为:

In principle, we could approximate this probability using equation 17.22 by drawing samples from Pr( z ) = Norm z[ 0 , I ] and computing:

原则上,我们可以使用公式 17.22,通过从 Pr (z) = Norm z[ 0 , I ] 中抽取样本来近似这一概率,并计算:

However, the curse of dimensionality means that almost all values of z n that we draw would have a very low probability Pr( x | z n); we would have to draw an enormous number of samples to get a reliable estimate. A better approach is to use importance sampling. Here, we sample z from an auxiliary distribution q( z ), evaluate Pr( x | z n), and rescale the resulting values by the probability q( z ) under the new distribution: where now we draw the samples from q( z ). If q( z ) is close to the region of z where the Pr( x | z ) has high likelihood, then we will focus the sampling on the relevant area of

然而,维度灾难意味着我们抽取的几乎所有 z n 都会具有非常低的概率 Pr (x | z n);我们必须抽取大量样本才能得到可靠的估计。更好的方法是使用重要性采样。这里,我们从辅助分布 q (z) 中采样 z,计算 Pr (x | z n),并按新分布下的概率 q (z) 对所得值进行缩放:其中,我们现在从 q (z) 中采样。如果 q (z) 接近 z 的区域,其中

space and estimate Pr( x ) much more efficiently.

Pr (x | z) 具有高似然,那么我们将把采样集中在空间的相关区域,并更高效地估计 Pr (x)。

The product Pr( x | z )Pr( z ) that we are trying to integrate is proportional to the posterior distribution Pr( z | x ) (by Bayes’ rule). Hence, a sensible choice of auxiliary distribution q( z ) is the variational posterior q( z | x ) computed by the encoder.

In this way, we can approximate the probability of new samples. With sufficient samples, this will provide a better estimate than the lower bound and could be used to evaluate the quality of the model by evaluating the log-likelihood of test data. Alternatively, it could be used as a criterion for determining whether new examples belong to the distribution or are anomalous.

我们试图积分的乘积 Pr( x | z )Pr( z ) 与后验分布 Pr( z | x ) (由贝叶斯法则)成正比。因此,辅助分布 q( z ) 的合理选择是由编码器计算的变分后验 q( z | x )。

这样,我们可以近似新样本的概率。在样本数量充足时,这将提供比下界更好的估计,并且可以通过评估测试数据的对数似然来评估模型的质量。或者,它可以用作判断新示例是否属于该分布或属于异常的标准。

图 17.12 从在 CELEBA 上训练的标准 VAE 进行采样。在每一列中,抽取一个隐变量 z ∗,并将其输入模型以预测均值 f[ z ∗, ϕ],然后添加
图 17.12 从在 CELEBA 上训练的标准 VAE 进行采样。在每一列中,抽取一个隐变量 z ∗,并将其输入模型以预测均值 f[ z ∗, ϕ],然后添加独立高斯噪声(见图 17.3)。 a) 一组样本,这些样本是 b) 预测均值与 c) 球面高斯噪声向量之和。在添加噪声之前,图像显得过于平滑;添加之后则显得噪声过多。这是典型现象,通常展示的是无噪声版本,因为噪声被认为代表图像中未被建模的方面。改编自 Dorta 等人(2018)。 d) 如今,利用层次先验、专用架构和细致的正则化,可以从 VAE 生成高质量图像。改编自 Vahdat 与 Kautz (2020)。Figure 17.12 Sampling from a standard VAE trained on CELEBA. In each column, a latent variable z ∗ is drawn and passed through the model to predict the mean f[ z ∗, ϕ] before adding independent Gaussian noise (see figure 17.3). a) A set of samples that are the sum of b) the predicted means and c) spherical Gaussian noise vectors. The images look too smooth before we add the noise and too noisy afterward. This is typical, and usually, the noise-free version is shown since the noise is considered to represent aspects of the image that are not modeled. Adapted from Dorta et al. (2018). d) It is now possible to generate high-quality images from VAEs using hierarchical priors, specialized architecture, and careful regularization. Adapted from Vahdat & Kautz (2020).
17.8.2 生成
17.8.2 Generation

VAEs build a probabilistic model, and it’s easy to sample from this model by drawing from the prior Pr( z ) over the latent variable, passing this result through the decoder f[ z , ϕ], and adding noise according to Pr( x | f[ z , ϕ]). Unfortunately, samples from vanilla VAEs are generally low-quality (figure 17.12a–c). This is partly because of the naïve spherical Gaussian noise model and partly because of the Gaussian models used for the prior and variational posterior. One trick to improve generation quality is to sample from the aggregatedposteriorq( z |θ) = (1/I)P

iq( z | x i, θ) rather than the prior; this is the average posterior over all samples and is a mixture of Gaussians that is more representative of true distribution in latent space.

Modern VAEs can produce high-quality samples (figure 17.12d), but only by using hierarchical priors and specialized network architecture and regularization techniques. Diffusion models (chapter 18) can be viewed as VAEs with hierarchical priors. These also create very high-quality samples.

VAE 构建概率模型,并且从该模型采样很容易:从隐变量上的先验 Pr( z ) 采样,将该结果通过解码器 f[ z ϕ],并根据 Pr( x | f[ z ϕ]) 添加噪声。不幸的是,来自普通 VAE 的生成质量通常较低(图 17.12a–c)。这部分归因于朴素的球面高斯噪声模型,也部分归因于用于先验和变分后验的高斯模型。提高生成质量的一个技巧是从聚合后验 q (z |θ) = (1/I) P

iq (z | x i, θ) 采样,而不是从先验采样;它是对所有样本的平均后验,并且是一种高斯混合,更能代表隐空间中的真实分布。

现代变分自编码器能够生成高质量样本(图 17.12d),但只有在使用分层先验、专用网络架构和正则化技术时才能做到。扩散模型(第 18 章)可以被视为具有分层先验的变分自编码器。它们也能生成非常高质量的样本。

变分自编码器也可用于修改真实数据。一个数据点 x 可以通过以下两种方式投影到隐空间: (i) 取编码器预测的分布的均值,或 (ii) 使用优化过程寻找隐变量 z,使其最大化后验概率;贝叶斯规则告诉我们,该后验概率正比于 Pr (x | z) Pr (z)。

17.8.3 重新合成
17.8.3 Resynthesis

VAEs can also be used to modify real data. A data point x can be projected into the latent space by either (i) taking the mean of the distribution predicted by the encoder or (ii) by using an optimization procedure to find the latent variable z that maximizes the posterior probability, which Bayes’ rule tells us is proportional to Pr( x | z )Pr( z ). In figure 17.13, multiple images labeled as “neutral” or “smiling” are projected into latent space. The vector representing this change is estimated by taking the difference in latent space between the means of these two groups. A second vector is estimated to represent “mouth closed” versus “mouth open.”

Now the image of interest is projected into the latent space, and then the representation is modified by adding or subtracting these vectors. To generate intermediate images, spherical linear interpolation or Slerp is used rather than linear interpolation.

在图 17.13 中,多张标签为“中性”或“微笑”的图像被投影到隐空间。表示这种变化的向量通过取这两组均值在隐空间中的差来估计。还估计了第二个向量,以表示“闭嘴”与“张嘴”。

现在,目标图像被投影到隐空间,然后通过添加或减去这些向量来修改其表示。为了生成中间图像,使用球面线性插值或 Slerp,而不是线性插值。

In 3D, this would be the difference between interpolating along the surface of a sphere versus digging a straight tunnel through its body.

The process of encoding (and possibly modifying) input data before decoding again is known as resynthesis. This can also be done with GANs and normalizing flows. However, in GANs, there is no encoder, so a separate procedure must be used to find the latent variable that corresponds to the observed data.

在三维中,这相当于沿着球面进行插值与在球体内部挖一条直线隧道之间的差异。

在对输入数据进行编码(以及可能的修改)之后再次解码的过程称为重新合成。这也可以通过 GAN 和归一化流来完成。然而,在 GAN 中没有编码器,因此必须使用单独的过程来找到与观测数据对应的隐变量。

17.8.4 解耦
17.8.4 Disentanglement

In the resynthesis example above, the directions in space representing interpretable properties had to be estimated using labeled training data. Other work attempts to improve the characteristics of the latent space so that its coordinate directions correspond to realworld properties. When each dimension represents an independent real-world factor, the latent space is described as disentangled. For example, when modeling face images, we might hope to uncover head pose or hair color as independent factors. Methods to encourage disentanglement typically add regularization terms to the loss function based on either (i) the posterior q( z | x , θ) over the latent variables z, or (ii) the

在上述重新合成示例中,表示可解释属性的空间方向必须使用带标签的训练数据进行估计。其他工作试图改善隐空间的特性,使其坐标方向与现实世界属性相对应。当每个维度都表示一个独立的现实世界因素时,隐空间被描述为解耦的。例如,在对人脸图像建模时,我们可能希望将头部姿态或发色作为独立因素揭示出来。促进解耦的方法通常基于以下二者之一,在损失函数中添加正则化项: (i) 隐变量

图 17.13 重新合成。左侧的原始图像通过编码器被投影到隐空间,并选取预测高斯分布的均值来表示该图像。网格中左中位置的图像是输入的重建结果。其他图像是在沿表示
图 17.13 重新合成。左侧的原始图像通过编码器被投影到隐空间,并选取预测高斯分布的均值来表示该图像。网格中左中位置的图像是输入的重建结果。其他图像是在沿表示微笑 / 中性(水平方向)和张口 / 闭口(垂直方向)的方向操控隐空间后得到的重建结果。改编自 White (2016)。Figure 17.13 Resynthesis. The original image on the left is projected into the latent space using the encoder, and the mean of the predicted Gaussian is chosen to represent the image. The center-left image in the grid is the reconstruction of the input. The other images are reconstructions after manipulating the latent space in directions representing smiling/neutral (horizontal) and mouth open/closed (vertical). Adapted from White (2016).

Here the regularization term r 1[•] is a function of the posterior and is weighted by λ1. The term r 2[•] is a function of the aggregated posterior and is weighted by λ2.

For example, the beta VAE upweights the second term in the ELBO (equation 17.18): ELBO[θ, ϕ]

其中,正则化项 r 1[•] 是后验的函数,其权重为 λ1。项 r 2[•] 是聚合后验的函数,其权重为 λ2。

例如, beta VAE 会提高证据下界(ELBO)中第二项的权重(公式 17.18): ELBO[θ, ϕ]

where β> 1 determines how much more the deviation from the prior Pr( z ) is weighted relative to the reconstruction error. Since the prior is usually a multivariate normal with a spherical covariance matrix, its dimensions are independent. Hence, up-weighting this term encourages the posterior distributions to be less correlated. Another variant is the total correlation VAE, which adds a term to decrease the total correlation between variables in the latent space (figure 17.14) and maximizes the mutual information between a small subset of the latent variables and the observations.

其中 β> 1 决定了与先验 Pr (z) 的偏差相对于重建误差被赋予多少额外权重。由于先验通常是具有球面协方差矩阵的多元正态分布,其各维度相互独立。因此,提高该项的权重会促使后验分布的相关性更低。另一种变体是总相关性 VAE,它添加一项以降低隐空间中变量之间的总相关性(图 17.14),并最大化一小部分隐变量与观测值之间的互信息。

17.9 总结
17.9 Summary

The VAE is an architecture that helps to learn a nonlinear latent variable model over x. This model can generate new examples by sampling from the latent variable, passing the result through a deep network, and then adding independent Gaussian noise.

VAE 是一种有助于学习针对 x 的非线性隐变量模型的架构。该模型可以通过从隐变量中采样、将结果传入深度网络,然后添加独立高斯噪声来生成新样本。

It is not possible to compute the likelihood of a data point in closed form, and this poses problems for training with maximum likelihood. However, we can define a lower bound on the likelihood and maximize this bound. Unfortunately, for the bound to be tight, we need to compute the posterior probability of the latent variable given the observed data, which is also intractable.

图 17.14 总相关性 VAE 中的解耦。 VAE 模型经过修改,使损失函数促使隐变量的总相关性最小化,从而促进解耦。在使用椅子图像数据集进行训练时,若干隐变
图 17.14 总相关性 VAE 中的解耦。 VAE 模型经过修改,使损失函数促使隐变量的总相关性最小化,从而促进解耦。在使用椅子图像数据集进行训练时,若干隐变量维度具有清晰的现实世界解释,包括 a) 旋转、 b) 整体尺寸,以及 c) 椅腿(转椅与普通椅)。在每种情况下,中间一列展示来自模型的样本,而当我们从左向右移动时,是在隐空间中减去或加上一个坐标向量。改编自 Chen 等人 (2018d)。Figure 17.14 Disentanglement in the total correlation VAE. The VAE model is modified so that the loss function encourages the total correlation of the latent variables to be minimized and hence encourages disentanglement. When trained on a dataset of images of chairs, several of the latent dimensions have clear realworld interpretations, including a) rotation, b) overall size, and c) legs (swivel chair versus normal). In each case, the central column depicts samples from the model, and as we move left to right, we are subtracting or adding a coordinate vector in latent space. Adapted from Chen et al. (2018d).

The solution is to make a variational approximation. This is a simpler distribution (usually a Gaussian) that approximates the posterior and whose parameters are computed by a second encoder network.

To create high-quality samples from the VAE, it seems to be necessary to model the latent space with more sophisticated probability distributions than the Gaussian prior and posterior.

One option is to use hierarchical priors (in which one latent variable generates another). The next chapter discusses diffusion models, which produce very high-quality examples and can be viewed as hierarchical VAEs.

无法以闭式形式计算数据点的似然,这给极大似然训练带来了问题。然而,我们可以定义似然的下界并最大化该下界。不幸的是,要使该下界足够紧,我们需要计算给定观测数据时隐变量的后验概率,而这也难以计算。解决方案是进行变分近似。这是一种更简单的分布(通常是高斯分布),用于近似后验,其参数由第二个编码器网络计算。

为了从 VAE 生成高质量样本,似乎需要用比高斯先验和后验更复杂的概率分布来建模隐空间。一种选择是使用层次化先验(其中一个隐变量生成另一个隐变量)。下一章将讨论扩散模型,它们能生成非常高质量的样本,并可被视为层次化 VAE。

注释
Notes

Applications:

Relation to other models:

Architectural variations:

Modifying likelihood:

Latent space, prior, and posterior:

Combination with other models:

Posterior collapse:

Blurry reconstructions:

Other problems:

Disentangling latent representation:

公式
图 17.15 期望最大化(EM)算法。EM算法交替调整辅助参数 θ (在彩色曲线之间移动)和模型参数 ϕ (沿彩色曲线移动),直到达到极大值。这些调整分别称为
图 17.15 期望最大化(EM)算法。EM算法交替调整辅助参数 θ (在彩色曲线之间移动)和模型参数 ϕ (沿彩色曲线移动),直到达到极大值。这些调整分别称为E步和M步。由于E步将 Pr(h|x ϕ) 用作 q(h|x θ)的后验概率分布,因此该界是紧的,并且在每次E步之后,彩色曲线都会触及黑色似然曲线。Figure 17.15 Expectation maximization (EM) algorithm. The EM algorithm alternately adjusts the auxiliary parameters θ (moves between colored curves) and model parameters ϕ (moves along colored curves) until the a maximum These adjustments are is reached. known as the E-step and the M-step, Because the E-Step uses respectively. distribution posterior
公式

Reparameterization trick:

Lower bound and the EM algorithm:

习题
Problems
公式
公式