Variational Autoencoder (VAE)


2026-08-29

最开始的文章是 (Kingma & Welling, 2014)

苏神博客亦有记载:(苏剑林, 2018a, 2018b)

其他参考资料:(Grover & Ermon, 2018)

给定样本 \(x_i\),后验分布 \(p_{\phi}(z\mid x_i)\sim \mathcal{N}(\mu_i, \sigma^2_i I)\)。其中我们希望 \(p_\phi(z)\sim\mathcal{N}(0, I)\)

构建网络 \(\left<\mu_k, \log \sigma_k^2\right>=\phi(x_i)\)

如果酱紫直接训的话,因为取正态分布的时候会带来噪声,所以模型肯定是希望 \(\sigma^2_k\) 越小越好,逐渐的会退化为普通的 autoencoder。

所以我们希望 \(p_\phi(z)\sim\mathcal{N}(0, I)\),这样子加上一个

\[ \begin{aligned} \mathcal{L}_{\mu, \sigma^2} &= D_\mathrm{KL}\left(\mathcal{N}(\mu, \sigma^2)\Vert\mathcal{N}(0, 1)\right)\\ &=\frac{1}{2}(-\log\sigma^2+\mu^2+\sigma^2-1) \end{aligned} \]

就行。

然后写代码的时候有个 reparameterization trick,其实就是如果我们算出 \(\mu_i\)\(\sigma_i^2\) 然后采样,那么是不可导的。方法其实就是 \(\mathcal{N}(\mu, \sigma^2 I)\sim \sigma\times\mathcal{N}(0, I)+\mu\) 就行。

References

Grover, A., & Ermon, S. (2018). Variational Autoencoders. Stanford CS236: Deep Generative Models Lecture Notes. deepgenerativemodels.github.io
Kingma, D. P., & Welling, M. (2014). Auto-Encoding Variational Bayes. International Conference on Learning Representations. doi.org
苏剑林. (2018a, March). 变分自编码器(一):原来是这么一回事. spaces.ac.cn
苏剑林. (2018b, March). 变分自编码器(二):从贝叶斯观点出发. spaces.ac.cn

Cite this post

@misc{pu2026mlrevisitvae,
  author = {Pu, Fanyi},
  title  = {Variational Autoencoder (VAE)},
  year   = {2026},
  month  = {8},
  url    = {https://pufanyi.com/blog/ml-revisit-vae}
}