Variational Autoencoders

Volume II, Chapter 9 — Part I. VAE objective from the ELBO, reparameterization trick proof, Gaussian KL closed form, posterior collapse, β-VAE, and connection to latent diffusion models.

Advanced

Table of Contents

  1. Learning Objectives
  2. Prerequisites
  3. Notation
  4. Core Intuition
  5. The Generative Model
  6. The ELBO for VAEs
  7. The Encoder and Amortized Inference
  8. Closed-Form KL Divergence
  9. The Reparameterization Trick
  10. The Complete VAE Objective
  11. Posterior Collapse
  12. β-VAE and Disentanglement
  13. VQ-VAE and Discrete Latents
  14. Connection to Diffusion Models
  15. Worked Examples
  16. Connection to the Broader Curriculum
  17. Common Pitfalls and Misconceptions
  18. Research Perspective
  19. Summary of Takeaways
  20. Exercises

Learning Objectives

After reading this chapter, you should be able to:

  1. Specify the VAE generative model pθ(xz)p(z)p_\theta(\mathbf{x} \mid \mathbf{z}) p(\mathbf{z}) and explain why pθ(x)p_\theta(\mathbf{x}) is intractable.
  2. Derive the VAE objective as the ELBO from Variational Inference.
  3. Prove the closed-form KL between diagonal Gaussian qϕq_\phi and standard normal prior.
  4. Apply the reparameterization trick and explain why it enables Backpropagation through stochastic nodes.
  5. Analyze posterior collapse and mitigation strategies.
  6. State the β\beta-VAE objective and VQ-VAE discrete latent formulation.

Prerequisites


Notation

  • x,z\mathbf{x}, \mathbf{z} — Observed data and latent variables
  • qϕ(zx)q_\phi(\mathbf{z} \mid \mathbf{x}) — Encoder (approximate posterior)
  • pθ(xz)p_\theta(\mathbf{x} \mid \mathbf{z}) — Decoder (likelihood)
  • p(z)p(\mathbf{z}) — Prior (typically N(0,I)\mathcal{N}(\mathbf{0}, \mathbf{I}))
  • LELBO\mathcal{L}_{\mathrm{ELBO}} — Variational lower bound

Core Intuition

A Variational Autoencoder (VAE) learns a generative model: sample latent code z\mathbf{z}, decode to data x\mathbf{x}. It simultaneously learns an encoder that maps x\mathbf{x} to a distribution over z\mathbf{z} — amortized approximate inference.

Training maximizes a lower bound on logpθ(x)\log p_\theta(\mathbf{x}) (the ELBO) because the true marginal is intractable. The VAE unites deep learning with Bayesian inference: neural networks parameterize both pθ(xz)p_\theta(\mathbf{x} \mid \mathbf{z}) (decoder) and qϕ(zx)q_\phi(\mathbf{z} \mid \mathbf{x}) (encoder).

VAEs underpin latent diffusion models (Stable Diffusion operates in a VAE latent space) and remain foundational for representation learning.

Series context. Chapter 9 (Generative Models), Volume II.

VAE Latent Space

z₁z₂Decoder
z₁
0.20
z₂
0.30
Latent pointEncoded data
Explore: Move z₁ and z₂ smoothly — the decoder output changes continuously. Nearby latent points produce similar reconstructions, enabling interpolation and generation.

The Generative Model

Definition 1 (VAE Generative Process).

zp(z)=N(0,Id),xpθ(xz).(1)\mathbf{z} \sim p(\mathbf{z}) = \mathcal{N}(\mathbf{0}, \mathbf{I}_d), \qquad \mathbf{x} \sim p_\theta(\mathbf{x} \mid \mathbf{z}). \tag{1}

Definition 2 (Marginal Likelihood).

pθ(x)=pθ(xz)p(z)dz.(2)p_\theta(\mathbf{x}) = \int p_\theta(\mathbf{x} \mid \mathbf{z})\, p(\mathbf{z})\, d\mathbf{z}. \tag{2}

Proposition 1. For neural network decoder pθp_\theta, integral (2) is intractable — no closed form for logpθ(x)\log p_\theta(\mathbf{x}) or its gradient.


The ELBO for VAEs

From Variational Inference, introduce qϕ(zx)q_\phi(\mathbf{z} \mid \mathbf{x}):

Theorem 1 (VAE ELBO).

logpθ(x)L(θ,ϕ;x)=Eqϕ(zx)[logpθ(xz)]DKL(qϕ(zx)p(z)).(3)\log p_\theta(\mathbf{x}) \geq \mathcal{L}(\theta, \phi; \mathbf{x}) = \mathbb{E}_{q_\phi(\mathbf{z} \mid \mathbf{x})}[\log p_\theta(\mathbf{x} \mid \mathbf{z})] - D_{\text{KL}}(q_\phi(\mathbf{z} \mid \mathbf{x}) \| p(\mathbf{z})). \tag{3}

Identity (ELBO Gap).

logpθ(x)=L(θ,ϕ;x)+DKL(qϕ(zx)pθ(zx)).(4)\log p_\theta(\mathbf{x}) = \mathcal{L}(\theta, \phi; \mathbf{x}) + D_{\text{KL}}(q_\phi(\mathbf{z} \mid \mathbf{x}) \| p_\theta(\mathbf{z} \mid \mathbf{x})). \tag{4}

Maximizing (3) minimizes the gap — tightest when qϕ=pθ(zx)q_\phi = p_\theta(\mathbf{z} \mid \mathbf{x}).


The Encoder and Amortized Inference

Definition 3 (Amortized Encoder).

qϕ(zx)=N(z;μϕ(x),diag(σϕ2(x))),(5)q_\phi(\mathbf{z} \mid \mathbf{x}) = \mathcal{N}(\mathbf{z}; \boldsymbol{\mu}_\phi(\mathbf{x}), \text{diag}(\boldsymbol{\sigma}_\phi^2(\mathbf{x}))), \tag{5}

where μϕ,logσϕ2\boldsymbol{\mu}_\phi, \log \boldsymbol{\sigma}_\phi^2 are neural network outputs.

Definition 4 (Amortization). One network qϕq_\phi serves all data points — vs. per-example variational optimization in classical VI.


Closed-Form KL Divergence

Theorem 2 (Gaussian KL). For q=N(μ,diag(σ2))q = \mathcal{N}(\boldsymbol{\mu}, \text{diag}(\boldsymbol{\sigma}^2)), p=N(0,I)p = \mathcal{N}(\mathbf{0}, \mathbf{I}):

DKL(qp)=12j=1d(σj2+μj21logσj2).(6)D_{\text{KL}}(q \| p) = \frac{1}{2}\sum_{j=1}^{d}\left(\sigma_j^2 + \mu_j^2 - 1 - \log \sigma_j^2\right). \tag{6}

Proof. Apply multivariate Gaussian KL formula with Σ1=diag(σ2)\boldsymbol{\Sigma}_1 = \text{diag}(\boldsymbol{\sigma}^2), Σ2=I\boldsymbol{\Sigma}_2 = \mathbf{I}, μ2=0\boldsymbol{\mu}_2 = \mathbf{0}:

DKL=12[tr(Σ1)+μTμdlogΣ1].(7)D_{\text{KL}} = \frac{1}{2}\left[\text{tr}(\boldsymbol{\Sigma}_1) + \boldsymbol{\mu}^T\boldsymbol{\mu} - d - \log|\boldsymbol{\Sigma}_1|\right]. \tag{7}

Since Σ1=jσj2|\boldsymbol{\Sigma}_1| = \prod_j \sigma_j^2: (6) follows. \blacksquare


The Reparameterization Trick

Problem: ϕEqϕ[f(z)]\nabla_\phi \mathbb{E}_{q_\phi}[f(\mathbf{z})] — distribution depends on ϕ\phi.

Theorem 3 (Reparameterization).

z=μϕ(x)+σϕ(x)ϵ,ϵN(0,I),(8)\mathbf{z} = \boldsymbol{\mu}_\phi(\mathbf{x}) + \boldsymbol{\sigma}_\phi(\mathbf{x}) \odot \boldsymbol{\epsilon}, \quad \boldsymbol{\epsilon} \sim \mathcal{N}(\mathbf{0}, \mathbf{I}), \tag{8} ϕEqϕ[f(z)]=Eϵ[ϕf(μϕ+σϕϵ)].(9)\nabla_\phi \mathbb{E}_{q_\phi}[f(\mathbf{z})] = \mathbb{E}_{\boldsymbol{\epsilon}}[\nabla_\phi f(\boldsymbol{\mu}_\phi + \boldsymbol{\sigma}_\phi \odot \boldsymbol{\epsilon})]. \tag{9}

Proof. See Variational Inference, Theorem 5. \blacksquare

Monte Carlo estimate with L=1L=1 sample enables Backpropagation.


The Complete VAE Objective

Definition 5 (VAE Loss).

LVAE=Eϵ[logpθ(xμϕ+σϕϵ)]+12j(σj2+μj21logσj2).(10)\mathcal{L}_{\text{VAE}} = -\mathbb{E}_{\boldsymbol{\epsilon}}[\log p_\theta(\mathbf{x} \mid \boldsymbol{\mu}_\phi + \boldsymbol{\sigma}_\phi \odot \boldsymbol{\epsilon})] + \frac{1}{2}\sum_j(\sigma_j^2 + \mu_j^2 - 1 - \log \sigma_j^2). \tag{10}

Decoder likelihoods:

  • Bernoulli (images): reconstruction = binary cross-entropy
  • Gaussian: reconstruction = MSE (equivalent to logp\log p up to constants)

Posterior Collapse

Definition 6 (Posterior Collapse). Encoder learns qϕ(zx)p(z)=N(0,I)q_\phi(\mathbf{z} \mid \mathbf{x}) \approx p(\mathbf{z}) = \mathcal{N}(\mathbf{0}, \mathbf{I}) — latents carry no information about x\mathbf{x}.

Cause: Powerful autoregressive decoder models p(x)p(\mathbf{x}) without z\mathbf{z}; KL term pushes qp(z)q \to p(\mathbf{z}).

Mitigations:

  • KL annealing: ramp β\beta from 0 to 1
  • Free bits: max(DKL,λ)\max(D_{\text{KL}}, \lambda)
  • β\beta-VAE with β<1\beta < 1

β-VAE and Disentanglement

Definition 7 (β\beta-VAE).

Lβ=Eq[logpθ(xz)]βDKL(qϕp(z)).(11)\mathcal{L}_{\beta} = \mathbb{E}_q[\log p_\theta(\mathbf{x} \mid \mathbf{z})] - \beta\, D_{\text{KL}}(q_\phi \| p(\mathbf{z})). \tag{11}
  • β=1\beta = 1: standard ELBO
  • β>1\beta > 1: stronger regularization, more disentangled latents
  • β<1\beta < 1: better reconstruction, risk of collapse

VQ-VAE and Discrete Latents

Definition 8 (Vector Quantization). Codebook {ek}k=1K\{\mathbf{e}_k\}_{k=1}^K. Encoder output ze\mathbf{z}_e quantized:

zq=ek,k=argminjzeej2.(12)\mathbf{z}_q = \mathbf{e}_k, \quad k = \arg\min_j \|\mathbf{z}_e - \mathbf{e}_j\|_2. \tag{12}

Loss:

LVQ=xx^2+sg[ze]ek2+βzesg[ek]2.(13)\mathcal{L}_{\text{VQ}} = \|\mathbf{x} - \hat{\mathbf{x}}\|^2 + \|\text{sg}[\mathbf{z}_e] - \mathbf{e}_k\|^2 + \beta\|\mathbf{z}_e - \text{sg}[\mathbf{e}_k]\|^2. \tag{13}

Foundation of latent diffusion (Forward Diffusion in VAE latent space).


Connection to Diffusion Models

Stable Diffusion: VQ-VAE encoder maps images to latent z\mathbf{z}; diffusion process operates on z\mathbf{z} rather than pixels — reducing dimensionality and compute. See Reverse Process.


Worked Examples

Example 1: KL for μ=0\boldsymbol{\mu} = \mathbf{0}, σ=1\boldsymbol{\sigma} = \mathbf{1}

DKL=0D_{\text{KL}} = 0 — encoder matches prior.

Example 2: Single-Dimensional

μ=2\mu = 2, σ2=0.5\sigma^2 = 0.5: DKL=12(0.5+41log0.5)2.1D_{\text{KL}} = \frac{1}{2}(0.5 + 4 - 1 - \log 0.5) \approx 2.1.


Connection to the Broader Curriculum


Common Pitfalls and Misconceptions

Pitfall 1: Confusing ELBO with true log-likelihood.

Pitfall 2: Ignoring posterior collapse with powerful decoders.

Pitfall 3: Using L=1L=1 Monte Carlo without noting variance.

Pitfall 4: β>1\beta > 1 invalidates ELBO as lower bound (still useful).


Research Perspective

VAE (Kingma & Welling, 2014; Rezende et al., 2014). β\beta-VAE (Higgins et al., 2017). VQ-VAE (van den Oord et al., 2017). Modern: hierarchical VAEs, NVAE, connection to diffusion and flow matching.


Summary of Takeaways

  • Encoder qϕ(zx)q_\phi(\mathbf{z} \mid \mathbf{x}) — Amortized posterior
  • Decoder pθ(xz)p_\theta(\mathbf{x} \mid \mathbf{z}) — Generative model
  • KL term (6) — Regularize toward prior
  • Reparameterization (8) — Differentiable sampling
  • ELBO gap (4) — Approximation quality

Next: Forward Diffusion


Exercises

Exercise 1. Derive (6) from scratch.

Exercise 2. Prove (9) for scalar zz.

Exercise 3. Explain posterior collapse with a autoregressive decoder.

Exercise 4. How does β\beta-VAE affect the ELBO as a valid bound?

Exercise 5. Derive VQ-VAE gradient through straight-through estimator.

Exercise 6. Compare VAE latent dimension to LoRA rank.

Exercise 7. When is L=1L=1 Monte Carlo sufficient?

Exercise 8. Connect VAE to Bayesian Linear Regression with linear decoder.