Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Variational Inference: Approximating Posteriors

Introduction

In our last lesson, we explored the architecture of autoencoders and saw how they excel at learning compressed, non-linear representations of data. We ended by noting a key limitation: while the encoder learns to map inputs to a latent space, this space is often unstructured. Simply picking a random point from the latent space and feeding it to the decoder is unlikely to produce a meaningful output. The model learns a mapping from the data, not a complete generative distribution for the data.

Today, we transition from simple reconstruction to true generative modeling. To do this, we need to adopt a probabilistic perspective. Instead of encoding an input as a single point in the latent space, what if we encoded it as a distribution? This shift forces us to confront a fundamental challenge in Bayesian machine learning: the problem of intractable posteriors. Our objective is to understand this problem and derive the core idea of variational inference (VI), the mathematical technique that provides a powerful and elegant solution. This concept is the "Variational" in Variational Autoencoder and is central to modern generative AI.

The Probabilistic View: Latent Variables and Intractable Posteriors

Let's formalize our goal. We want to build a generative model of our data . A powerful way to do this is to assume that the data is generated by some underlying, unobserved latent variables . This defines a generative process:

  1. A latent variable is sampled from a prior distribution . We often choose something simple, like a standard normal distribution, .
  2. An observable data point is sampled from a likelihood distribution . This distribution is conditioned on the latent variable and is often complex, modeled by a neural network (our decoder).

Together, these define the joint distribution . We can write down and evaluate the functional forms of and , and therefore the joint.

However, a critical task is inference: given an observed data point , what were the likely latent variables that generated it? We want to compute the posterior distribution . Using Bayes' theorem, we know:

The numerator, which is the joint distribution , is tractable. The problem lies in the denominator, , known as the marginal likelihood or evidence. To compute it, we must integrate (or "marginalize") over all possible latent variables:

This integral is the source of our difficulties. Why? In any non-trivial model, the latent space is high-dimensional, and the likelihood is a complex, non-linear function (like a deep neural network). The integral has no analytical closed-form solution and is computationally intractable to approximate numerically.

The following video provides a concrete example of a simple generative model and demonstrates exactly why this marginalization step is intractable.

The challenges in Variational Inference (+ visualization)

The video "The challenges in Variational Inference" by Machine Learning & Simulation will walk us through a simple exponential-normal model to make the problem of intractability tangible.

Please watch from the beginning of the example. The first part introduces a simple model with one latent variable z and one observed variable x. The second part shows which distributions we can evaluate: the prior, the likelihood, and the joint. The third and most important part explains why the marginal p(x) and therefore the posterior p(z|x) are intractable to compute.

Since we cannot compute directly, we cannot perform exact inference. This is the central problem that variational inference was developed to solve.

The Variational Solution: From Intractable Problem to Tractable Optimization

The core idea behind variational inference is this: if we cannot compute the true posterior , let's approximate it with another, simpler distribution that we can control. This distribution, called the variational posterior or surrogate posterior, is chosen from a tractable family of distributions, such as the Gaussian family. It has its own set of parameters, , which we can optimize.

Our goal is now to make as "close" as possible to the true, intractable posterior . From Module 4, you'll recall that a standard way to measure the "distance" between two probability distributions is the Kullback-Leibler (KL) divergence.

So, our objective becomes finding the parameters that minimize the KL divergence between our approximation and the true posterior:

Now, we seem to have hit the same wall again. The formula for KL divergence involves the very distribution we deemed intractable:

This expression still requires us to evaluate . This is where a remarkable mathematical rearrangement comes into play, leading us to a tractable objective. The following video meticulously derives this result.

Variational Inference | Evidence Lower Bound (ELBO) | Intuition & Visualization

The video "Variational Inference | Evidence Lower Bound (ELBO)" provides the crucial derivation that forms the basis of VI.

First, watch the segment from the remedy, which introduces the idea of a surrogate distribution q and frames the problem as minimizing the KL divergence. Then, watch the core derivation from rearranging the KL divergence. This is the most important part of the lesson. Follow the steps carefully as the intractable KL divergence objective is transformed into a tractable one: the Evidence Lower Bound (ELBO).

The Evidence Lower Bound (ELBO)

As derived in the video, by expanding the KL divergence and rearranging terms, we arrive at one of the most important identities in modern machine learning:

where is the Evidence Lower Bound (ELBO). Let's analyze this equation:

  1. : The log of the model evidence. For a given data point , this is a fixed value. It does not depend on the parameters of our variational distribution.
  2. : The KL divergence between our approximation and the true posterior . We know this is always non-negative ().
  3. : The ELBO. Because the KL divergence is non-negative, the ELBO is always less than or equal to the log evidence: . Hence its name: a "lower bound" on the evidence.

From this identity, the grand insight of variational inference emerges: since is constant with respect to , maximizing the ELBO is equivalent to minimizing the KL divergence.

We have successfully transformed our original, intractable goal (minimizing a KL divergence that contains ) into a new, tractable one: maximizing the ELBO. The ELBO is defined as:

This expression is tractable because it only involves the joint distribution and our approximation , both of which we can evaluate. We have sidestepped the need to compute the marginal entirely.

For a deeper, text-based walkthrough of this derivation, the following article is excellent. It derives the same identity without relying on Jensen's inequality, providing a more direct and intuitive proof.

How I learned to stop worrying and love the Free Energy Principle

This article by Jake Vision provides a fantastic, first-principles derivation of the ELBO.

First, read the section Hidden causes, which reinforces the concept of a generative model and the intractability of the posterior. Next, study the derivation in Deriving the Evidence Lower Bound. Pay close attention to the second part, "Without Jensen (the exact identity)". This section shows precisely how the log p(x) can be decomposed into the ELBO and the KL divergence, proving that maximizing one is equivalent to minimizing the other.

This act of approximating a complex probability distribution by optimizing a simpler, "variable" one is what gives variational inference its name. It's a technique rooted in a branch of mathematics called variational calculus, which deals with optimizing functionals (functions of functions).

A visual comparison of an exact posterior distribution versus a variational approximation. The image shows the true mean of a distribution (left) and the mean of the posterior learned via variational inference (middle). The plots on the right show that for individual parameters, the simple variational distribution (e.g., a Gaussian) can be a very close match to the exact posterior.

Conclusion

In this lesson, we have confronted a core challenge in probabilistic modeling and established the fundamental principles of its solution. We have moved from the deterministic world of standard autoencoders into a probabilistic framework, setting the stage for building true generative models.

Key Takeaways:

  • Intractable Posterior: In a latent variable model, computing the posterior distribution is often intractable due to the high-dimensional integral required to calculate the marginal likelihood, .
  • The Variational Approach: Variational Inference (VI) solves this by introducing a simpler, tractable distribution (the variational posterior) to approximate the true posterior.
  • The Optimization Goal: The goal of VI is to make the approximation as close as possible to the true posterior by minimizing the KL divergence between them.
  • The Evidence Lower Bound (ELBO): By rearranging the definition of KL divergence, we arrive at the identity . This shows that maximizing the ELBO is equivalent to minimizing the KL divergence.
  • From Inference to Optimization: VI elegantly transforms an intractable inference problem into a tractable optimization problem. We can now use techniques like gradient descent to find the parameters that maximize the ELBO.

Preview of the Next Lesson:

We now have the theoretical tool—variational inference—needed to build a generative autoencoder. In our next lesson, we will integrate these ideas into the autoencoder architecture to derive the Variational Autoencoder (VAE). We will formulate the VAE's objective function using the ELBO and uncover a final, clever challenge: how do you backpropagate gradients through a sampling operation? The solution, known as the reparameterization trick, is a cornerstone of training VAEs and many other modern generative models.

Can't find a good explanation? Sign up and we'll make it for you

Sign up