ELBO objective and KL term
Overview
The Evidence Lower BOund (ELBO) is the central optimization objective in variational inference and the training loss for Variational Autoencoders (VAEs). It provides a tractable way to approximate intractable posterior distributions by maximizing a lower bound on the log-likelihood, while simultaneously including a KL divergence term that regularizes the approximate posterior.

Derivation from First Principles
Step 1: Start with the intractable log-likelihood
We want to maximize for observed data , where the generative model is:
Why this is hard: For most choices of (like a neural network decoder), this integral has no closed form and is exponentially expensive in the dimension of .
Step 2: Introduce the approximate posterior
Since we can't compute directly, we introduce an approximate posterior (the encoder network in VAEs). This is our variational distribution—we'll optimize to make close to .
Step 3: Derive the ELBO using Jensen's inequality
Start with the log-likelihood:
Multiply and divide by :
Key insight: is a concave function, so by Jensen's inequality (which for concave functions gives ):
Applying this:
Why this step? Jensen's inequality lets us move the inside the expectation at the cost of introducing an inequality. Because is concave, the inequality points the right way ( of an average average of s), giving us a valid lower bound. This makes the expression computable via Monte Carlo sampling.
Step 4: Rewrite using conditional and prior
Using :
Rearrange:
Why this form?
- The first term rewards the model for reconstructing from latent codes sampled from the encoder.
- The second term penalizes the encoder distribution for deviating from the prior , preventing it from collapsing or overfitting.
Understanding the KL Term
Why we need it:
- Prevents posterior collapse: Without the KL term, the encoder could learn to map each to a deterministic, wildly different , making the latent space discontinuous and unusable for generation.
- Enables sampling: By keeping , we can generate new samples by drawing and decoding, even for we've never seen.
- Regularization: Acts like a complexity penalty—simpler, more "prior-like" encodings are preferred.
Mathematical Properties
Key properties:
- always (Gibbs' inequality)
- iff almost everywhere
- Not symmetric: in general
Why this direction? We use not because:
- is the distribution we control (encoder)
- Expectations are taken w.r.t. , which we can sample from
- This is called reverse KL or exclusive KL—it makes underestimate the support of (zero-forcing behavior)
The Gap: ELBO vs True Log-Likelihood
Step 5: Quantifying the gap
Start from Bayes' rule:
Taking logs:
Take expectation over :
Add and subtract :
Why this matters:
- The gap between ELBO and true log-likelihood is exactly the KL divergence between our approximation and the true posterior
- As we maximize ELBO, we simultaneously improve the generative model () and the approximation quality ()
- When , ELBO equals log-likelihood
Worked Examples
ELBO:
Why this step? The reconstruction term becomes negative squared error because .
Closed-form KL for diagonal Gaussians:
Derivation of this formula: Starting from definition:
Using and :
Why this matters: This closed form makes VAE training efficient—no Monte Carlo estimation needed for the KL term.
Step 1: Reconstruction term
Why this step? We evaluate how well the decoder reconstructed the original from the sampled latent code.
Step 2: KL term (using , so and )
ELBO:
Why this step? We penalize the encoder for producing a distribution that deviates from the standard normal prior. Note the crucial detail: the KL formula uses , not —forgetting the factor of 2 is a common error.
What happens:
- : Prioritizes reconstruction, KL can grow large → better reconstructions, entangled latents
- : Standard ELBO, balanced trade-off
- : Prioritizes matching prior → more disentangled latents, worse reconstructions
Why this works: The KL term's strength controls the information bottleneck. Higher forces the encoder to use fewer, more independent dimensions of , leading to disentanglement.
Common Mistakes and Misconceptions
The truth: The KL term isn't ad-hoc regularization; it arises naturally from the variational inference framework. It's the gap between your approximate posterior and the true posterior. Removing it doesn't give you a "less regularized" VAE—it breaks the mathematical justification for why you're optimizing this objective at all.
How to fix it: Remember the derivation: ELBO + KL gap = log-likelihood. The KL term is part of the bound, not an add-on.
The nuance: Maximizing ELBO w.r.t. does improve . But when we also optimize , we're tightening the bound by reducing the KL gap. The log-likelihood only increases if the ELBO increase exceeds the gap reduction. In practice, we jointly optimize, so both happen together.
How to fix it: ELBO is a lower bound for fixed . When changes, the bound itself moves. The guarantee is: always, but improving improves the model.
The truth: KL is asymmetric. (reverse KL, used in ELBO) is zero-forcing: it makes wherever , leading to underestimation of the true posterior's support. (forward KL) is zero-avoiding: it makes wherever , leading to overestimation of support.
Why it matters for VAEs: Reverse KL can cause posterior collapse where the encoder ignores certain modes of the true posterior. Forward KL would make the approximate posterior overly broad and diffuse.
How to fix it: Remember the direction: we want expectations w.r.t. (what we can sample), so we use .
The truth: ELBO is a lower bound on log-probability, which can be negative (probabilities can be < 1). What matters is that ELBO increases during training, not its absolute value. A typical ELBO value might be -1000 or -10000 depending on data dimensionality.
How to fix it: Track ELBO improvement, not absolute value. Compare ELBO across architectures on the same data for meaningful comparisons.
Active Recall Practice
Recall Explain ELBO to a 12-year-old
Imagine you have a magic box that can create pictures. But you want to understand what secret codes the box uses to make each picture. The problem is, you can't peek inside the box directly.
So you build a guesser machine that looks at picture and guesses what secret code created it. Now you play a game:
- Show your guesser a picture → it guesses a code
- Feed that code to the magic box → see if you get the same picture back
- You get points if the picture comes out right
But there's a catch! Your guesser can't just make up any random codes—it has to use codes that are similar to the ones the magic box naturally uses (like numbers between 0 and 1, not crazy numbers like 999).
ELBO is your score in this game: You get points for recreating pictures correctly (reconstruction term), but you lose points if your guesser uses weird, un-natural codes (KL term). Maximizing ELBO means getting better at both guessing codes and recreating pictures while keeping your guesses reasonable.
For KL direction: "Queen Prefers Priors" → , we measure how far our Queen (encoder) is from the Prior.
Connections
- Variational Inference: ELBO is the fundamental objective in VI; VAEs are a deep learning application
- KL Divergence: Deep dive into properties, forward vs reverse, f-divergences
- VAE Architecture: How ELBO is implemented in neural networks with reparameterization trick
- Expectation Maximization: ELBO appears in EM algorithm for latent variable models
- Information Bottleneck: β-VAE connects to IB principle via weighted KL term
- Posterior Collapse: What happens when KL → 0 and the model ignores latent variables
- Importance Weighted Autoencoders: Tighter bounds than ELBO using multiple samples
- Normalizing Flows: Alternative to diagonal Gaussian encoder for more expressive
Summary
The ELBO objective is a tractable lower bound on the intractable log-likelihood . It decomposes into:
- Reconstruction term: Reward accurate decoding
- KL regularization: Keep encoder close to prior
The gap between ELBO and log-likelihood is exactly —the approximation error of the variational posterior. Maximizing ELBO jointly improves the generative model and reduces the approximation gap. The KL term is not ad-hoc regularization but a fundamental consequence of the variational framework that enables sampling, prevents collapse, and provides a principled training objective for VAEs.
#flashcards/ai-ml
What is the ELBO objective in variational inference?
Why is ELBO called a "lower bound"?
What are the two components of the ELBO?
What is the gap between ELBO and true log-likelihood?
Why do we use instead of in ELBO?
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Dekho, yaha core baat ye hai ki generative models me hum chahte hain ki apne data ka log-likelihood maximize karein, taaki model realistic samples generate kar sake. Lekin problem ye hai ki compute karna hai to sabhi possible latent codes ke upar integrate karna padta hai, jo practically impossible hai — ek exponentially bada integral. To hum seedha wo cheez maximize hi nahi kar sakte jo hume chahiye. Isiliye ELBO aata hai: ye ek aisi quantity hai jo hum actually compute kar sakte hain, aur jo hamesha se chhoti ya barabar rehti hai. Iska matlab agar hum ELBO ko upar push karein, to woh niche se hamare unreachable ceiling ko bhi upar dhakelta hai. Bilkul jaise ek tower banake ceiling ki minimum height ka guarantee dena.
Ab ELBO ke andar do parts hote hain, aur yahi samajhna sabse zaroori hai. Pehla hai reconstruction term — ye reward karta hai jab model latent code se wapas original ko sahi se bana pata hai. Doosra hai KL divergence term, jo encoder ki distribution ko prior (usually simple Gaussian) ke paas rehne pe force karta hai. Jensen's inequality ki wajah se — kyunki ek concave function hai — hum ko expectation ke andar la paate hain, aur bound sahi direction me point karta hai, matlab valid lower bound milta hai jise hum Monte Carlo sampling se estimate kar sakte hain.
Ye why-matters isliye hai ki ye poora balance hi VAE ki jaan hai. Agar sirf reconstruction pe focus karein, to model overfit ho jayega aur latent space bekhar (messy) ho jayega — smooth samples generate nahi honge. KL term isko regularize karke ensure karta hai ki latent space organized aur continuous rahe, taaki tum us space se naye points sample karke fresh, meaningful data generate kar sako. To samajh lo, ELBO ek clever trick hai jo intractable problem ko tractable bana deta hai, aur ye hi variational inference aur modern generative modeling ki foundation hai.