Cross-entropy explained

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 when the 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)=-\operatorname{E}p[logq]

,

where

Ep[]

is the expected value operator with respect to the distribution

p

.

DKL(p\parallelq)

, divergence of

p

from

q

(also known as the relative entropy of

p

with respect to

q

).

H(p,q)=H(p)+DKL(p\parallelq),

where

H(p)

is the entropy of

p

.

For discrete probability distributions

p

and

q

with the same support

l{X}

, this means

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

-\intl{X}P(x)logQ(x)dx=\operatorname{E}p[-logQ],

and therefore

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,\ldots,xn\}

can be seen as representing an implicit probability distribution

q(xi)=\left(

1
2
\elli
\right)
over

\{x1,\ldots,xn\}

, where

\elli

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

\operatorname_p[\ell] = - \operatorname_p\left[\frac{\ln{q(x)}}{\ln(2)}\right] = - \operatorname_p\left[\log_2 {q(x)}\right] = - \sum_ p(x_i)\, \log_2 q(x_i) = -\sum_x p(x)\, \log_2 q(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)=

N
-\sum
i=1
1
N

log2q(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)

.

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\theta

, with

\theta

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

\theta

of the model is then given by the product over all probabilities

q\theta(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:=

H(p,q\theta)
{e}
the perplexity, which can be seen to equal
{style\prod
xi
} q_(X=x_i)^ by the calculation rules for the logarithm, and where the product is over the values without double counting. So

l{L}(\theta;{x})=\prodiq\theta(X=xi)=

\prod
xi

q\theta

\#xi
(X=x
i)

=PP-N=

-NH(p,q\theta)
{e}
or

logl{L}(\theta;{x})=-NH(p,q\theta).

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

Cross-entropy minimization

See 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\parallelq)

, 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. Mao, Mohri, and Zhong (2023) give an extensive analysis of the properties of the family of cross-entropy loss functions in machinelearning, including theoretical learning guarantees and extensions toadversarial learning.[3] 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[4] or logistic loss);[5] the terms "log loss" and "cross-entropy loss" are used interchangeably.[6]

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=\hat{y}\equivg(wx)=

1
1+e-wx

,

where the vector of weights

w

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-\hat{y}.

Having set up our notation,

p\in\{y,1-y\}

and

q\in\{\hat{y},1-\hat{y}\}

, we can use cross-entropy to get a measure of dissimilarity between

p

and

q

:

H(p,q) = -\sumipilogqi = -ylog\hat{y}-(1-y)log(1-\hat{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. Other loss functions that penalize errors differently can be also used for training, resulting in models with different final test accuracy.[7] 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:

\begin{align} J(w)&= 

1N\sum
n=1

NH(pn,q

N[y
n

log\hatyn+(1-yn)log(1-\hatyn)], \end{align}

where

\hat{y}n\equivg(wxn)=

-wxn
1/(1+e

)

, with

g(z)

the logistic function as before.

(In this case, the binary label is often denoted by .[8])

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

T=\begin{pmatrix} 1&x
X
11

&...&x1p\ 1&x21&&x2p\ \vdots&\vdots&&\vdots\\ 1&xn1&&xnp\ \end{pmatrix}\inRn x (p+1),

\hat{yi}=\hat{f}(xi1,...,xip)=

1
1+\exp(-\beta0-\beta1xi1-...-\betapxip)

,

L(\boldsymbol{\beta})=

N
-\sum
i=1

\left[yilog\hat{y}i+(1-yi)log(1-\hat{y}i)\right].

Then we have the result

\partial
\partial\boldsymbol{\beta
}L(\boldsymbol)=X^T(\hat-Y).

The proof is as follows. For any

\hat{y}i

, we have
\partialln
\partial\beta0
1
-\beta0+k0
1+e

=

-\beta0+k0
e
-\beta0+k0
1+e

,

\partial
\partial\beta0

ln\left(1-

1\right)=
-\beta0+k0
1+e
-1
-\beta0+k0
1+e

,

\begin{align} \partial
\partial\beta0

L(\boldsymbol{\beta})&=

N
-\sum\left[
i=1
y
-\beta0+k0
e
i
-\beta0+k0
1+e
-(1-y
i)1
-\beta0+k0
1+e

\right]\\ &=-

N
\sum
i=1

\left[yi-\hat{y}i\right] =

N
\sum
i=1

(\hat{y}i-yi), \end{align}

\partial
\partial\beta1

ln

1
-\beta1xi1+k1
1+e

=

x
k1
e
i1
\beta1xi1
e
k1
+e

,

\partialln\left[1-
\partial\beta1
1
-\beta1xi1+k1
1+e

\right]=

-x
\beta1xi1
e
i1
\beta1xi1
e
k1
+e

,

\partial
\partial\beta1

L(\boldsymbol{\beta})=

N
-\sum
i=1

xi1(yi-\hat{y}i)=

N
\sum
i=1

xi1(\hat{y}i-yi).

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

Amended cross-entropy

It may be beneficial to train an ensemble of models that have diversity, such that when they are combined, their predictive accuracy is augmented.[9] [10] Assuming a simple ensemble of

K

classifiers is assembled via averaging the outputs, then the amended cross-entropy is given by

ek=

k)-λ
K
H(p,q

\sumjH(qj,qk)

where

ek

is the cost function of the

kth

classifier,

qk

is the output probability of the

k

classifier,

p

is the true probability to be estimated, and

λ

is a parameter between 0 and 1 that defines the 'diversity' that we would like to establish among the ensemble. When

λ=0

we want each classifier to do its best regardless of the ensemble and when

λ=1

we would like the classifier to be as diverse as possible.

See also

Further reading

Notes and 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. Anqi Mao, Mehryar Mohri, Yutao Zhong. Cross-entropy loss functions: Theoretical analysis and applications. ICML 2023. https://arxiv.org/pdf/2304.07288.pdf
  4. The Mathematics of Information Coding, Extraction and Distribution, by George Cybenko, Dianne P. O'Leary, Jorma Rissanen, 1999, p. 82
  5. 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."
  6. https://scikit-learn.org/stable/modules/generated/sklearn.metrics.log_loss.html sklearn.metrics.log_loss
  7. 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 . cs.NE . 2303.09935 .
  8. Book: Murphy . Kevin. 2012 . Machine Learning: A Probabilistic Perspective . MIT . 978-0262018029 .
  9. Shoham . Ron . Permuter . Haim H. . Dolev . Shlomi . Hendler . Danny . Lodha . Sachin . Yung . Moti . Amended Cross-Entropy Cost: An Approach for Encouraging Diversity in Classification Ensemble (Brief Announcement) . 10.1007/978-3-030-20951-3_18 . 202–207 . Springer . Lecture Notes in Computer Science . Cyber Security Cryptography and Machine Learning – Third International Symposium, CSCML 2019, Beer-Sheva, Israel, June 27–28, 2019, Proceedings . 11527 . 2019. 978-3-030-20950-6 .
  10. Shoham . Ron . Permuter . Haim . Amended Cross Entropy Cost: Framework For Explicit Diversity Encouragement . 2020 . cs.LG . 2007.08140 .