Hello! In our last lesson, we derived the reparameterization trick, a clever method for backpropagating through stochastic nodes in a computational graph. This gave us a low-variance way to compute gradients for the part of a VAE's objective that involves reconstructing the input. However, that's only half the story. The full VAE objective, the Evidence Lower Bound (ELBO), involves another critical component: a term that regularizes the latent space.
This regularization term is almost always a Kullback-Leibler (KL) divergence. To fully grasp how VAEs work, and indeed how many generative models and classification problems are framed, we need to build a solid, intuitive understanding of the core concepts from information theory. This lesson is dedicated to defining and exploring three of the most important measures: entropy, cross-entropy, and KL divergence. We will not only formalize them but also compute them for both simple discrete cases and for the Gaussian distributions that are central to models like VAEs and diffusion models.
1. From Surprise to Entropy: Measuring Uncertainty
The most intuitive way to approach information theory is by starting with the concept of "surprise." An event that is very unlikely to happen is more surprising when it occurs than a very common event. Information theory quantifies this intuition.
A mathematically convenient way to define surprise, or self-information, is with a logarithm. We want surprise to be additive for independent events (the surprise of two independent dice rolls should be the sum of their individual surprises), while probabilities multiply. The logarithm neatly converts products to sums.
The surprisal of an outcome with probability is defined as:
The negative sign ensures that surprise is non-negative, as is between 0 and 1. As , . As , .
While surprisal applies to a single outcome, entropy characterizes the distribution as a whole. It is the average or expected surprise over all possible outcomes.
For a random variable with probability distribution , the entropy or is:
Specifically:
- For a discrete variable with probability mass function :
- For a continuous variable with probability density function , this is called differential entropy:
The following video provides an excellent visual and conceptual foundation for these ideas, building from the ground up.
The Key Equation Behind Probability
This video from Artem Kirsanov provides a wonderfully intuitive derivation of entropy starting from the idea of surprise.
Watch the first part, which formalizes the notion of surprise and how it relates to probability: Surprise and Log-Probability. Continue with the next segment, which defines entropy as the average surprise and gives a concrete example: From Surprise to Entropy.
Entropy measures the inherent unpredictability or uncertainty in a distribution. A distribution with a single certain outcome has zero entropy. For a discrete distribution with a fixed number of outcomes, a uniform distribution (where all outcomes are equally likely) has the maximum possible entropy.
2. Cross-Entropy and KL Divergence: Measuring the Mismatch
In machine learning, we rarely have access to the true data-generating distribution, which we can call . Instead, we build a model, let's call it , to approximate . A natural question arises: how much "average surprise" do we experience if we observe events drawn from the true distribution , but we evaluate their surprise using the probabilities from our model ? This measure is the cross-entropy.
Formally, the cross-entropy between and is:
Notice the key difference from entropy: we are taking an expectation over samples from , but the logarithm is applied to the probabilities from .
Cross-entropy is the foundation for the loss function in nearly all classification tasks. When you use a "cross-entropy loss", you are implicitly minimizing the cross-entropy between the true distribution of labels (e.g., a one-hot vector) and the predicted probabilities from your model.
Now, we can relate these concepts.
- The entropy is the minimum possible average surprise, achieved only if we have a perfect model ().
- The cross-entropy is the average surprise we actually experience using our (likely imperfect) model .
The difference between them is the extra surprise, or inefficiency, caused by using an approximate model instead of the true one. This difference is the Kullback-Leibler (KL) divergence.
By substituting the definitions and using the properties of logarithms, we get the standard form:
This relationship, , is fundamental. It tells us that minimizing the cross-entropy with respect to our model is equivalent to minimizing the KL divergence, because is a constant that doesn't depend on .
The following resources provide further intuition and formal definitions for these crucial concepts.
Fantastic KL Divergence and How to (Actually) Compute It
This video by Jia-Bin Huang offers a compact, practical explanation using the analogy of optimal coding.
Watch the first part which quickly recaps surprise and entropy: Surprise and Entropy. Then, watch the explanation of cross-entropy and KL divergence as the "extra bits" needed for a suboptimal code: Cross-Entropy and KL Divergence.
The D2L.ai book provides concise, formal definitions and discusses the key properties of these measures.
First, review the formal definitions of Entropy for both discrete and continuous cases. Next, read the definition of KL Divergence and its properties. Finally, read the formal definition of Cross-Entropy and note its relationship to the other two measures.
An important property of KL divergence is that it is not symmetric: in general, . This asymmetry has profound consequences in how different generative models behave, a topic we will explore in a future lesson.
3. Computation for Discrete Distributions
Let's make this concrete with a simple discrete example. Suppose the true distribution for a 3-sided die is . Our model believes the die is fair, so . Let's calculate the information-theoretic measures (using log base 2 for "bits").
-
Entropy of P:
This is the inherent uncertainty of the true distribution.
-
Cross-Entropy H(P, Q):
This is the average number of bits we'd need to encode outcomes from P using a code optimized for Q.
-
KL Divergence D_KL(P || Q):
This is the "cost" or "wasted information" from using the uniform model Q to describe the data from P.

4. Derivation for Gaussian Distributions
For VAEs, the most important application is calculating the KL divergence between two Gaussian distributions. The encoder network typically outputs the parameters of an approximate posterior distribution , and we want to measure its divergence from a simple prior, usually a standard normal .
Let's derive the general formula for the KL divergence between two univariate Gaussians, and .
The definition is:
This is equivalent to .
The log-probability for a Gaussian is:
Let's calculate the expectation of each log-term under :
-
:
Since by definition of variance, this simplifies to:
This is the negative of the differential entropy of the Gaussian.
-
:
For the expectation term, we can expand it: .
Using linearity of expectation, and that , this becomes .
Substituting back:
Finally, we compute :
This closed-form expression is what you will implement in PyTorch for the VAE. It allows you to compute the KL divergence term of the loss analytically, without needing Monte Carlo estimation.

Conclusion
Today, we've journeyed through the foundational concepts of information theory that are indispensable for modern machine learning. You now have both an intuitive and a formal understanding of these key measures.
Key Takeaways:
- Entropy measures the inherent uncertainty or "average surprise" of a probability distribution.
- Cross-Entropy measures the average surprise when observing data from a true distribution but using a model to calculate probabilities. It's a standard loss function in classification.
- Kullback-Leibler (KL) Divergence measures the "extra surprise" or inefficiency from using model instead of the true distribution . It quantifies the mismatch between two distributions.
- The relationship shows why minimizing cross-entropy is equivalent to minimizing KL divergence for model optimization.
- For the crucial case of two Gaussian distributions, the KL divergence can be calculated analytically, a key component for training VAEs.
You now possess all the mathematical components needed to understand the objective function of a Variational Autoencoder. In our next lesson, we will put everything together by deriving the Evidence Lower Bound (ELBO), and you will see exactly how the KL divergence we derived today serves as the essential regularization term that structures the latent space.