What Cross-Entropy Connects: Compression, Language Models and KL Divergence

Grant Sanderson builds cross-entropy from code lengths and uses it to explain next-token training and distillation.

GSGrant Sanderson登壇者 · 3Blue1Brown
チャンネル
3Blue1Brown
公開日
2026年7月16日
動画
33:51
読了時間
4 分
原文
英語
YouTube で開く
スクロールすると動画は右下に小さくなって再生を続けます。字幕は閲覧言語に合わせて切り替わります。
閲覧言語2 言語で読めます
この記事はまだ日本語に翻訳されていないため、英語の原文を表示しています。
要約

30 秒でわかる

A 3Blue1Brown lesson derives cross-entropy through an idealized compression example, then connects it to language-model pre-training, distillation and KL divergence.

  1. 1Cross-entropyH(P,Q) averages code lengths based on Q over outcomes drawn from P.
  2. 2The minimumFor fixed P, cross-entropy is minimized when Q matches P.
  3. 3Next-token lossLanguage-model pre-training averages the negative log probability of observed tokens.
  4. 4DistillationA teacher distribution supplies a richer training signal than one observed token.
  5. 5KL divergenceThe gap between cross-entropy and entropy measures excess ideal code length.

ポイント

  1. Information content translates probability into ideal code length.
  2. Cross-entropy measures average code length when the code and source distributions differ.
  3. For a fixed source, cross-entropy is minimized by matching its distribution.
  4. Next-token log loss is an empirical cross-entropy.
  5. KL divergence measures excess code length and is asymmetric.
全文

チャプターごとに整えた全文を読む

文字起こしのみ
01

Compression and language families

Grant Sanderson begins with a 2002 experiment: compressing text from different languages together can reveal similarities and help recover a language family tree. The method uses general compression rather than built-in linguistic knowledge.

Grant Sanderson begins with a 2002 experiment: compressing text from different languages together can reveal similarities and help recover a language family tree. The method uses general compression rather than built-in linguistic knowledge.

Append a snippet of document B to document A, compress the result, and compare its size with compressed A alone. The added size roughly indicates how well a compressor adapted to A handles B; similar patterns tend to compress more efficiently.

The motivating idea is cross-entropy: how well does a coding scheme fitted to one distribution work on data from another? The same mathematics later appears in language-model training.

02

Codes and information

In a toy robot example, instructions up, down, left and right occur with probabilities 1/2, 1/4, 1/8 and 1/8. A suitable prefix code uses one, two, three and three bits respectively.

In a toy robot example, instructions up, down, left and right occur with probabilities 1/2, 1/4, 1/8 and 1/8. A suitable prefix code uses one, two, three and three bits respectively.

For an ideal code, a symbol with probability q carries −log₂q bits of information. Real probabilities need not yield whole numbers of bits per symbol, but longer messages can approach the corresponding average.

03

Defining cross-entropy

Now change the actual distribution: up and down occur 1/8 each, left 1/4 and right 1/2. If the old code is retained, weighting its lengths by the new frequencies gives an average of 2.625 bits per instruction.

Now change the actual distribution: up and down occur 1/8 each, left 1/4 and right 1/2. If the old code is retained, weighting its lengths by the new frequencies gives an average of 2.625 bits per instruction.

Call the actual distribution P and the distribution used to design the code Q. Cross-entropy is H(P,Q) = −Σ pᵢ log₂qᵢ: the average ideal code length under P when lengths are based on Q. Conventions for naming the order vary, so the formula matters.

Consider two outcomes. A code based on Q = (1/2, 1/2) uses one bit per symbol even if reality is P = (0.9, 0.1). Switching the roles changes the result: a code optimized for the skewed distribution costs about 1.74 bits when the true outcomes are evenly split.

Cross-entropy is asymmetric: changing which distribution supplies frequencies and which supplies code lengths changes the value. It should not be treated as an ordinary symmetric distance.

Fix P and vary Q. The minimum occurs when Q matches P, and that minimum is the entropy H(P). In compression language, matching the code to the actual source gives the best possible average ideal length.

04

Compression as an empirical estimate

The language-tree experiment uses a related empirical idea rather than computing exact cross-entropy. A text is only a sample from its language, and gzip finds repeated sequences rather than achieving theoretical optimal compression.

The language-tree experiment uses a related empirical idea rather than computing exact cross-entropy. A text is only a sample from its language, and gzip finds repeated sequences rather than achieving theoretical optimal compression.

05

Language-model pre-training

For a language model, the input is a sequence of tokens and the output is a probability distribution for the next token. Training changes model parameters to reduce a loss measured over a large collection of text.

For a language model, the input is a sequence of tokens and the output is a probability distribution for the next token. Training changes model parameters to reduce a loss measured over a large collection of text.

At each position, take the probability the model assigns to the token that actually occurs. Its negative logarithm is the token’s surprise from the model’s perspective; averaging these values gives the standard next-token log loss.

A model is penalized heavily when it assigns a very small probability to an observed token. Machine-learning implementations commonly use natural logarithms; changing the base scales the loss by a constant.

At this level of abstraction, pre-training minimizes average next-token surprise. Sanderson notes that batching, optimizers and making training run at enormous scale are substantial additional engineering work.

A single observed token can be represented as a one-hot distribution, so its log loss is a cross-entropy. Sanderson finds the deeper explanation more satisfying: consider all examples sharing the same context and their empirical distribution of next tokens.

06

Why the logarithm?

For a prompt such as “my name is,” some names occur more often than others. Averaging the negative log probability over those occurrences produces cross-entropy between the data’s empirical distribution P and the model’s predictions Q.

For a prompt such as “my name is,” some names occur more often than others. Averaging the negative log probability over those occurrences produces cross-entropy between the data’s empirical distribution P and the model’s predictions Q.

Minimizing that average over Q favors matching the frequencies in the data. Sanderson sketches a Lagrange-multiplier argument for the logarithm when the per-example loss depends only on the probability assigned to the observed outcome and must recover arbitrary target distributions.

The constraint is that all predicted probabilities sum to one. The stationarity condition leads to a derivative proportional to 1/q, yielding a logarithm under the assumptions of this local scoring form. Other scoring rules can use the whole predicted distribution, so this is not a claim that every useful loss must be log loss.

07

Distillation and compression

Distillation makes the two distributions explicit. A smaller student model is trained to approximate a larger teacher model: for a given context, compare the student’s full distribution with the teacher’s full distribution.

Distillation makes the two distributions explicit. A smaller student model is trained to approximate a larger teacher model: for a given context, compare the student’s full distribution with the teacher’s full distribution.

The teacher’s probabilities give a softer and richer signal than a single observed token. Sanderson compares it to learning chess from a strong player who explains several possible moves, rather than seeing only the move that happened in one game.

The compression and training perspectives seem different, but both use the same quantity. The model’s average surprise can be interpreted as the ideal code length for text under its predictions.

Sanderson previews a later lesson on turning a predictor into a compressor. A suitable coding algorithm can make the length of an encoded text approximately equal to its information content according to the predictor.

08

KL divergence

KL divergence is the gap between cross-entropy and entropy: DKL(P‖Q) = H(P,Q) − H(P). In compression terms, it measures the extra ideal bits per symbol spent by using a code based on Q for a source distributed as P.

KL divergence is the gap between cross-entropy and entropy: DKL(P‖Q) = H(P,Q) − H(P). In compression terms, it measures the extra ideal bits per symbol spent by using a code based on Q for a source distributed as P.

KL divergence is zero when the distributions agree, but it is generally asymmetric. It is therefore useful as a measure of mismatch, not a symmetric metric.

As an exercise, Sanderson asks what would change if distillation used KL divergence instead of cross-entropy. With a fixed teacher P, H(P) is constant with respect to the student Q, so the two objectives have the same student gradient and optimum.

同じチャンネルの記事

スライド 20 枚40:44EN
3Blue1Brown· 2026年10月16日Grant Sanderson: There's never been a better time to go into mathGrant Sanderson, Constantin Kogler · 3Blue1BrownStudents fear AI has solved mathematics. Grant Sanderson argues the opposite: even if machines prove theorems better than we do, the institution of math stays valuable—and this is a rare moment when newcomers can reshape it.MathematicsAI for scienceHuman-AI Collaboration+2
スライド 29 枚51:55EN
3Blue1Brown· 2026年9月18日The last IMO problem AI could not solveGrant Sanderson · 3Blue1BrownGrant Sanderson walks through IMO 2025 Problem 6 — a tiling puzzle that frontier models missed — and the visual patience it takes to prove a construction is optimal.MathematicsAI researchReasoning & test-time compute+2
0:55EN
3Blue1Brown· 2026年7月24日The 64 Sugar Cubes Puzzle: Six Equal DistancesA 4×4×4 block has three colors; prove that six same-colored pairs share a distance.MathematicsEducation
スライド2:25EN
3Blue1Brown· 2026年6月12日How Shannon Measured the Entropy of EnglishA guessing game helped Claude Shannon estimate how much information English text contains, and why prediction is central to compression.MathematicsAI History
スライド1:02EN
3Blue1Brown· 2026年6月10日A Robot Walk and the Puzzle of Optimal Binary EncodingHow few bits does it take to send directions that occur with unequal frequencies?MathematicsScience Communication
スライド 4 枚32:19EN
3Blue1Brown· 2026年6月7日Reinventing Entropy: Why Prediction Helps Compress LanguageA robot’s movement code leads from prefix-free encoding to Shannon information and the entropy of language.MathematicsScience Communication