Skip to content
Infinitesimal Dynamics home
Notebook: 0 XP

Information Theory / Beyond entropy

KL Divergence and Cross-Entropy

The cost, in bits, of believing the wrong distribution, and the loss function behind modern classifiers.

3 min read · 1 sections · 1 checks

Contents 2 sections
  1. §1Extra bits for a wrong model
  2. ?Check yourself

By the end you can

  • Compute KL divergence
  • Relate cross-entropy, entropy and KL divergence
  • Explain why cross-entropy is used as a loss

Helps to know: Compression and Huffman Codes

§1Extra bits for a wrong model

DKL(p ∥ q)=∑xp(x)log⁡2p(x)q(x)H(p,q)=H(p)+DKL(p ∥ q)D_{\mathrm{KL}}(p \,\|\, q) = \sum_x p(x) \log_2 \frac{p(x)}{q(x)} \qquad H(p, q) = H(p) + D_{\mathrm{KL}}(p \,\|\, q)

If data comes from pp but you design your code for qq, you pay DKLD_{\mathrm{KL}} extra bits per symbol. It is never negative, is zero only when p=qp = q, and is not symmetric.

Check yourself

  1. Q1
    DKL(p ∥ p)=D_{\mathrm{KL}}(p \,\|\, p) =