Skip to main content
The VAE architecture introduces an inference network q(z∣x;ϕ)q(\mathbf z | \mathbf x ; \mathbf \phi) that approximates the intractable true posterior p(z∣x;θ)p(\mathbf z | \mathbf x ; \mathbf \theta). Training jointly the encoder parameters ϕ\mathbf \phi and the decoder parameters θ\mathbf \theta requires an objective that is tractable yet aligned with maximizing the marginal log-likelihood log⁡p(x;θ)\log p(\mathbf x ; \mathbf \theta). The Evidence Lower Bound (ELBO) is exactly that objective.

From KL divergence to the ELBO

During the treatment of entropy, we have met the concept of relative entropy or KL divergence that measures the “distance” between two distributions referenced on one of them. KL(q ∥ p)  =  Eq ⁣[log⁡q(x)−log⁡p(x)]  =  −∑xq(x)log⁡p(x)q(x)KL(q \,\|\, p) \;=\; \mathbb{E}_q\!\left[\log q(\mathbf x) - \log p(\mathbf x)\right] \;=\; -\sum_{\mathbf x} q(\mathbf x) \log \frac{p(\mathbf x)}{q(\mathbf x)} We will use KL divergence to obtain a suitable loss function that will be used in the optimization of this approximation via the DNNencDNN_{enc} network. Ultimately we are trying to minimize the KL divergence between the true posterior p(z∣x;θ)p(\mathbf z| \mathbf x ; \mathbf \theta) and the approximate posterior q(z∣x;ϕ)q(\mathbf z | \mathbf x ; \mathbf \phi): KL(q(z∣x;ϕ) ∥ p(z∣x;θ))  =  −∑zq(z∣x;ϕ)log⁡p(z∣x;θ)q(z∣x;ϕ)KL(q(\mathbf z | \mathbf x ; \mathbf \phi) \,\|\, p(\mathbf z | \mathbf x; \mathbf \theta)) \;=\; -\sum_{\mathbf z} q(\mathbf z | \mathbf x ; \mathbf \phi) \log \frac{p(\mathbf z | \mathbf x; \mathbf \theta)}{q(\mathbf z | \mathbf x ; \mathbf \phi)} Applying Bayes’ rule to replace the posterior p(z∣x;θ)=p(z,x;θ)p(x;θ)p(\mathbf z | \mathbf x; \mathbf \theta) = \frac{p(\mathbf z, \mathbf x; \mathbf \theta)}{p(\mathbf x; \mathbf \theta)}: =−∑zq(z∣x;ϕ)log⁡p(z,x;θ)q(z∣x;ϕ) p(x;θ)= -\sum_{\mathbf z} q(\mathbf z | \mathbf x ; \mathbf \phi) \log \frac{p(\mathbf z , \mathbf x; \mathbf \theta)}{q(\mathbf z | \mathbf x ; \mathbf \phi) \, p(\mathbf x; \mathbf \theta)} Separating the log of the product: =−∑zq(z∣x;ϕ)[log⁡p(z,x;θ)q(z∣x;ϕ)−log⁡p(x;θ)]= -\sum_{\mathbf z} q(\mathbf z | \mathbf x ; \mathbf \phi) \left[ \log \frac{p(\mathbf z , \mathbf x; \mathbf \theta)}{q(\mathbf z | \mathbf x ; \mathbf \phi)} - \log p(\mathbf x; \mathbf \theta) \right] Distributing the sum: =−∑zq(z∣x;ϕ)log⁡p(z,x;θ)q(z∣x;ϕ)+∑zq(z∣x;ϕ)log⁡p(x;θ)= -\sum_{\mathbf z} q(\mathbf z | \mathbf x ; \mathbf \phi) \log \frac{p(\mathbf z , \mathbf x; \mathbf \theta)}{q(\mathbf z | \mathbf x ; \mathbf \phi)} + \sum_{\mathbf z} q(\mathbf z | \mathbf x ; \mathbf \phi) \log p(\mathbf x; \mathbf \theta) Since log⁡p(x;θ)\log p(\mathbf x; \mathbf \theta) does not depend on z\mathbf z, it can be pulled out of the sum. And since qq is a valid distribution, ∑zq(z∣x;ϕ)=1\sum_{\mathbf z} q(\mathbf z | \mathbf x ; \mathbf \phi) = 1: =−∑zq(z∣x;ϕ)log⁡p(z,x;θ)q(z∣x;ϕ)+log⁡p(x;θ)= -\sum_{\mathbf z} q(\mathbf z | \mathbf x ; \mathbf \phi) \log \frac{p(\mathbf z , \mathbf x; \mathbf \theta)}{q(\mathbf z | \mathbf x ; \mathbf \phi)} + \log p(\mathbf x; \mathbf \theta) Rearranging: ⇒log⁡p(x;θ)  =  KL ⁣(q(z∣x;ϕ) ∥ p(z∣x;θ))  +  ∑zq(z∣x;ϕ)log⁡p(z,x;θ)q(z∣x;ϕ)⏟L(ϕ,θ) = Evidence Lower Bound (ELBO)\Rightarrow \log p(\mathbf x; \mathbf \theta) \;=\; KL\!\left(q(\mathbf z | \mathbf x ; \mathbf \phi) \,\|\, p(\mathbf z | \mathbf x; \mathbf \theta)\right) \;+\; \underbrace{\sum_{\mathbf z} q(\mathbf z | \mathbf x ; \mathbf \phi) \log \frac{p(\mathbf z , \mathbf x; \mathbf \theta)}{q(\mathbf z | \mathbf x ; \mathbf \phi)}}_{\mathcal L(\phi, \theta) \,=\, \text{Evidence Lower Bound (ELBO)}} The bracketed quantity L(ϕ,θ)\mathcal L(\phi, \theta) is the Evidence Lower Bound (ELBO). It is a function of both the encoder parameters ϕ\phi (through qϕq_\phi) and the decoder parameters θ\theta (through the joint pθ(z,x)p_\theta(\mathbf z, \mathbf x)).

Why is it a lower bound?

The KL divergence is non-negative, KL ⁣(q(z∣x;ϕ) ∥ p(z∣x;θ))  ≥  0,KL\!\left(q(\mathbf z | \mathbf x ; \mathbf \phi) \,\|\, p(\mathbf z | \mathbf x; \mathbf \theta)\right) \;\ge\; 0, with equality if and only if q(z∣x;ϕ)=p(z∣x;θ)q(\mathbf z | \mathbf x ; \mathbf \phi) = p(\mathbf z | \mathbf x; \mathbf \theta) almost everywhere. Dropping the KL term from the equality above therefore turns it into an inequality: log⁡p(x;θ)  ≥  ∑zq(z∣x;ϕ)log⁡p(z,x;θ)q(z∣x;ϕ)⏟L(ϕ,θ)\log p(\mathbf x; \mathbf \theta) \;\ge\; \underbrace{\sum_{\mathbf z} q(\mathbf z | \mathbf x ; \mathbf \phi) \log \frac{p(\mathbf z , \mathbf x; \mathbf \theta)}{q(\mathbf z | \mathbf x ; \mathbf \phi)}}_{\mathcal L(\phi, \theta)} The KL is the gap between the bound and the true marginal log-likelihood. The closer the encoder approximates the true posterior, the tighter the bound.

A practical form: reconstruction minus KL

The joint p(z,x;θ)p(\mathbf z, \mathbf x; \mathbf \theta) factors as p(x∣z;θ) p(z)p(\mathbf x | \mathbf z ; \mathbf \theta)\, p(\mathbf z), where p(z)p(\mathbf z) is the (fixed, parameter-free) prior, typically N(0,I)\mathcal N(\mathbf 0, \mathbf I). Substituting and expanding the log: L(ϕ,θ)  =  Eqϕ(z∣x) ⁣[log⁡p(x∣z;θ)]⏟reconstruction term  −  KL ⁣(qϕ(z∣x) ∥ p(z))⏟regularizer\mathcal L(\phi, \theta) \;=\; \underbrace{\mathbb{E}_{q_\phi(\mathbf z | \mathbf x)}\!\left[\log p(\mathbf x | \mathbf z; \mathbf \theta)\right]}_{\text{reconstruction term}} \;-\; \underbrace{KL\!\left(q_\phi(\mathbf z | \mathbf x) \,\|\, p(\mathbf z)\right)}_{\text{regularizer}} This is the form actually computed in code:
  • The reconstruction term is the expected log-likelihood of the observed data x\mathbf x under the decoder when z\mathbf z is drawn from the encoder. Maximizing it pushes the decoder to put high probability on real data.
  • The KL regularizer pulls the encoder’s posterior toward the prior p(z)p(\mathbf z). This prevents the encoder from collapsing into a different point distribution for every datapoint and is what makes the latent space dense and continuous.
Note carefully which KL is which: this regularizer uses the prior p(z)p(\mathbf z), not the true posterior p(z∣x;θ)p(\mathbf z | \mathbf x; \mathbf \theta). The KL against the true posterior is the (unobservable) bound gap from the previous section; the KL against the prior is part of the loss you actually optimize.

Why is the ELBO useful for optimization?

Re-arrange the identity to put the ELBO on one side: L(ϕ,θ)  =  log⁡p(x;θ)  −  KL ⁣(q(z∣x;ϕ) ∥ p(z∣x;θ))  ≤  log⁡p(x;θ).\mathcal L(\phi, \theta) \;=\; \log p(\mathbf x; \mathbf \theta) \;-\; KL\!\left(q(\mathbf z | \mathbf x ; \mathbf \phi) \,\|\, p(\mathbf z | \mathbf x; \mathbf \theta)\right) \;\le\; \log p(\mathbf x; \mathbf \theta). Joint SGD over (ϕ,θ)(\phi, \theta) on the ELBO does two coupled jobs at once:
  • Encoder step (gradients on ϕ\phi). With θ\theta fixed, log⁡p(x;θ)\log p(\mathbf x; \mathbf \theta) is constant in ϕ\phi, so maximizing L\mathcal L over ϕ\phi is exactly equivalent to minimizing the posterior KL. The encoder learns to track the true posterior, tightening the bound.
  • Decoder step (gradients on θ\theta). Increasing L\mathcal L over θ\theta raises a lower bound on log⁡p(x;θ)\log p(\mathbf x; \mathbf \theta). It does not guarantee that the marginal itself rises. Because log⁡p(x;θ)=L+KL(qϕ ∥ pθ(z∣x))\log p(\mathbf x; \mathbf \theta) = \mathcal L + KL(q_\phi \,\|\, p_\theta(\mathbf z \mid \mathbf x)), a step that raises L\mathcal L can lower log⁡p(x;θ)\log p(\mathbf x; \mathbf \theta) when the gap shrinks by more than L\mathcal L grows. The encoder’s quality controls the slack.
The standard variational picture below illustrates this: the KL gap separates the ELBO from the true log-likelihood, and the gap closes only when qϕq_\phi matches the true posterior. Bishop KL represents the tightness of the ELBO bound In short: the ELBO is the surrogate that lets you train both networks with a single gradient. Optimizing it moves the decoder toward the data (through θ\theta) and the encoder toward the posterior (through ϕ\phi) at the same time. The bound is tight only to the extent that the second move succeeds.

The aggregate posterior and posterior collapse

Average the KL regularizer over the data distribution and define the aggregate posterior qϕ(z)=Epdata(x) qϕ(z∣x)q_\phi(\mathbf z) = \mathbb E_{p_{\text{data}}(\mathbf x)}\, q_\phi(\mathbf z \mid \mathbf x). Then Epdata(x) KL ⁣(qϕ(z∣x) ∥ p(z))=Iq(X;Z)+KL ⁣(qϕ(z) ∥ p(z)),\begin{aligned} \mathbb E_{p_{\text{data}}(\mathbf x)}\, KL\!\left(q_\phi(\mathbf z \mid \mathbf x) \,\|\, p(\mathbf z)\right) &= I_q(\mathbf X; \mathbf Z) \\ &\quad + KL\!\left(q_\phi(\mathbf z) \,\|\, p(\mathbf z)\right), \end{aligned} where IqI_q is the mutual information under the joint pdata(x) qϕ(z∣x)p_{\text{data}}(\mathbf x)\, q_\phi(\mathbf z \mid \mathbf x). The identity follows by writing log⁡qϕ(z∣x)p(z)=log⁡qϕ(z∣x)qϕ(z)+log⁡qϕ(z)p(z)\log \frac{q_\phi(\mathbf z \mid \mathbf x)}{p(\mathbf z)} = \log \frac{q_\phi(\mathbf z \mid \mathbf x)}{q_\phi(\mathbf z)} + \log \frac{q_\phi(\mathbf z)}{p(\mathbf z)} and taking the expectation over the joint. The first term penalizes the information the code carries about x\mathbf x. The second penalizes the mismatch between the aggregate posterior and the prior, and it is the term that makes sampling from the prior a good way to generate. Minimizing the regularizer pushes both down. When the decoder is powerful enough to model x\mathbf x without help from z\mathbf z, the cheapest solution sets qϕ(z∣x)=p(z)q_\phi(\mathbf z \mid \mathbf x) = p(\mathbf z) for every x\mathbf x. Then Iq=0I_q = 0 and the latent code is ignored, which is posterior collapse. This same ELBO reappears as the DDPM training loss when the single latent z\mathbf z is replaced by a Markov noise chain x1:T\mathbf x_{1:T} and the encoder is fixed in advance: see Training objective: from VAE ELBO to the DDPM loss on the diffusion introduction page for the bridge.
Found an error on this page? Report an issue.