MoonSay /Tutorials/Machine Learning Interview #8
Contents
  1. 01Introduction
  2. 02Surprise
  3. 03Entropy
  4. 04Cross-entropy
  5. 05KL divergence
  6. 06Information gain and mutual information
  7. 07Why training minimizes cross-entropy
  8. 08Common misconceptions
  9. 09Interview questions and model answers
  10. 10Summary
  11. References

09Interview questions and model answers

10 questions · click a question to see a brief answer
Q1Define entropy, cross-entropy and KL divergence. How are they related?
Brief answer

Entropy H(p)=\sum_x\;p(x)\log\frac1{p(x)} is the average surprise when you know the true distribution. Cross-entropy H_{\mathrm{ce}}(p,q)=\sum_x\;p(x)\log\frac1{q(x)} is the average surprise when reality is p but you expect q. KL is the difference: D_{\mathrm{KL}}(p\,\|\,q)=\sum_x\;p(x)\log\frac{p(x)}{q(x)}=H_{\mathrm{ce}}(p,q)-H(p). A Cupertino native in Miami: 4.62=0.99+3.63 bits per day.

Q2Why is KL divergence never negative?
Brief answer

By Jensen’s inequality for the concave log: -D_{\mathrm{KL}}=\E_p[\log\frac qp]\le\log\E_p[\frac qp]\le\log1=0, with equality only when q=p. Hence cross-entropy is never below entropy, and the uniform distribution maximizes entropy, H\le\log K.

Q3Is KL divergence a distance metric?
Brief answer

No. It is not symmetric (between the two cities, 3.63 bits one way and 1.15 the other) and it violates the triangle inequality (for coins 0.1, 0.5, 0.9: 2.54>0.53+0.74). It is a divergence: non-negative and zero only when the distributions are equal.

Q4Why do we minimize cross-entropy rather than KL? Why is it the same as maximum likelihood?
Brief answer

H_{\mathrm{ce}}(\hat p,q_\theta)=H(\hat p)+D_{\mathrm{KL}}(\hat p\,\|\,q_\theta), and H(\hat p) does not depend on \theta, so both have the same minimizer and gradient; cross-entropy is cheaper because it needs no \log\hat p. On the empirical distribution, cross-entropy is the average negative log-likelihood, so minimizing it is maximum likelihood.

Q5What is the difference between forward and reverse KL? Where is each used?
Brief answer

Forward D_{\mathrm{KL}}(p\,\|\,q) averages over p and punishes q\approx0 where p>0: mode-covering, the maximum-likelihood fit. Reverse D_{\mathrm{KL}}(q\,\|\,p) averages over q and punishes q>0 where p\approx0: mode-seeking. Fitting one Gaussian to Old Faithful: forward gives \mu=3.49,\sigma=1.14 over both humps; reverse gives \mu=4.28,\sigma=0.43 on one. Variational inference and variational autoencoders use reverse KL.

Q6What happens if a model assigns probability zero to an outcome that occurs?
Brief answer

Its surprise \log\frac10 and hence the cross-entropy and KL are infinite. Models therefore output strictly positive probabilities (softmax, sigmoid), and implementations work with logits and log-probabilities for numerical stability; label smoothing also keeps targets away from exact zeros and ones.

Q7What is information gain in a decision tree?
Brief answer

The drop in label entropy from a split: H(Y)-\sum_vP(X=v)H(Y\mid X=v), which equals the mutual information I(X;Y). Not knowing the city, tomorrow’s weather has 0.86 bits; knowing it, 0.51; the split on city gains 0.35 bits. It favors many-valued features, hence the gain ratio of C4.5.

Q8What is perplexity?
Brief answer

2 to the cross-entropy in bits (e to the cross-entropy in nats): the effective number of equally likely choices the model is choosing between. About 2 for Miami’s daily forecast; 101 for GPT-2 on a sentence from Shannon’s 1951 experiment (6.66 bits per token). 1 is a perfect model.

Q9Bits or nats: why does machine learning use the natural log?
Brief answer

The base only sets the unit (1 nat =1.443 bits). Models output probabilities that are e raised to a power (softmax, sigmoid, Gaussian), and \ln undoes the exponential, so losses and gradients are simple, e.g. -\ln\mathrm{softmax}_c=-z_c+\ln\sum_je^{z_j} with gradient p-y. Coding theory uses bits, since a bit is one yes-or-no answer.

Q10Which distribution has the largest entropy?
Brief answer

On K outcomes, the uniform distribution, with H=\log K, because D_{\mathrm{KL}}(p\,\|\,u)=\log K-H(p)\ge0. Among densities with a fixed variance, the Gaussian. Entropy is 0 for a certain outcome.