Deriving the VAE ELBO and explaining reparameterization
The ELBO arises by applying Jensen’s inequality to rewrite the marginal log-likelihood as a tractable lower bound involving the approximate posterior, with ELBO = E_{q} [log p(x|z)] minus the KL divergence between q and the prior. The reparameterization trick rewrites sampling z from q_φ as a deterministic function of φ and noise ε, enabling gradients to flow through the stochastic layer by isolating randomness outside the parameter-dependent transformation, thus allowing efficient low-variance gradient estimates via backpropagation.
A variational auto–encoder couples a latent‑variable model
[
p_\theta(x,z) ;=; p_\theta(x\mid z),p(z)
]
with an encoder (q_\phi(z\mid x)) that is meant to approximate the true posterior (p_\theta(z\mid x)). The quantity you would like to maximise is the marginal likelihood of an observed (x),
[
\log p_\theta(x);=;\log\int p_\theta(x\mid z)p(z),\mathrm{d}z,
]
but the integral is intractable for all but the simplest choices of likelihood. The standard variational derivation introduces (q_\phi(z\mid x)) and either uses Jensen's inequality or the non–negativity of a KL divergence to obtain a tractable lower bound.
One way to see it is to write the KL between the approximate and true posteriors,
[
\mathrm{KL}\bigl(q_\phi(z\mid x),|,p_\theta(z\mid x)\bigr)
= \int q_\phi(z\mid x),\Bigl[\log q_\phi(z\mid x)-\log p_\theta(z\mid x)\Bigr],\mathrm{d}z;\geq 0.
]
Using Bayes' rule (p_\theta(z\mid x)=p_\theta(x,z)/p_\theta(x)) and rearranging gives
[
\log p_\theta(x)
= \underbrace{\int q_\phi(z\mid x),\bigl[\log p_\theta(x,z)-\log q_\phi(z\mid x)\bigr],\mathrm{d}z}_{\mathcal{L}(\theta,\phi;x)}
- \mathrm{KL}\bigl(q_\phi(z\mid x),|,p_\theta(z\mid x)\bigr),
]
so (\mathcal{L}(\theta,\phi;x)) is a lower bound on (\log p_\theta(x)) because the KL term is non–negative. Expanding (\log p_\theta(x,z)=\log p_\theta(x\mid z)+\log p(z)) yields the usual VAE evidence lower bound (ELBO)
[
\mathcal{L}(\theta,\phi;x)
= \mathbb{E}{q\phi(z\mid x)}\bigl[\log p_\theta(x\mid z)\bigr]
- \mathrm{KL}\bigl(q_\phi(z\mid x),|,p(z)\bigr),
]
which is the objective maximised in practice.
The expectation over (q_\phi(z\mid x)) is taken with respect to a distribution that itself depends on (\phi). To optimise (\mathcal{L}) by stochastic gradient ascent we approximate the expectation with samples (z^{(l)}\sim q_\phi(z\mid x)). Naïvely differentiating through the sampling step fails because the “draw (z) from (q_\phi(\cdot\mid x))” operation is not a differentiable map of (\phi).
The reparameterization trick fixes this by expressing a sample from (q_\phi) as a deterministic transformation of a parameter–free noise source. For a diagonal Gaussian encoder this is
[
z \sim q_\phi(z\mid x)=\mathcal{N}\bigl(\mu_\phi(x),\sigma_\phi^2(x)\bigr)
\qquad\Longleftrightarrow\qquad
z = \mu_\phi(x) + \sigma_\phi(x)\odot \varepsilon,;;\varepsilon\sim\mathcal{N}(0,I),
]
or, more generally, (z=g_\phi(x,\varepsilon)) with (\varepsilon) drawn from a fixed, simple distribution. With this change of variables the expectation becomes
[
\mathbb{E}{q\phi(z\mid x)}[,\cdot,] = \mathbb{E}{\varepsilon\sim\mathcal{N}(0,I)}\bigl[,\cdot,\bigl|{z=g_\phi(x,\varepsilon)}\bigr],
]
and the dependence on (\phi) is now through the differentiable function (g_\phi). This “pushes the randomness to (\varepsilon)” and allows gradients of the Monte Carlo estimate of the ELBO to be computed by ordinary back‑propagation through (g_\phi) and the decoder, yielding low–variance gradient estimates. Without such a reparameterization one would have to resort to score–function estimators such as REINFORCE to differentiate through the sampling step.