Cross entropy

Short description: Information theory measure

Information theory

* Entropy
* Differential entropy
* Conditional entropy
* Joint entropy
* Mutual information
* Conditional mutual information
* Relative entropy
* Entropy rate
* Limiting density of discrete points

* Asymptotic equipartition property
* Rate–distortion theory

* Shannon's source coding theorem
* Channel capacity
* Noisy-channel coding theorem
* Shannon–Hartley theorem

* v
* t
* e

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.

## Contents

* 1 Definition
* 2 Motivation
* 3 Estimation
* 4 Relation to maximum likelihood
* 5 Cross-entropy minimization
* 6 Cross-entropy loss function and logistic regression
* 7 See also
* 8 References
* 9 External links

## 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[log⁡q],

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

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

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

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)log⁡q(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)log⁡Q(x)dr(x)=Ep[−log⁡Q]

and therefore

H(p,q)=−∫𝒳P(x)log⁡Q(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[ln⁡q(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

In classification problems we want to estimate the probability of different outcomes. Let the estimated probability of outcome i be qθ(X=i) with to-be-optimized parameters θ and let the frequency (empirical probability) of outcome i in the training set be p(X=i). Given N conditionally independent samples in the training set, then the likelihood of the parameters θ of the model qθ(X=x) on the training set is

ℒ(θ)=∏i∈X(est. probability of i)number of occurrences of i=∏iqθ(X=i)Np(X=i)

where the last expression is due to the definition of the multinomial PMF. Therefore, the log-likelihood, divided by N is

1Nlog⁡(ℒ(θ))=1Nlog⁡∏iqθ(X=i)Np(X=i)=∑ip(X=i)log⁡qθ(X=i)=−H(p,q)

so that maximizing the likelihood with respect to the parameters θ is the same as minimizing the cross-entropy.[citation needed]

## Cross-entropy minimization

Main article: 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(p∥q), 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+e−z) 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=1−y^

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

H(p,q) = −∑ipilog⁡qi = −ylog⁡y^−(1−y)log⁡(1−y^)

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. 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(𝐰) = 1N∑n=1NH(pn,qn) = −1N∑n=1N [ynlog⁡y^n+(1−yn)log⁡(1−y^n)],

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

The logistic loss is sometimes called cross-entropy loss. It is also known as log loss (In this case, the binary label is often denoted by {−1,+1}).[6]

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=(1x11…x1p1x21⋯x2p⋮⋮⋮1xn1⋯xnp)∈ℝn×(p+1)
yi^=f^(xi1,…,xip)=11+exp⁡(−β0−β1xi1−…−βpxip)
L(β->)=−∑i=1N[yilog⁡y^i+(1−yi)log⁡(1−y^i)]

Then we have the result

∂∂β->L(β->)=XT(Y^−Y)

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

∂∂β0ln⁡11+e−β0+k0=e−β0+k01+e−β0+k0
∂∂β0ln⁡(1−11+e−β0+k0)=−11+e−β0+k0
∂∂β0L(β->)=−∑i=1N[yi⋅e−β0+k01+e−β0+k0−(1−yi)11+e−β0+k0]=−∑i=1N[yi−y^i]=∑i=1N(y^i−yi)
∂∂β1ln⁡11+e−β1xi1+k1=xi1ek1eβ1xi1+ek1
∂∂β1ln⁡[1−11+e−β1xi1+k1]=−xi1eβ1xi1eβ1xi1+ek1
∂∂β1L(β->)=−∑i=1Nxi1(yi−y^i)=∑i=1Nxi1(y^i−yi)

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

## See also

* Cross-entropy method
* Logistic regression
* Conditional entropy
* Maximum likelihood estimation
* Mutual information

## 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 Table, 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. ↑ Murphy, Kevin (2012). Machine Learning: A Probabilistic Perspective. MIT. ISBN 978-0262018029.

## External links

* Cross Entropy

0.00

(0 votes)

|

Retrieved from "https://handwiki.org/wiki/index.php?title=Cross_entropy&oldid=222897"
*[v]: View this template
*[t]: Discuss this template
*[e]: Edit this template