Halloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCHalloween DealsAmazon USOctober picks are moving - compare before checkoutAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

Entropy: How Cross-Entropy Adds a Penalty for Mismatched Predictions

Entropy averages the uncertainty in a distribution; cross-entropy scores outcomes from one distribution using another’s probabilities. A simple classification example shows the difference and their link to KL divergence.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Entropy measures the average uncertainty in a probability distribution. Cross-entropy measures how costly it is to score outcomes from one distribution using probabilities from another. Their key relationship is H(p,q) = H(p) + DKL(p‖q): cross-entropy is the source’s entropy plus a nonnegative penalty for using a mismatched probability model.

A small example: how much probability did the model give the right answer?

Suppose the true class is known to be A. Represent that target distribution as p(A)=1, with probability zero for other classes. A model predicts a distribution q.

As an Amazon Associate I earn from qualifying purchases.

  • If q(A)=0.8, the cross-entropy for this example is −log2(0.8) ≈ 0.322 bits.
  • If q(A)=0.1, it is −log2(0.1) ≈ 3.322 bits.

These values follow directly from the definition. The second model receives a larger loss because it assigned less probability to the known target. For a one-hot target, only the probability assigned to the correct class contributes to that example’s cross-entropy.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What entropy measures

For a discrete random variable X with probability mass function p(x), Shannon entropy is the expected surprisal of its outcomes:

H(X) = −Σx p(x) log2 p(x)

The surprisal, or self-information, of one outcome x is −log2 p(x). An unlikely outcome has greater surprisal than a likely one. Entropy averages those values over outcomes according to p; it is not the information content of one particular observation.

For a variable with m possible outcomes, entropy is zero if one outcome is certain and is no greater than log2(m). It reaches that maximum for a uniform distribution. With base-2 logarithms, the unit is bits; using natural logarithms gives nats instead. Carnegie Mellon’s Information Theory I lecture notes discuss these properties and entropy’s coding interpretation.

Rank #2
Sale
Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems
  • Use scikit-learn to track an example ML project end to end
  • Explore several models, including support vector machines, decision trees, random forests, and ensemble methods
  • Exploit unsupervised learning techniques such as dimensionality reduction, clustering, and anomaly detection
  • Dive into neural net architectures, including convolutional nets, recurrent nets, generative adversarial networks, autoencoders, diffusion models, and transformers
  • Use TensorFlow and Keras to build and train neural nets for computer vision, natural language processing, generative models, and deep reinforcement learning

What cross-entropy measures

For two distributions over the same outcomes, cross-entropy is

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

H(p,q) = −Σx p(x) log2 q(x)

Here p supplies the outcomes and the weights used in the average, while q supplies the probabilities being scored. In plain terms, imagine outcomes generated according to p, but evaluated using the probabilities assigned by q. If the distributions match, cross-entropy equals entropy: H(p,p)=H(p).

The order matters. H(p,q) averages according to p; switching the distributions generally changes the result. If q(x)=0 for any outcome with p(x)>0, the cross-entropy is infinite under the standard discrete definition. Terms where p(x)=0 contribute zero by the limiting convention 0 log(0/q)=0.

The bridge to KL divergence

The Kullback–Leibler divergence from p to q is

DKL(p‖q) = Σx p(x) log2[p(x)/q(x)]

Rearranging the definitions gives the central identity:

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

So cross-entropy consists of two parts: the uncertainty inherent in the source distribution p, and the extra cost caused by representing it with q. KL divergence is nonnegative, so cross-entropy cannot be less than H(p); the mismatch penalty is zero when the distributions match. When q assigns zero probability to an outcome that has positive probability under p, the divergence is infinite as well.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

KL divergence is generally asymmetric: DKL(p‖q) need not equal DKL(q‖p). It is therefore more precise to call it a divergence or mismatch measure than a distance. Carnegie Mellon’s lecture notes define cross-entropy and KL divergence and explain their relationship and asymmetry.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Why the distinction matters in classification

A classifier predicts probabilities over classes. For a one-hot target with correct class y, the per-example cross-entropy reduces to the negative log probability assigned to that class:

−log q(y)

Across a fixed dataset, the mean of these per-example losses is the average negative log-likelihood of the observed labels. Minimizing it is therefore equivalent to maximizing their likelihood under the model’s predicted probabilities.

Targets can also be probability distributions rather than one-hot labels. For target probabilities p(x), the loss is −Σ p(x)log q(x). If the target distribution is fixed, H(p) is constant, so minimizing cross-entropy over q also minimizes DKL(p‖q). This equivalence concerns these distributions and fixed target data; it does not make every loss function or training setup interchangeable. The LMU Statistical Learning and Data Science chapter on cross-entropy and KL treats this connection in machine learning.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Why cross-entropy also appears in coding

Entropy gives the lower bound on the expected code length for a source when its probabilities are known. If a code is optimized for q but used on outcomes drawn from p, the expected description length corresponds to cross-entropy H(p,q). The overhead compared with a code based on the true distribution is DKL(p‖q). Carnegie Mellon’s lecture notes explain this coding interpretation.

A note about continuous variables

For continuous variables, entropy is defined using a probability density and an integral rather than a sum. This differential entropy can be negative, unlike the nonnegative discrete entropy described above, so discrete intuitions do not all carry over unchanged. MIT’s 6.441 Information Theory lecture notes distinguish differential entropy from discrete entropy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.