Cross-Entropy Loss › Information Theory Foundations
Language AI HandbookAll chapters Cross-Entropy Loss › Information Theory FoundationsOne section · Sections in this chapter2 / 15 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
To understand cross-entropy, we must first grasp entropy and how it quantifies uncertainty. The concept of entropy originates from thermodynamics and statistical mechanics, but Claude Shannon adapted it for information theory in 1948. This provides the mathematical vocabulary for measuring information content. Shannon's framework allows us to quantify how much information is contained in a random event, which directly translates to how surprised we should be when observing different outcomes. For language modeling, this means measuring how unexpected each token is given our current understanding of the language's statistical structure.
Shannon's key insight was that information and uncertainty are two sides of the same coin. An event carries more information precisely because it was unexpected. When a coin lands heads after you knew it would land heads because it is double-headed, you learn nothing. When that same coin lands heads despite being weighted toward tails, you learn something meaningful. This seemingly philosophical distinction has concrete mathematical consequences that turn out to be exactly what we need to train language models.
Shannon Entropy
Entropy measures the uncertainty inherent in a probability distribution. It answers the question: on average, how surprised will we be when we observe an outcome from this distribution? For a discrete distribution over outcomes , entropy is defined as:
where:
- : the entropy of probability distribution , measured in bits (base-2 log) or nats (natural log)
- : a possible outcome from the set of all events
- : the probability of outcome occurring
- : the logarithm function (typically base 2 for bits or natural log for nats)
The logarithm ensures that rare events contribute more to the total uncertainty than common ones. This mathematical property aligns with our intuition: when an unlikely event occurs, we experience more surprise and receive more information than when a common event occurs. When an event is certain (), its contribution is zero because we gain no new information from observing something we already knew would happen. When an event is impossible (), we define by continuity. This reflects that impossible events contribute nothing to the entropy since they never occur.
To see why the logarithm specifically encodes surprise, consider two independent events each with probability . The probability of both occurring together is . If surprise is additive (seeing two independent rare events is twice as surprising as seeing one), then the measure of surprise should satisfy . The only continuous function satisfying this property for all probabilities is the logarithm. So is the only mathematically consistent measure of surprise for an event with probability , and entropy is the expected value of this surprise over all possible outcomes.
Entropy represents the expected information content or "surprise" of observing a random variable. A uniform distribution over outcomes has maximum entropy , while a deterministic distribution has zero entropy. This means that when all outcomes are equally likely, we are maximally uncertain about what will happen next, whereas when one outcome is guaranteed, we have perfect certainty and zero uncertainty. From a compression perspective, entropy is the basic lower bound on how many bits per symbol any lossless compression algorithm can achieve when encoding data drawn from distribution .
Consider a vocabulary of 50,000 tokens. A uniform distribution where every token has probability yields entropy:
This means we need approximately 15.6 bits on average to encode each token if we use an optimal code designed for this uniform distribution. This represents the worst-case scenario for compression, where no token is more predictable than any other. In contrast, if our model is perfect and assigns probability 1 to the correct token and 0 elsewhere, entropy drops to zero, requiring no bits to transmit the message because there is no uncertainty. The difference between these extremes represents the potential for compression that good language models exploit.
Real language is far from uniform. Common words like "the" and "is" appear far more frequently than rare technical terms. Good models learn these frequency differences and assign higher probability to common tokens, reducing average surprise below the maximum. The entropy of real English text has been estimated at 0.6 to 1.2 bits per character depending on context length, far below the 4.7 bits per character that would be required for a uniform distribution over the 26 letters and a space character. This gap is what enables compression algorithms, and it is the gap that language models are trained to exploit.

Kullback-Leibler Divergence
While entropy measures uncertainty within a single distribution, the Kullback-Leibler (KL) divergence measures how one probability distribution diverges from a reference distribution . It quantifies the inefficiency of assuming that the distribution is when the true distribution is :
where:
- : the Kullback-Leibler divergence from distribution to reference distribution
- : the probability of outcome under the true distribution
- : the probability of outcome under the model distribution
- : an outcome from the support of
KL divergence is always non-negative and equals zero if and only if everywhere. This non-negativity follows from Jensen's inequality applied to the concave logarithm function. It represents the extra bits required when using a code optimized for to encode data that follows distribution . This interpretation reveals that KL divergence measures the penalty we pay for using the wrong model: if we build a compression scheme based on our predicted distribution , but reality follows , we will waste bits proportional to the KL divergence.
The asymmetry of KL divergence is worth understanding carefully. is not the same as in general. When we minimize with respect to , we penalize cases where but . This is called the "forward" or "inclusive" KL, and it encourages to cover all regions where has mass. When we minimize (the "reverse" or "exclusive" KL), we penalize cases where but , which encourages to concentrate on modes of . Language model training uses the forward KL form through cross-entropy, which means models are penalized for assigning low probability to tokens that occur in training data, encouraging broad coverage of plausible continuations.
However, KL divergence has an asymmetry that matters for machine learning: it requires knowing the true distribution over all possible outcomes. In practice, we only observe specific outcomes from , not the full distribution. We see individual tokens in our training data, but we do not know the complete probability distribution over all possible next tokens for every context. This limitation prevents us from computing the KL divergence directly during training, necessitating the use of cross-entropy instead.