
Generative Models · v1.0.16
2026-11-10 01:06:03
Discriminative
\[Pr(y \mid x)\]
Generative
\[Pr(x)\]
The generative model must support sampling \(x^* \sim Pr(x)\).

Data occupy a manifold of dimension \(d \ll D_x\). Its coordinates are taken as the latent variable \(z\).
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)\).
\(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\).


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 same two-stage procedure applies when \(z\) is continuous.
\(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}\]
\(f[z,\phi]\) returns a conditional mean; \(x\) follows by adding noise of covariance \(\sigma^2 I\).

Input \((B, D_z)\), output \((B, D_x)\). All nonlinearity resides in \(f\).
\[ \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]\).

\[ \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.

Sampling requires no integral; evaluating \(Pr(x|\phi)\) requires the integral of 17.7.
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}\]
17.8 requires \(Pr(x_i|\phi)\), which 17.9 does not provide in closed form.
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}\]

17.13 bounds \(\log\mathbb{E}[\cdot]\) below by \(\mathbb{E}[\log\cdot]\), placing the logarithm inside the integral.
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\).
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}\]
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} \]
The VAE evaluates 17.18.
\[Pr(z|x, \phi) = \frac{Pr(x|z, \phi)\, Pr(z)}{\class{hg-alt}{Pr(x|\phi)}} \tag{17.19}\]
The denominator of 17.19 is the integral of 17.9; the posterior is therefore unavailable.
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}\]


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.
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}\]

For each \(x\) in a mini-batch:
\[\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)\]
Naming: variational from the Gaussian approximation to the posterior, autoencoder from the path \(x \to z \to x\).

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} \]

The next unit is Chapter 18: Diffusion models.
Diffusion models retain the latent-variable formulation and the bound; the single-step \(q\) becomes multi-step.