Welcome to the course! I'm excited to guide you through the intricacies of diffusion models. Our journey begins not with diffusion itself, but with the essential statistical and machine learning foundations upon which these models are built. Your background in computer science and extensive experience as a data scientist will be a great asset as we delve into the mathematical underpinnings of modern generative modeling.
This first lesson focuses on a fundamental concept: the Kullback-Leibler (KL) divergence. It's a way to measure the "distance" or difference between two probability distributions. This idea is central to variational inference, which we'll explore throughout this module. A solid grasp of KL divergence is the first step toward understanding Variational Autoencoders (VAEs) and, eventually, the mechanics of diffusion models.
Specifically, by the end of this lesson, you will be able to derive the KL divergence between two Gaussian distributions and explain the crucial implications of its asymmetry.
1. What is KL Divergence?
Imagine you have two probability distributions, and . How would you quantify how different they are with a single number? This is the core question that KL divergence answers. It's not a true distance metric in the formal mathematical sense (like Euclidean distance), primarily because it's not symmetric, but it serves a similar purpose.
To build an intuition for how the KL divergence formula comes about, let's watch a segment from the video "The KL Divergence : Data Science Basics". The presenter uses a simple analogy of a kindergarten teacher tracking students' favorite fruits over two years to constructively derive the formula from first principles.
The KL Divergence : Data Science Basics
This video provides an excellent step-by-step construction of the KL divergence formula.
Please watch the following segments: This part sets up the problem and explains why asymmetry is a desirable property for our metric. This segment explores initial ideas, like taking ratios of probabilities, and motivates the use of the logarithm to handle skewed averages and create an "equal but opposite" effect for reciprocal changes. Finally, this part introduces probabilistic weighting and arrives at the final formula for discrete KL divergence.
As you saw, the KL divergence from a "reference" or "true" distribution to a "model" or "approximating" distribution is defined as the expectation of the logarithmic difference between the probabilities. In the video, year 1 was the reference distribution and year 2 was the current distribution . The final form for discrete distributions is:
For continuous distributions, the sum is replaced by an integral:
An alternative and powerful perspective comes from information theory. KL divergence can be interpreted as the "extra information" required to encode samples from when using a code optimized for . It's the difference between the cross-entropy of and , and the entropy of itself.
2. Deriving the KL Divergence for Gaussian Distributions
In machine learning, we frequently approximate complex, unknown distributions with simpler, tractable ones like the Gaussian distribution. This is a core component of variational inference. Therefore, being able to calculate the KL divergence between two Gaussians analytically is extremely useful.
We will now work through the derivation for the KL divergence between two multivariate normal distributions, and . The derivation relies on properties of logarithms, expectations, and the trace operator, which should be familiar from your background in applied mathematics.
The following resource, "Kullback-Leibler divergence for the multivariate normal distribution", provides a concise and rigorous proof. I've broken it down into a few steps.
Kullback-Leibler divergence for the multivariate normal ...
Let's begin the derivation. First, we'll set up the integral by plugging the multivariate normal probability density functions (PDFs) into the definition of KL divergence.
Start from the beginning of the proof. Read the section that starts with the definition and follow the substitutions down to the expression labeled (2). Focus on how the properties of logarithms are used to simplify the ratio of the two PDFs.
The expression is now an expectation taken with respect to . The next step is to manipulate the terms inside the expectation so that we can apply standard formulas for expectations of quadratic forms. This involves the clever use of the trace operator, . A key property used here is that for a scalar , , which allows us to convert the quadratic forms into a trace, and then use the cyclic property of the trace, .
Kullback-Leibler divergence for the multivariate normal ...
Now, let's continue the derivation by applying the trace operator and linearity of expectation.
Continue from equation (3), which starts here. Follow the algebraic manipulations and the justification for moving the expectation operator inside the trace.
Finally, we can resolve the expectations using known results for multivariate normal distributions and simplify the expression to get our closed-form solution.
Kullback-Leibler divergence for the multivariate normal ...
This is the final step where we substitute the known expectation formulas and rearrange to get the final result.
Read from the paragraph that begins "Using the expectation..." to see how the final expression is reached.
After these steps, we arrive at the final expression for the KL divergence between two multivariate normal distributions and :
where is the dimensionality of the vector .
For the simpler univariate case, where and , this formula simplifies to:
Take a moment to inspect these formulas. It should be clear that if you swap the roles of and (i.e., swap with ), you will get a different result. This brings us to the crucial property of asymmetry.
3. The Asymmetry of KL Divergence and Its Consequences
The fact that is not just a mathematical curiosity; it has profound practical consequences in machine learning. The choice of which distribution comes first determines the behavior of optimization algorithms that rely on minimizing KL divergence.
Let's use to denote the true, often complex, data distribution and to be our simpler, approximate model distribution.
-
Forward KL Divergence:
The expectation is taken over the true data distribution . The expression is . For this value to be small, our model must assign a non-trivial probability anywhere that does. If but for some , the ratio becomes very large, and the log term blows up to infinity. This forces to "cover" all the modes of . This is often called mode-covering or mean-seeking behavior. -
Reverse KL Divergence:
The expectation is taken over our model distribution . The expression is . For this to be small, must avoid assigning probability mass where has none. If but , the term blows up. This encourages to find and focus on one of the modes of where the probability is high, ignoring other modes. This is known as mode-seeking or zero-avoiding behavior.
The following blog post and video provide excellent visual intuition for this.
Kullback-Leibler divergence asymmetry | Ben Moran
This article clearly explains the consequences of KL divergence asymmetry when approximating a complex distribution.
Please read the section titled "Consequences for optimization". It contrasts the results of minimizing the two forms of KL divergence.
The video "Fantastic KL Divergence and How to (Actually) Compute It" also has a great segment that animates this exact concept.
Fantastic KL Divergence and How to (Actually) Compute It
This short clip provides a clear visual demonstration of mode-covering versus mode-seeking behavior.
Please watch from this point. Notice how minimizing the forward KL makes the approximation spread out, while minimizing the reverse KL makes it latch onto one of the peaks.
The image below, from a blog post by Jessica Stringham, provides a static summary of this effect. The "real" distribution is a bimodal mixture of two Gaussians (black curve), and the "approx" distribution is a single Gaussian (blue curve).

In variational inference, which we will study next, the objective function we derive (the ELBO) is equivalent to minimizing the reverse KL divergence, . This is why VAEs are sometimes criticized for producing "average" or blurry samples—their approximate posterior is encouraged to be mode-seeking and may not capture the full variation of the true posterior distribution.
Conclusion
In this introductory lesson, we have established a foundational understanding of KL divergence. The key takeaways are:
- Definition: KL divergence measures the inefficiency or "surprise" of using an approximate distribution to represent a true distribution .
- Asymmetry: It is not a true distance metric because . This is because the expectation is taken with respect to the first distribution in the argument.
- Gaussian Case: We worked through the derivation for the closed-form expression of KL divergence between two Gaussian distributions, a result that will be crucial in our study of VAEs.
- Optimization Behavior: The asymmetry leads to distinct outcomes in optimization. Minimizing the forward KL, , is mode-covering. Minimizing the reverse KL, , is mode-seeking.
In our next lesson, we will use what we've learned about KL divergence to derive the Evidence Lower Bound (ELBO). You will see how maximizing the ELBO is equivalent to minimizing the reverse KL divergence between an approximate and true posterior distribution, forming the core optimization objective for all variational methods, including VAEs.