back

Information Theory for Machine Learning

November 20, 2023 · 18 min read

Note: This is an old notebook and content was re-formated with AI in June, 2026.


Information Theory, according to Wikipedia, delves into the quantification, storage, and communication of information. In this write-up, we try to systematically understand each of underlying concept of information theory. Let’s begin by asking what “information” truly signifies.

Surprise and self-information

One way to understand information is through the concept of surprise. If an event xx has probability p(x)p(x), how surprised should we be when it happens? Rare events should surprise us more, and a sure event (p=1p=1) should not surprise us at all. The most obvious quantity with that shape is the inverse probability:

surprise(x)  =?  1p(x)\text{surprise}(x) \;\overset{?}{=}\; \frac{1}{p(x)}

Plotting it against probability gives the curve below:

Surprise versus probability

As the curve shows, this formulation gives a high information score to events with low probability, but it has two ugly properties for an honest measure of information:

  • A certain event still carries one unit of surprise. 1/p(x)=11/p(x) = 1 when p(x)=1p(x)=1, not zero. We want the surprise of a guaranteed outcome to be exactly zero, since seeing something we already knew tells us nothing new.
  • Independent events should add, but 1/p1/p multiplies. If xx and yy are independent, p(x,y)=p(x)p(y)p(x,y)=p(x)p(y), so 1p(x,y)=1p(x)1p(y)\frac{1}{p(x,y)} = \frac{1}{p(x)}\cdot\frac{1}{p(y)}. Learning two unrelated facts should be twice as informative as learning one, a sum rather than a product.

The function that fixes both problems at once is the logarithm. Taking log ⁣(1/p(x))=logp(x)\log\!\big(1/p(x)\big) = -\log p(x) gives us:

I(x)  =  logp(x)I(x) \;=\; -\log p(x)

The figure below plots this new score and contrasts it with the earlier 1/p(x)1/p(x):

Information using the logarithm

Now I(x)=0I(x)=0 when p(x)=1p(x)=1 (first problem solved), and for independent events I(x,y)=logp(x)p(y)=I(x)+I(y)I(x,y) = -\log p(x)p(y) = I(x)+I(y) (second problem solved). Choosing log2\log_2 measures information in bits, while choosing ln\ln measures it in nats. The base only scales the whole thing uniformly, so it changes no optimization problem. In deep-learning, we default to ln\ln because it is the derivative-friendly choice.

Note: Shannon showed that logp-\log p is not just one function with these properties but the only one, up to a multiplicative constant: any measure that is continuous in pp, monotonically decreasing, and additive across independent events is forced to be clogp-c\log p.

Entropy

Self-information scores a single outcome. To characterize a whole random variable rather than one event, we average self-information over the distribution itself, and that average is the entropy:

H(X)  =  Exp[I(x)]  =  xp(x)logp(x)H(X) \;=\; \mathbb{E}_{x\sim p}\big[I(x)\big] \;=\; -\sum_x p(x)\log p(x)

It answers a natural question: on average, how uncertain am I about XX? Two extremes pin down the intuition:

  • Deterministic. If XX takes one value with probability 1, then H(X)=1log1=0H(X) = -1\cdot\log 1 = 0. There is nothing to learn from a sample, because we already know the answer.
  • Uniform. If p(x)=1/Kp(x)=1/K for each of KK outcomes, then H(X)=1Klog1K=logKH(X) = -\sum \frac{1}{K}\log\frac{1}{K} = \log K, the maximum possible for a KK-outcome distribution. Every outcome is equally surprising.

The figure below makes this concrete, showing the entropy of several distributions: largest for the uniform ones and zero for the deterministic ones.

Entropy across probability distributions

In ML, entropy shows up wherever we want a distribution to be “spread out”: exploration bonuses in RL, regularizing softmax outputs to avoid overconfidence, and encouraging diverse generations in language models via temperature.

Joint and conditional entropy

Entropy describes the uncertainty in one variable. As soon as we have two variables with joint distribution p(x,y)p(x,y), the same averaging idea produces two more quantities.

Joint entropy is the uncertainty in the pair (X,Y)(X,Y) taken together:

H(X,Y)  =  x,yp(x,y)logp(x,y)H(X,Y) \;=\; -\sum_{x,y} p(x,y)\log p(x,y)

Conditional entropy is the uncertainty left in YY after we have already seen XX:

H(YX)  =  x,yp(x,y)logp(yx)H(Y\mid X) \;=\; -\sum_{x,y} p(x,y)\log p(y\mid x)

If XX already determines YY, this is zero; if XX tells us nothing about YY, it collapses back to H(Y)H(Y).

Chain rule. These two quantities combine cleanly:

H(X,Y)  =  H(X)+H(YX)  =  H(Y)+H(XY)H(X,Y) \;=\; H(X) + H(Y\mid X) \;=\; H(Y) + H(X\mid Y)

In words, the uncertainty about the pair equals the uncertainty in the first variable plus the leftover uncertainty in the second once the first is known. The proof simply factors p(x,y)=p(x)p(yx)p(x,y)=p(x)\,p(y\mid x) inside the logarithm and splits the sum, the same move (turning a product of probabilities into a sum of log-probabilities) that powered the definition of self-information. It extends to any number of variables in the obvious way.

Cross entropy

Every quantity so far has used a single distribution, measuring a variable against its own probabilities. Real machine learning almost always involves two distributions on the same outcomes: a true distribution pp (the data, the labels, the world) and a model qq we are trying to fit. The first quantity that relates them is the cross-entropy of pp relative to qq, the average surprise that the model qq assigns to samples drawn from the true distribution pp:

H(p,q)  =  Exp[logq(x)]  =  xp(x)logq(x)H(p,q) \;=\; \mathbb{E}_{x\sim p}\big[-\log q(x)\big] \;=\; -\sum_x p(x)\log q(x)

The surprise of observing xx under the model is logq(x)-\log q(x), which is large when qq thinks xx is rare. The world, however, sends samples from pp rather than qq, so the relevant average is taken under the true distribution. Intuitively, H(p,q)H(p,q) is the per-sample description cost of building our encoding around qq and then running it on samples from pp: we pay more nats per symbol than we would have under the right model.

The figure below shows cross-entropy for a few source and target pairs, spiking when the model places little probability on outcomes the truth considers likely:

Cross entropy

It helps to check that the cross-entropy definition behaves the way we would expect, and two observations do exactly that. First, when q=pq=p, cross-entropy collapses back to plain entropy, H(p,p)=H(p)H(p,p)=H(p), so the right model wastes nothing. Second, H(p,q)H(q,p)H(p,q)\neq H(q,p) in general, because the first argument is the sampling distribution (which outcomes we actually see) while the second is the scoring distribution (how surprised the model is). Swap them and both roles flip. That asymmetry reappears, sharper, in the quantity we turn to next.

KL divergence

Cross-entropy measures the total cost of describing pp‘s samples with qq. Some of that cost is unavoidable, since even the perfect model still pays H(p)H(p). Subtracting this floor leaves the extra cost of using the wrong model, and that excess is the Kullback–Leibler divergence:

KL(pq)  =  H(p,q)H(p)  =  Exp ⁣[logp(x)q(x)]  =  xp(x)logp(x)q(x)\mathrm{KL}(p \parallel q) \;=\; H(p,q) - H(p) \;=\; \mathbb{E}_{x\sim p}\!\left[\log\frac{p(x)}{q(x)}\right] \;=\; \sum_x p(x)\log\frac{p(x)}{q(x)}

Read it as a question: on average over samples from pp, how many extra nats does it cost to describe them using qq instead of pp? If q=pq=p, every log(p/q)\log(p/q) term is log1=0\log 1 = 0, so KL(pp)=0\mathrm{KL}(p\parallel p)=0. The further qq drifts from pp, the higher the penalty.

A Jensen-inequality argument on the concave logarithm shows that KL(pq)0\mathrm{KL}(p\parallel q)\ge 0 always, with equality only when p=qp=q almost everywhere. This non-negativity is not an extra axiom; it is exactly the statement that we cannot out-compress the source by using the wrong model. A few properties are worth internalizing:

  • Non-negative: KL(pq)0\mathrm{KL}(p\parallel q)\ge 0, and zero only when p=qp=q.
  • Asymmetric: KL(pq)KL(qp)\mathrm{KL}(p\parallel q)\neq \mathrm{KL}(q\parallel p) in general, so it is not a distance.
  • Mode behavior matters. KL(pq)\mathrm{KL}(p\parallel q) punishes qq for putting low mass where pp has mass (mass-covering, so qq spreads out), while KL(qp)\mathrm{KL}(q\parallel p) punishes qq for putting mass where pp has none (mode-seeking, so qq collapses onto a peak).
  • Which direction shows up where. Variational inference (including VAEs) and most policy-gradient RL use the reverse form KL(qp)\mathrm{KL}(q\parallel p), because that is the side we can sample from, since the expectation is taken under qq, the distribution we control. Maximum-likelihood training uses the forward form KL(pq)\mathrm{KL}(p\parallel q), because data gives us samples from pp for free. Availability of samples, not aesthetics, picks the direction, and the direction picks the qualitative answer.

Cross entropy and KL: the relation

We arrived at KL by subtracting H(p)H(p) from cross-entropy, so rearranging recovers the relation directly:

H(p,q)  =  H(p)+KL(pq)H(p,q) \;=\; H(p) + \mathrm{KL}(p \parallel q)

The first term is the irreducible uncertainty in pp, fixed by the source, and the second is the gap our model leaves on the table. Cross-entropy is the absolute description cost, while KL is the part of that cost that is our fault.

This identity drives most of supervised deep learning: minimizing H(p,q)H(p,q) over qq is the same as minimizing KL(pq)\mathrm{KL}(p\parallel q), because H(p)H(p) does not depend on the model. The two objectives differ only by an additive constant that the gradient never sees.

In supervised classification, pp is the one-hot label distribution (p(y)=1p(y^\star)=1 and zero elsewhere) and qq is the model’s softmax qθ(yx)q_\theta(y\mid x). Since the one-hot pp has H(p)=0H(p)=0, cross-entropy and KL coincide exactly, and both reduce to a single term:

L(x,y)  =  logqθ(yx)\mathcal{L}(x,y^\star) \;=\; -\log q_\theta(y^\star\mid x)

which is the negative log-likelihood of the true label. So “training a classifier with cross-entropy loss,” “minimizing KL to the one-hot label,” and “maximum likelihood estimation” are the same procedure described in three vocabularies.

Beyond unifying these three views, cross-entropy has a practical virtue that explains why it became the default loss: it is remarkably well-behaved under gradient descent. If qθq_\theta is a softmax over logits zRKz\in\mathbb{R}^K, a short computation gives

zyH(p,qθ)  =  qθ(y)p(y)\frac{\partial}{\partial z_y}\, H(p,q_\theta) \;=\; q_\theta(y) - p(y)

The gradient with respect to the logits is simply prediction minus target. There is no softmax derivative to chase and no vanishing-gradient pathology even when qθq_\theta is confidently wrong. Every other natural classification loss, such as squared error on probabilities or hinge, gives up some of this, because the logarithm is exactly what undoes the exponential in the softmax. This identity is why cross-entropy is the default and why training stays numerically stable across many orders of magnitude in qθq_\theta.

Label smoothing. Replace the one-hot target with a softer one that gives the true class 1ϵ1-\epsilon and spreads ϵ\epsilon over the rest. The target pp now has nonzero mass on every class, so qq can never push a logit to -\infty without paying for it. This gently prevents the model from becoming arbitrarily confident, which often improves calibration and generalization. More broadly, almost every “match this distribution to that one” loss in deep learning is a KL in disguise, and most of those KLs reach us through the cross-entropy door.

Jensen–Shannon divergence

KL answers an inherently asymmetric question: how badly does qq describe samples from pp? The moment we try to use it as a general-purpose distance between two distributions on equal footing, three pathologies surface. It is asymmetric, it is unbounded (taking values in [0,+][0,+\infty]), and it blows up to ++\infty whenever the two distributions have disjoint support. That last case is common, since two distributions on low-dimensional manifolds in a high-dimensional space generically have disjoint support, so KL reports “infinitely far apart” and offers no usable gradient.

The Jensen–Shannon divergence fixes all three problems by measuring each distribution’s gap to their average rather than to each other. With m=12(p+q)m=\tfrac12(p+q):

JSD(pq)  =  12KL(pm)+12KL(qm)\mathrm{JSD}(p\parallel q) \;=\; \tfrac12\,\mathrm{KL}(p\parallel m) + \tfrac12\,\mathrm{KL}(q\parallel m)

Because mm inherits the union of the two supports, neither KL inside can blow up. JSD is symmetric, bounded in [0,log2][0,\log 2] (the upper limit reached exactly when pp and qq have disjoint support), and JSD\sqrt{\mathrm{JSD}} is a genuine metric. Collecting terms gives a clean identity in entropy alone:

JSD(pq)  =  H(m)12H(p)12H(q)\mathrm{JSD}(p\parallel q) \;=\; H(m) - \tfrac12 H(p) - \tfrac12 H(q)

So JSD is the entropy of the mixture minus the mixture of entropies, which is the Jensen gap of entropy under mixing and the source of its name.

Where it shows up: GANs. For the optimal discriminator, the original GAN objective reduces to minG2JSD(pdatapg)2log2\min_G 2\,\mathrm{JSD}(p_\text{data}\parallel p_g) - 2\log 2. But the very property that makes JSD well-defined on disjoint supports, namely saturating at log2\log 2, is also what kills its gradient in practice. When pdatap_\text{data} and pgp_g live on different manifolds, JSD equals log2\log 2 almost everywhere and the gradient vanishes. This is the pathology that Wasserstein GANs were designed to escape, by switching to a divergence whose value tracks geometric distance rather than support overlap.

Mutual information

JSD compares two arbitrary distributions. One particular comparison is so useful that it earns its own name: measuring a joint distribution against the product of its marginals. That is mutual information, and it asks how much does knowing XX reduce my uncertainty about YY? Equivalently, it asks how far the joint p(x,y)p(x,y) sits from the product p(x)p(y)p(x)p(y) that would hold if the two variables were independent:

I(X;Y)  =  KL(p(x,y)p(x)p(y))  =  x,yp(x,y)logp(x,y)p(x)p(y)I(X;Y) \;=\; \mathrm{KL}\big(p(x,y)\parallel p(x)p(y)\big) \;=\; \sum_{x,y} p(x,y)\log\frac{p(x,y)}{p(x)p(y)}

Three equivalent rewrites are worth knowing by heart, since each gives a different angle on the same number:

I(X;Y)=H(X)H(XY)=H(Y)H(YX)=H(X)+H(Y)H(X,Y)I(X;Y) = H(X) - H(X\mid Y) = H(Y) - H(Y\mid X) = H(X) + H(Y) - H(X,Y)

Mutual information is symmetric, non-negative, and zero exactly when XX and YY are independent. Unlike correlation, it captures any statistical dependence rather than only linear ones. In representation learning it is the quantity we try to maximize between an input and its embedding (to preserve information) and minimize between an embedding and a nuisance variable (to remove it).

Information gain: MI under another name. The quantity H(Y)H(YX)=I(X;Y)H(Y)-H(Y\mid X)=I(X;Y) is called information gain in two ML lineages. In decision trees (ID3, C4.5) it scores a feature split, where we pick the XX that maximizes I(Y;X)I(Y;X). In Bayesian active learning and RL exploration it scores a query or action by how much its outcome would shrink the posterior over parameters. Different names, same mutual information.

Two structural results: DPI and Fano

Mutual information obeys two inequalities that appear often enough in ML to deserve their own names.

Data processing inequality. If XYZX\to Y\to Z forms a Markov chain (so ZZ depends on XX only through YY), then

I(X;Z)    I(X;Y)I(X;Z) \;\le\; I(X;Y)

Post-processing cannot create information. A learned representation YY of an input XX, followed by any classifier Z=f(Y)Z=f(Y), can at best preserve information about XX and never manufacture more. This is the formal reason that every layer of a deep network is at best information-preserving in XX, and that lossy compression has a real floor.

Fano’s inequality. Where DPI says high mutual information is necessary for good prediction, Fano makes the statement quantitative. The conditional uncertainty H(YX)=H(Y)I(X;Y)H(Y\mid X) = H(Y)-I(X;Y) lower-bounds how well any prediction rule can do. If XX and YY are not informative enough about each other, no classifier, however clever, can drive the error below the Fano floor. This is the inequality behind information-theoretic lower bounds on sample complexity.

KL in variational inference: the ELBO and VAEs

These quantities are not only descriptive; they appear directly inside training objectives. The clearest example is the variational autoencoder, a latent-variable generative model with three pieces: a prior p(z)p(z) over latents (usually a standard Gaussian), a decoder pθ(xz)p_\theta(x\mid z) that maps a latent back to a distribution over observations, and an encoder qϕ(zx)q_\phi(z\mid x) that maps an observation to a distribution over latents. The encoder produces Gaussian parameters μ\mu and σ\sigma, and the reparameterized sample z=μ+σϵz=\mu+\sigma\odot\epsilon with ϵN(0,I)\epsilon\sim\mathcal{N}(0,I) is passed through the decoder to a reconstruction.

The goal is the same as for any generative model: maximize the log-likelihood logpθ(x)=logpθ(xz)p(z)dz\log p_\theta(x) = \log\int p_\theta(x\mid z)p(z)\,dz. The trouble is that this marginal is intractable. The integral has no closed form once pθp_\theta is a neural network, and naive Monte Carlo from the prior collapses, because the prior covers the latent space democratically while the integrand cares about a tiny neighborhood, and democratic sampling does not mix well with concentrated mass.

The variational move introduces a tractable surrogate posterior qϕ(zx)q_\phi(z\mid x) and applies Jensen’s inequality to the logarithm:

logpθ(x)    Eqϕ(zx)[logpθ(xz)]reconstruction    KL(qϕ(zx)p(z))prior matching    ELBO(x;θ,ϕ)\log p_\theta(x) \;\ge\; \underbrace{\mathbb{E}_{q_\phi(z\mid x)}\big[\log p_\theta(x\mid z)\big]}_{\text{reconstruction}} \;-\; \underbrace{\mathrm{KL}\big(q_\phi(z\mid x)\parallel p(z)\big)}_{\text{prior matching}} \;\equiv\; \mathrm{ELBO}(x;\theta,\phi)

The gap between this evidence lower bound and the true log-likelihood is exactly KL(qϕ(zx)pθ(zx))\mathrm{KL}(q_\phi(z\mid x)\parallel p_\theta(z\mid x)), so tightening the bound is pushing the approximate posterior toward the true one. The two ELBO terms have clear physical meanings. Reconstruction asks the decoder to actually rebuild xx from its latent, and the KL term keeps the encoder honest by stopping it from inventing latent codes that look nothing like samples from the prior. The whole VAE objective is one entropy quantity balanced against one KL.

The information bottleneck

The VAE balances reconstruction against a KL penalty. A closely related idea makes the trade-off between compression and prediction completely explicit. A representation ZZ of an input XX used to predict a target YY should forget everything about XX that is not useful for YY, and the information bottleneck formalizes this as a trade-off:

minp(zx)  I(X;Z)βI(Z;Y)\min_{p(z\mid x)}\; I(X;Z) - \beta\, I(Z;Y)

The first term penalizes how much ZZ remembers about XX (compression), and the second rewards how much ZZ tells us about YY (prediction). The coefficient β\beta slides between a maximally compressive and a maximally predictive representation. The deep variational information bottleneck replaces the intractable mutual informations with variational bounds and trains end to end, and algorithmically it looks much like a supervised VAE: the same KL-versus-reconstruction trade-off with a different motivation.

Estimating mutual information with neural networks

Both the VAE and the information bottleneck lean on mutual information, which is easy to write down and notoriously hard to estimate. In high dimensions we cannot enumerate p(x,y)p(x,y), and density-ratio estimation is fragile. Two ideas have become standard.

MINE uses a Donsker–Varadhan bound on KL. It trains a network Tθ(x,y)T_\theta(x,y) to distinguish samples from the joint p(x,y)p(x,y) from samples drawn from the product of marginals p(x)p(y)p(x)p(y), and reads off a lower bound on I(X;Y)I(X;Y) from how well the network does. The network itself is a learned critic.

InfoNCE (contrastive predictive coding) optimizes a lower bound on mutual information with a noise-contrastive softmax. Given a query xx, one positive yy drawn from the joint, and K1K-1 negatives drawn from the marginal, the model classifies which candidate is the positive. The resulting loss is the backbone of essentially all modern self-supervised learning (SimCLR, MoCo, CLIP). The maximum mutual information any InfoNCE-style estimator can certify is logK\log K, a fundamental ceiling, which is why batch size matters so much in contrastive learning.

Entropy as a regularizer: maximum-entropy RL

Mutual information served as a training signal in the last few sections. Entropy can play the same role directly. A pure reward-maximizing objective in reinforcement learning collapses the policy onto whichever action looks best so far, which kills exploration. Maximum-entropy RL adds an entropy bonus to the reward, so the agent chases return while keeping its action distribution spread out:

J(π)  =  Eπ ⁣[tr(st,at)+αH(π(st))]J(\pi) \;=\; \mathbb{E}_\pi\!\left[\sum_t r(s_t,a_t) + \alpha\,H\big(\pi(\cdot\mid s_t)\big)\right]

The temperature α\alpha trades exploitation against exploration, and Soft Actor-Critic is the canonical implementation.

The entropy term is not a standalone trick; it is a KL in disguise. For a uniform reference policy π0(as)=1/A\pi_0(a\mid s)=1/|\mathcal{A}|,

KL(π(s)π0)  =  H(π(s))+logA\mathrm{KL}\big(\pi(\cdot\mid s)\parallel \pi_0\big) \;=\; -H\big(\pi(\cdot\mid s)\big) + \log|\mathcal{A}|

so maximizing entropy is, up to a constant, the same as minimizing the KL from the policy to a uniform prior. Replace the uniform π0\pi_0 with a learned reference and we recover the KL-to-reference objective used in RLHF and in PPO-with-reference. The entropy bonus, the KL penalty in RLHF, and the trust-region term in PPO are all the same regularizer with different reference distributions.

From discrete to continuous

Every formula so far has used sums over a discrete alphabet. Moving to continuous variables means replacing sums with integrals and probabilities with densities, which turns entropy into differential entropy:

h(X)  =  p(x)logp(x)dxh(X) \;=\; -\int p(x)\log p(x)\,dx

The change from HH to hh flags that something is different. Differential entropy can be negative, because a density can exceed 1 on small regions, so logp-\log p can be negative and the integral can come out negative too. It is best treated as a relative quantity rather than an absolute one. It is also not reparametrization-invariant, since a change of variables leaks in a Jacobian term, so rescaling a VAE’s latent changes its entropy.

The reassuring news is that KL and mutual information stay well-behaved in the continuous case, because the Jacobian appears in both densities and cancels in the ratio p/qp/q or p(x,y)/(p(x)p(y))p(x,y)/(p(x)p(y)). This is why almost every information-theoretic objective in deep learning is a KL or a mutual information rather than a raw entropy. Entropy still appears, in the ELBO and in maximum-entropy RL, but only inside combinations where the bad behavior cancels.

Summary

Three quantities have carried this entire story of information theory. First, Entropy measures the uncertainty inside a single distribution, built up from the surprise of one outcome. Second, KL divergence measures the gap between two distributions, the excess cost of describing one with the other. Third, Mutual information measures how tightly two variables are coupled, and it is itself a KL between a joint distribution and the product of its marginals.

Everything else has been a re-skin of these three concepts. Cross-entropy is entropy plus a KL. The classification loss, maximum likelihood, and KL to a one-hot label are one objective wearing three names. The VAE balances a reconstruction term against a KL to the prior, the information bottleneck trades one mutual information against another, contrastive learning maximizes a bound on mutual information, and maximum-entropy RL is a KL to a reference policy in an entropy costume. Even the move from discrete to continuous leaves KL and mutual information intact while only entropy needs extra care.