Latent variable models: hidden $\mathbf{z}$ models complex data; marginal $p(\mathbf{x}|\boldsymbol{\theta}) = \int p(\mathbf{x}, \mathbf{z}|\boldsymbol{\theta}) \, d\mathbf{z}$.
GMM (discrete latent): $p(\mathbf{x}|\boldsymbol{\theta}) = \sum_{k=1}^K \pi_k \, \mathcal{N}(\mathbf{x}|\boldsymbol{\mu}_k, \boldsymbol{\Sigma}_k)$ — tractable sum, but the log-of-sum blocks closed-form MLE.
EM algorithm — iterate when direct MLE is intractable:
We saw that EM works. Today: why does it work — and how does it generalize when the posterior becomes intractable?
Our goal is to maximize the log marginal likelihood:
The obstacle is again the log of a sum — it couples all components and prevents a closed form. The fix: build a lower bound we can optimize.
For a concave function $f$ (like $\log$) and any distribution $p$ over $z$:
Applied to the logarithm with a discrete distribution:
The log of a weighted average is at least the weighted average of the logs.
Introduce a variational distribution $q(\mathbf{z}|\mathbf{x})$ over the latents and apply Jensen:
This lower bound is the Evidence Lower Bound (ELBO):
The KL divergence measures how different two distributions are:
Rewrite the ELBO using the chain rule $p(\mathbf{x}, z|\boldsymbol{\theta}) = p(z|\mathbf{x}, \boldsymbol{\theta}) \cdot p(\mathbf{x}|\boldsymbol{\theta})$:
Rearranging gives the fundamental decomposition:
Since $D_{\text{KL}} \geq 0$, the ELBO is a lower bound on the evidence. The gap is the KL from $q$ to the true posterior — and it is zero (tight) exactly when $q = p(z|\mathbf{x}, \boldsymbol{\theta})$.
At iteration $t$ with parameters $\boldsymbol{\theta}^{(t)}$, maximize the ELBO w.r.t. $q$. Since $\log p(\mathbf{x}|\boldsymbol{\theta}^{(t)})$ is constant in $q$, this minimizes the KL:
For GMMs this is exactly the responsibility update:
After the E-step the bound is tight: $\log p(\mathbf{x}|\boldsymbol{\theta}^{(t)}) = \text{ELBO}(q^{(t+1)}, \boldsymbol{\theta}^{(t)})$.
Now fix $q = q^{(t+1)}$ and maximize the ELBO w.r.t. $\boldsymbol{\theta}$. The entropy term $-\mathbb{E}_q[\log q]$ is constant in $\boldsymbol{\theta}$, leaving the Q-function (expected complete-data log-likelihood):
Substituting the E-step responsibilities $\gamma_{ik}$ and the factorized GMM joint:
The log is now inside the sum over $k$ — each term involves only one component, so maximization is closed-form (the familiar weighted mean / covariance / mixing-weight updates). This raises the ELBO.
Chaining the two steps:
The log-likelihood is monotonically non-decreasing → EM converges to a local maximum. This E-then-M "tighten then raise" pattern is the template for the VAE.
The likelihood is not manufactured from nothing — EM is coordinate ascent on one bounded objective $F(q, \boldsymbol{\theta}) = \mathbb{E}_q[\log p(\mathbf{x}, \mathbf{z}|\boldsymbol{\theta})] - \mathbb{E}_q[\log q]$.
Each round's gain splits into two non-negative but bounded pieces (after the M-step, $q$ is stale, so the KL gap reopens):
$\log p(\mathbf{x}|\boldsymbol{\theta})$ has a ceiling (fixed dataset) → a monotone, bounded sequence must converge. Not a perpetuum mobile but a ball relaxing to the bottom of a landscape: it moves while the parameters still mis-fit the data, and stops at a local optimum. The energy was in the data all along.
GMM was tractable because the latent was discrete and the components Gaussian:
We want more expressive models:
This is the deep latent variable model — but what breaks?
The E-step needs the posterior $p(\mathbf{z}|\mathbf{x}, \boldsymbol{\theta})$:
The denominator is the problem:
With $\boldsymbol{\mu}_{\boldsymbol{\theta}}(\mathbf{z}) = \text{NeuralNet}(\mathbf{z})$ over a continuous $\mathbf{z}$, the marginal has no closed form — so we cannot compute the exact E-step.
Key idea: if we can't compute $p(\mathbf{z}|\mathbf{x}, \boldsymbol{\theta})$, learn to approximate it with an encoder network $q(\mathbf{z}|\mathbf{x}, \boldsymbol{\phi})$:
This is amortized inference: instead of solving a separate optimization for each $\mathbf{x}$ (as EM's E-step does), we amortize the cost across the dataset by sharing encoder parameters $\boldsymbol{\phi}$.
Same ELBO decomposition, $\log p(\mathbf{x}|\boldsymbol{\theta}) = \text{ELBO}(q, \boldsymbol{\theta}) + D_{\text{KL}}(q(\mathbf{z}|\mathbf{x}) \,\|\, p(\mathbf{z}|\mathbf{x}, \boldsymbol{\theta}))$, but:
Because we cannot make the bound tight, we no longer alternate E/M. Instead we optimize both $\boldsymbol{\theta}$ and $\boldsymbol{\phi}$ jointly by gradient ascent on the ELBO.
Start from the ELBO with the learned encoder and apply the chain rule $p(\mathbf{x}, \mathbf{z} | \boldsymbol{\theta}) = p(\mathbf{x}|\mathbf{z}, \boldsymbol{\theta}) \, p(\mathbf{z})$:
We maximize it jointly over both networks:
Reconstruction — how well the decoder rebuilds $\mathbf{x}$ from $\mathbf{z} \sim q$. Encourages the latent code to preserve information about $\mathbf{x}$.
Regularization — how close the encoder is to the prior $\mathcal{N}(\mathbf{0}, \mathbf{I})$. Keeps the latent space well-structured and prevents the encoder from collapsing each $\mathbf{x}$ to a spike.
Trade-off: reconstruction wants $q$ specific to each $\mathbf{x}$; regularization wants $q$ near the prior. Two obstacles remain: the reconstruction expectation is intractable, and it depends on $\boldsymbol{\phi}$ through sampling.
The reconstruction term is an intractable integral:
Monte Carlo: approximate the expectation with samples $\mathbf{z}^{(l)} \sim q(\mathbf{z}|\mathbf{x}, \boldsymbol{\phi})$:
Unbiased, variance $\propto 1/L$ (Law of Large Numbers). In practice $L=1$ suffices during training.
The gradient w.r.t. the decoder is straightforward:
The encoder gradient is the hard one — the samples themselves depend on $\boldsymbol{\phi}$:
Rewrite the sampling to separate randomness from parameters.
Before (non-differentiable):
After (differentiable):
The expectation becomes $\mathbb{E}_{\boldsymbol{\epsilon} \sim \mathcal{N}(\mathbf{0}, \mathbf{I})}\left[ f(\boldsymbol{\mu}_{\boldsymbol{\phi}} + \boldsymbol{\sigma}_{\boldsymbol{\phi}} \odot \boldsymbol{\epsilon}) \right]$ — now $\nabla_{\boldsymbol{\phi}}$ moves inside the expectation, and $L=1$ sample gives an unbiased gradient.
For $q(\mathbf{z}|\mathbf{x}, \boldsymbol{\phi}) = \mathcal{N}(\boldsymbol{\mu}, \text{diag}(\boldsymbol{\sigma}^2))$ and prior $p(\mathbf{z}) = \mathcal{N}(\mathbf{0}, \mathbf{I})$, the KL between two Gaussians has a closed form — no Monte Carlo needed:
where $j$ indexes the $d$ dimensions of the latent vector $\mathbf{z} \in \mathbb{R}^d$. Gradients w.r.t. $\boldsymbol{\phi}$ are direct.
So the two ELBO terms are handled differently: reconstruction via Monte Carlo + reparameterization, regularization via this closed form.
With a Gaussian decoder $p(\mathbf{x}|\mathbf{z}, \boldsymbol{\theta}) = \mathcal{N}(\mathbf{x} | \boldsymbol{\mu}_{\boldsymbol{\theta}}(\mathbf{z}), \sigma^2 \mathbf{I})$, take the log:
Since $\sigma^2$ is a fixed constant:
Maximizing the Gaussian log-likelihood is minimizing mean squared error — exactly the MLE → loss link from Lecture 1, now applied to the decoder ($\hat{\mathbf{x}} = \boldsymbol{\mu}_{\boldsymbol{\theta}}(\mathbf{z})$).
Substituting the Gaussian decoder and negating the ELBO gives the loss:
The relation $\beta = 2\sigma^2$ shows $\beta$ implicitly sets the assumed decoder variance — larger $\beta$ ⇒ noisier decoder.
VAE trades exactness for expressiveness: GMM gives exact inference with a limited model; the VAE gives approximate inference with a powerful neural-network model.
The through-line: EM's ELBO, generalized to intractable posteriors by learning the inference network.