Introduction
这是我大二暑假写的变分自编码器(VAE)学习笔记,主要参考 Kingma 和 Welling 的 An Introduction to Variational Autoencoders 。原文用 Typst 排版,网页版由 Typst 源码借助 Gemini 转换,之后做过修订。原版 PDF 见下面的链接。
Motivation
机器学习模型大致可以分成判别模型和生成模型。判别模型学习从观测到预测目标的映射;生成模型则学习所有变量的联合分布。
生成模型描述的是数据在现实中如何产生。科学中的建模大多也是这个意思:提出关于生成过程的理论,再用观测去检验。气象学家用偏微分方程描述天气背后的物理过程,生物学家、化学家、经济学家也是类似的做法。
理解数据的生成过程还有一个好处:它往往对应因果关系,而因果关系比单纯的相关性更容易推广到新情况。
生成模型也可以用来做判别。用贝叶斯公式,可以把生成模型给出的联合分布转换成分类所需的条件概率。
两者的方向是相反的。以图像为例,可以设想现实中的图像是这样产生的:先有物体,生成三维形状,再投影到像素网格上。判别模型则反过来,直接把像素作为输入,映射到标签。
生成模型通常比判别模型对数据做出更强的假设。假设不准确时(实际中几乎总会有一些偏差),生成模型会有更大的渐近偏差。所以,如果目标只是判别,而且数据足够多,纯判别模型通常错误更少。但在数据有限时,生成模型可以帮助训练判别器。比如半监督学习中,标注样本很少而未标注样本很多,这时可以用数据的生成模型来改进分类。
更一般地说,学习生成过程可以得到对世界的有用抽象,这些抽象能服务于多个下游任务。寻找数据中解耦的、有语义的、统计独立的变化因素,通常称为无监督表示学习,VAE 在这方面被广泛使用。也可以把它看成一种隐式正则化:要求表示能解释数据的生成过程,就约束了从输入到表示的映射。
VAE 由两个耦合、但分别参数化的模型组成:编码器(识别模型)和解码器(生成模型)。识别模型为生成模型提供后验分布的近似,生成模型在类似"期望最大化"的迭代中用它来更新参数。反过来,生成模型为识别模型提供了学习有意义表示的框架。按贝叶斯公式,识别模型可以看作生成模型的近似逆。
与传统变分推断(VI)相比,VAE 的识别模型是输入的一个(随机)函数。传统 VI 给每个数据点单独设一个变分分布,大数据集上效率很低。识别模型用一组共享参数刻画输入与潜变量之间的关系,这称为摊销推断(amortized inference)。识别模型可以很复杂,但推断只需要一次前向计算,所以仍然很快。代价是采样会给梯度引入噪声。
VAE 框架最重要的贡献可能是重参数化技巧:一种重新组织梯度计算的简单方法,可以降低梯度估计的方差。
Aim
VAE 提供了一种有原则的方法:用随机梯度下降,同时学习深度潜变量模型和对应的推断模型。它在生成建模、半监督学习和表示学习中都有广泛应用。
Probabilistic Models and Variational Inference
概率模型中有未知量,而数据通常不足以完全确定它们,所以需要用(条件)概率分布来描述这种不确定性。最完整的概率模型是给出模型中所有变量的联合分布,它刻画了变量之间所有的相关和高阶依赖。
用 x \mathbf{x} x 表示所有观测变量组成的向量,我们希望对它的联合分布建模。(这里把观测变量展平、拼接成一个向量。)
假设 x \mathbf{x} x 是某个未知过程产生的随机样本,真实分布 p ∗ ( x ) p^*(\mathbf{x}) p ∗ ( x ) 未知。我们用一个参数为 θ \theta θ 的模型 p θ ( x ) p_\theta(\mathbf{x}) p θ ( x ) 来近似它:
x ∼ p θ ( x ) \begin{equation}
\mathbf{x} \sim p_\theta(\mathbf{x})
\end{equation} x ∼ p θ ( x )
学习就是寻找参数 θ \theta θ ,使模型分布尽量接近真实分布:
p θ ( x ) ≈ p ∗ ( x ) \begin{equation}
p_\theta(\mathbf{x}) \approx p^*(\mathbf{x})
\end{equation} p θ ( x ) ≈ p ∗ ( x )
Conditional Models
分类或回归问题关心的不是无条件模型 p θ ( x ) p_\theta(\mathbf{x}) p θ ( x ) ,而是条件模型 p θ ( y ∣ x ) p_\theta(y|\mathbf{x}) p θ ( y ∣ x ) ,它近似真实的条件分布 p ∗ ( y ∣ x ) p^*(y|\mathbf{x}) p ∗ ( y ∣ x ) ,即给定观测 x \mathbf{x} x 时 y y y 的分布。这时 x \mathbf{x} x 称为模型的输入。和无条件情形一样,我们选择并优化模型,使得对任意 x \mathbf{x} x 和 y y y :
p θ ( y ∣ x ) ≈ p ∗ ( y ∣ x ) \begin{equation}
p_\theta(y|\mathbf{x}) \approx p^*(y|\mathbf{x})
\end{equation} p θ ( y ∣ x ) ≈ p ∗ ( y ∣ x )
Directed Graphical Models and Neural Networks
我们使用有向概率图模型,也就是贝叶斯网络。其中所有变量组成一个有向无环图,联合分布分解为每个变量在其父节点条件下的分布的乘积:
p θ ( x 1 , . . . , x M ) = ∏ j = 1 M p θ ( x j ∣ P a ( x j ) ) \begin{equation}
p_\theta(\mathbf{x}_1, ..., \mathbf{x}_M) = \prod_{j=1}^{M} p_\theta(\mathbf{x}_j | Pa(\mathbf{x}_j))
\end{equation} p θ ( x 1 , ... , x M ) = j = 1 ∏ M p θ ( x j ∣ P a ( x j ))
P a ( x j ) Pa(\mathbf{x}_j) P a ( x j ) 是节点 j j j 在图中的父节点集合。没有父节点的变量,条件分布就是它的先验。各个条件分布可以用神经网络参数化。
Dataset
数据集 D \mathcal{D} D 由 N ≥ 1 N \ge 1 N ≥ 1 个数据点组成:
D = { x ( 1 ) , x ( 2 ) , . . . , x ( N ) } ≡ { x ( i ) } i = 1 N ≡ x ( 1 : N ) \begin{equation}
\mathcal{D} = \{\mathbf{x}^{(1)}, \mathbf{x}^{(2)}, ..., \mathbf{x}^{(N)}\} \equiv \{\mathbf{x}^{(i)}\}_{i=1}^N \equiv \mathbf{x}^{(1:N)}
\end{equation} D = { x ( 1 ) , x ( 2 ) , ... , x ( N ) } ≡ { x ( i ) } i = 1 N ≡ x ( 1 : N )
假设数据点是同一个系统的独立测量,即独立同分布(i.i.d.)。在给定参数的条件下,数据集的概率等于各数据点概率的乘积,对数概率为:
log p θ ( D ) = ∑ x ∈ D log p θ ( x ) \begin{equation}
\log p_\theta(\mathcal{D}) = \sum_{\mathbf{x} \in \mathcal{D}} \log p_\theta(\mathbf{x})
\end{equation} log p θ ( D ) = x ∈ D ∑ log p θ ( x )
Maximum Likelihood and Minibatch SGD
最常用的训练准则是最大对数似然。最大化对数似然等价于最小化数据分布与模型分布之间的 KL 散度。常用的优化方法是随机梯度下降(SGD)。
数据集很大时,每一步都用全部数据计算梯度太贵。做法是随机抽一个大小为 N M N_\mathcal{M} N M 的小批量 M \mathcal{M} M ,在它上面算梯度。我们需要这个梯度在期望上等于全数据集上的梯度。
全数据集和小批量的对数似然分别是:
log p θ ( D ) = ∑ x ∈ D log p θ ( x ) , log p θ ( M ) = ∑ x ∈ M log p θ ( x ) \begin{equation}
\log p_\theta(\mathcal{D}) = \sum_{\mathbf{x} \in \mathcal{D}} \log p_\theta(\mathbf{x}), \qquad \log p_\theta(\mathcal{M}) = \sum_{\mathbf{x} \in \mathcal{M}} \log p_\theta(\mathbf{x})
\end{equation} log p θ ( D ) = x ∈ D ∑ log p θ ( x ) , log p θ ( M ) = x ∈ M ∑ log p θ ( x )
按每个数据点平均后,小批量给出全数据集平均对数似然的无偏估计,梯度也是如此:
1 N log p θ ( D ) ≈ 1 N M log p θ ( M ) = 1 N M ∑ x ∈ M log p θ ( x ) \begin{equation}
\frac{1}{N} \log p_\theta(\mathcal{D}) \approx \frac{1}{N_\mathcal{M}} \log p_\theta(\mathcal{M}) = \frac{1}{N_\mathcal{M}} \sum_{\mathbf{x} \in \mathcal{M}} \log p_\theta(\mathbf{x})
\end{equation} N 1 log p θ ( D ) ≈ N M 1 log p θ ( M ) = N M 1 x ∈ M ∑ log p θ ( x )
1 N ∇ θ log p θ ( D ) ≈ 1 N M ∇ θ log p θ ( M ) = 1 N M ∑ x ∈ M ∇ θ log p θ ( x ) \begin{equation}
\frac{1}{N} \nabla_\theta \log p_\theta(\mathcal{D}) \approx \frac{1}{N_\mathcal{M}} \nabla_\theta \log p_\theta(\mathcal{M}) = \frac{1}{N_\mathcal{M}} \sum_{\mathbf{x} \in \mathcal{M}} \nabla_\theta \log p_\theta(\mathbf{x})
\end{equation} N 1 ∇ θ log p θ ( D ) ≈ N M 1 ∇ θ log p θ ( M ) = N M 1 x ∈ M ∑ ∇ θ log p θ ( x )
小批量梯度的无偏性
下面验证这一点。全数据集的梯度是:
∇ θ log p θ ( D ) = ∑ i = 1 N ∇ θ log p θ ( x i ) \begin{equation}
\nabla_\theta \log p_\theta(\mathcal{D}) = \sum_{i=1}^N \nabla_\theta \log p_\theta(\mathbf{x}_i)
\end{equation} ∇ θ log p θ ( D ) = i = 1 ∑ N ∇ θ log p θ ( x i )
小批量只有 N M N_\mathcal{M} N M 个点,要估计全数据集的梯度(而不是平均梯度),需要乘以比例 N N M \frac{N}{N_\mathcal{M}} N M N 来补偿样本数的差异:
∇ ~ θ log p θ ( M ) = N N M ∑ j = 1 N M ∇ θ log p θ ( x j ) \begin{equation}
\tilde{\nabla}_\theta \log p_\theta(\mathcal{M}) = \frac{N}{N_\mathcal{M}} \sum_{j=1}^{N_\mathcal{M}} \nabla_\theta \log p_\theta(\mathbf{x}_j)
\end{equation} ∇ ~ θ log p θ ( M ) = N M N j = 1 ∑ N M ∇ θ log p θ ( x j )
每个 x j \mathbf{x}_j x j 都是从 D \mathcal{D} D 中均匀随机抽取的,所以:
E [ ∇ ~ θ log p θ ( M ) ] = N N M E [ ∑ j = 1 N M ∇ θ log p θ ( x j ) ] = N N M N M E [ ∇ θ log p θ ( x ) ] = N E [ ∇ θ log p θ ( x ) ] \begin{align}
\mathbb{E}[\tilde{\nabla}_\theta \log p_\theta(\mathcal{M})] &= \frac{N}{N_\mathcal{M}} \mathbb{E}\left[\sum_{j=1}^{N_\mathcal{M}} \nabla_\theta \log p_\theta(\mathbf{x}_j)\right] \\
&= \frac{N}{N_\mathcal{M}} N_\mathcal{M} \, \mathbb{E}[\nabla_\theta \log p_\theta(\mathbf{x})] \\
&= N \, \mathbb{E}[\nabla_\theta \log p_\theta(\mathbf{x})]
\end{align} E [ ∇ ~ θ log p θ ( M )] = N M N E [ j = 1 ∑ N M ∇ θ log p θ ( x j ) ] = N M N N M E [ ∇ θ log p θ ( x )] = N E [ ∇ θ log p θ ( x )]
而均匀抽取一个点的期望梯度就是全数据集梯度的平均值:
N E [ ∇ θ log p θ ( x ) ] = ∑ i = 1 N ∇ θ log p θ ( x i ) = ∇ θ log p θ ( D ) \begin{equation}
N \, \mathbb{E}[\nabla_\theta \log p_\theta(\mathbf{x})] = \sum_{i=1}^N \nabla_\theta \log p_\theta(\mathbf{x}_i) = \nabla_\theta \log p_\theta(\mathcal{D})
\end{equation} N E [ ∇ θ log p θ ( x )] = i = 1 ∑ N ∇ θ log p θ ( x i ) = ∇ θ log p θ ( D )
所以乘以 N N M \frac{N}{N_\mathcal{M}} N M N 之后,小批量梯度是全数据集梯度的无偏估计。实践中通常直接用平均形式,两者只差一个常数倍,可以并入学习率。
Learning and Inference in Deep Latent Variable Models
Latent Variables
可以把上面的全观测有向模型扩展到含潜变量的情形。潜变量是模型的一部分,但没有被观测到,不在数据集里,通常记作 z \mathbf{z} z 。对观测变量 x \mathbf{x} x 建模时,有向图模型表示的是 x \mathbf{x} x 和 z \mathbf{z} z 的联合分布 p θ ( x , z ) p_\theta(\mathbf{x}, \mathbf{z}) p θ ( x , z ) 。x \mathbf{x} x 的边缘分布为:
p θ ( x ) = ∫ p θ ( x , z ) d z \begin{equation}
p_\theta(\mathbf{x}) = \int p_\theta(\mathbf{x}, \mathbf{z}) \, d\mathbf{z}
\end{equation} p θ ( x ) = ∫ p θ ( x , z ) d z
把它看作 θ \theta θ 的函数时,称为(单个数据点的)边缘似然或模型证据。
这种隐式定义的分布可以很灵活。如果 z \mathbf{z} z 是离散的,p θ ( x ∣ z ) p_\theta(\mathbf{x}|\mathbf{z}) p θ ( x ∣ z ) 是高斯分布,那么 p θ ( x ) p_\theta(\mathbf{x}) p θ ( x ) 就是高斯混合。如果 z \mathbf{z} z 连续,p θ ( x ) p_\theta(\mathbf{x}) p θ ( x ) 可以看作无穷多个分量的混合,表达能力可能比离散混合更强。这样的边缘分布也叫复合概率分布。
Deep Latent Variable Models
深度潜变量模型(deep latent variable model, DLVM)指分布由神经网络参数化的潜变量模型 p θ ( x , z ) p_\theta(\mathbf{x}, \mathbf{z}) p θ ( x , z ) ,也可以带上条件,如 p θ ( x , z ∣ y ) p_\theta(\mathbf{x}, \mathbf{z} | \mathbf{y}) p θ ( x , z ∣ y ) 。它的一个重要优点是:即使每个因子(先验或条件分布)都比较简单,比如条件高斯,边缘分布 p θ ( x ) p_\theta(\mathbf{x}) p θ ( x ) 也可以非常复杂,几乎能包含任意依赖关系。这让 DLVM 适合用来近似复杂的真实分布 p ∗ ( x ) p^*(\mathbf{x}) p ∗ ( x ) 。
最简单、也最常见的 DLVM 是这样分解的:
p θ ( x , z ) = p θ ( z ) p θ ( x ∣ z ) \begin{equation}
p_\theta(\mathbf{x}, \mathbf{z}) = p_\theta(\mathbf{z}) p_\theta(\mathbf{x}|\mathbf{z})
\end{equation} p θ ( x , z ) = p θ ( z ) p θ ( x ∣ z )
p θ ( z ) p_\theta(\mathbf{z}) p θ ( z ) 和 p θ ( x ∣ z ) p_\theta(\mathbf{x}|\mathbf{z}) p θ ( x ∣ z ) 的形式由我们指定。p ( z ) p(\mathbf{z}) p ( z ) 不依赖任何观测,称为潜变量的先验分布。
DLVM 的困难在于,边缘似然 p θ ( x ) p_\theta(\mathbf{x}) p θ ( x ) 和后验 p θ ( z ∣ x ) = p θ ( x , z ) / p θ ( x ) p_\theta(\mathbf{z}|\mathbf{x}) = p_\theta(\mathbf{x}, \mathbf{z}) / p_\theta(\mathbf{x}) p θ ( z ∣ x ) = p θ ( x , z ) / p θ ( x ) 一般都没有解析形式,无法直接计算和优化。VAE 就是为了解决这个问题。
Variational Autoencoders
Encoder or Approximate Posterior
VAE 框架提供了一种高效的方法,用 SGD 同时优化 DLVM 和对应的推断模型。为了让后验推断和学习变得可处理,引入一个带参数的推断模型 q ϕ ( z ∣ x ) q_\phi(\mathbf{z}|\mathbf{x}) q ϕ ( z ∣ x ) ,也称为编码器或识别模型。ϕ \phi ϕ 是它的参数,称为变分参数。我们优化 ϕ \phi ϕ ,使得:
q ϕ ( z ∣ x ) ≈ p θ ( z ∣ x ) \begin{equation}
q_\phi(\mathbf{z}|\mathbf{x}) \approx p_\theta(\mathbf{z}|\mathbf{x})
\end{equation} q ϕ ( z ∣ x ) ≈ p θ ( z ∣ x )
和 DLVM 一样,推断模型可以是(几乎)任意的有向图模型:
q ϕ ( z ∣ x ) = q ϕ ( z 1 , . . . , z M ∣ x ) = ∏ j = 1 M q ϕ ( z j ∣ P a ( z j ) , x ) \begin{equation}
q_\phi(\mathbf{z}|\mathbf{x}) = q_\phi(\mathbf{z}_1, ..., \mathbf{z}_M | \mathbf{x}) = \prod_{j=1}^{M} q_\phi(\mathbf{z}_j | Pa(\mathbf{z}_j), \mathbf{x})
\end{equation} q ϕ ( z ∣ x ) = q ϕ ( z 1 , ... , z M ∣ x ) = j = 1 ∏ M q ϕ ( z j ∣ P a ( z j ) , x )
P a ( z j ) Pa(\mathbf{z}_j) P a ( z j ) 是 z j \mathbf{z}_j z j 在图中的父变量集合。q ϕ ( z ∣ x ) q_\phi(\mathbf{z}|\mathbf{x}) q ϕ ( z ∣ x ) 同样可以用深度神经网络参数化,例如:
( μ , log σ ) = EncoderNeuralNet ϕ ( x ) \begin{equation}
(\mu, \log\sigma) = \text{EncoderNeuralNet}_\phi(\mathbf{x})
\end{equation} ( μ , log σ ) = EncoderNeuralNet ϕ ( x )
q ϕ ( z ∣ x ) = N ( z ; μ , diag ( σ 2 ) ) \begin{equation}
q_\phi(\mathbf{z}|\mathbf{x}) = \mathcal{N}(\mathbf{z}; \mu, \text{diag}(\sigma^2))
\end{equation} q ϕ ( z ∣ x ) = N ( z ; μ , diag ( σ 2 ))
数据集中所有数据点共用同一个编码器做后验推断。这和传统变分推断不同:后者的变分参数不共享,需要对每个数据点单独迭代优化。摊销推断省去了每个数据点的优化循环,也能充分利用 SGD 的效率。
Evidence Lower Bound (ELBO)
VAE 通过编码器和解码器,结合先验、近似后验和重建分布,对复杂的数据分布进行建模和生成。
对任意推断模型 q ϕ ( z ∣ x ) q_\phi(\mathbf{z}|\mathbf{x}) q ϕ ( z ∣ x ) ,对数似然可以分解为:
log p θ ( x ) = E q ϕ ( z ∣ x ) [ log p θ ( x ) ] = E q ϕ ( z ∣ x ) [ log p θ ( x , z ) p θ ( z ∣ x ) ] = E q ϕ ( z ∣ x ) [ log p θ ( x , z ) q ϕ ( z ∣ x ) q ϕ ( z ∣ x ) p θ ( z ∣ x ) ] = E q ϕ ( z ∣ x ) [ log p θ ( x , z ) q ϕ ( z ∣ x ) ] ⏟ L θ , ϕ ( x ) ("ELBO") + E q ϕ ( z ∣ x ) [ log q ϕ ( z ∣ x ) p θ ( z ∣ x ) ] ⏟ D KL ( q ϕ ( z ∣ x ) ∣ ∣ p θ ( z ∣ x ) ) \begin{align}
\log p_\theta(\mathbf{x}) &= \mathbb{E}_{q_\phi(\mathbf{z}|\mathbf{x})} [\log p_\theta(\mathbf{x})] \\
&= \mathbb{E}_{q_\phi(\mathbf{z}|\mathbf{x})} \left[\log \frac{p_\theta(\mathbf{x},\mathbf{z})}{p_\theta(\mathbf{z}|\mathbf{x})}\right] \\
&= \mathbb{E}_{q_\phi(\mathbf{z}|\mathbf{x})} \left[\log \frac{p_\theta(\mathbf{x},\mathbf{z})}{q_\phi(\mathbf{z}|\mathbf{x})} \frac{q_\phi(\mathbf{z}|\mathbf{x})}{p_\theta(\mathbf{z}|\mathbf{x})}\right] \\
&= \underbrace{\mathbb{E}_{q_\phi(\mathbf{z}|\mathbf{x})} \left[\log \frac{p_\theta(\mathbf{x},\mathbf{z})}{q_\phi(\mathbf{z}|\mathbf{x})}\right]}_{\mathcal{L}_{\theta, \phi}(\mathbf{x}) \text{ ("ELBO")}} + \underbrace{\mathbb{E}_{q_\phi(\mathbf{z}|\mathbf{x})} \left[\log \frac{q_\phi(\mathbf{z}|\mathbf{x})}{p_\theta(\mathbf{z}|\mathbf{x})}\right]}_{D_{\text{KL}}(q_\phi(\mathbf{z}|\mathbf{x}) || p_\theta(\mathbf{z}|\mathbf{x}))}
\end{align} log p θ ( x ) = E q ϕ ( z ∣ x ) [ log p θ ( x )] = E q ϕ ( z ∣ x ) [ log p θ ( z ∣ x ) p θ ( x , z ) ] = E q ϕ ( z ∣ x ) [ log q ϕ ( z ∣ x ) p θ ( x , z ) p θ ( z ∣ x ) q ϕ ( z ∣ x ) ] = L θ , ϕ ( x ) ("ELBO") E q ϕ ( z ∣ x ) [ log q ϕ ( z ∣ x ) p θ ( x , z ) ] + D KL ( q ϕ ( z ∣ x ) ∣∣ p θ ( z ∣ x )) E q ϕ ( z ∣ x ) [ log p θ ( z ∣ x ) q ϕ ( z ∣ x ) ]
第二项是 q ϕ ( z ∣ x ) q_\phi(\mathbf{z}|\mathbf{x}) q ϕ ( z ∣ x ) 与真实后验 p θ ( z ∣ x ) p_\theta(\mathbf{z}|\mathbf{x}) p θ ( z ∣ x ) 之间的 KL 散度。它非负,并且当且仅当 q ϕ ( z ∣ x ) q_\phi(\mathbf{z}|\mathbf{x}) q ϕ ( z ∣ x ) 等于真实后验时为零:
D KL ( q ϕ ( z ∣ x ) ∣ ∣ p θ ( z ∣ x ) ) ≥ 0 \begin{equation}
D_{\text{KL}}(q_\phi(\mathbf{z}|\mathbf{x}) || p_\theta(\mathbf{z}|\mathbf{x})) \ge 0
\end{equation} D KL ( q ϕ ( z ∣ x ) ∣∣ p θ ( z ∣ x )) ≥ 0
第一项称为变分下界,或证据下界(ELBO):
L θ , ϕ ( x ) = E q ϕ ( z ∣ x ) [ log p θ ( x , z ) − log q ϕ ( z ∣ x ) ] \begin{equation}
\mathcal{L}_{\theta,\phi}(\mathbf{x}) = \mathbb{E}_{q_\phi(\mathbf{z}|\mathbf{x})} [\log p_\theta(\mathbf{x},\mathbf{z}) - \log q_\phi(\mathbf{z}|\mathbf{x})]
\end{equation} L θ , ϕ ( x ) = E q ϕ ( z ∣ x ) [ log p θ ( x , z ) − log q ϕ ( z ∣ x )]
由于 KL 散度非负,ELBO 是对数似然的下界:
L θ , ϕ ( x ) = log p θ ( x ) − D KL ( q ϕ ( z ∣ x ) ∣ ∣ p θ ( z ∣ x ) ) ≤ log p θ ( x ) \begin{align}
\mathcal{L}_{\theta,\phi}(\mathbf{x}) &= \log p_\theta(\mathbf{x}) - D_{\text{KL}}(q_\phi(\mathbf{z}|\mathbf{x}) || p_\theta(\mathbf{z}|\mathbf{x})) \\
&\le \log p_\theta(\mathbf{x})
\end{align} L θ , ϕ ( x ) = log p θ ( x ) − D KL ( q ϕ ( z ∣ x ) ∣∣ p θ ( z ∣ x )) ≤ log p θ ( x )
这个分解说明,最大化 ELBO 同时做了两件事:提高对数似然,让生成模型变好;缩小 KL 散度,让推断模型更接近真实后验。
Stochastic Gradient-Based Optimization of the ELBO
ELBO 的一个重要性质是,可以用 SGD 对所有参数(ϕ \phi ϕ 和 θ \theta θ )联合优化。从随机初始化开始,不断更新直到收敛。数据集上的目标是各个数据点 ELBO 之和:
L θ , ϕ ( D ) = ∑ x ∈ D L θ , ϕ ( x ) \begin{equation}
\mathcal{L}_{\theta, \phi}(\mathcal{D}) = \sum_{\mathbf{x} \in \mathcal{D}} \mathcal{L}_{\theta, \phi}(\mathbf{x})
\end{equation} L θ , ϕ ( D ) = x ∈ D ∑ L θ , ϕ ( x )
一般来说,单个数据点的 ELBO 及其梯度 ∇ θ , ϕ L θ , ϕ ( x ) \nabla_{\theta,\phi} \mathcal{L}_{\theta,\phi}(\mathbf{x}) ∇ θ , ϕ L θ , ϕ ( x ) 无法精确计算。但它们有好的无偏估计量 ∇ ~ θ , ϕ L θ , ϕ ( x ) \tilde{\nabla}_{\theta,\phi}\mathcal{L}_{\theta,\phi}(\mathbf{x}) ∇ ~ θ , ϕ L θ , ϕ ( x ) ,所以仍然可以做小批量 SGD。对生成模型参数 θ \theta θ ,无偏梯度很容易得到,因为期望的分布不依赖 θ \theta θ :
∇ θ L θ , ϕ ( x ) = ∇ θ E q ϕ ( z ∣ x ) [ log p θ ( x , z ) − log q ϕ ( z ∣ x ) ] = E q ϕ ( z ∣ x ) [ ∇ θ ( log p θ ( x , z ) − log q ϕ ( z ∣ x ) ) ] ≈ ∇ θ ( log p θ ( x , z ) − log q ϕ ( z ∣ x ) ) = ∇ θ log p θ ( x , z ) \begin{align}
\nabla_{\theta} \mathcal{L}_{\theta, \phi}(\mathbf{x}) &= \nabla_{\theta} \mathbb{E}_{q_{\phi}(\mathbf{z}|\mathbf{x})} [\log p_{\theta}(\mathbf{x}, \mathbf{z}) - \log q_{\phi}(\mathbf{z}|\mathbf{x})] \\
&= \mathbb{E}_{q_{\phi}(\mathbf{z}|\mathbf{x})} [\nabla_{\theta}(\log p_{\theta}(\mathbf{x}, \mathbf{z}) - \log q_{\phi}(\mathbf{z}|\mathbf{x}))] \\
&\approx \nabla_{\theta}(\log p_{\theta}(\mathbf{x}, \mathbf{z}) - \log q_{\phi}(\mathbf{z}|\mathbf{x})) \\
&= \nabla_{\theta}\log p_{\theta}(\mathbf{x}, \mathbf{z})
\end{align} ∇ θ L θ , ϕ ( x ) = ∇ θ E q ϕ ( z ∣ x ) [ log p θ ( x , z ) − log q ϕ ( z ∣ x )] = E q ϕ ( z ∣ x ) [ ∇ θ ( log p θ ( x , z ) − log q ϕ ( z ∣ x ))] ≈ ∇ θ ( log p θ ( x , z ) − log q ϕ ( z ∣ x )) = ∇ θ log p θ ( x , z )
倒数第二行是用一个样本 z ∼ q ϕ ( z ∣ x ) \mathbf{z} \sim q_\phi(\mathbf{z}|\mathbf{x}) z ∼ q ϕ ( z ∣ x ) 做的蒙特卡罗估计。对变分参数 ϕ \phi ϕ 就没这么简单了:期望本身是在 q ϕ q_\phi q ϕ 下取的,依赖 ϕ \phi ϕ ,不能直接把梯度移进期望里。这正是重参数化技巧要解决的问题。
Reparameterization Trick
对连续潜变量,并且编码器和生成模型都可微时,可以通过变量替换直接对 ELBO 关于 ϕ \phi ϕ 和 θ \theta θ 求导,这称为重参数化技巧。
Change of variables
首先,把随机变量 z ∼ q ϕ ( z ∣ x ) \mathbf{z} \sim q_{\phi}(\mathbf{z}|\mathbf{x}) z ∼ q ϕ ( z ∣ x ) 写成另一个随机变量 ϵ \epsilon ϵ 在给定 x \mathbf{x} x 和 ϕ \phi ϕ 下的可微(可逆)变换:
z = g ( ϵ , ϕ , x ) \begin{equation}
\mathbf{z} = g(\epsilon, \phi, \mathbf{x})
\end{equation} z = g ( ϵ , ϕ , x )
其中 ϵ \epsilon ϵ 的分布与 x \mathbf{x} x 和 ϕ \phi ϕ 都无关。这样,随机性全部来自 ϵ \epsilon ϵ ,而 z \mathbf{z} z 对 ϕ \phi ϕ 是可微的,梯度可以穿过采样过程反向传播。
Algorithm 1 : ELBO 的随机优化。噪声既来自小批量采样,也来自 p ( ϵ ) p(\epsilon) p ( ϵ ) 的采样,因此这是一个双重随机的优化过程。这个算法也称为自编码变分贝叶斯(Auto-Encoding Variational Bayes, AEVB)。
Data:
D \mathcal{D} D : Dataset
q ϕ ( z ∣ x ) q_\phi(\mathbf{z}|\mathbf{x}) q ϕ ( z ∣ x ) : Inference model
p θ ( x , z ) p_\theta(\mathbf{x},\mathbf{z}) p θ ( x , z ) : Generative model
Result:
θ , ϕ \theta, \phi θ , ϕ : Learned parameters
( θ , ϕ ) ← (\theta, \phi) \leftarrow ( θ , ϕ ) ← Initialize parameters
while SGD not converged do
M ∼ D \mathcal{M} \sim \mathcal{D} M ∼ D (Random minibatch of data)
ϵ ∼ p ( ϵ ) \epsilon \sim p(\epsilon) ϵ ∼ p ( ϵ ) (Random noise for every datapoint in M \mathcal{M} M )
Compute L ~ θ , ϕ ( M , ϵ ) \tilde{\mathcal{L}}_{\theta,\phi}(\mathcal{M},\epsilon) L ~ θ , ϕ ( M , ϵ ) and its gradients ∇ θ , ϕ L ~ θ , ϕ ( M , ϵ ) \nabla_{\theta,\phi} \tilde{\mathcal{L}}_{\theta,\phi}(\mathcal{M},\epsilon) ∇ θ , ϕ L ~ θ , ϕ ( M , ϵ )
Update θ \theta θ and ϕ \phi ϕ using SGD optimizer
end
Gradient of expectation under change of variable
做了变量替换后,期望可以改写成关于 ϵ \epsilon ϵ 的期望:
E q ϕ ( z ∣ x ) [ f ( z ) ] = E p ( ϵ ) [ f ( z ) ] \begin{equation}
\mathbb{E}_{q_{\phi}(\mathbf{z}|\mathbf{x})} [f(\mathbf{z})] = \mathbb{E}_{p(\epsilon)} [f(\mathbf{z})]
\end{equation} E q ϕ ( z ∣ x ) [ f ( z )] = E p ( ϵ ) [ f ( z )]
其中 z = g ( ϵ , ϕ , x ) \mathbf{z}=g(\epsilon,\phi,\mathbf{x}) z = g ( ϵ , ϕ , x ) 。现在期望的分布 p ( ϵ ) p(\epsilon) p ( ϵ ) 不依赖 ϕ \phi ϕ ,梯度和期望可以交换,再用从 p ( ϵ ) p(\epsilon) p ( ϵ ) 采样的单个 ϵ \epsilon ϵ 做蒙特卡罗估计:
∇ ϕ E q ϕ ( z ∣ x ) [ f ( z ) ] = ∇ ϕ E p ( ϵ ) [ f ( z ) ] = E p ( ϵ ) [ ∇ ϕ f ( z ) ] ≈ ∇ ϕ f ( z ) \begin{align}
\nabla_{\phi} \mathbb{E}_{q_{\phi}(\mathbf{z}|\mathbf{x})} [f(\mathbf{z})] &= \nabla_{\phi} \mathbb{E}_{p(\epsilon)} [f(\mathbf{z})] \\
&= \mathbb{E}_{p(\epsilon)} [\nabla_{\phi} f(\mathbf{z})] \\
&\approx \nabla_{\phi} f(\mathbf{z})
\end{align} ∇ ϕ E q ϕ ( z ∣ x ) [ f ( z )] = ∇ ϕ E p ( ϵ ) [ f ( z )] = E p ( ϵ ) [ ∇ ϕ f ( z )] ≈ ∇ ϕ f ( z )
上图对比了原始形式和重参数化形式。
左图:原始形式。 灰色节点是确定性节点(如目标函数 f f f ),蓝色节点是随机节点(如潜变量 z \mathbf{z} z )。z \mathbf{z} z 从 q ϕ ( z ∣ x ) q_\phi(\mathbf{z}|\mathbf{x}) q ϕ ( z ∣ x ) 中采样,f f f 依赖 z \mathbf{z} z 。我们想对 ϕ \phi ϕ 求梯度,但梯度无法穿过"采样"这一步反向传播。
右图:重参数化形式。 随机性被移到噪声 ϵ \epsilon ϵ 上,ϵ \epsilon ϵ 从简单分布 p ( ϵ ) p(\epsilon) p ( ϵ ) 中采样,z = g ( ϕ , x , ϵ ) \mathbf{z} = g(\phi, \mathbf{x}, \epsilon) z = g ( ϕ , x , ϵ ) 变成确定性节点。由于 z \mathbf{z} z 是 ϕ \phi ϕ 的可微函数,梯度可以沿 f → z → ϕ f \to \mathbf{z} \to \phi f → z → ϕ 反向传播。
总结一下,重参数化分三步:
变量替换:把 z \mathbf{z} z 写成噪声、参数和输入的可微函数 z = g ( ϵ , ϕ , x ) \mathbf{z} = g(\epsilon, \phi, \mathbf{x}) z = g ( ϵ , ϕ , x ) 。
改写期望:E q ϕ ( z ∣ x ) [ f ( z ) ] = E p ( ϵ ) [ f ( z ) ] \mathbb{E}_{q_\phi(\mathbf{z}|\mathbf{x})} [f(\mathbf{z})] = \mathbb{E}_{p(\epsilon)} [f(\mathbf{z})] E q ϕ ( z ∣ x ) [ f ( z )] = E p ( ϵ ) [ f ( z )] 。
计算梯度:交换梯度与期望,用蒙特卡罗采样估计。
这样就能用普通的反向传播计算梯度,而且得到的梯度估计方差通常比其他无偏估计(如 score function 估计)低得多。
Gradient of ELBO
用重参数化,把关于 q ϕ ( z ∣ x ) q_\phi(\mathbf{z}|\mathbf{x}) q ϕ ( z ∣ x ) 的期望换成关于 p ( ϵ ) p(\epsilon) p ( ϵ ) 的期望,ELBO 可以写成:
L θ , ϕ ( x ) = E q ϕ ( z ∣ x ) [ log p θ ( x , z ) − log q ϕ ( z ∣ x ) ] = E p ( ϵ ) [ log p θ ( x , z ) − log q ϕ ( z ∣ x ) ] \begin{align}
\mathcal{L}_{\theta, \phi}(\mathbf{x}) &= \mathbb{E}_{q_{\phi}(\mathbf{z}|\mathbf{x})} [\log p_{\theta}(\mathbf{x}, \mathbf{z}) - \log q_{\phi}(\mathbf{z}|\mathbf{x})] \\
&= \mathbb{E}_{p(\epsilon)} [\log p_{\theta}(\mathbf{x}, \mathbf{z}) - \log q_{\phi}(\mathbf{z}|\mathbf{x})]
\end{align} L θ , ϕ ( x ) = E q ϕ ( z ∣ x ) [ log p θ ( x , z ) − log q ϕ ( z ∣ x )] = E p ( ϵ ) [ log p θ ( x , z ) − log q ϕ ( z ∣ x )]
于是可以用单个噪声样本构造单个数据点 ELBO 的蒙特卡罗估计 L ~ θ , ϕ ( x ) \tilde{\mathcal{L}}_{\theta, \phi}(\mathbf{x}) L ~ θ , ϕ ( x ) :
ϵ ∼ p ( ϵ ) z = g ( ϕ , x , ϵ ) L ~ θ , ϕ ( x ) = log p θ ( x , z ) − log q ϕ ( z ∣ x ) \begin{align}
\epsilon &\sim p(\epsilon) \\
\mathbf{z} &= g(\phi, \mathbf{x}, \epsilon) \\
\tilde{\mathcal{L}}_{\theta, \phi}(\mathbf{x}) &= \log p_{\theta}(\mathbf{x}, \mathbf{z}) - \log q_{\phi}(\mathbf{z}|\mathbf{x})
\end{align} ϵ z L ~ θ , ϕ ( x ) ∼ p ( ϵ ) = g ( ϕ , x , ϵ ) = log p θ ( x , z ) − log q ϕ ( z ∣ x )
这一串运算可以在 PyTorch、TensorFlow 等框架里写成计算图,对 θ \theta θ 和 ϕ \phi ϕ 自动求导。
这个算法最初称为 AEVB 算法。更一般地,重参数化的 ELBO 估计称为随机梯度变分贝叶斯(SGVB)估计,它也可以用来估计模型参数的后验。
Computation of log q ϕ ( z ∣ x ) \log q_\phi(\mathbf{z}|\mathbf{x}) log q ϕ ( z ∣ x )
计算 ELBO 估计时,需要在给定 x \mathbf{x} x 和 z \mathbf{z} z (或者等价地 ϵ \epsilon ϵ )时计算 log q ϕ ( z ∣ x ) \log q_\phi(\mathbf{z}|\mathbf{x}) log q ϕ ( z ∣ x ) 。只要变换 g g g 选得合适,这一步很简单。
噪声分布 p ( ϵ ) p(\epsilon) p ( ϵ ) 是我们自己选的,密度已知。只要 g g g 可逆,ϵ \epsilon ϵ 和 z \mathbf{z} z 的密度有如下关系(推导见下一小节):
log q ϕ ( z ∣ x ) = log p ( ϵ ) − log d ϕ ( x , ϵ ) \begin{equation}
\log q_{\phi}(\mathbf{z}|\mathbf{x}) = \log p(\epsilon) - \log d_{\phi}(\mathbf{x}, \epsilon)
\end{equation} log q ϕ ( z ∣ x ) = log p ( ϵ ) − log d ϕ ( x , ϵ )
第二项是雅可比矩阵 ∂ z ∂ ϵ \frac{\partial \mathbf{z}}{\partial \epsilon} ∂ ϵ ∂ z 行列式绝对值的对数:
log d ϕ ( x , ϵ ) = log ∣ det ( ∂ z ∂ ϵ ) ∣ \begin{equation}
\log d_{\phi}(\mathbf{x}, \epsilon) = \log \left| \det\left(\frac{\partial \mathbf{z}}{\partial \epsilon}\right) \right|
\end{equation} log d ϕ ( x , ϵ ) = log det ( ∂ ϵ ∂ z )
∂ z ∂ ϵ = ∂ ( z 1 , . . . , z k ) ∂ ( ϵ 1 , . . . , ϵ k ) = ( ∂ z 1 ∂ ϵ 1 ⋯ ∂ z 1 ∂ ϵ k ⋮ ⋱ ⋮ ∂ z k ∂ ϵ 1 ⋯ ∂ z k ∂ ϵ k ) \begin{equation}
\frac{\partial \mathbf{z}}{\partial \epsilon} = \frac{\partial(z_1, ..., z_k)}{\partial(\epsilon_1, ..., \epsilon_k)} = \begin{pmatrix} \frac{\partial z_1}{\partial \epsilon_1} & \cdots & \frac{\partial z_1}{\partial \epsilon_k} \\ \vdots & \ddots & \vdots \\ \frac{\partial z_k}{\partial \epsilon_1} & \cdots & \frac{\partial z_k}{\partial \epsilon_k} \end{pmatrix}
\end{equation} ∂ ϵ ∂ z = ∂ ( ϵ 1 , ... , ϵ k ) ∂ ( z 1 , ... , z k ) = ∂ ϵ 1 ∂ z 1 ⋮ ∂ ϵ 1 ∂ z k ⋯ ⋱ ⋯ ∂ ϵ k ∂ z 1 ⋮ ∂ ϵ k ∂ z k
推导
设 z = g ( ϵ , ϕ , x ) \mathbf{z} = g(\epsilon, \phi, \mathbf{x}) z = g ( ϵ , ϕ , x ) ,g g g 可微且可逆。由概率密度的变量替换公式:
q ϕ ( z ∣ x ) = p ( ϵ ) ∣ det ( ∂ ϵ ∂ z ) ∣ \begin{equation}
q_{\phi}(\mathbf{z}|\mathbf{x}) = p(\epsilon) \left| \det\left(\frac{\partial \epsilon}{\partial \mathbf{z}}\right) \right|
\end{equation} q ϕ ( z ∣ x ) = p ( ϵ ) det ( ∂ z ∂ ϵ )
我们通常知道的是 ∂ z ∂ ϵ \frac{\partial \mathbf{z}}{\partial \epsilon} ∂ ϵ ∂ z ,而不是 ∂ ϵ ∂ z \frac{\partial \epsilon}{\partial \mathbf{z}} ∂ z ∂ ϵ 。利用逆映射的雅可比行列式互为倒数:
∣ det ( ∂ ϵ ∂ z ) ∣ = 1 ∣ det ( ∂ z ∂ ϵ ) ∣ \begin{equation}
\left| \det\left(\frac{\partial \epsilon}{\partial \mathbf{z}}\right) \right| = \frac{1}{\left| \det\left(\frac{\partial \mathbf{z}}{\partial \epsilon}\right) \right|}
\end{equation} det ( ∂ z ∂ ϵ ) = det ( ∂ ϵ ∂ z ) 1
代入得到:
q ϕ ( z ∣ x ) = p ( ϵ ) ∣ det ( ∂ z ∂ ϵ ) ∣ − 1 \begin{equation}
q_{\phi}(\mathbf{z}|\mathbf{x}) = p(\epsilon) \left| \det\left(\frac{\partial \mathbf{z}}{\partial \epsilon}\right) \right|^{-1}
\end{equation} q ϕ ( z ∣ x ) = p ( ϵ ) det ( ∂ ϵ ∂ z ) − 1
取对数就是上面的公式。
Factorized Gaussian posteriors
常见的选择是因子化高斯编码器:
q ϕ ( z ∣ x ) = N ( z ; μ , diag ( σ 2 ) ) \begin{equation}
q_{\phi}(\mathbf{z}|\mathbf{x}) = \mathcal{N}(\mathbf{z}; \mu, \text{diag}(\sigma^2))
\end{equation} q ϕ ( z ∣ x ) = N ( z ; μ , diag ( σ 2 ))
其中 μ \mu μ 和 log σ \log\sigma log σ 由编码器网络给出:
( μ , log σ ) = EncoderNeuralNet ϕ ( x ) \begin{equation}
(\mu, \log\sigma) = \text{EncoderNeuralNet}_{\phi}(\mathbf{x})
\end{equation} ( μ , log σ ) = EncoderNeuralNet ϕ ( x )
后验分解为每个潜变量各自的高斯分布的乘积:
q ϕ ( z ∣ x ) = ∏ i q ϕ ( z i ∣ x ) = ∏ i N ( z i ; μ i , σ i 2 ) \begin{equation}
q_{\phi}(\mathbf{z}|\mathbf{x}) = \prod_i q_{\phi}(z_i|\mathbf{x}) = \prod_i \mathcal{N}(z_i; \mu_i, \sigma_i^2)
\end{equation} q ϕ ( z ∣ x ) = i ∏ q ϕ ( z i ∣ x ) = i ∏ N ( z i ; μ i , σ i 2 )
重参数化时,把 z \mathbf{z} z 写成标准正态噪声 ϵ \epsilon ϵ 的线性变换:
ϵ ∼ N ( 0 , I ) \begin{equation}
\epsilon \sim \mathcal{N}(0, \mathbf{I})
\end{equation} ϵ ∼ N ( 0 , I )
z = μ + σ ⊙ ϵ \begin{equation}
\mathbf{z} = \mu + \sigma \odot \epsilon
\end{equation} z = μ + σ ⊙ ϵ
从 ϵ \epsilon ϵ 到 z \mathbf{z} z 的雅可比矩阵是对角矩阵,对角元为 σ \sigma σ :
∂ z ∂ ϵ = diag ( σ ) \begin{equation}
\frac{\partial \mathbf{z}}{\partial \epsilon} = \text{diag}(\sigma)
\end{equation} ∂ ϵ ∂ z = diag ( σ )
对角矩阵的行列式是对角元之积,所以对数行列式是对角元的对数之和:
log d ϕ ( x , ϵ ) = log ∣ det ( ∂ z ∂ ϵ ) ∣ = ∑ i log σ i \begin{equation}
\log d_{\phi}(\mathbf{x}, \epsilon) = \log \left| \det\left(\frac{\partial \mathbf{z}}{\partial \epsilon}\right) \right| = \sum_i \log \sigma_i
\end{equation} log d ϕ ( x , ϵ ) = log det ( ∂ ϵ ∂ z ) = i ∑ log σ i
于是后验对数密度为:
log q ϕ ( z ∣ x ) = log p ( ϵ ) − log d ϕ ( x , ϵ ) = ∑ i ( log N ( ϵ i ; 0 , 1 ) − log σ i ) \begin{align}
\log q_{\phi}(\mathbf{z}|\mathbf{x}) &= \log p(\epsilon) - \log d_{\phi}(\mathbf{x}, \epsilon) \\
&= \sum_i \left( \log \mathcal{N}(\epsilon_i; 0, 1) - \log \sigma_i \right)
\end{align} log q ϕ ( z ∣ x ) = log p ( ϵ ) − log d ϕ ( x , ϵ ) = i ∑ ( log N ( ϵ i ; 0 , 1 ) − log σ i )
Full-covariance Gaussian posterior
因子化高斯可以推广到全协方差高斯:
q ϕ ( z ∣ x ) = N ( z ; μ , Σ ) \begin{equation}
q_{\phi}(\mathbf{z}|\mathbf{x}) = \mathcal{N}(\mathbf{z}; \mu, \Sigma)
\end{equation} q ϕ ( z ∣ x ) = N ( z ; μ , Σ )
对应的重参数化为:
ϵ ∼ N ( 0 , I ) z = μ + L ϵ \begin{align}
\epsilon &\sim \mathcal{N}(0, \mathbf{I}) \\
\mathbf{z} &= \mu + \mathbf{L}\epsilon
\end{align} ϵ z ∼ N ( 0 , I ) = μ + L ϵ
L \mathbf{L} L 是对角元非零的下(或上)三角矩阵,非对角元刻画了 z \mathbf{z} z 各分量之间的相关性。这样构造的 z \mathbf{z} z 协方差正是 Σ \Sigma Σ :
Σ = E [ ( z − μ ) ( z − μ ) T ] = E [ L ϵ ϵ T L T ] = L E [ ϵ ϵ T ] L T = L L T \begin{equation}
\Sigma = \mathbb{E}[(\mathbf{z} - \mu)(\mathbf{z} - \mu)^T] = \mathbb{E}[\mathbf{L}\epsilon \epsilon^T \mathbf{L}^T] = \mathbf{L}\,\mathbb{E}[\epsilon \epsilon^T]\,\mathbf{L}^T = \mathbf{L}\mathbf{L}^T
\end{equation} Σ = E [( z − μ ) ( z − μ ) T ] = E [ L ϵ ϵ T L T ] = L E [ ϵ ϵ T ] L T = L L T
这里用到了 E [ ϵ ϵ T ] = I \mathbb{E}[\epsilon \epsilon^T] = \mathbf{I} E [ ϵ ϵ T ] = I 。也就是说,L \mathbf{L} L 是 Σ \Sigma Σ 的 Cholesky 因子。雅可比矩阵很简单:
∂ z ∂ ϵ = L \begin{equation}
\frac{\partial \mathbf{z}}{\partial \epsilon} = \mathbf{L}
\end{equation} ∂ ϵ ∂ z = L
三角矩阵的行列式等于对角元之积,所以:
log ∣ det ( ∂ z ∂ ϵ ) ∣ = ∑ i log ∣ L i i ∣ \begin{equation}
\log \left| \det\left(\frac{\partial \mathbf{z}}{\partial \epsilon}\right) \right| = \sum_i \log|L_{ii}|
\end{equation} log det ( ∂ ϵ ∂ z ) = i ∑ log ∣ L ii ∣
后验对数密度为:
log q ϕ ( z ∣ x ) = log p ( ϵ ) − ∑ i log ∣ L i i ∣ \begin{equation}
\log q_{\phi}(\mathbf{z}|\mathbf{x}) = \log p(\epsilon) - \sum_i \log|L_{ii}|
\end{equation} log q ϕ ( z ∣ x ) = log p ( ϵ ) − i ∑ log ∣ L ii ∣
实践中,可以让编码器同时输出 μ \mu μ 、log σ \log\sigma log σ 和一个矩阵 L ′ \mathbf{L}' L ′ :
( μ , log σ , L ′ ) ← EncoderNeuralNet ϕ ( x ) \begin{equation}
(\mu, \log\sigma, \mathbf{L}') \leftarrow \text{EncoderNeuralNet}_{\phi}(\mathbf{x})
\end{equation} ( μ , log σ , L ′ ) ← EncoderNeuralNet ϕ ( x )
再这样构造 L \mathbf{L} L :
L ← L mask ⊙ L ′ + diag ( σ ) \begin{equation}
\mathbf{L} \leftarrow \mathbf{L}_{\text{mask}} \odot \mathbf{L}' + \text{diag}(\sigma)
\end{equation} L ← L mask ⊙ L ′ + diag ( σ )
L mask \mathbf{L}_{\text{mask}} L mask 是掩码矩阵,对角线以下的元素为 1,对角线及以上为 0。这样 L \mathbf{L} L 一定是下三角矩阵,对角元就是 σ \sigma σ ,因此对数行列式和因子化情形一样:
log ∣ det ( ∂ z ∂ ϵ ) ∣ = ∑ i log σ i \begin{equation}
\log \left| \det\left(\frac{\partial \mathbf{z}}{\partial \epsilon}\right) \right| = \sum_i \log \sigma_i
\end{equation} log det ( ∂ ϵ ∂ z ) = i ∑ log σ i
Algorithm 2 : 计算单个数据点 ELBO 的无偏估计。例子中推断模型是全协方差高斯,生成模型是因子化伯努利分布。L mask \mathbf{L}_{\text{mask}} L mask 是掩码矩阵,对角线及以上为 0,对角线以下为 1。
Data:
x \mathbf{x} x : a datapoint, and optionally other conditioning information
ϵ \epsilon ϵ : a random sample from p ( ϵ ) = N ( 0 , I ) p(\epsilon) = \mathcal{N}(0,\mathbf{I}) p ( ϵ ) = N ( 0 , I )
θ \theta θ : Generative model parameters
ϕ \phi ϕ : Inference model parameters
q ϕ ( z ∣ x ) q_\phi(\mathbf{z}|\mathbf{x}) q ϕ ( z ∣ x ) : Inference model
p θ ( x , z ) p_\theta(\mathbf{x},\mathbf{z}) p θ ( x , z ) : Generative model
Result:
L ~ \tilde{\mathcal{L}} L ~ : 单个数据点 ELBO L θ , ϕ ( x ) \mathcal{L}_{\theta,\phi}(\mathbf{x}) L θ , ϕ ( x ) 的无偏估计
( μ , log σ , L ′ ) ← EncoderNeuralNet ϕ ( x ) (\mu, \log\sigma, \mathbf{L}') \leftarrow \text{EncoderNeuralNet}_\phi(\mathbf{x}) ( μ , log σ , L ′ ) ← EncoderNeuralNet ϕ ( x )
L ← L mask ⊙ L ′ + diag ( σ ) \mathbf{L} \leftarrow \mathbf{L}_{\text{mask}} \odot \mathbf{L}' + \text{diag}(\sigma) L ← L mask ⊙ L ′ + diag ( σ )
ϵ ∼ N ( 0 , I ) \epsilon \sim \mathcal{N}(0,\mathbf{I}) ϵ ∼ N ( 0 , I )
z ← L ϵ + μ \mathbf{z} \leftarrow \mathbf{L}\epsilon + \mu z ← L ϵ + μ
L ~ logqz ← − ∑ i ( 1 2 ( ϵ i 2 + log ( 2 π ) ) + log σ i ) \tilde{\mathcal{L}}_{\text{logqz}} \leftarrow -\sum_i \left(\frac{1}{2}(\epsilon_i^2 + \log(2\pi)) + \log\sigma_i\right) L ~ logqz ← − ∑ i ( 2 1 ( ϵ i 2 + log ( 2 π )) + log σ i ) (即 log q ϕ ( z ∣ x ) \log q_\phi(\mathbf{z}|\mathbf{x}) log q ϕ ( z ∣ x ) )
L ~ logpz ← − ∑ i 1 2 ( z i 2 + log ( 2 π ) ) \tilde{\mathcal{L}}_{\text{logpz}} \leftarrow -\sum_i \frac{1}{2}(z_i^2 + \log(2\pi)) L ~ logpz ← − ∑ i 2 1 ( z i 2 + log ( 2 π )) (即 log p θ ( z ) \log p_\theta(\mathbf{z}) log p θ ( z ) )
p ← DecoderNeuralNet θ ( z ) \mathbf{p} \leftarrow \text{DecoderNeuralNet}_\theta(\mathbf{z}) p ← DecoderNeuralNet θ ( z )
L ~ logpx ← ∑ i ( x i log p i + ( 1 − x i ) log ( 1 − p i ) ) \tilde{\mathcal{L}}_{\text{logpx}} \leftarrow \sum_i \left(x_i \log p_i + (1 - x_i) \log(1 - p_i)\right) L ~ logpx ← ∑ i ( x i log p i + ( 1 − x i ) log ( 1 − p i ) ) (即 log p θ ( x ∣ z ) \log p_\theta(\mathbf{x}|\mathbf{z}) log p θ ( x ∣ z ) )
L ~ = L ~ logpx + L ~ logpz − L ~ logqz \tilde{\mathcal{L}} = \tilde{\mathcal{L}}_{\text{logpx}} + \tilde{\mathcal{L}}_{\text{logpz}} - \tilde{\mathcal{L}}_{\text{logqz}} L ~ = L ~ logpx + L ~ logpz − L ~ logqz
Estimation of the Marginal Likelihood
训练好 VAE 之后,可以用采样估计模型赋予数据的概率。单个数据点的边缘似然可以写成:
log p θ ( x ) = log E q ϕ ( z ∣ x ) [ p θ ( x , z ) q ϕ ( z ∣ x ) ] \begin{equation}
\log p_{\theta}(\mathbf{x}) = \log \mathbb{E}_{q_{\phi}(\mathbf{z}|\mathbf{x})} \left[ \frac{p_{\theta}(\mathbf{x}, \mathbf{z})}{q_{\phi}(\mathbf{z}|\mathbf{x})} \right]
\end{equation} log p θ ( x ) = log E q ϕ ( z ∣ x ) [ q ϕ ( z ∣ x ) p θ ( x , z ) ]
从 q ϕ ( z ∣ x ) q_\phi(\mathbf{z}|\mathbf{x}) q ϕ ( z ∣ x ) 采样 L L L 次,得到蒙特卡罗估计:
log p θ ( x ) ≈ log ( 1 L ∑ l = 1 L p θ ( x , z ( l ) ) q ϕ ( z ( l ) ∣ x ) ) \begin{equation}
\log p_{\theta}(\mathbf{x}) \approx \log \left( \frac{1}{L} \sum_{l=1}^{L} \frac{p_{\theta}(\mathbf{x}, \mathbf{z}^{(l)})}{q_{\phi}(\mathbf{z}^{(l)}|\mathbf{x})} \right)
\end{equation} log p θ ( x ) ≈ log ( L 1 l = 1 ∑ L q ϕ ( z ( l ) ∣ x ) p θ ( x , z ( l ) ) )
每个 z ( l ) ∼ q ϕ ( z ∣ x ) \mathbf{z}^{(l)} \sim q_{\phi}(\mathbf{z}|\mathbf{x}) z ( l ) ∼ q ϕ ( z ∣ x ) 是从推断模型中独立采样的。对每个样本计算重要性权重 p θ ( x , z ( l ) ) q ϕ ( z ( l ) ∣ x ) \frac{p_{\theta}(\mathbf{x}, \mathbf{z}^{(l)})}{q_{\phi}(\mathbf{z}^{(l)}|\mathbf{x})} q ϕ ( z ( l ) ∣ x ) p θ ( x , z ( l ) ) ,取平均后再取对数。L L L 越大,估计越接近真实的边缘似然。L = 1 L = 1 L = 1 时,这个估计的期望恰好是 ELBO。
Marginal Likelihood and ELBO as KL Divergences
边缘似然很难直接计算,优化 ELBO 是间接地优化它。提高 ELBO 的紧致程度,也就是缩小 ELBO 和真实边缘似然之间的差距,一个办法是提高生成模型的灵活性。这一点可以从 ELBO 和 KL 散度的关系看出来。
最大似然准则。 对大小为 N D N_\mathcal{D} N D 的 i.i.d. 数据集 D \mathcal{D} D ,按数据点平均的对数似然为:
1 N D log p θ ( D ) = 1 N D ∑ x ∈ D log p θ ( x ) = E q D ( x ) [ log p θ ( x ) ] \begin{align}
\frac{1}{N_{\mathcal{D}}} \log p_{\theta}(\mathcal{D}) &= \frac{1}{N_{\mathcal{D}}} \sum_{\mathbf{x} \in \mathcal{D}} \log p_{\theta}(\mathbf{x}) \\
&= \mathbb{E}_{q_{\mathcal{D}}(\mathbf{x})}[\log p_{\theta}(\mathbf{x})]
\end{align} N D 1 log p θ ( D ) = N D 1 x ∈ D ∑ log p θ ( x ) = E q D ( x ) [ log p θ ( x )]
其中 q D ( x ) q_\mathcal{D}(\mathbf{x}) q D ( x ) 是经验数据分布:
q D ( x ) = 1 N ∑ i = 1 N q D ( i ) ( x ) \begin{equation}
q_{\mathcal{D}}(\mathbf{x}) = \frac{1}{N} \sum_{i=1}^{N} q_{\mathcal{D}}^{(i)}(\mathbf{x})
\end{equation} q D ( x ) = N 1 i = 1 ∑ N q D ( i ) ( x )
对连续数据,每个分量 q D ( i ) ( x ) q_\mathcal{D}^{(i)}(\mathbf{x}) q D ( i ) ( x ) 通常是以 x ( i ) \mathbf{x}^{(i)} x ( i ) 为中心的 Dirac delta 分布;对离散数据,则是把全部概率放在 x ( i ) \mathbf{x}^{(i)} x ( i ) 上的离散分布。
数据分布和模型分布之间的 KL 散度可以写成:
D K L ( q D ( x ) ∣ ∣ p θ ( x ) ) = − E q D ( x ) [ log p θ ( x ) ] + E q D ( x ) [ log q D ( x ) ] = − 1 N D log p θ ( D ) + const \begin{align}
D_{KL}(q_{\mathcal{D}}(\mathbf{x}) || p_{\theta}(\mathbf{x})) &= -\mathbb{E}_{q_{\mathcal{D}}(\mathbf{x})}[\log p_{\theta}(\mathbf{x})] + \mathbb{E}_{q_{\mathcal{D}}(\mathbf{x})}[\log q_{\mathcal{D}}(\mathbf{x})] \\
&= -\frac{1}{N_{\mathcal{D}}} \log p_{\theta}(\mathcal{D}) + \text{const}
\end{align} D K L ( q D ( x ) ∣∣ p θ ( x )) = − E q D ( x ) [ log p θ ( x )] + E q D ( x ) [ log q D ( x )] = − N D 1 log p θ ( D ) + const
常数是 − H ( q D ( x ) ) -\mathcal{H}(q_{\mathcal{D}}(\mathbf{x})) − H ( q D ( x )) ,与 θ \theta θ 无关。所以最小化这个 KL 散度等价于最大化数据的对数似然。
ELBO 准则。 把经验数据分布和推断模型组合起来,得到 x \mathbf{x} x 和 z \mathbf{z} z 的联合分布:
q D , ϕ ( x , z ) = q D ( x ) q ϕ ( z ∣ x ) \begin{equation}
q_{\mathcal{D}, \phi}(\mathbf{x}, \mathbf{z}) = q_{\mathcal{D}}(\mathbf{x}) q_{\phi}(\mathbf{z}|\mathbf{x})
\end{equation} q D , ϕ ( x , z ) = q D ( x ) q ϕ ( z ∣ x )
它与 p θ ( x , z ) p_\theta(\mathbf{x},\mathbf{z}) p θ ( x , z ) 之间的 KL 散度,等于负的 ELBO 加一个常数:
D K L ( q D , ϕ ( x , z ) ∥ p θ ( x , z ) ) = − E q D ( x ) [ E q ϕ ( z ∣ x ) [ log p θ ( x , z ) − log q ϕ ( z ∣ x ) ] ] + E q D ( x ) [ log q D ( x ) ] = − 1 N D L θ , ϕ ( D ) + const \begin{align}
& D_{KL}\!\left(q_{\mathcal{D}, \phi}(\mathbf{x}, \mathbf{z}) \,\|\, p_{\theta}(\mathbf{x}, \mathbf{z})\right) \nonumber \\
&\qquad = - \mathbb{E}_{q_{\mathcal{D}}(\mathbf{x})}\!\left[ \mathbb{E}_{q_{\phi}(\mathbf{z}|\mathbf{x})}\!\left[ \log p_{\theta}(\mathbf{x}, \mathbf{z}) - \log q_{\phi}(\mathbf{z}|\mathbf{x}) \right] \right] + \mathbb{E}_{q_{\mathcal{D}}(\mathbf{x})}\!\left[ \log q_{\mathcal{D}}(\mathbf{x}) \right] \\
&\qquad = - \frac{1}{N_{\mathcal{D}}} \mathcal{L}_{\theta, \phi}(\mathcal{D}) + \text{const}
\end{align} D K L ( q D , ϕ ( x , z ) ∥ p θ ( x , z ) ) = − E q D ( x ) [ E q ϕ ( z ∣ x ) [ log p θ ( x , z ) − log q ϕ ( z ∣ x ) ] ] + E q D ( x ) [ log q D ( x ) ] = − N D 1 L θ , ϕ ( D ) + const
常数同样是 − H ( q D ( x ) ) -\mathcal{H}(q_{\mathcal{D}}(\mathbf{x})) − H ( q D ( x )) 。
两个准则之间的关系可以总结为:
D K L ( q D , ϕ ( x , z ) ∣ ∣ p θ ( x , z ) ) = D K L ( q D ( x ) ∣ ∣ p θ ( x ) ) + E q D ( x ) [ D K L ( q ϕ ( z ∣ x ) ∣ ∣ p θ ( z ∣ x ) ) ] ≥ D K L ( q D ( x ) ∣ ∣ p θ ( x ) ) \begin{align}
D_{KL}(q_{\mathcal{D}, \phi}(\mathbf{x}, \mathbf{z}) \,||\, p_{\theta}(\mathbf{x}, \mathbf{z})) &= D_{KL}(q_{\mathcal{D}}(\mathbf{x}) \,||\, p_{\theta}(\mathbf{x})) \nonumber \\
&\quad + \mathbb{E}_{q_{\mathcal{D}}(\mathbf{x})}\!\left[ D_{KL}(q_{\phi}(\mathbf{z}|\mathbf{x}) \,||\, p_{\theta}(\mathbf{z}|\mathbf{x})) \right] \\
&\ge D_{KL}(q_{\mathcal{D}}(\mathbf{x}) \,||\, p_{\theta}(\mathbf{x}))
\end{align} D K L ( q D , ϕ ( x , z ) ∣∣ p θ ( x , z )) = D K L ( q D ( x ) ∣∣ p θ ( x )) + E q D ( x ) [ D K L ( q ϕ ( z ∣ x ) ∣∣ p θ ( z ∣ x )) ] ≥ D K L ( q D ( x ) ∣∣ p θ ( x ))
所以,ELBO 可以看作一个扩展空间上的最大似然目标。对固定的编码器 q ϕ ( z ∣ x ) q_\phi(\mathbf{z}|\mathbf{x}) q ϕ ( z ∣ x ) ,q D , ϕ ( x , z ) q_{\mathcal{D},\phi}(\mathbf{x},\mathbf{z}) q D , ϕ ( x , z ) 可以看作一个扩展的经验分布:每个数据点 x \mathbf{x} x 都附带了一个随机的辅助特征 z \mathbf{z} z 。最大化 ELBO 就是让生成模型 p θ ( x , z ) p_\theta(\mathbf{x},\mathbf{z}) p θ ( x , z ) 去拟合这个扩展分布。
Challenges
Optimization issues
直接优化原始的 ELBO,随机优化可能陷入一个不理想的稳定平衡点。训练初期,似然项 log p ( x ∣ z ) \log p(\mathbf{x}|\mathbf{z}) log p ( x ∣ z ) 提供的信号比较弱,于是最先达到的状态是 q ( z ∣ x ) ≈ p ( z ) q(\mathbf{z}|\mathbf{x}) \approx p(\mathbf{z}) q ( z ∣ x ) ≈ p ( z ) ,即编码器忽略输入、潜变量不携带信息,而这个状态很难摆脱。这通常称为后验坍缩(posterior collapse)。
一个解决办法是对潜变量的代价 D KL ( q ( z ∣ x ) ∣ ∣ p ( z ) ) D_{\text{KL}}(q(\mathbf{z}|\mathbf{x}) || p(\mathbf{z})) D KL ( q ( z ∣ x ) ∣∣ p ( z )) 做退火:它的权重在若干个 epoch 内从 0 慢慢升到 1。
另一个办法是 free bits:把潜变量维度分成 K K K 组,在小批量目标中保证每一组用到的信息量(KL 项)不少于一个阈值 λ \lambda λ ,低于阈值的部分不再惩罚。这样编码器不会把某些潜变量完全关掉。
ML 与 ELBO 的区别
最大似然目标可以看作最小化 D K L ( q D ( x ) ∣ ∣ p θ ( x ) ) D_{KL}(q_{\mathcal{D}}(\mathbf{x}) || p_{\theta}(\mathbf{x})) D K L ( q D ( x ) ∣∣ p θ ( x )) ,其中 q D ( x ) q_\mathcal{D}(\mathbf{x}) q D ( x ) 是数据分布,p θ ( x ) p_\theta(\mathbf{x}) p θ ( x ) 是模型分布。
ELBO 目标可以看作最小化 D K L ( q D , ϕ ( x , z ) ∣ ∣ p θ ( x , z ) ) D_{KL}(q_{\mathcal{D}, \phi}(\mathbf{x}, \mathbf{z}) || p_{\theta}(\mathbf{x}, \mathbf{z})) D K L ( q D , ϕ ( x , z ) ∣∣ p θ ( x , z )) ,其中 q D , ϕ ( x , z ) = q D ( x ) q ϕ ( z ∣ x ) q_{\mathcal{D},\phi}(\mathbf{x},\mathbf{z}) = q_\mathcal{D}(\mathbf{x})q_\phi(\mathbf{z}|\mathbf{x}) q D , ϕ ( x , z ) = q D ( x ) q ϕ ( z ∣ x ) 。
如果模型无法完美拟合,由于 KL 散度的方向,p θ ( x , z ) p_\theta(\mathbf{x},\mathbf{z}) p θ ( x , z ) 的方差通常会比 q D , ϕ ( x , z ) q_{\mathcal{D},\phi}(\mathbf{x},\mathbf{z}) q D , ϕ ( x , z ) 的方差大。
左图:编码过程。 数据 x \mathbf{x} x 服从数据分布 q D ( x ) q_\mathcal{D}(\mathbf{x}) q D ( x ) ,经过编码器 q ϕ ( z ∣ x ) q_\phi(\mathbf{z}|\mathbf{x}) q ϕ ( z ∣ x ) 映射到潜空间,在潜空间中边缘化得到 q ϕ ( z ) q_\phi(\mathbf{z}) q ϕ ( z ) 。
右图:解码过程。 潜变量从先验 p θ ( z ) p_\theta(\mathbf{z}) p θ ( z ) 出发,通常取标准正态分布,经过解码器 p θ ( x ∣ z ) p_\theta(\mathbf{x}|\mathbf{z}) p θ ( x ∣ z ) 映射回数据空间,在数据空间中边缘化得到模型分布 p θ ( x ) p_\theta(\mathbf{x}) p θ ( x ) 。
Blurriness of generative model
上一节说过,优化 ELBO 相当于最小化 D KL ( q D , ϕ ( x , z ) ∣ ∣ p θ ( x , z ) ) D_{\text{KL}}(q_{\mathcal{D},\phi}(\mathbf{x}, \mathbf{z})||p_{\theta}(\mathbf{x}, \mathbf{z})) D KL ( q D , ϕ ( x , z ) ∣∣ p θ ( x , z )) 。如果 q D , ϕ ( x , z ) q_{\mathcal{D},\phi}(\mathbf{x}, \mathbf{z}) q D , ϕ ( x , z ) 和 p θ ( x , z ) p_\theta(\mathbf{x}, \mathbf{z}) p θ ( x , z ) 不能完美匹配,那么 p θ ( x , z ) p_\theta(\mathbf{x}, \mathbf{z}) p θ ( x , z ) 和 p θ ( x ) p_\theta(\mathbf{x}) p θ ( x ) 的方差最终会比 q D , ϕ ( x , z ) q_{\mathcal{D},\phi}(\mathbf{x}, \mathbf{z}) q D , ϕ ( x , z ) 和数据分布 q D ( x ) q_{\mathcal{D}}(\mathbf{x}) q D ( x ) 的方差大。原因在于 KL 散度的方向。
如果某个 ( x , z ) (\mathbf{x},\mathbf{z}) ( x , z ) 在 q D , ϕ q_{\mathcal{D},\phi} q D , ϕ 下有概率,而在 p θ p_\theta p θ 下概率几乎为零,那么 E q D , ϕ ( x , z ) [ log p θ ( x , z ) ] \mathbb{E}_{q_{\mathcal{D},\phi}(\mathbf{x}, \mathbf{z})} [\log p_{\theta}(\mathbf{x}, \mathbf{z})] E q D , ϕ ( x , z ) [ log p θ ( x , z )] 会趋于负无穷,惩罚非常大。反过来却不成立:生成模型在 q D , ϕ q_{\mathcal{D},\phi} q D , ϕ 没有支撑的区域放一些概率,只会受到很轻的惩罚。所以模型倾向于把概率铺得更开,生成的样本看起来就比较模糊。
因此,选择足够灵活的推断模型和(或)生成模型,可以缓解这种模糊。