Cross-entropy measures the average logarithmic loss incurred when outcomes follow one probability distribution , but a model assigns probabilities using another probability distribution . For a finite alphabet, write their probability mass functions as and . With base-two logarithms,
The expectation is under the true or stipulated source law , rather than the model . Terms with contribute nothing. If but for any outcome, the cross-entropy is : the model rules out an event that can occur. The result is measured in bits; natural logarithms give nats.
The useful identity is cross-entropy equals entropy plus relative entropy:
The source's information entropy is the baseline uncertainty. The Kullback-Leibler divergence is the extra average cost of a mismatched model, with the true law first and model second. Thus optimizing for a fixed reduces the mismatch, rather than changing the source's own uncertainty. For finite alphabets, the minimum is , attained exactly at .
Take a three-symbol source with . A matched binary prefix code assigns , , , with lengths . A mismatched prefix code instead assigns , , , with lengths , corresponding to . Both prefix codes decode exactly, but the most common symbol is shorter in the matched code. Under the same source ,
The mismatched code costs an extra (1/4) bit per source symbol on average. No information is lost; the penalty is inefficient representation of this source. The mismatch penalty in lossless coding explains when this coding interpretation is exact.
Figure 1.
Two lossless prefix codes have different average lengths for the same source
. Both sets of codewords are prefix codes. The source probabilities are fixed at . Each colored contribution is source probability times codeword length; changing which symbol receives the shortest word increases the total from to bits per source symbol. Details.
Cross-entropy is also useful when evaluating a predictive distribution. A report that places tiny probability on the realized outcome receives a large logarithmic loss, even when it usually predicts the most common outcome correctly. The loss convention is the negative of the reward convention in the logarithmic scoring rule. Its expected loss is uniquely minimized by the correct probability distribution, making it a strictly proper scoring rule. Strictly Proper Scoring Rules, Prediction, and Estimation by Tilmann Gneiting and others treats this connection in §3.1, Example 3; its discussion uses rewards, so the signs are reversed for the losses here.[ref]
I. J. Good's Rational Decisions by I. J. Good appeared in 1952. Its published summary describes a method for encouraging accurate probability estimates and a connection with information theory. Strictly Proper Scoring Rules, Prediction, and Estimation by Tilmann Gneiting and others traces the logarithmic scoring rule at least as far back as this 1952 paper.[ref]
For finite probability mass functions, with whenever , subtract the two expectations of logarithmic loss:
The Gibbs inequality makes the difference nonnegative and zero exactly when . If assigns zero probability to an outcome with positive probability under , both the cross-entropy and the Kullback-Leibler divergence are infinite. The finite information entropy still leaves the displayed identity valid in the extended sense. In more general settings, undefined differences such as cannot be used as a derivation.
Suppose a binary prefix code has integer codeword lengths . Under a source with probability mass function , its average codeword length is exactly cross-entropy, . The Kraft inequality is satisfied because . This exact interpretation requires suitable dyadic rational probabilities; a general is not an integer codeword length. For positive , a prefix code with lengths exists by the Kraft inequality and has average length between and . The ceiling function causes the one-symbol rounding overhead. The cross-entropy term describes mismatch overhead; those are distinct costs.
Let follow a Bernoulli distribution with true event probability , and let the model report event probability . The binary cross-entropy is its expected logarithmic loss:
For , it is minimized at , where the value is binary entropy . An individual outcome has loss when and when ; the binary cross-entropy averages those two cases using the true probabilities.
For , truthful reporting gives bits. Reporting gives one bit. Reporting gives
Both and select zero if one predicts only the more likely binary outcome. Their predicted probabilities are very different, however, and cross-entropy reveals the cost of underestimating the event that occurs a quarter of the time. Endpoint losses are computed using the realized outcome rather than multiplying zero by an undefined logarithm.
Figure 1.
The expected logarithmic loss is minimized by reporting the true event probability
. The true Bernoulli distribution has parameter . The blue curve averages the orange event loss and gray nonevent loss with weights and . Its minimum is at , while both endpoint limits diverge because both outcomes have positive probability. These are calculated expectations, rather than measurements. Details.
For observations of a categorical variable, empirical cross-entropy is the average logarithmic loss under the reported probability mass function . Writing the empirical distribution as gives
Under independent and identically distributed observations from and a fixed model with positive probability for each possible outcome, the law of large numbers gives convergence to . A finite empirical distribution is an estimate of , rather than the true source law; unobserved outcomes need not be impossible. If the same data are used to choose , evaluating the resulting empirical cross-entropy on that data need not estimate its prediction performance on fresh observations without bias.
For a parametric statistical model with probability mass function and an independent and identically distributed model, minimizing empirical cross-entropy is maximum likelihood estimation. The logarithm is strictly increasing, so maximizing is equivalent to maximizing . Negating and dividing by gives the displayed minimization. Restricting the model family can prevent the optimum from matching the empirical distribution, and a finite optimizing parameter need not exist. This algebra does not by itself establish accuracy on new data.[ref]

Articles by others on the same topic (1)

Cross-entropy is a measure from the field of information theory that quantifies the difference between two probability distributions. It is commonly used in machine learning, particularly in classification problems, as a loss function to assess the performance of models, especially in the context of neural networks.