ArticleslgStudy

mathematics

Cross-entropy

Cross-entropy is a mathematics topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Cross-entropy rather than just read about it. In short: 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 distribu…

Cross-entropy — main illustration
Cross-entropy — illustration

Key takeaways

  • Cross-entropy belongs to mathematics; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Cross-entropy to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Cross-entropy from memory before moving on to harder problems.

Reference excerpt

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)=−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

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)dx=Ep[−log⁡Q], 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,…,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).

Relation to maximum likelihood The cross entropy arises in classification problems when introducing a logarithm in the guise of the log-likelihood function. This section concerns the estimation of the probabilities of different 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 x is denoted by #x, then the frequency of that value equals #x/N. If p(X=x) is the underlying probability distribution, for large N we expect p(X=x)≈#x/N, by the law of large numbers. Writing our likelihood function as the product of observations from the distribution qθ: ℒ(θ;𝐱)=∏iqθ(X=xi)=∏xqθ(X=x)#x≈∏xqθ(X=x)N⋅p(X=x)=exp⁡log⁡[∏xqθ(X=x)N⋅p(X=x)]=exp⁡(∑xN⋅p(X=x)log⁡qθ(X=x)), where we have used the calculation rules for the logarithm in the final line. Notice how the exponent contains a −H(p,qθ) term. Taking the logarithm of both sides gives: log⁡ℒ(θ;𝐱)=−N⋅H(p,qθ). Since the logarithm is a monotonically increasing function, the maximizing value of θ is unaffected by this final step. Similarly, the maximizing value of θ is unaffected by the factor of N. So we observe that the likelihood maximization amounts to minimization of the cross-entropy.

Cross-entropy minimization

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 and Good. On the other hand, H(p,q) does not agree with the literature and can be misleading.

… excerpt ends here. Continue reading the full article.

Illustrations

Cross-entropy: 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.
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.

Worked examples

Example 1 — a first encounter with Cross-entropy

Start with the simplest possible case. Write down what Cross-entropy claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In mathematics, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Cross-entropy before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Cross-entropy ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Cross-entropy

In research
Cross-entropy appears in mathematics research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Cross-entropy in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Cross-entropy is common in secondary-school and first-year university syllabi. It links to neighbouring topics Entropy and information, Loss functions, so understanding it makes those chapters shorter.
In everyday life
Look for Cross-entropy outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Cross-entropy” →

How to study Cross-entropy in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Cross-entropy means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Cross-entropy out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Cross-entropy in simple terms?

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 distri…

Why does Cross-entropy matter?

Because it connects several mathematics ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Cross-entropy?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Cross-entropy.

Tags

  • Entropy and information
  • Loss functions

Keep exploring