β

Cross entropy

In information theory, the cross entropy between two probability distributionsp{\displaystyle p} and q{\displaystyle 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 is used that is optimized for an "unnatural" probability distribution q{\displaystyle q}, rather than the "true" distribution p{\displaystyle p}.

The cross entropy for the distributions p{\displaystyle p} and q{\displaystyle q} over a given set is defined as follows:

where H(p){\displaystyle H(p)} is the entropy of p{\displaystyle p}, and DKL(p‖q){\displaystyle D_{\mathrm {KL} }(p\|q)} is the Kullback–Leibler divergence of q{\displaystyle q} from p{\displaystyle p} (also known as the relative entropy of p with respect to q — note the reversal of emphasis).

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

Contents

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

There are many situations where cross-entropy needs to be measured but the distribution of p{\displaystyle p} is unknown. An example is language modeling, where a model is created based on a training set T{\displaystyle 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{\displaystyle p} is the true distribution of words in any corpus, and q{\displaystyle 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:

where N{\displaystyle N} is the size of the test set, and q(x){\displaystyle q(x)} is the probability of event x{\displaystyle x} estimated from the training set. The sum is calculated over N{\displaystyle N}. This is a Monte Carlo estimate of the true cross entropy, where the training set is treated as samples from p(x){\displaystyle p(x)}.

Cross-entropy minimization is frequently used in optimization and rare-event probability estimation; see the cross-entropy method.

When comparing a distribution q{\displaystyle q} against a fixed reference distribution p{\displaystyle p}, cross entropy and KL divergence are identical up to an additive constant (since p{\displaystyle p} is fixed): both take on their minimal values when p=q{\displaystyle p=q}, which is 0{\displaystyle 0} for KL divergence, and H(p){\displaystyle \mathrm {H} (p)} for cross entropy.[1] In the engineering literature, the principle of minimising 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{\displaystyle q} is the fixed prior reference distribution, and the distribution p{\displaystyle p} is optimised to be as close to q{\displaystyle q} as possible, subject to some constraint. In this case the two minimisations are not equivalent. This has led to some ambiguity in the literature, with some authors attempting to resolve the inconsistency by redefining cross-entropy to be DKL(p‖q){\displaystyle D_{\mathrm {KL} }(p\|q)}, rather than H(p,q){\displaystyle H(p,q)}.

Cross entropy can be used to define the loss function in machine learning and optimization. The true probability pi{\displaystyle p_{i}} is the true label, and the given distribution qi{\displaystyle q_{i}} is the predicted value of the current model.

More specifically, let us consider logistic regression, which (in its most basic form) deals with classifying a given set of data points into two possible classes generically labelled 0{\displaystyle 0} and 1{\displaystyle 1}. The logistic regression model thus predicts an output y∈{0,1}{\displaystyle y\in \{0,1\}}, given an input vector x{\displaystyle \mathbf {x} }. The probability is modeled using the logistic functiong(z)=1/(1+e−z){\displaystyle g(z)=1/(1+e^{-z})}. Namely, the probability of finding the output y=1{\displaystyle y=1} is given by

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

qy=0=1−y^{\displaystyle q_{y=0}\ =\ 1-{\hat {y}}}

The true (observed) probabilities can be expressed similarly as py=1=y{\displaystyle p_{y=1}=y} and py=0=1−y{\displaystyle p_{y=0}=1-y}.

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

The typical cost function that one uses in logistic regression is computed by taking the average of all cross-entropies in the sample. For example, suppose we have N{\displaystyle N} samples with each sample labeled by n=1,…,N{\displaystyle n=1,\dots ,N}. The loss function is then given by: