Cross-entropy

From HandWiki
Short description: Information-theoretic measure

In information theory, the cross-entropy between two probability distributions p and q over the same underlying set of events measures the average number of bits needed to identify an event drawn from the set if a coding scheme used for the set is optimized for an estimated probability distribution q, rather than the true distribution p.

Definition

The cross-entropy of the distribution q relative to a distribution p over a given set is defined as follows:

H(p,q)=Ep[logq],

where Ep[] is the expected value operator with respect to the distribution p.

The definition may be formulated using the Kullback–Leibler divergence DKL(pq), divergence of p from q (also known as the relative entropy of p with respect to q).

H(p,q)=H(p)+DKL(pq),

where H(p) is the entropy of p.

For discrete probability distributions p and q with the same support 𝒳, this means

H(p,q)=x𝒳p(x)logq(x).

 

 

 

 

(Eq.1)

The situation for continuous distributions is analogous. We have to assume that p and q are absolutely continuous with respect to some reference measure r (usually r is a Lebesgue measure on a Borel σ-algebra). Let P and Q be probability density functions of p and q with respect to r. Then

𝒳P(x)logQ(x)dr(x)=Ep[logQ],

and therefore

H(p,q)=𝒳P(x)logQ(x)dr(x).

 

 

 

 

(Eq.2)

NB: The notation H(p,q) is also used for a different concept, the joint entropy of p and q.

Motivation

In information theory, the Kraft–McMillan theorem establishes that any directly decodable coding scheme for coding a message to identify one value xi out of a set of possibilities {x1,,xn} can be seen as representing an implicit probability distribution q(xi)=(12)i over {x1,,xn}, where i is the length of the code for xi in bits. Therefore, cross-entropy can be interpreted as the expected message-length per datum when a wrong distribution q is assumed while the data actually follows a distribution p. That is why the expectation is taken over the true probability distribution p and not q. Indeed the expected message-length under the true distribution p is

Ep[]=Ep[lnq(x)ln(2)]=Ep[log2q(x)]=xip(xi)log2q(xi)=xp(x)log2q(x)=H(p,q).

Estimation

There are many situations where cross-entropy needs to be measured but the distribution of p is unknown. An example is language modeling, where a model is created based on a training set T, and then its cross-entropy is measured on a test set to assess how accurate the model is in predicting the test data. In this example, p is the true distribution of words in any corpus, and q is the distribution of words as predicted by the model. Since the true distribution is unknown, cross-entropy cannot be directly calculated. In these cases, an estimate of cross-entropy is calculated using the following formula:

H(T,q)=i=1N1Nlog2q(xi)

where N is the size of the test set, and q(x) is the probability of event x estimated from the training set. In other words, q(xi) is the probability estimate of the model that the i-th word of the text is xi. The sum is averaged over the N words of the test. This is a Monte Carlo estimate of the true cross-entropy, where the test set is treated as samples from p(x)[citation needed].

Relation to maximum likelihood

The cross entropy arises in classification problems when introducing a logarithm in the guise of the log-likelihood function.

The section is concerned with the subject of estimation of the probability of different possible discrete outcomes. To this end, denote a parametrized family of distributions by qθ, with θ subject to the optimization effort. Consider a given finite sequence of N values xi from a training set, obtained from conditionally independent sampling. The likelihood assigned to any considered parameter θ of the model is then given by the product over all probabilities qθ(X=xi). Repeated occurrences are possible, leading to equal factors in the product. If the count of occurrences of the value equal to xi (for some index i) is denoted by #xi, then the frequency of that value equals #xi/N. Denote the latter by p(X=xi), as it may be understood as empirical approximation to the probability distribution underlying the scenario. Further denote by PP:=eH(p,qθ) the perplexity, which can be seen to equal xiqθ(X=xi)p(X=xi) by the calculation rules for the logarithm, and where the product is over the values without double counting. So

(θ;𝐱)=iqθ(X=xi)=xiqθ(X=xi)#xi=PPN=eNH(p,qθ)

or

log(θ;𝐱)=NH(p,qθ).

Since the logarithm is monotone function increasing function, it does not affect extremization. So observe that the likelihood maximization amounts to minimization of the cross-entropy.

Cross-entropy minimization

Main page: Cross-entropy method

Cross-entropy minimization is frequently used in optimization and rare-event probability estimation. When comparing a distribution q against a fixed reference distribution p, cross-entropy and KL divergence are identical up to an additive constant (since p is fixed): According to the Gibbs' inequality, both take on their minimal values when p=q, which is 0 for KL divergence, and H(p) for cross-entropy. In the engineering literature, the principle of minimizing KL divergence (Kullback's "Principle of Minimum Discrimination Information") is often called the Principle of Minimum Cross-Entropy (MCE), or Minxent.

However, as discussed in the article Kullback–Leibler divergence, sometimes the distribution q is the fixed prior reference distribution, and the distribution p is optimized to be as close to q as possible, subject to some constraint. In this case the two minimizations are not equivalent. This has led to some ambiguity in the literature, with some authors attempting to resolve the inconsistency by restating cross-entropy to be DKL(pq), rather than H(p,q). In fact, cross-entropy is another name for relative entropy; see Cover and Thomas[1] and Good.[2] On the other hand, H(p,q) does not agree with the literature and can be misleading.

Cross-entropy loss function and logistic regression

Cross-entropy can be used to define a loss function in machine learning and optimization. The true probability pi is the true label, and the given distribution qi is the predicted value of the current model. This is also known as the log loss (or logarithmic loss[3] or logistic loss);[4] the terms "log loss" and "cross-entropy loss" are used interchangeably.[5]

More specifically, consider a binary regression model which can be used to classify observations into two possible classes (often simply labelled 0 and 1). The output of the model for a given observation, given a vector of input features x, can be interpreted as a probability, which serves as the basis for classifying the observation. In logistic regression, the probability is modeled using the logistic function g(z)=1/(1+ez) where z is some function of the input vector x, commonly just a linear function. The probability of the output y=1 is given by

qy=1=y^g(𝐰𝐱)=11+e𝐰𝐱,

where the vector of weights 𝐰 is optimized through some appropriate algorithm such as gradient descent. Similarly, the complementary probability of finding the output y=0 is simply given by

qy=0=1y^.

Having set up our notation, p{y,1y} and q{y^,1y^}, we can use cross-entropy to get a measure of dissimilarity between p and q:

H(p,q) = ipilogqi = ylogy^(1y)log(1y^).
Plot shows different loss functions that can be used to train a binary classifier. Only the case where the target output is 1 is shown. It is observed that the loss is zero when the target is equal to the output and increases as the output becomes increasingly incorrect.

Logistic regression typically optimizes the log loss for all the observations on which it is trained, which is the same as optimizing the average cross-entropy in the sample. Other loss functions that penalize errors differently can be also used for training, resulting in models with different final test accuracy.[6] For example, suppose we have N samples with each sample indexed by n=1,,N. The average of the loss function is then given by:

J(𝐰) = 1Nn=1NH(pn,qn) = 1Nn=1N [ynlogy^n+(1yn)log(1y^n)],

where y^ng(𝐰𝐱n)=1/(1+e𝐰𝐱n), with g(z) the logistic function as before.

Template:Duplication span (In this case, the binary label is often denoted by {−1,+1}.[7])

Remark: The gradient of the cross-entropy loss for logistic regression is the same as the gradient of the squared-error loss for linear regression. That is, define

XT=(1x11x1p1x21x2p1xn1xnp)n×(p+1),
yi^=f^(xi1,,xip)=11+exp(β0β1xi1βpxip),
L(β)=i=1N[yilogy^i+(1yi)log(1y^i)].

Then we have the result

βL(β)=XT(Y^Y).

The proof is as follows. For any y^i, we have

β0ln11+eβ0+k0=eβ0+k01+eβ0+k0,
β0ln(111+eβ0+k0)=11+eβ0+k0,
β0L(β)=i=1N[yieβ0+k01+eβ0+k0(1yi)11+eβ0+k0]=i=1N[yiy^i]=i=1N(y^iyi),
β1ln11+eβ1xi1+k1=xi1ek1eβ1xi1+ek1,
β1ln[111+eβ1xi1+k1]=xi1eβ1xi1eβ1xi1+ek1,
β1L(β)=i=1Nxi1(yiy^i)=i=1Nxi1(y^iyi).

In a similar way, we eventually obtain the desired result.

See also

References

  1. Thomas M. Cover, Joy A. Thomas, Elements of Information Theory, 2nd Edition, Wiley, p. 80
  2. I. J. Good, Maximum entropy for hypothesis formulation, especially for multidimensional contingency tables, Ann. of Math. Statistics, 1963
  3. The Mathematics of Information Coding, Extraction and Distribution, by George Cybenko, Dianne P. O'Leary, Jorma Rissanen, 1999, p. 82
  4. Probability for Machine Learning: Discover How To Harness Uncertainty With Python, Jason Brownlee, 2019, p. 220: "Logistic loss refers to the loss function commonly used to optimize a logistic regression model. It may also be referred to as logarithmic loss (which is confusing) or simply log loss."
  5. sklearn.metrics.log_loss
  6. Noel, Mathew; Banerjee, Arindam; D, Geraldine Bessie Amali; Muthiah-Nakarajan, Venkataraman (March 17, 2023). Alternate loss functions for classification and robust regression can improve the accuracy of artificial neural networks. 
  7. Murphy, Kevin (2012). Machine Learning: A Probabilistic Perspective. MIT. ISBN 978-0262018029. 

Further reading