Chapter 17: Variational autoencoders

Generative Models · v1.0.16

2026-11-10 01:06:03

Where we are

Where this fits

  • Preceding units: a map \(x \mapsto y\) from input to prediction
  • This unit: the density \(Pr(x)\) itself
  • Assumed: marginalisation, and the Kullback-Leibler divergence \(D_{KL}[q \,\|\, p] \ge 0\), with equality exactly when \(q = p\)

Outline of key topics

  1. Latent variable models — a density as the marginal of a joint
  2. The nonlinear latent variable model — a decoder supplies the conditional mean
  3. Maximum likelihood and the ELBO — Jensen’s inequality and its consequence
  4. The variational approximation — an encoder, amortised over the data
  5. The variational autoencoder — objective, reparameterization, architecture

Latent variable models

Discriminative and generative models

Discriminative

\[Pr(y \mid x)\]

  • \(x\) is given
  • one conditional per input

Generative

\[Pr(x)\]

  • no input is given
  • the model must synthesise \(x\)

The generative model must support sampling \(x^* \sim Pr(x)\).

The data manifold

Data occupy a manifold of dimension \(d \ll D_x\). Its coordinates are taken as the latent variable \(z\).

Definition: latent variable model

A joint density over the data \(x\) and an unobserved \(z\), with \(x\) recovered by marginalisation:

\[Pr(x) = \int Pr(x, z)\, dz = \int \underbrace{Pr(x|z)}_{\text{likelihood}}\; \underbrace{Pr(z)}_{\text{prior}}\, dz \tag{17.1-17.2}\]

Tractable \(Pr(x|z)\) and \(Pr(z)\) define an expressive \(Pr(x)\).

Example 1: mixture of Gaussians

\(z\) discrete on \(\{1, \dots, N\}\), so the integral is a finite sum:

\[ Pr(z=n) = \lambda_n, \qquad Pr(x|z=n) = \text{Norm}_x\!\left[\mu_n,\ \sigma_n^2\right] \tag{17.3} \]

\[ \begin{aligned} Pr(x) &= \sum_{n=1}^{N} \class{hg-key}{Pr(x|z=n)} \cdot \class{hg-alt}{Pr(z=n)} \\[2pt] &= \sum_{n=1}^{N} \class{hg-alt}{\lambda_n} \cdot \class{hg-key}{\text{Norm}_x\!\left[\mu_n, \sigma_n^2\right]} \end{aligned} \tag{17.4} \]

Blue: likelihood. Rust: prior. \(z\) indexes the component that generated \(x\).

The marginal of a mixture

A weighted sum of Gaussian components (dashed).

The same sum as the marginal of \(Pr(x,z)\) over discrete \(z\).

Ancestral sampling

Two draws, in order:

\[ \begin{aligned} \class{hg-alt}{z^*} &\sim \text{Cat}[\lambda_1, \dots, \lambda_N] \\[4pt] \class{hg-key}{x^*} &\sim \text{Norm}_x\!\left[\mu_{\class{hg-alt}{z^*}},\ \sigma_{\class{hg-alt}{z^*}}^2\right] \end{aligned} \]

  • the latent is drawn first, unconditionally
  • the second draw is conditioned on the first

The same two-stage procedure applies when \(z\) is continuous.

The nonlinear latent variable model

Definition: nonlinear latent variable model

\(z \in \mathbb{R}^{D_z}\) and \(x \in \mathbb{R}^{D_x}\) continuous, with \(D_z \ll D_x\):

\[Pr(z) = \text{Norm}_z[0, I] \tag{17.5}\]

\[Pr(x|z, \phi) = \text{Norm}_x\!\left[\ \class{hg-key}{f[z, \phi]},\ \ \sigma^2 I\ \right] \tag{17.6}\]

  • the prior is parameter-free
  • \(\class{hg-key}{f[z,\phi]}\) is a deep network: the decoder
  • the covariance is spherical and fixed

\(f[z,\phi]\) returns a conditional mean; \(x\) follows by adding noise of covariance \(\sigma^2 I\).

The decoder

Input \((B, D_z)\), output \((B, D_x)\). All nonlinearity resides in \(f\).

The marginal density

\[ \begin{aligned} Pr(x|\phi) &= \int \class{hg-key}{Pr(x|z, \phi)} \cdot \class{hg-alt}{Pr(z)}\, dz \\[2pt] &= \int \class{hg-key}{\text{Norm}_x\!\left[f[z, \phi], \sigma^2 I\right]} \cdot \class{hg-alt}{\text{Norm}_z[0, I]}\, dz \end{aligned} \tag{17.7} \]

17.7 is a continuous mixture of spherical Gaussians with weights \(Pr(z)\) and means \(f[z,\phi]\).

The density this induces

Left: the joint \(Pr(x,z|\phi)\). Right: its marginal over \(z\). Each slice at fixed \(z\) is spherical; their means trace a curve, and integration superposes them along it.

Generation

\[ \begin{aligned} \class{hg-alt}{z^*} &\sim \text{Norm}_z[0, I] \\[4pt] \class{hg-key}{x^*} &\sim \text{Norm}_x\!\left[f[\class{hg-alt}{z^*}, \phi],\ \sigma^2 I\right] \end{aligned} \]

Cost: one prior draw, one evaluation of \(f\), one Gaussian draw.

Repeating the pair recovers \(Pr(x|\phi)\).

Sampling requires no integral; evaluating \(Pr(x|\phi)\) requires the integral of 17.7.

Maximum likelihood and the ELBO

Maximum likelihood

Given observations \(\{x_i\}_{i=1}^{I}\) and known \(\sigma^2\):

\[\hat\phi = \operatorname*{argmax}_{\phi}\left[\sum_{i=1}^{I} \log\!\left[Pr(x_i|\phi)\right]\right] \tag{17.8}\]

\[Pr(x_i|\phi) = \int \text{Norm}_{x_i}\!\left[f[z, \phi],\ \sigma^2 I\right] \cdot \text{Norm}_z[0, I]\, dz \tag{17.9}\]

  • the integrand composes a Gaussian with a network: no closed form
  • quadrature over \(D_z\) dimensions is infeasible
  • the logarithm lies outside the integral

17.8 requires \(Pr(x_i|\phi)\), which 17.9 does not provide in closed form.

Proposition: Jensen’s inequality

For a concave \(g\), any density \(Pr(y)\) and any function \(h\),

\[g\!\left[\mathbb{E}[h[y]]\right] \ \ge\ \mathbb{E}\!\left[g[h[y]]\right].\]

Taking \(g = \log\),

\[\log\!\left[\int Pr(y)\, h[y]\, dy\right] \ \ge\ \int Pr(y) \log\!\left[h[y]\right] dy \tag{17.13}\]

A chord of a concave curve lies under it.

17.13 bounds \(\log\mathbb{E}[\cdot]\) below by \(\mathbb{E}[\log\cdot]\), placing the logarithm inside the integral.

Derivation of the ELBO

Introduce any density \(q(z|\theta)\) over the latents, then apply 17.13 with \(Pr(y) = q(z|\theta)\) and \(h[z] = Pr(x,z|\phi)/q(z|\theta)\):

\[ \begin{aligned} \log\!\left[Pr(x|\phi)\right] &= \log\!\left[\int \class{hg-key}{q(z|\theta)}\, \frac{Pr(x, z|\phi)}{\class{hg-key}{q(z|\theta)}}\, dz\right] \\[6pt] &\ge\ \int q(z|\theta)\, \class{hg-alt}{\log}\!\left[\frac{Pr(x, z|\phi)}{q(z|\theta)}\right] dz \ \equiv\ \text{ELBO}[\theta, \phi] \end{aligned} \tag{17.14-17.16} \]

The evidence lower bound. It is evaluated by sampling and maximised jointly over \(\theta\) and \(\phi\).

Proposition: the gap in the bound

Factoring \(Pr(x,z|\phi) = Pr(z|x,\phi)\,Pr(x|\phi)\) and using \(\int q(z|\theta)\, dz = 1\):

\[\text{ELBO}[\theta, \phi] = \class{hg-alt}{\log\!\left[Pr(x|\phi)\right]} - D_{KL}\!\left[q(z|\theta) \,\middle\|\, \class{hg-key}{Pr(z|x, \phi)}\right] \tag{17.17}\]

  • \(D_{KL} \ge 0\), hence \(\text{ELBO}[\theta,\phi] \le \log Pr(x|\phi)\)
  • it is zero exactly when \(q(z|\theta) = \class{hg-key}{Pr(z|x,\phi)}\), the posterior
  • optimising \(\theta\) tightens the bound; optimising \(\phi\) improves the model

Corollary: reconstruction minus KL

Factoring instead as \(Pr(x,z|\phi) = Pr(x|z,\phi)\,Pr(z)\):

\[ \text{ELBO}[\theta, \phi] = \underbrace{\int q(z|\theta) \log\!\left[\class{hg-key}{Pr(x|z, \phi)}\right] dz}_{\text{reconstruction}} \ -\ \underbrace{D_{KL}\!\left[q(z|\theta) \,\middle\|\, \class{hg-alt}{Pr(z)}\right]}_{\text{KL to the prior}} \tag{17.18} \]

  • 17.17 states how tight the bound is; 17.18 states what to compute
  • both terms of 17.18 are available: one by sampling, one in closed form

The VAE evaluates 17.18.

The variational approximation

The posterior

\[Pr(z|x, \phi) = \frac{Pr(x|z, \phi)\, Pr(z)}{\class{hg-alt}{Pr(x|\phi)}} \tag{17.19}\]

  • the numerator is computable
  • \(\class{hg-alt}{Pr(x|\phi)}\) is the integral of 17.9

The denominator of 17.19 is the integral of 17.9; the posterior is therefore unavailable.

Definition: the encoder

The posterior depends on \(x\), so the approximating family does too:

\[q(z|x, \theta) = \text{Norm}_z\!\left[\ \class{hg-key}{g_\mu[x, \theta]},\ \ \class{hg-key}{g_\Sigma[x, \theta]}\ \right], \qquad \Sigma \text{ diagonal} \tag{17.20}\]

  • \(\class{hg-key}{g[x,\theta]}\) is a second network: the encoder
  • one parameter set replaces a separate \((\mu_i, \Sigma_i)\) per example — amortisation
  • diagonal \(\Sigma\) makes both sampling and the KL term closed-form

Quality of the approximation

A unimodal posterior is matched closely.

A multi-modal posterior is not; the residual \(D_{KL}\) is the slack in the bound.

The variational autoencoder

The training objective

Substituting 17.20 into 17.18 and estimating the expectation with one sample \(z^* \sim q(z|x,\theta)\):

\[\text{ELBO}[\theta, \phi] \approx \log\!\left[Pr(x|\class{hg-key}{z^*}, \phi)\right] - D_{KL}\!\left[q(z|x, \theta) \,\middle\|\, Pr(z)\right] \tag{17.21-17.23}\]

\[D_{KL}\!\left[q(z|x, \theta) \,\middle\|\, Pr(z)\right] = \frac{1}{2}\left(\text{Tr}[\Sigma] + \mu^{T}\mu - D_z - \log\!\left[\det[\Sigma]\right]\right) \tag{17.24}\]

17.24 is exact. Sampling variance enters only through the reconstruction term.

The reparameterized estimator

A draw \(z^* \sim \text{Norm}_z[\mu, \Sigma]\) admits no derivative in \(\mu\) or \(\Sigma\). Drawing the noise separately gives the same distribution and an affine dependence:

\[z^* = \mu + \Sigma^{1/2}\class{hg-key}{\epsilon^*}, \qquad \class{hg-key}{\epsilon^*} \sim \text{Norm}_\epsilon[0, I] \tag{17.25}\]

  • \(\class{hg-key}{\epsilon^*}\) is an input on a parameter-free branch
  • \(\partial z^*/\partial\mu = I\) and \(\partial z^*/\partial\Sigma^{1/2} = \class{hg-key}{\epsilon^*}\)
  • without 17.25 the encoder parameters \(\theta\) receive no gradient

The architecture

Encoder \(g[x,\theta]\) predicts \(\mu, \Sigma\); \(\epsilon^*\) enters on its own branch; decoder \(f[z^*,\phi]\) predicts \(x\). The loss is the negative ELBO.

Algorithm

For each \(x\) in a mini-batch:

  1. \(\mu, \Sigma \leftarrow g[x, \theta]\)
  2. \(\epsilon^* \sim \text{Norm}_\epsilon[0, I]\), then \(z^* \leftarrow \mu + \Sigma^{1/2}\epsilon^*\)
  3. evaluate the loss, 17.23 with 17.24 substituted:

\[\mathcal{L}[\theta, \phi] = -\log\!\left[Pr(x|z^*, \phi)\right] + \frac{1}{2}\left(\text{Tr}[\Sigma] + \mu^{T}\mu - D_z - \log\!\left[\det[\Sigma]\right]\right)\]

  1. descend the gradient of \(\mathcal{L}\) in \(\theta\) and \(\phi\) with SGD or Adam

Naming: variational from the Gaussian approximation to the posterior, autoencoder from the path \(x \to z \to x\).

Optimisation

Changing \(\theta\) moves between the curves, tightening the bound; changing \(\phi\) moves along the current curve, raising the likelihood. A single gradient step updates both.

Generation from the trained model

The encoder is discarded; the generative model is 17.5–17.6:

\[ \begin{aligned} z^* &\sim \text{Norm}_z[0, I] \\[4pt] x^* &\sim \text{Norm}_x\!\left[f[z^*, \phi],\ \sigma^2 I\right] \end{aligned} \]

  • sample quality is limited by the spherical noise model and by the Gaussian prior and posterior
  • substituting the aggregated posterior \(\tfrac{1}{I}\sum_i q(z|x_i,\theta)\) for the prior improves it

Samples from a trained VAE

CELEBA. (b) decoder means \(f[z^*,\phi]\); (c) the spherical noise drawn from \(\sigma^2 I\); (a) their sum, the sampled \(x^*\); (d) a modern VAE with hierarchical priors and a specialised architecture.

Summary

Summary

  • A latent variable model expresses \(Pr(x)\) as the marginal of a joint over an unobserved \(z\)
  • Its marginal likelihood has no closed form; ancestral sampling does not require one
  • Jensen’s inequality yields the ELBO, valid for every \(q\)
  • The gap in the bound is \(D_{KL}[q \,\|\, Pr(z|x,\phi)]\); the computable form is reconstruction minus KL to the prior
  • An encoder amortises the variational approximation across the dataset
  • The reparameterization \(z^* = \mu + \Sigma^{1/2}\epsilon^*\) restores differentiability

Where this goes next

The next unit is Chapter 18: Diffusion models.

  • sample quality is limited by the Gaussian prior and posterior
  • a hierarchical prior has one latent generate another
  • diffusion models are hierarchical VAEs

Diffusion models retain the latent-variable formulation and the bound; the single-step \(q\) becomes multi-step.