Skip to content

Cross-Entropy Loss Explained With a Classifier Forecast

Cross-entropy scores a classifier's forecast q against a target distribution p, as minus the sum of p times log q, and it is smallest when q equals p. Goodfellow, Bengio, and Courville define it in Section 3.13 of Deep Learning: "A quantity that is closely related to the KL divergence is the cross-entropy H(P, Q) = H(P) + D_KL(P‖Q), which is similar to the KL divergence but lacking the term on the left: H(P, Q) = −E_{x∼P} log Q(x)." And: "Minimizing the cross-entropy with respect to Q is equivalent to minimizing the KL divergence, because Q does not participate in the omitted term." They note that the KL divergence "is 0 if and only if P and Q are the same distribution in the case of discrete variables" and that "in machine learning we usually use nats and the natural logarithm." For expressions of the form 0 log 0, "By convention, in the context of information theory, we treat these expressions as" zero.

In neural network training, Section 6.2.1.1 says: "Most modern neural networks are trained using maximum likelihood. This means that the cost function is simply the negative log-likelihood, equivalently described as the cross-entropy between the training data and the model distribution."

Try it by hand. Use natural logarithms, so the units are nats. Let the target be p = (0.7, 0.2, 0.1). Its own entropy, which is the cross-entropy of p with itself, is −(0.7 ln 0.7 + 0.2 ln 0.2 + 0.1 ln 0.1) = 0.802. Now a forecast q = (0.5, 0.3, 0.2) gives H(p, q) = −(0.7 ln 0.5 + 0.2 ln 0.3 + 0.1 ln 0.2) = 0.485 + 0.241 + 0.161 = 0.887. The difference, 0.887 − 0.802 = 0.085, is the KL divergence, as the book's identity says. A uniform forecast of (1/3, 1/3, 1/3) scores ln 3 = 1.099, worse, since it ignores what is known. Order matters: H(q, p) = −(0.5 ln 0.7 + 0.3 ln 0.2 + 0.2 ln 0.1) = 1.122, not 0.887. Now the common classification case. If the target is one-hot, p = (1, 0, 0), every term with p = 0 is dropped by the 0 log 0 convention and the loss is just −ln of the probability given to the true class: 0.357 when the forecast gives it 0.7, and 2.303 when it gives it 0.1. Last, a warning. If the target gives positive probability to an outcome and the forecast gives it zero, the loss is infinite. With q = (0.8999, 0.1, 0.0001) for the target above, the loss is already 1.455. Take the family q = (0.9 − ε, 0.1, ε), which always sums to 1: as ε shrinks toward zero the loss grows without bound. I computed all of these in code, and the distributions are invented.

The trap: treating self-entropy as the general cross-entropy, or reading the formula as symmetric. H(p, p) is the entropy of p, the special case where the forecast equals the target, and it is the smallest value H(p, q) can take over all forecasts q, because the KL divergence is not negative. For a forecast that differs from the target, the number is larger. A caution of my own, not from the sections I read: the logarithm base only sets the units, so loss values in bits, nats, and library defaults are not interchangeable, and a forecast that rules out something that can happen is punished without limit.

Prof Rod's rule: compute cross-entropy with the target first and the forecast second, drop the terms where the target is zero, and keep the forecast away from exact zeros.

Goodfellow, Bengio, and Courville, Deep Learning, MIT Press, 2016, Section 3.13, pp. 72–73, and Section 6.2.1.1, p. 174