Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 38

Deriving and Interpreting the Evidence Lower Bound (ELBO)

Recommended resources

Welcome back! In our last lesson, we built a solid information-theoretic toolkit, defining entropy, cross-entropy, and Kullback-Leibler (KL) divergence. We even derived the analytical form for the KL divergence between two Gaussian distributions, noting its importance for Variational Autoencoders. Today, we will use this foundation to assemble the full VAE objective function.

Our goal is to derive and understand the Evidence Lower Bound (ELBO). This single objective function lies at the heart of variational inference, a powerful Bayesian method for approximating intractable probability distributions. You will see how the concepts we've discussed—from pathwise gradients to KL divergence—come together in a principled way. By the end of this lesson, you will be able to derive the ELBO using two different approaches, including one involving Jensen's inequality, and clearly interpret its two fundamental components: a reconstruction term and a regularization term.

1. The Central Problem: Intractable Posteriors

Let's begin by formalizing the generative model that a VAE assumes. We imagine that our data, , is generated by a two-step process:

  1. A latent variable is sampled from a simple prior distribution, . Typically, this is a standard multivariate Gaussian, .
  2. A data point is sampled from a conditional distribution . This is the "decoder" or "generative network," parameterized by .

The joint probability is thus . In this framework, our primary goal is often inference: given a data point , what is the distribution of latent variables that could have generated it? This is the posterior distribution, . Using Bayes' theorem, we can write:

The problem lies in the denominator, , which is the marginal likelihood of the data, also known as the "evidence". For a model where the decoder is a complex neural network, this integral is computationally intractable. It requires integrating over all possible latent variables , a high-dimensional and complex space. Since we cannot compute the evidence , we cannot compute the true posterior .


2. The Variational Solution: Introducing an Approximation

Variational inference provides an elegant solution: if the true posterior is intractable, we approximate it with a simpler, tractable distribution . This distribution is called the variational posterior or approximate posterior.

We choose a family of distributions for (e.g., Gaussians) and then use a neural network, called the "encoder" and parameterized by , to map an input to the parameters of this distribution. For instance, the encoder might output the mean and variance of a Gaussian, so that .

The goal then becomes to find the parameters that make our approximation as "close" as possible to the true posterior . As we learned in the last lesson, the natural way to measure the dissimilarity between two distributions is the KL divergence. We want to minimize .

This diagram captures the essence of variational inference. The blue curve represents the complex, intractable true posterior \(p(Z|D)\). The orange curve is our simpler, tractable surrogate distribution \(q(Z)\), which we aim to fit to the true posterior.

The following reading explains this setup from a probabilistic modeling perspective.

Tutorial - What is a variational autoencoder? – Jaan Lı 李

This tutorial by Jaan Altosaar provides a clear bridge between the neural network view and the probabilistic model view of VAEs. It begins by introducing the intractability at the heart of the problem.

Focus on the section The probability model perspective. It formally defines the generative process and explains why computing the true posterior p(z|x) is intractable due to the evidence term in the denominator.


3. Deriving the Evidence Lower Bound (ELBO)

We've established that our objective is to minimize the KL divergence between the approximate and true posteriors. However, computing this KL divergence directly still requires knowing the true posterior , which is intractable. This seems like a catch-22.

Let's see what happens when we expand the definition of this KL divergence:

Using Bayes' rule, we can rewrite . Substituting this in:

Since does not depend on , we can pull it out of the expectation:

Rearranging this equation to solve for the log-evidence, , gives us the fundamental identity of variational inference:

Let's define the second term as the Evidence Lower Bound (ELBO):

Our fundamental identity becomes:

We know that KL divergence is always non-negative (). This implies that . The ELBO is a lower bound on the log-evidence of our data. Because the gap between the ELBO and the log-evidence is the very KL divergence we want to minimize, maximizing the ELBO is equivalent to minimizing the KL divergence between our approximation and the true posterior.

The ELBO is something we can actually compute and optimize! It depends only on (which is just ) and , both of which are defined by our model.

The following resources walk through this derivation in detail.

Tutorial - What is a variational autoencoder? – Jaan Lı 李

The Jaan Altosaar tutorial continues by showing exactly how the ELBO emerges from the KL divergence.

Read the section from the start of the ELBO derivation through its conclusion. Pay close attention to the final equation, log p(x) = ELBO + KL, as it is the key insight.

Variational Autoencoder - Model, ELBO, loss function and maths explained easily!

This video by Umar Jamil provides an excellent, step-by-step algebraic derivation of the relationship between the log-likelihood, the ELBO, and the KL-divergence, using a helpful analogy.

Watch the segment from the mathematical derivation to see this relationship derived from a slightly different starting point.

An Alternative Derivation with Jensen's Inequality

There is another, more direct way to derive the ELBO using Jensen's inequality, which you may find more concise given your mathematical background. For any concave function (like the logarithm) and random variable , Jensen's inequality states that .

We start with the log-evidence and introduce our variational distribution :

Now, we apply Jensen's inequality. Since is concave, we can move it inside the expectation, which flips the equality to an inequality:

The right-hand side is exactly our definition of the ELBO. This directly shows that the ELBO is a lower bound on the log-evidence.

Variational Autoencoders | Generative AI Animated

The "bonus" section of this animated video from Deepia provides a clear, step-by-step visual derivation of the ELBO using Jensen's inequality.

Watch the derivation in the section labeled "Derivation of the Loss (Bonus)": ELBO Derivation.


4. Interpreting the Terms of the ELBO

So, our objective is to maximize the ELBO. To gain intuition, let's rearrange it into a more interpretable form.

Starting with our definition:

We expand the joint probability :

Now, we group the terms:

The second term is exactly the definition of . So, the final, most common form of the ELBO is:

Maximizing the ELBO means maximizing the reconstruction term while minimizing the KL divergence. In practice, we minimize the negative ELBO as our loss function:

Let's analyze these two terms:

  1. Reconstruction Loss: This term pushes the decoder () to learn to reconstruct the input accurately from latent codes sampled from the encoder's output. Maximizing the log-likelihood is equivalent to minimizing a reconstruction loss. For example, if the decoder assumes a Gaussian output, this becomes a mean squared error (MSE) loss. If it assumes a Bernoulli output (for binary images), this becomes a binary cross-entropy (BCE) loss.

  2. KL Regularizer: This term measures the divergence between the approximate posterior and the prior . It acts as a regularizer, forcing the distributions produced by the encoder to stay close to the prior distribution (e.g., a standard normal). This encourages the latent space to be well-structured and continuous, which is crucial for being able to generate new data by sampling from the prior. We can compute this term analytically using the formula we derived in the previous lesson.

The VAE training dynamic is a trade-off between these two terms. The model must learn to encode information into to reconstruct well, but not so well that the distribution of strays too far from the simple prior.

Tutorial - What is a variational autoencoder? – Jaan Lı 李

The Jaan Altosaar tutorial concludes by showing this final decomposition and interpreting the terms.

Read the section where the ELBO is decomposed into its final form: ELBO for a single datapoint. Then, circle back to the very beginning of the article, The VAE loss function, to see how this principled derivation matches the initial, more heuristic definition of the VAE loss.

Variational Autoencoder - Model, ELBO, loss function and maths explained easily!

The Umar Jamil video uses a fantastic analogy of profit, revenue, and cost to explain the interplay between the two terms of the ELBO.

Watch the interpretation part of the video, from the ELBO in detail.


Conclusion

In this lesson, we have derived the central objective function for training Variational Autoencoders from first principles. We have connected the practical problem of generative modeling with the theoretical framework of variational inference.

Key Takeaways:

  • Generative modeling with latent variables often leads to an intractable posterior because the evidence is impossible to compute.
  • Variational inference approximates the true posterior with a simpler, tractable distribution (the encoder).
  • The objective is to minimize the KL divergence between the approximate and true posteriors, which is equivalent to maximizing the Evidence Lower Bound (ELBO).
  • The ELBO is composed of two competing terms:
    • A reconstruction term that encourages the decoder to faithfully reconstruct the input data.
    • A regularization term (a KL divergence) that forces the encoder's output distribution to match a simple prior, ensuring a well-structured latent space.
  • The final VAE loss function is the negative ELBO: .

You now have a complete picture of the VAE objective. In our next lesson, we will briefly explore a subtle but important detail: the difference between forward and reverse KL divergence and its implications for how models like VAEs learn, a phenomenon often described as "mode-seeking" versus "mode-covering." This will be our final piece of theory before we move on to implementation.