Before you readTwo models look at a scan that does show a tumour. One says the probability is 0.6, the other 0.99. Both are right. Which deserves the better score, and by how much?
Watch: A Short Introduction to Entropy, Cross-Entropy and KL-Divergence · Aurélien Géron
An outcome with probability p carries −log2p bits of surprise: a certain event carries none, and a one-in-1,024 event carries 10. The entropy of a distribution is its average surprise:
H(p)=−x∑p(x)logp(x).
It is 0 when one outcome is certain, and largest, log2K bits, when all K outcomes are equally likely.
If the data come from p but you predict with a model q, your average surprise is the cross-entropy, and the extra over the entropy is the KL divergence:
KL is zero only when q=p, and it is not symmetric: in general KL(p∥q)=KL(q∥p).
Classifiers are trained by minimising cross-entropy. The label is a p that puts all its mass on the true class, so the loss for one example is −logq(true class). That answers the question above: −ln0.6≈0.51 against −ln0.99≈0.01. Minimising it is the same as maximising the likelihood (see Statistics and optimisation). In ML the log is usually natural, which measures surprise in nats instead of bits.
A coin lands heads with chance p, and a model predicts q. The pink curve is the cross-entropy H(p, q), the average surprise when you expect q. Its lowest point is at q = p, where it meets the dashed entropy H(p). The purple gap is the KL divergence, the extra bits paid for the wrong model.
Recall
What is the entropy H(p) of a distribution?
Recall
What is the cross-entropy H(p,q), and when is it smallest?
Recall
What is the cross-entropy loss for one labelled example?
Exercise
What is the entropy, in bits, of a fair four-sided die?
Exercise
What is the entropy, in bits, of p=(21,41,41)?
Exercise
A classifier gives the true class probability 0.8. What is its cross-entropy loss in nats, to three decimal places?