Concepts / Entropy and Predictability of Data

Entropy and Predictability of Data

Cross entropy measures how difficult it is for a language model to predict what comes next in a dataset.

  • Programming

Guessing the Next Word

Imagine you are shown the first part of a sentence taken from a large collection of text and asked to guess the next word. Some sentences make the next word obvious. Others leave you with many reasonable options. A language model faces this same task every time it is trained on a dataset. Its job is to become good at guessing what comes next, and cross entropy is the number that tells you how hard that guessing job is for the model on a given dataset.

Cross entropy is useful in two directions at once. It describes a property of the data itself, namely how predictable the data is. It also describes how well the model's guesses line up with that data. Reading cross entropy well means keeping both ideas in view together, and that is what this article builds toward.

What do you think happens?

Suppose the next item in a dataset is either outcome A or outcome B, and each occurs half the time, so the distribution is P = (0.5, 0.5). How many bits of entropy does this data have?

  • 0 bits

  • 0.5 bits

  • 1 bit

  • 2 bits

Reveal answer
Answer:

1 bit

Each outcome has probability 0.5 and contributes -0.5 log2(0.5) = 0.5 bits. Two outcomes contribute 0.5 + 0.5 = 1 bit in total. Two equally likely options are exactly as unpredictable as one fair coin flip.

Tracing a Next Item Through the Model

Start with the data alone, before any model enters the picture. If the next item in a dataset is almost always the same, the data is very predictable and there is little uncertainty to deal with. If the next item could be any of many options with roughly equal likelihood, the data is hard to predict. The quantity that captures this predictability is the entropy of the training data, written H(P), where P is the true distribution of the training data.

Notice that entropy never refers to the model. It belongs to the data. Two different models trained on the same dataset face the same entropy value, because nothing about a model changes the true distribution of the data.

Now bring in the model. It has learned its own distribution, called Q, over the possible next items. Each time an item actually arrives, the model has given it some probability. When Q assigns high probability to what really comes next, the model is doing well. When Q spreads its probability onto options that rarely occur, or puts little weight on what really happens, the model pays for it. The figure below traces one next item through this scoring process, from the data to the accumulated cross entropy.

Next item in datadrawn from PModel probabilityassigned by Q to that itemPer-item costweight p times -log2 qAccumulated sumover all outcomesCross entropyH(P,Q) in bitslooked up in modelscoredsummed over outcomesgives
How does each next item in the dataset flow to the model's predicted probability, and how do the per-item terms accumulate into cross entropy?

Each outcome therefore contributes one term to the total. An outcome that occurs often in the data and receives little probability from the model adds a large term. An outcome that the model already expects adds a small one. Adding up these terms across the distribution gives the cross entropy, which measures the full cost of predicting the data using the model's beliefs Q rather than the true distribution P.

Splitting Cross Entropy into Two Costs

Definition

Cross entropy H(P,Q) is the cost of scoring the true data distribution P using the model's distribution Q. In the notation used here, H(P,Q) = -sum over outcomes x of P(x) times log2 Q(x).

Formula

The key relationship ties the three quantities together. The cross entropy of the model equals the entropy of the data plus a second term that measures how far the model's distribution has drifted from the true one. That second term is the Kullback-Leibler divergence, written $D_{KL}(P \| Q)$.

Formula
Formula, step by step
  1. 1
    H(P,Q) is the total cost of scoring the data P with the model's beliefs Q.
  2. 2
    H(P) is the entropy of the data. It is the unavoidable uncertainty the data carries, and it does not depend on the model.
  3. 3
    D_KL(P||Q) is the extra cost caused by the model's distribution differing from the truth. It is the only part that learning can shrink.
  4. 4
    Because H(P) is fixed by the data, no model can push cross entropy below the entropy of the data.
Cross entropyH(P,Q), total costData entropyirreducible floorKL divergenceextra cost of the modelincludesincludes
How do the cross entropy, the data's own entropy, and the KL divergence fit together, and which part is the irreducible floor versus the model's extra cost?

Checking the Split on a Two-Outcome Case

Suppose the next item is one of two outcomes, A or B. In the true data, A and B each occur half the time, so $P = (0.5, 0.5)$. A model has learned $Q = (0.9, 0.1)$, meaning it strongly believes A is coming. All quantities are measured in bits, which means base-2 logarithms.

Worked example

Cross entropy, entropy, and KL divergence for P = (0.5, 0.5) and Q = (0.9, 0.1)

Compute H(P), H(P,Q), and D_KL(P||Q) in bits, then check that H(P,Q) = H(P) + D_KL(P||Q).

  1. 1
    Entropy of the data

    $H(P) = -(0.5\log_2 0.5 + 0.5\log_2 0.5) = 0.5 + 0.5 = 1$ bit. This value is fixed by the data and no model can remove it.

  2. 2
    Cross entropy with the model

    $H(P,Q) = -(0.5\log_2 0.9 + 0.5\log_2 0.1)$. Since $\log_2 0.9 \approx -0.152$ and $\log_2 0.1 \approx -3.322$, this gives $H(P,Q) \approx 0.076 + 1.661 = 1.737$ bits.

  3. 3
    KL divergence

    $D_{KL}(P\|Q) = 0.5\log_2\frac{0.5}{0.9} + 0.5\log_2\frac{0.5}{0.1}$. With $\log_2\frac{5}{9} \approx -0.848$ and $\log_2 5 \approx 2.322$, this gives $D_{KL}(P\|Q) \approx -0.424 + 1.161 = 0.737$ bits.

  4. 4
    Check the split

    $H(P) + D_{KL}(P\|Q) = 1 + 0.737 = 1.737 = H(P,Q)$. The split holds numerically.

Result

H(P) = 1 bit, H(P,Q) is about 1.737 bits, and D_KL(P||Q) is about 0.737 bits. The model's overconfidence in A adds roughly 0.737 bits on top of the data's unavoidable 1 bit.

Notice what the example reveals. The data has 1 bit of entropy that no model can remove. The model's overconfidence in A adds about 0.737 bits on top. A model that learned Q equal to P would add nothing, and its cross entropy would sit right at the data's entropy.

Why Swapping the Distributions Changes the Answer

A tempting shortcut is to assume that the cross entropy between two distributions is the same no matter which one you treat as the true distribution. It is not. Cross entropy is not symmetric. The order of the two arguments tells you whose beliefs are being scored against whose. Each term weights an outcome by the probability from one distribution and takes the logarithm of the probability from the other, so swapping the roles changes both the weights and the logarithms.

Weights from data P0.5 and 0.5Logs of model Qlog2 0.9 and log2 0.1Value1.737 bitsWeights from modelQ0.9 and 0.1Logs of data Plog2 0.5 and log2 0.5Value1 bitpaired withsummed topaired withsummed to
Why does swapping the data distribution and the model distribution give a different cross entropy, and which distribution supplies the weights and which supplies the logarithms?
QuantityWeights come fromLogarithms come fromValue for P = (0.5, 0.5), Q = (0.9, 0.1)
H(P,Q)P, the true dataQ, the modelabout 1.737 bits
H(Q,P)Q, the modelP, the true data1 bit

The same two distributions give different values depending on the order of the arguments.

The two values differ, so the notation carries real meaning. In language modeling, the order that matters is the one that scores the data P using the model's beliefs Q. That is why the training objective is written with the data's distribution as the weights.

Mistakes When Reading Cross Entropy

  • Common mistake

    Treating cross entropy as a property of the model alone.

    Why it fails

    Entropy of the data depends only on the true distribution P. The model only enters through the KL term and the cross entropy as a whole.

    Do this instead

    Read H(P) as the data's own floor of uncertainty, and read the KL term as the model's extra cost.

  • Common mistake

    Assuming H(P,Q) and H(Q,P) are interchangeable.

    What it looks like

    With P = (0.5, 0.5) and Q = (0.9, 0.1), H(P,Q) is about 1.737 bits but H(Q,P) is exactly 1 bit.

    Why it fails

    Cross entropy is not symmetric. The argument order determines which distribution supplies the weights and which supplies the logarithms.

    Do this instead

    Always check the order of the arguments before comparing two cross entropy values.

  • Common mistake

    Expecting a model to push cross entropy below the entropy of the data.

    Why it fails

    The data's entropy is a floor. Because the KL divergence is never negative, the cross entropy cannot fall below H(P).

    Do this instead

    Treat the data entropy as the best possible result, reached only when the KL divergence is 0.

  • Common mistake

    Mixing up entropy and cross entropy when the two distributions are identical, which makes the KL term 0 and leaves only the data's entropy.

Testing the Split Yourself

EASY

Let P = (0.5, 0.5) be the true next-item distribution, and let the model learn Q = (0.5, 0.5). Before calculating, predict the KL divergence D_KL(P||Q) and the cross entropy H(P,Q). Then check whether the split H(P,Q) = H(P) + D_KL(P||Q) still holds.

Hints
  • Compare each outcome's probability in P with its probability in Q. Are they the same?
  • If P and Q are identical, what is each term p log2(p/q) inside the KL sum?
  • Compare H(P,Q) with H(P) directly. Does the model add any extra cost?
MEDIUM

Using the same P = (0.5, 0.5) and Q = (0.9, 0.1) from the worked example, explain in your own words why H(Q,P) is exactly 1 bit even though the model is strongly confident in A. Which distribution supplies the weights in that calculation?

Hints
  • Write out H(Q,P) = -sum of q(x) log2 p(x) and substitute the values.
  • Notice that every logarithm is log2 0.5, which equals -1.

Key Takeaways

  • Cross entropy measures how difficult it is for a language model to predict what comes next in a dataset, judged using the model's own distribution Q.
  • Cross entropy splits into the entropy of the data H(P), which is an unavoidable floor, plus the KL divergence D_KL(P||Q), which is the extra cost of the model's drift from the truth.
  • For P = (0.5, 0.5) and Q = (0.9, 0.1), H(P) is 1 bit, H(P,Q) is about 1.737 bits, and D_KL(P||Q) is about 0.737 bits, so the split checks out numerically.
  • Cross entropy is not symmetric. H(P,Q) and H(Q,P) differ in this example (about 1.737 versus 1 bit), so the order of the two distributions determines whose beliefs are scored against whose.
  • Training pushes the model toward the data distribution. When the KL divergence reaches 0, cross entropy equals the data's entropy.

More concepts starting with E