← Probability & Information Theory
Contents · Probability & Information Theory

Part 0 · Expectation & Monte Carlo

Part 1 · Entropy & Divergence

  • Entropy, KL Divergence, and Cross-Entropy

Part 2 · Score-Function Gradient

Part 3 · Bayesian Estimation

Part 1 · Entropy & Divergence

Entropy, KL Divergence, and Cross-Entropy

Reinforcement learning is full of distributions: a policy over actions, a target over labels, an old policy and a new one. Three quantities measure them. Entropy scores how spread out one distribution is, KL divergence scores how far one sits from another, and cross-entropy is the sum of the two. Every policy regularizer and trust region in modern RL is built from these.

Entropy: how uncertain is one distribution

For a discrete distribution pp, the Shannon entropy is

H(p)=−∑xp(x) log⁡p(x)=Ex∼p ⁣[−log⁡p(x)]≥0.H(p) = -\sum_x p(x)\,\log p(x) = \mathbb{E}_{x\sim p}\!\left[-\log p(x)\right] \ge 0 .

It is the average surprise, in nats when log⁡\log is natural. A point mass has H=0H = 0; the uniform distribution over kk outcomes maximizes it at log⁡k\log k. For a continuous density the sum becomes an integral, the differential entropy h(p)=−∫p(x)log⁡p(x) dxh(p) = -\int p(x)\log p(x)\,dx, which for a Gaussian is1

h(N(μ,σ2))=12log⁡ ⁣(2πe σ2).h\big(\mathcal{N}(\mu,\sigma^2)\big) = \tfrac{1}{2}\log\!\big(2\pi e\,\sigma^2\big) .

Only the variance enters: a wider Gaussian is more uncertain.

KL divergence: how far apart are two

The Kullback-Leibler divergence of pp from qq is

D(q ∥ p)=∑xq(x) log⁡q(x)p(x)=Ex∼q ⁣[log⁡q(x)p(x)].D(q\,\|\,p) = \sum_x q(x)\,\log\frac{q(x)}{p(x)} = \mathbb{E}_{x\sim q}\!\left[\log\frac{q(x)}{p(x)}\right] .

By Jensen's inequality D(q ∥ p)≥0D(q\,\|\,p) \ge 0, with equality iff q=pq = p almost everywhere.2 It is not a metric: it is asymmetric, D(q ∥ p)≠D(p ∥ q)D(q\,\|\,p) \ne D(p\,\|\,q) in general, and obeys no triangle inequality. The expectation is taken under qq, so D(q ∥ p)D(q\,\|\,p) penalizes placing qq-mass where pp is small, and ignores regions qq never visits.

Cross-entropy ties them together

The cross-entropy of qq relative to pp is H(q,p)=−∑xq(x)log⁡p(x)H(q,p) = -\sum_x q(x)\log p(x). Splitting the log of the ratio gives the identity that connects all three:

H(q,p)=−∑xq(x)log⁡p(x)=−∑xq(x)log⁡q(x)⏟H(q)+∑xq(x)log⁡q(x)p(x)⏟D(q ∥ p).H(q,p) = -\sum_x q(x)\log p(x) = \underbrace{-\sum_x q(x)\log q(x)}_{H(q)} + \underbrace{\sum_x q(x)\log\frac{q(x)}{p(x)}}_{D(q\,\|\,p)} .  H(q,p)=H(q)+D(q ∥ p) \boxed{\,H(q,p) = H(q) + D(q\,\|\,p)\,}

Since H(q)H(q) does not depend on pp, minimizing cross-entropy over pp is exactly minimizing D(q ∥ p)D(q\,\|\,p). That is why the classification and behavior-cloning loss is written as a cross-entropy: with qq the fixed data (or expert) distribution, driving H(q,p)H(q,p) down pulls the model pp toward qq.

Why RL cares

Three uses, one toolkit.

  • Entropy bonus. Maximum-entropy RL augments the reward with the policy entropy, optimizing E[∑trt+α H(π(⋅∣st))]\mathbb{E}\big[\sum_t r_t + \alpha\,H(\pi(\cdot\mid s_t))\big] so the agent solves the task while staying as random as possible. This keeps exploration alive and yields more robust policies; Soft Actor-Critic makes the entropy term the center of the objective.3
  • KL trust region. TRPO limits how far each update moves the policy by constraining Es[D(πold ∥ πθ)]≤δ\mathbb{E}_s\big[D(\pi_{\text{old}}\,\|\,\pi_\theta)\big] \le \delta, a trust region in distribution space rather than in parameters.
  • Cross-entropy loss. Supervised heads, including behavior cloning of an expert, minimize cross-entropy, which by the identity above is fitting pp to the data distribution qq by KL.

Worked example: KL between two Gaussians

The KL of one 1D Gaussian from another has a closed form,1

D ⁣(N(μ1,σ12) ∥ N(μ2,σ22))=log⁡σ2σ1+σ12+(μ1−μ2)22σ22−12.D\!\big(\mathcal{N}(\mu_1,\sigma_1^2)\,\|\,\mathcal{N}(\mu_2,\sigma_2^2)\big) = \log\frac{\sigma_2}{\sigma_1} + \frac{\sigma_1^2 + (\mu_1-\mu_2)^2}{2\sigma_2^2} - \frac{1}{2} .

Take q=N(0,1)q = \mathcal{N}(0,1) and p=N(1,1)p = \mathcal{N}(1,1), so only the means differ by one. Then log⁡(σ2/σ1)=log⁡1=0\log(\sigma_2/\sigma_1) = \log 1 = 0, and

D(q ∥ p)=0+1+(0−1)22⋅1−12=22−12=0.5.D(q\,\|\,p) = 0 + \frac{1 + (0-1)^2}{2\cdot 1} - \frac{1}{2} = \frac{2}{2} - \frac{1}{2} = 0.5 .

With equal variances the reverse is identical, D(p ∥ q)=0.5D(p\,\|\,q) = 0.5: the mean gap enters symmetrically. The asymmetry shows up once the widths differ. Compare a broad q=N(0,4)q = \mathcal{N}(0,4) against a narrow reference p=N(0,1)p = \mathcal{N}(0,1):

D(q ∥ p)=log⁡12+4+02⋅1−12=−0.693+2−0.5=0.807,D(q\,\|\,p) = \log\tfrac{1}{2} + \frac{4 + 0}{2\cdot 1} - \tfrac{1}{2} = -0.693 + 2 - 0.5 = 0.807 , D(p ∥ q)=log⁡2+1+02⋅4−12=0.693+0.125−0.5=0.318.D(p\,\|\,q) = \log 2 + \frac{1 + 0}{2\cdot 4} - \tfrac{1}{2} = 0.693 + 0.125 - 0.5 = 0.318 .

Same pair of Gaussians, two different numbers. The forward direction D(q ∥ p)D(q\,\|\,p) is larger because qq has mass out in the tails where the narrow pp is nearly zero, and log⁡(q/p)\log(q/p) blows up there; the reverse averages over the narrow pp, which never sees those tails.

Entropy and KL of two Gaussiansinteractive
H(q) nats
1.419
D(q‖p)
0.500
D(p‖q)
0.500
H(q,p) = H(q)+D(q‖p)
1.919
|D(q‖p) − D(p‖q)|
0.000

Solid accent is q = N(μ, σ²); dashed is the reference p = N(0,1). The two divergences move independently: only μ = 0, σ = 1 sends both to zero.

Fixed reference p = N(0,1) (dashed, muted) and movable q = N(μ, σ²) (solid, accent). The shaded band is where the two densities disagree. Readouts give the differential entropy H(q) = ½ log(2πe σ²), both divergences from the closed form, and the cross-entropy H(q,p) = H(q) + D(q‖p). Start at μ = 1, σ = 1 to reproduce 0.5, 0.5; then raise σ and watch D(q‖p) and D(p‖q) split apart.

The single case that zeroes both divergences is μ=0, σ=1\mu = 0,\ \sigma = 1, where q=pq = p. Everywhere else the two numbers differ, which is why the direction of the KL is a modeling choice, not a detail: a trust region on D(πold ∥ πθ)D(\pi_{\text{old}}\,\|\,\pi_\theta) constrains something genuinely different from its reverse.

Footnotes

  1. Cover & Thomas, Elements of Information Theory (2006). ↩ ↩2

  2. MacKay, Information Theory, Inference, and Learning Algorithms (2003). ↩

  3. Haarnoja et al., Soft Actor-Critic (2018). ↩