KL divergence
KL(P∥Q) is target-weighted log probability ratio, equal to cross-entropy minus target entropy.
On this page 7 sections
01KL as excess log loss#
KL divergence measures the extra expected log loss caused by using a forecast when outcomes follow . The target’s own entropy is unavoidable uncertainty; cross-entropy includes that baseline plus the cost of mismatch. Their difference isolates the mismatch.
KL divergence measures excess expected log loss from using forecast Q when the target distribution is P, with that argument order.
Target entropy 0.75 bits and cross-entropy 1.25 bits give KL 0.5 bits. Subtract the target’s own uncertainty to isolate the excess loss.
If target entropy is 0.75 bits and forecast cross-entropy is 1.25 bits, the excess is bits. The order matters: supplies the expectation weights. Reversing the distributions asks a different question and can even change a finite value to infinity.
bits and bits. Find in bits.
Show answer and explanation
Subtract target entropy from cross-entropy: bits.
02Computing the divergence#
You can also compute directly: use P as weights on the log probability ratios.
Individual log ratios may be negative; the complete divergence is nonnegative.
Same order: P=, Q=. Use log₂(P/Q)=. Find D(P∥Q) in bits, rounded to 4 decimals.
Show answer and explanation
P-weighted terms: 0−0.25+0.5=0.25 bits.
03Zero probabilities and argument order#
Zero P mass contributes zero. Positive P mass where Q is zero makes D(P∥Q) infinite.
KL need not stay the same when its arguments are swapped. It is not a symmetric distance, even though some particular pairs have equal values.
Same order: P=, Q=. Draft: KL is symmetric. Correct (D(P∥Q), D(Q∥P)), bits.
Show answer and explanation
Reverse support fails.
Comparing probability models with KL requires specifying which distribution supplies the outcomes. Although it is nonnegative, it is not a distance metric: it need not be symmetric. Individual weighted log-ratio terms can be negative, so sum every category before interpreting the total.
Keep P as the weights, preserve argument order, and check support before calculating.
- Compute a finite-distribution KL divergence with valid support handling.
Sources & further reading
- [1]Cosma Shalizi: Information Theory I, KL and support conventions ↗Cosma Shalizi · Article
- [2]Dive into Deep Learning §22.11 ↗Dive into Deep Learning §22.11 · Article