The interview problem
You are choosing a distribution-matching objective for a model Q that approximates a target P. Explain why KL(P||Q) and KL(Q||P) behave differently. Compute both directions, examine zero probabilities, and explain when descriptions such as “mode covering” and “mode seeking” are useful or misleading.
Original ML fundamentals practice, not a reported company interview. All distributions and calculations are illustrative. Primary reference retrieved September 27, 2026; publication date not stated; interview date not applicable.
1. Name both arguments and the expectation
For normalized distributions on a finite shared alphabet, KL(P||Q)=Σi pi log(pi/qi). Use natural logarithms here, so the unit is nats. The expectation is taken under the first argument P. Reversing arguments changes both the sampling weights and the log ratio. Always state what is fixed and what is optimized before calling one direction forward or reverse.
A term with pi=0 contributes zero, including when qi=0. If pi>0 and qi=0, KL(P||Q) is infinite. Thus finite forward KL requires Q to assign positive probability wherever P does. Reverse KL requires the opposite support inclusion. For continuous distributions the analogous statement concerns absolute continuity, not merely whether sampled points look similar.
KL is nonnegative for normalized distributions, with zero exactly when they agree up to null sets. It is not a metric: symmetry fails, and a triangle inequality is not generally available. Individual summands can be negative even when the total is nonnegative.
2. Compute the asymmetry
Let P=(0.9,0.1) and Q=(0.5,0.5). Forward KL is 0.9 log(1.8)+0.1 log(0.2)=0.368064 nats. Reverse KL is 0.5 log(5/9)+0.5 log(5)=0.510826 nats. Both are valid comparisons of the same distributions, but they weight different outcomes.
The second forward term, 0.1 log(0.2), is about −0.160944. Do not clamp it to zero: doing so changes the divergence. Nonnegativity applies to the sum under the probability constraints, not to every elementwise relative-entropy term.
Now take P=(0.5,0.5,0) and Q=(1,0,0). Forward KL is infinite because P gives positive mass to the second outcome and Q gives it none. Reverse KL is log(2)=0.693147 nats. In reverse KL, outcomes never sampled under Q contribute no direct expectation term, although normalization still couples the probabilities.
In the opposite support situation, P=(1,0) and Q=(0.9,0.1), forward KL is log(1/0.9)=0.105361 nats and reverse KL is infinite. A direction cannot be chosen solely because its name sounds safer: the support contract matters.
3. Connect forward KL to data fitting
With P fixed, KL(P||Qθ)=−H(P)−EP[log Qθ(X)]. The entropy term is independent of θ, so minimizing this KL has the same minimizers as expected negative log likelihood, provided the quantities are well defined. Training on samples from P estimates the second term without needing to evaluate P's probabilities.
For categorical logits z with softmax probabilities q, the cross-entropy gradient for a target distribution p is q−p. This result includes the normalization coupling between classes; treating each qi as an unconstrained independent number would produce a different derivative. With a one-hot label, the target distribution for that example is concentrated on the observed class.
Maximum likelihood on a finite dataset fits its observed evidence. It does not prove that all population modes were sampled. A rare event absent from training can remain poorly modeled even when empirical forward KL or training cross entropy is small.
4. Examine what reverse KL requires
KL(Qθ||P)=EQθ[log Qθ(X)−log P(X)]. Evaluation requires either probabilities under P or an equivalent tractable target density, as well as samples or a sum under Qθ. If only samples from P are available, this expression is not automatically computable. Density estimation or another objective changes the problem and introduces assumptions.
For a fixed finite alphabet, fixed P with positive probabilities, and positive differentiable Qθ, differentiating gives Σi (∇θ qi)[log(qi/pi)+1]. Since Σi ∇θ qi=0, the constant part cancels. Equivalently the gradient is EQθ[∇θ log Qθ(X)(log Qθ(X)−log P(X))]. The distribution used to take the expectation depends on θ; simply differentiating a fixed sample average as if those samples were independent of θ can miss terms.
If P(x)=p̃(x)/Z and the normalizing constant Z is independent of θ, reverse KL differs from EQθ[log Qθ−log p̃] by the constant log Z. This can permit optimization without knowing Z. It does not give the numerical KL without Z, and the argument fails if Z itself changes with the optimized parameters.
5. Test the mode intuition against a restricted family
If Q can equal P and optimization reaches that solution, both directions are minimized by Q=P. Different behavior appears when the approximation family, optimization or available data constrains the solution. “Reverse KL always chooses one mode” is therefore too strong.
Use target P=(0.49,0.02,0.49) and restrict the candidate set to a broad approximation B=(0.25,0.5,0.25) or a concentrated approximation L=(0.98,0.01,0.01). Direct calculation gives KL(P||B)≈0.595108 and KL(P||L)≈1.581213, so forward KL prefers B among these two choices. It penalizes L for giving little probability to the third outcome.
In reverse, KL(B||P)≈1.272966 while KL(L||P)≈0.633435, so reverse KL prefers L. B spends much of its own probability on the low-target-probability middle outcome. L avoids that region but neglects the other high-probability outcome. Adding a reflected version of L would give an equally good reverse candidate by symmetry.
This finite candidate example explains a tendency, not a theorem about every neural network or optimizer. If P were added to the candidate set, its zero divergence would win in both directions. Always connect the mode explanation to a specific target and approximation constraint.
6. Implement the intended objective
define normalized P and Q on the same outcomes
↓
choose expectation direction and support
↓
compute stable log probabilities
↓
apply zero conventions, sum over outcomes
↓
average over examples with explicit weights
↓
verify hand examples and report chosen unitsUse stable log-softmax when the model produces logits. Probabilities that round to zero can create numerical infinities even when the mathematical softmax is positive. Do not silently replace every infinity with a convenient constant; distinguish numerical underflow from an actual support mismatch.
Adding epsilon changes the distributions; treat it as an explicit smoothing rule, not a mathematically neutral repair. For instance, replacing Q=(1,0) with Qε=(1−ε,ε) makes KL((0.5,0.5)||Qε) finite, but it grows without bound as ε approaches zero. Report the smoothing policy and normalize the result. Clamping probabilities independently without normalization may no longer compute a divergence between probability distributions.
Define reduction carefully. Summing over classes gives a per-example KL; averaging over both classes and examples introduces an additional class-count factor. Confirm an API's argument order and whether it expects probabilities or log probabilities using a hand-checkable asymmetric example. An apparently small loss may just reflect a different reduction.
7. Validate the decision, not only the scalar
Test identical distributions, swapped arguments, a zero target probability, a zero model probability and distributions with very small positive mass. For the finite examples, compute exact sums. For sampled estimates, report sampling uncertainty and the sampling distribution. A Monte Carlo average of log ratios can be negative due to sampling error even though the exact KL is nonnegative. In contrast, exactly summing KL between two normalized empirical histograms remains nonnegative.
Inspect where the fitted model allocates mass and which target outcomes it misses. A favorable average divergence can coexist with poor behavior on an important rare subgroup. If the task has asymmetric consequences, evaluate those consequences directly rather than treating a divergence direction as a complete utility function.
For conditional predictions, define the distribution over conditioning contexts used to average KL. Reweighting contexts changes the objective even when the per-context formula is unchanged. Ensure P and Q use aligned vocabularies and outcome semantics; matching vector positions with different meanings produces a meaningless result.
8. Trade-offs and follow-ups
Forward KL connects naturally to observed-data likelihood but can demand probability across all target-supported outcomes. Reverse KL can be tractable for an unnormalized fixed target but may prefer a concentrated approximation under restrictions. Symmetrizing an objective changes these incentives and can still be infinite when supports differ. A finite alternative has its own interpretation and should be chosen deliberately.
Follow-ups: Why is cross entropy different from KL numerically? What changes with base-two logarithms? Can you estimate either direction from samples alone? What happens when a target has disjoint modes? How does smoothing change the optimum? Why can an individual KL summand be negative?
9. Self-review rubric
- Name the fixed target, fitted model and expectation distribution.
- Apply zero-probability conventions in the correct direction.
- Compute an asymmetric example without clamping negative terms.
- Explain the likelihood connection and its fixed-target assumption.
- Account for parameter-dependent sampling in reverse-KL gradients.
- Qualify mode behavior by the approximation family.
- Check normalization, numerical stability, reduction and outcome alignment.
Sources and related practice
SciPy: rel_entr, publication date not stated; retrieved September 27, 2026. Official reference for elementwise relative entropy and zero conventions. The distributions, derivations and model-selection examples here are original worked practice.
Continue with Jensen's inequality and distillation objectives.
Continue your preparation
Choose a connected learning path or browse more ML fundamentals questions. Read our editorial method and attribution policy.