TL;DR
Jensen–Shannon divergence measures how different two probability distributions are. It compares each distribution to their average, and it averages the two resulting KL divergences. The result is symmetric, always finite, and never larger than (one bit).
Its square root is a true distance metric. That makes it a convenient, well-behaved score when KL divergence is too one-sided or infinite.
Where you'll see it: in the original analysis of generative adversarial networks, where training is shown to minimise it, and in comparing word or topic distributions, where a bounded and symmetric score is easy to read.
Intuition
KL divergence asks how badly you are surprised if you believe when the truth is . The question is one-sided. Swap the roles and you get a different number, and if puts zero probability on something can produce, the answer is infinite. That is awkward when you want a plain "how different are these two?"
Jensen–Shannon fixes both problems with one idea. Make a neutral reference point that is halfway between the two distributions, then measure how far each of them sits from that midpoint. Neither distribution is privileged, so the score is symmetric. The midpoint has both distributions' outcomes in it, so it is never zero where either one is nonzero, so nothing blows up.
flowchart LR P[Distribution P] --> M[Midpoint M = average of P and Q] Q[Distribution Q] --> M P --> D1[KL of P from M] Q --> D2[KL of Q from M] M --> D1 M --> D2 D1 --> S[Average of the two = JS divergence] D2 --> S
Think of two bags of coloured marbles. If the bags contain the same colours in the same proportions, both are close to their mixture and the score is zero. If one bag holds only red and the other only blue, each is far from the 50-50 mixture, and the score is at its maximum.
The name has a second meaning. The midpoint's entropy is at least the average of the two entropies. This is a case of Jensen's inequality, because entropy is a concave function. The gap between those two quantities is the divergence.
The math
Let and be probability distributions over the same outcomes. Their midpoint, or mixture, is
Here is the KL divergence of from , and the sum is replaced by an integral for continuous distributions. The logarithm sets the unit. With natural logs the answer is in nats, and with base-2 logs it is in bits.
Equivalent entropy form
Expand the logarithms and the definition becomes a statement about Entropy. Write for Shannon entropy. Then
This form is often the fastest way to compute the divergence by hand, and it shows what the score measures. It is the entropy gained by mixing the two distributions, relative to the average entropy before mixing. Mixing can only make a distribution more spread out, so the score is never negative.
Bounds and zero
Three properties follow from the definition.
- Symmetry. , because swapping and leaves unchanged and just swaps the two terms.
- Finiteness. Wherever , we have . So each KL term in the definition is finite, even when and have disjoint supports. This is the key difference from KL divergence, which is infinite whenever and .
- Bounds and zero. , and the value is zero exactly when . The upper bound is reached when and have disjoint supports. In bits, the maximum is 1.
The square root is a metric. It is symmetric, zero only for identical distributions, and satisfies the triangle inequality. This result was shown by Endres and Schindelin in 2003, and it is why the square root is often used as a distance.
Generalisation to several distributions
For distributions with weights summing to 1, the same idea gives
Its maximum is , the entropy of the weights, so with equal weights on two distributions the maximum is as before.
Worked examples
Let and over outcomes . Use natural logs.
- Midpoint: .
- KL of from : . The last term is zero by the convention .
- KL of from : by symmetry of the example, the same value, .
- JSD: nats.
- Check with the entropy form: , and . Then , which matches.
Interpretation: the two distributions share half their mass on and disagree on the rest. The score is , or exactly half a bit, which is half the maximum of one bit. Half of the mass is in disagreement, and the score reflects that.
Let and . The midpoint is .
- .
- by the same reasoning.
- nats, which is exactly 1 bit.
Interpretation: the score is at its ceiling. It does not matter whether the two outcomes are next to each other or on opposite ends of a long scale. The score says only that they do not overlap, and this is the property that makes JSD a poor training signal in some settings, as discussed below.
A GAN trains a discriminator to tell real samples, drawn from , from generated samples, drawn from a generator distribution . The standard value function is .
For a fixed generator, the best possible discriminator is . Substituting it in and writing gives
Using the example above, where , we get .
Interpretation: the generator is trying to make small. The minimum is zero, reached when , and then the value is . That is the result Goodfellow and colleagues used to show that the generator's objective is tied to a divergence between the real and generated distributions.
Tradeoffs and failure modes
JSD is a good choice when you want a symmetric, bounded score and both distributions are discrete, or when you can estimate densities. Its bounded range makes thresholds easy to set, and the square root gives a metric you can cluster with.
Its weak point is the flat region. Because the score sits at whenever supports are disjoint, moving one distribution a little without making it overlap the other gives no change in the score. For a learner that updates by gradients, this means no signal. This happened in early GAN training, where the generator's distribution and the real distribution sit on lower-dimensional manifolds with little overlap. The Wasserstein distance, which keeps changing as the distributions move closer, was proposed partly to fix this.
It also does not say which way a mismatch goes. KL divergence in either direction tells you that puts mass where does not, or misses mass where has it, and the two directions behave differently. JSD mixes both penalties into one number.
| Situation | Problem | Better choice |
|---|---|---|
| Two discrete distributions, symmetric comparison needed | KL is one-sided and can be infinite | JSD, or its square root as a metric |
| Continuous distributions with disjoint supports | JSD is flat at and gives no gradient | Wasserstein distance |
| Need to know which side is missing mass | JSD blends both directions | One-sided KL in the direction you care about |
| Fitting a model by maximum likelihood | Likelihood already equals KL up to a constant | [[maximum-likelihood-estimation |
| Estimating from samples in high dimensions | Densities are hard to estimate | Classifier-based estimates, such as the GAN discriminator |
History
Shannon introduced entropy in 1948, and Kullback and Leibler defined their divergence in 1951. Jianhua Lin's 1991 paper introduced the symmetrised, bounded version and named it after Jensen, because the construction uses the concavity of entropy, which is the same property that drives Jensen's inequality.
The divergence then sat mostly in information theory and signal processing until the 2010s, when it became central to deep learning. Goodfellow and colleagues' 2014 paper on generative adversarial networks showed that the optimal discriminator turns the generator's objective into a constant plus twice the JSD between the real and generated distributions. That made JSD the working explanation for GAN training, and it also exposed the flat-region problem that later work tried to fix. Endres and Schindelin's 2003 result, that the square root is a metric, had already made it a usable distance for clustering and similarity search.
Check yourself
QuestionWrite the Jensen–Shannon divergence of and in terms of and KL divergences, and say what is.Show answer
is the midpoint of the two distributions. Then . Each distribution is compared to the midpoint, and the two comparisons are averaged.
QuestionLet and . Compute in nats and in bits.Show answer
The midpoint is . . . So nats, which is about bits.
QuestionWhy is JSD always finite, even when KL divergence from to is infinite?Show answer
KL from to is infinite when but for some outcome. In JSD, each KL is taken to the midpoint , and wherever . So the logarithms never see a zero denominator, and both terms stay finite.
QuestionA GAN generator produces samples that lie on a completely different region of space from the real data. Explain why the JSD-based objective gives little signal to improve, and name a distance that avoids the problem.Show answer
When the supports are disjoint, the JSD is at its maximum , and small moves of the generator's distribution that do not create overlap leave the value unchanged. The gradient is therefore zero or uninformative. The Wasserstein distance keeps changing with how far apart the distributions are, so it still gives a useful gradient in this case.
Sources
- Jensen–Shannon divergence (Wikipedia): https://en.wikipedia.org/wiki/Jensen%E2%80%93Shannon_divergence
- Kullback–Leibler divergence (Wikipedia): https://en.wikipedia.org/wiki/Kullback%E2%80%93Leibler_divergence
- Goodfellow et al., Generative Adversarial Networks (2014): https://arxiv.org/abs/1406.2661
- Cover and Thomas, Elements of Information Theory (Wiley): https://onlinelibrary.wiley.com/doi/book/10.1002/047174882X