1. Home
  2. Explore
  3. Linguistics
  4. Language Trees via Compression

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Language Trees via Compression

Cross-Entropy, Compression, and Language Models

Compression as a Tool for Discovering Language Structure

A striking result from the early 2000s showed that general-purpose file compression can reveal meaningful relationships between languages without using pre-built linguistic knowledge.

Suppose a collection of documents is written in several different languages. A system could be asked to:

  • Cluster documents according to their language.

  • Determine which languages are most closely related.

  • Reconstruct a tree representing shared linguistic ancestry.

  • Identify the likely author of an unknown document.

The surprising part is that these tasks can be approached using ordinary compression software such as gzip, rather than a specialized language model or linguistic database.

The co-compression idea

Given two documents, AAA and BBB:

  1. Compress document AAA by itself.

  2. Append a short portion of BBB to AAA.

  3. Compress the combined document.

  4. Compare the compressed sizes.

A compressor trained primarily on AAA becomes optimized for the repeated patterns, symbols, and structures found in AAA. If the appended text from BBB has similar patterns, it should compress relatively efficiently. If BBB is written in a very different language, its patterns will be less familiar to the compressor and the combined file will be larger.

Thus, the difference in compressed size acts as an approximate measure of how different the two documents are.

Core idea: A compression scheme optimized for one source performs better on another source when the two sources share statistical patterns.

By applying this comparison to many pairs of documents, it becomes possible to construct a distance measure between languages. The resulting distances can be used to produce a language-family tree.

Why this connects to machine learning

Compression and language modeling appear to be different areas:

  • Compression focuses on representing data with as few bits as possible.

  • Machine learning focuses on learning patterns and making predictions.

However, both depend on estimating probabilities. A good compressor assigns short descriptions to likely events and longer descriptions to unlikely events. A good language model assigns high probabilities to likely next tokens and low probabilities to unlikely ones.

The common mathematical concept linking these tasks is cross-entropy.


Information Content and Optimal Compression

Symbols as probabilistic events

A message can be treated as a sequence of symbols. Symbols might be:

  • Characters in a text.

  • Words or pieces of words.

  • Robot movement instructions.

  • Any other discrete events.

If each symbol is sampled from a probability distribution, the probabilities determine how efficiently the message can theoretically be compressed.

A toy robot example

Consider a robot that receives one of four instructions:

  • Up

  • Down

  • Left

  • Right

Suppose the instructions are sampled according to the following distribution:

  • Up: probability 12\frac{1}{2}21​

  • Down: probability 14\frac{1}{4}41​

  • Left: probability 18\frac{1}{8}81​

  • Right: probability 18\frac{1}{8}81​

A sensible encoding assigns shorter bit strings to more probable symbols:

  • Up: 111 bit

  • Down: 222 bits

  • Left: 333 bits

  • Right: 333 bits

The common pattern is:

bits assigned to symbol i≈−log⁡2(qi)\text{bits assigned to symbol } i \approx -\log_2(q_i)bits assigned to symbol i≈−log2​(qi​)

where qiq_iqi​ is the probability of symbol iii.

For the robot instructions:

−log⁡2(12)=1-\log_2\left(\frac{1}{2}\right)=1−log2​(21​)=1
−log⁡2(14)=2-\log_2\left(\frac{1}{4}\right)=2−log2​(41​)=2
−log⁡2(18)=3-\log_2\left(\frac{1}{8}\right)=3−log2​(81​)=3

Information content

The quantity

−log⁡2(qi)-\log_2(q_i)−log2​(qi​)

is the information content of an event with probability qiq_iqi​.

It measures how surprising the event is:

  • Very probable events carry little information.

  • Rare events carry more information.

  • An event with probability 111 carries zero information.

  • An event with probability 12\frac{1}{2}21​ carries one bit of information.

The negative logarithm is useful because it converts multiplication of probabilities into addition:

−log⁡2(ab)=−log⁡2(a)−log⁡2(b)-\log_2(ab)=-\log_2(a)-\log_2(b)−log2​(ab)=−log2​(a)−log2​(b)

This means that the information content of a sequence can be obtained by adding the information content of its individual symbols.

Fractional information

Probabilities do not usually produce whole-number values of −log⁡2(qi)-\log_2(q_i)−log2​(qi​). For example, if an event has probability 13\frac{1}{3}31​, its information content is:

−log⁡2(13)=log⁡2(3)-\log_2\left(\frac{1}{3}\right)=\log_2(3)−log2​(31​)=log2​(3)

which is approximately 1.5851.5851.585 bits.

Although an individual symbol cannot normally be assigned a fractional-length ordinary bit string, fractional information has a real meaning when averaged over many messages. Advanced compression methods can approach these average limits using techniques such as arithmetic coding.


Entropy: The Best Possible Average Compression

For a probability distribution QQQ with symbol probabilities qiq_iqi​, the information content of symbol iii is:

−log⁡2(qi)-\log_2(q_i)−log2​(qi​)

The average information per symbol is the probability-weighted sum:

H(Q)=−∑iqilog⁡2(qi)H(Q)=-\sum_i q_i\log_2(q_i)H(Q)=−∑i​qi​log2​(qi​)

This quantity is the entropy of QQQ.

Entropy represents the minimum possible average number of bits per symbol for an ideal compression scheme when the true distribution is QQQ.

Visual interpretation of entropy

Entropy can be visualized as a collection of bars:

  • The width of bar iii is qiq_iqi​.

  • The height of bar iii is −log⁡2(qi)-\log_2(q_i)−log2​(qi​).

  • The area of bar iii is qi[−log⁡2(qi)]q_i[-\log_2(q_i)]qi​[−log2​(qi​)].

  • The sum of all bar areas is the entropy.

Thus, entropy is the average information content of events generated by the distribution.

Entropy and uncertainty

Entropy is larger when probability is spread more evenly among possible outcomes.

For example:

  • A distribution in which one event is nearly certain has low entropy.

  • A distribution in which many events are similarly likely has higher entropy.

For a two-event distribution with equal probabilities, the entropy is:

H(Q)=−12log⁡2(12)−12log⁡2(12)=1H(Q) = -\frac{1}{2}\log_2\left(\frac{1}{2}\right) -\frac{1}{2}\log_2\left(\frac{1}{2}\right) =1H(Q)=−21​log2​(21​)−21​log2​(21​)=1

The distribution has one bit of entropy per symbol.


Cross-Entropy

Using the wrong compression model

Suppose the true distribution of symbols is PPP, but the compression code was designed for a different distribution QQQ.

The code optimized for QQQ assigns symbol iii approximately:

−log⁡2(qi)-\log_2(q_i)−log2​(qi​)

bits.

However, symbols actually occur according to PPP, so the average number of bits used is:

H(P,Q)=−∑ipilog⁡2(qi)H(P,Q) = -\sum_i p_i\log_2(q_i)H(P,Q)=−∑i​pi​log2​(qi​)

This is the cross-entropy of PPP relative to QQQ.

The terminology can vary in different sources, but the essential roles are:

  • PPP: the distribution that generates the data or represents reality.

  • QQQ: the distribution used by the model or compression code.

Cross-entropy measures how well a code optimized for QQQ performs when data is actually generated according to PPP.

The weights in the average come from PPP, because PPP determines how often each symbol appears. The bit lengths come from QQQ, because QQQ determines the code being used.

Visual interpretation of cross-entropy

Cross-entropy uses bars similar to the entropy diagram:

  • The width of bar iii is pip_ipi​.

  • The height of bar iii is −log⁡2(qi)-\log_2(q_i)−log2​(qi​).

  • The total area is H(P,Q)H(P,Q)H(P,Q).

The difference from entropy is that the distribution determining how often a symbol occurs and the distribution determining its code length may be different.

Robot example with a changed distribution

Initially, the robot instructions had distribution QQQ:

  • Up: 12\frac{1}{2}21​

  • Down: 14\frac{1}{4}41​

  • Left: 18\frac{1}{8}81​

  • Right: 18\frac{1}{8}81​

The code optimized for this distribution uses:

  • Up: 111 bit

  • Down: 222 bits

  • Left: 333 bits

  • Right: 333 bits

Now suppose the actual distribution changes to PPP:

  • Up: 18\frac{1}{8}81​

  • Down: 18\frac{1}{8}81​

  • Left: 14\frac{1}{4}41​

  • Right: 12\frac{1}{2}21​

The old code is still used. The average number of bits becomes:

H(P,Q)=18(1)+18(2)+14(3)+12(3)=2.625\begin{aligned} H(P,Q) &= \frac{1}{8}(1) + \frac{1}{8}(2) + \frac{1}{4}(3) + \frac{1}{2}(3) \\ &= 2.625 \end{aligned}H(P,Q)​=81​(1)+81​(2)+41​(3)+21​(3)=2.625​

The value 2.6252.6252.625 bits per symbol is the cross-entropy: the average cost of using a code optimized for the old distribution when the new distribution is the one actually occurring.


Cross-Entropy Is Not Symmetric

Cross-entropy depends on the order of its arguments:

H(P,Q)≠H(Q,P)H(P,Q)\neq H(Q,P)H(P,Q)=H(Q,P)

This is natural because the two distributions have different roles:

  • The first distribution determines the frequencies of events.

  • The second distribution determines the code lengths or predicted probabilities.

Two-outcome example

Consider two possible events.

Case 1: QQQ is uniform and PPP is skewed

Let:

  • Q=(0.5,0.5)Q=(0.5,0.5)Q=(0.5,0.5)

  • P=(0.9,0.1)P=(0.9,0.1)P=(0.9,0.1)

Because QQQ assigns probability 0.50.50.5 to each event, both events receive one bit of information:

−log⁡2(0.5)=1-\log_2(0.5)=1−log2​(0.5)=1

Therefore:

H(P,Q)=0.9(1)+0.1(1)=1H(P,Q)=0.9(1)+0.1(1)=1H(P,Q)=0.9(1)+0.1(1)=1

Even though PPP is skewed, the code remains one bit per symbol because the code optimized for QQQ gives both outcomes the same code length.

The entropy of QQQ is also:

H(Q)=1H(Q)=1H(Q)=1

Case 2: QQQ is skewed and PPP is uniform

Now reverse the roles:

  • Q=(0.9,0.1)Q=(0.9,0.1)Q=(0.9,0.1)

  • P=(0.5,0.5)P=(0.5,0.5)P=(0.5,0.5)

The code optimized for QQQ assigns:

  • The common event a short code.

  • The rare event a much longer code.

The entropy of QQQ is less than one bit because QQQ is skewed. However, under the actual distribution PPP, both events occur equally often. The rare event under QQQ is encountered much more frequently than expected, making the average code length about:

H(P,Q)≈1.74 bitsH(P,Q)\approx 1.74\text{ bits}H(P,Q)≈1.74 bits

Thus, swapping PPP and QQQ changes the result substantially.


The Minimum of Cross-Entropy

For a fixed actual distribution PPP, consider cross-entropy as a function of the proposed distribution QQQ:

H(P,Q)=−∑ipilog⁡2(qi)H(P,Q)=-\sum_i p_i\log_2(q_i)H(P,Q)=−∑i​pi​log2​(qi​)

The cross-entropy reaches its minimum when:

Q=PQ=PQ=P

At that point:

H(P,Q)=H(P)H(P,Q)=H(P)H(P,Q)=H(P)

Therefore:

The best possible code for data generated according to PPP is the code optimized for PPP, and its minimum average cost is the entropy of PPP.

If QQQ differs from PPP, the code is less efficient and cross-entropy becomes larger.

Geometric intuition

In a two-outcome distribution:

  • PPP can be described by one parameter, such as p1p_1p1​, because the other probability is 1−p11-p_11−p1​.

  • QQQ can similarly be described by q1q_1q1​.

If PPP is fixed and cross-entropy is plotted against QQQ, the curve has a minimum at:

Q=PQ=PQ=P

If the value of PPP is moved, the locations of these minima trace out the entropy curve:

H(P)=−plog⁡2(p)−(1−p)log⁡2(1−p)H(P)=-p\log_2(p)-(1-p)\log_2(1-p)H(P)=−plog2​(p)−(1−p)log2​(1−p)

This demonstrates that entropy is the lowest cross-entropy achievable for a given true distribution.


Compression and Language Similarity

The language-tree experiment can be understood as an approximate application of cross-entropy.

Document compression as model transfer

A compressor trained on document AAA is optimized for the patterns present in AAA. When a snippet from document BBB is appended, the compressor must encode BBB using a model shaped by AAA.

The resulting additional compressed length asks:

How efficiently can patterns from BBB be encoded using a compression scheme optimized for AAA?

That is essentially the same question as cross-entropy.

Why the method works

If AAA and BBB are written in closely related languages:

  • They may share vocabulary patterns.

  • They may use similar character sequences.

  • They may share grammatical regularities.

  • They may have similar spelling and punctuation patterns.

  • They may contain recurring structures that a compressor can reuse.

The snippet from BBB will then tend to compress well after AAA.

If the languages are unrelated, fewer patterns transfer from AAA to BBB, so the appended text will require more bits.

Why gzip is only an approximation

The compression distance is not exactly cross-entropy because:

  1. A text document is treated as a sample from an underlying language distribution rather than as an explicit probability distribution.

  2. Gzip is not an ideal compressor and does not generally achieve the theoretical Shannon limit.

  3. Gzip primarily detects repetitions and replaces later repetitions with references to earlier sequences.

  4. The specific distance measure may include additional normalization or comparison details.

Nevertheless, the approximation can still be useful. Even an imperfect compressor may preserve enough information about statistical similarity to distinguish languages and authors.


Language Models as Predictive Distributions

A modern language model can be viewed as a function with a large collection of adjustable parameters.

Tokens

Language models do not necessarily process complete words. They divide text into tokens, which may be:

  • Whole words.

  • Pieces of words.

  • Individual characters.

  • Punctuation marks.

  • Other frequently occurring text fragments.

A text is represented as a sequence of tokens.

Model input and output

Given an input sequence of tokens, a language model produces a probability distribution over all possible next tokens.

For example, after receiving:

“The robot moved”

the model might assign probabilities to possible continuations such as:

  • “up”

  • “quickly”

  • “through”

  • “because”

The model’s output is not merely one guess. It is a complete probability distribution describing its uncertainty about what comes next.

Training objective

To train the model, a loss function measures the quality of its predictions. Training then adjusts the model’s parameters to reduce this loss.

The model is optimized using standard deep-learning machinery, especially:

  • Gradient descent, which changes parameters in directions that reduce the loss.

  • Backpropagation, which computes how each parameter contributed to the loss.

  • Optimization algorithms that make this process practical at large scale.

The loss landscape technically has one dimension for every model parameter, potentially billions of dimensions. Two-dimensional hill diagrams are only simplified visual analogies.


Pre-Training and Per-Token Information

The next-token training procedure

For a training sequence:

  1. Provide the model with an initial sequence of tokens.

  2. Ask the model for a probability distribution over the next token.

  3. Identify the actual next token in the training data.

  4. Record the probability assigned to that true token.

  5. Convert that probability into a loss using its negative logarithm.

  6. Repeat for every position in the sequence and across the entire training corpus.

  7. Average the losses.

For a true next token with model-assigned probability qqq, the per-token loss is:

−log⁡(q)-\log(q)−log(q)

In machine learning, the natural logarithm is usually used:

−ln⁡(q)-\ln(q)−ln(q)

Using ln⁡\lnln instead of log⁡2\log_2log2​ changes the values only by a constant factor:

log⁡2(q)=ln⁡(q)ln⁡(2)\log_2(q)=\frac{\ln(q)}{\ln(2)}log2​(q)=ln(2)ln(q)​

That constant factor can be absorbed into the learning rate during optimization.

Why the negative logarithm is appropriate

The negative logarithm has the desired behavior:

  • If the model assigns high probability to the true token, the loss is small.

  • If the model assigns low probability to the true token, the loss is large.

  • If the model assigns an extremely small probability, the penalty grows sharply.

  • If the model assigns probability 111, the loss is zero.

Thus, the loss strongly punishes confident but incorrect predictions.

A poorly trained or random model gives low probability to the actual next token and therefore receives a high average loss. A knowledgeable model assigns higher probabilities to tokens that actually occur and receives a lower loss.

Pre-training as average information

The total pre-training loss is the average negative log-probability of the true next token:

L=−1N∑j=1Nln⁡qjL = -\frac{1}{N} \sum_{j=1}^{N} \ln q_jL=−N1​∑j=1N​lnqj​

where:

  • NNN is the number of prediction events.

  • qjq_jqj​ is the model’s probability for the true token at event jjj.

This can be interpreted as:

The average information, or surprise, of the observed text from the model’s perspective.

At a high level, pre-training attempts to make the model’s predictions approach the statistical structure of the language represented by the training data.

Why the loss is called cross-entropy

For an individual training example, the target distribution is often represented as a one-hot distribution:

  • Probability 111 for the actual next token.

  • Probability 000 for every other token.

If the model’s predicted distribution is QQQ, and the target distribution is PPP, then cross-entropy is:

H(P,Q)=−∑ipiln⁡(qi)H(P,Q)=-\sum_i p_i\ln(q_i)H(P,Q)=−∑i​pi​ln(qi​)

Because every pip_ipi​ is zero except for the true token, the expression reduces to:

H(P,Q)=−ln⁡(qtrue)H(P,Q)=-\ln(q_{\text{true}})H(P,Q)=−ln(qtrue​)

This explains the standard implementation of next-token loss, but it does not fully explain why logarithms and cross-entropy are the right choices. A deeper explanation comes from requiring the loss to reward matching the true data distribution.


Why the Logarithm Is Mathematically Natural

Suppose the per-example loss is represented by a general decreasing function FFF of the model’s assigned probability:

F(q)F(q)F(q)

The loss should decrease as qqq increases because assigning higher probability to the correct outcome should be rewarded.

Consider a repeated pattern such as:

“My name is ___”

Suppose different names occur in the training data with proportions pip_ipi​. The model assigns probabilities qiq_iqi​ to those names.

The average loss over all examples of this pattern is:

∑ipiF(qi)\sum_i p_iF(q_i)∑i​pi​F(qi​)

The desired property is that this average loss should be minimized exactly when the model distribution matches the data distribution:

qi=piq_i=p_iqi​=pi​

subject to the requirement that the model’s probabilities sum to one:

∑iqi=1\sum_i q_i=1∑i​qi​=1

A constrained optimization argument using a Lagrange multiplier shows that the derivative of FFF must have the form:

F′(q)=CqF'(q)=\frac{C}{q}F′(q)=qC​

for some constant CCC.

The only functions whose derivatives have this reciprocal form are logarithmic functions:

F(q)=Cln⁡(q)+DF(q)=C\ln(q)+DF(q)=Cln(q)+D

Because the loss must decrease as qqq increases, the relevant choice is the negative logarithm:

F(q)=−ln⁡(q)F(q)=-\ln(q)F(q)=−ln(q)

Therefore, the logarithmic loss is not merely one convenient decreasing function among many. It is forced by the requirement that the average loss be minimized when the model’s output distribution matches the empirical distribution in the data.

When the negative logarithm is averaged over all possible outcomes, the result is exactly cross-entropy:

H(P,Q)=−∑ipiln⁡(qi)H(P,Q)=-\sum_i p_i\ln(q_i)H(P,Q)=−∑i​pi​ln(qi​)

Distillation: Learning from a Larger Model

Motivation

Pre-training a small model directly from raw text provides only one explicit target at each position: the token that actually occurred.

A larger model, however, can provide a complete probability distribution over possible next tokens. A smaller model can be trained to imitate this richer distribution.

This process is called knowledge distillation or simply distillation.

The goals are usually to:

  • Preserve much of a large model’s performance.

  • Reduce the number of parameters.

  • Lower memory and computation requirements.

  • Make inference faster and cheaper.

Distillation procedure

At each prediction position:

  1. The small model produces a probability distribution QQQ.

  2. The large model produces a probability distribution PPP.

  3. The loss is the cross-entropy between these distributions:

H(P,Q)=−∑ipiln⁡(qi)H(P,Q)=-\sum_i p_i\ln(q_i)H(P,Q)=−∑i​pi​ln(qi​)
  1. The small model’s parameters are adjusted to make QQQ resemble PPP.

The large model is often called the teacher, while the smaller model is the student.

Why distillation contains more information than ordinary pre-training

Ordinary pre-training treats the observed next token as the only target with positive weight. It does not directly communicate how plausible the other possible tokens were.

For example, if the correct next token is “Alice,” ordinary training says:

  • “Alice” is the observed answer.

  • Every other token is treated as incorrect for this particular example.

The teacher model might instead assign a distribution such as:

  • “Alice”: high probability

  • “Bob”: moderate probability

  • “Charlie”: low but nonzero probability

  • Other names: very small probabilities

This distribution provides information about the teacher’s uncertainty and the relationships among possible answers.

Analogy

Learning from ordinary pre-training is like watching a chess game and seeing only which move was played.

Distillation is like having a stronger player explain:

  • Which moves were considered.

  • Which moves were good.

  • Which moves were nearly as good.

  • How strongly each move was preferred.

For a pattern such as “My name is ___,” a student model might need many examples before it learns a useful distribution over names through ordinary pre-training. Distillation can provide a similar distributional signal from a single example by comparing the student’s output with the teacher’s full prediction.


Cross-Entropy as a General Comparison Tool

Cross-entropy is useful whenever one wants to compare statistical patterns in one setting with a model or code optimized for another.

Examples include:

  • Comparing the patterns of two languages.

  • Measuring how well a model predicts a language.

  • Evaluating a compression code on new data.

  • Training a student model to imitate a teacher model.

  • Measuring the difference between an empirical distribution and a predicted distribution.

Its central interpretation remains the same:

Cross-entropy is the average cost of using distribution QQQ to represent data generated according to distribution PPP.

This explains why it appears both in compression theory and in machine learning.


From Predictors to Compressors

A language model predicts a probability distribution for each next token. In principle, those predictions can be used to compress text.

If the model assigns probability qiq_iqi​ to a token, that token has model-based information content:

−log⁡2(qi)-\log_2(q_i)−log2​(qi​)

A compression algorithm can use these probabilities to allocate approximately that many bits to the token. Across an entire text, the total number of bits approaches the sum of the information contents:

−∑t=1Nlog⁡2(qt)-\sum_{t=1}^{N}\log_2(q_t)−∑t=1N​log2​(qt​)

where qtq_tqt​ is the model’s probability for the observed token at position ttt.

Therefore, a model with a lower average cross-entropy assigns shorter descriptions to the observed text. Training the model to minimize cross-entropy can be interpreted as training it to compress text more efficiently.

This gives a powerful reinterpretation of language-model training:

  • The usual description is next-token prediction.

  • The compression description is learning an efficient representation of text.

  • Both descriptions use the same negative log-probability objective.

An ideal model would approach the entropy of the language, representing text close to its theoretical minimum description length.


Kullback–Leibler Divergence

Definition

Cross-entropy is always at least as large as entropy:

H(P,Q)≥H(P)H(P,Q)\geq H(P)H(P,Q)≥H(P)

The difference between them is called Kullback–Leibler divergence, or KL divergence:

DKL(P∥Q)=H(P,Q)−H(P)D_{\mathrm{KL}}(P\|Q) = H(P,Q)-H(P)DKL​(P∥Q)=H(P,Q)−H(P)

Expanding the definitions gives:

DKL(P∥Q)=−∑ipiln⁡(qi)−(−∑ipiln⁡(pi))=∑ipiln⁡(piqi)\begin{aligned} D_{\mathrm{KL}}(P\|Q) &= -\sum_i p_i\ln(q_i) - \left(-\sum_i p_i\ln(p_i)\right) \\ &= \sum_i p_i\ln\left(\frac{p_i}{q_i}\right) \end{aligned}DKL​(P∥Q)​=−i∑​pi​ln(qi​)−(−i∑​pi​ln(pi​))=i∑​pi​ln(qi​pi​​)​

Thus, the compact expression and the entropy-difference expression are equivalent.

Compression interpretation

In compression, KL divergence measures the excess cost of using a code optimized for QQQ when the true distribution is PPP.

  • H(P)H(P)H(P): the ideal average number of bits per symbol when the code is optimized for PPP.

  • H(P,Q)H(P,Q)H(P,Q): the average number of bits when the code is optimized for QQQ.

  • DKL(P∥Q)D_{\mathrm{KL}}(P\|Q)DKL​(P∥Q): the number of extra bits wasted by using the mismatched code.

For the robot example:

DKL(P∥Q)=bits per symbol with the old code−bits per symbol with the optimal new codeD_{\mathrm{KL}}(P\|Q) = \text{bits per symbol with the old code} - \text{bits per symbol with the optimal new code}DKL​(P∥Q)=bits per symbol with the old code−bits per symbol with the optimal new code

Properties of KL divergence

KL divergence has several important properties:

  • It is always nonnegative:

DKL(P∥Q)≥0D_{\mathrm{KL}}(P\|Q)\geq 0DKL​(P∥Q)≥0
  • It equals zero when the distributions are identical:

DKL(P∥Q)=0if and only ifP=QD_{\mathrm{KL}}(P\|Q)=0 \quad\text{if and only if}\quad P=QDKL​(P∥Q)=0if and only ifP=Q
  • It grows as the distributions become more different.

  • It is asymmetric:

DKL(P∥Q)≠DKL(Q∥P)D_{\mathrm{KL}}(P\|Q)\neq D_{\mathrm{KL}}(Q\|P)DKL​(P∥Q)=DKL​(Q∥P)

Therefore, KL divergence is often described as a divergence or discrepancy rather than a true mathematical distance. A true distance generally needs symmetry, while KL divergence does not have that property.

KL divergence in machine learning

In machine learning, KL divergence can compare:

  • A data distribution and a model distribution.

  • A teacher model and a student model.

  • An original model and an approximating model.

  • A prior distribution and a learned posterior distribution.

For distillation, minimizing the teacher-to-student KL divergence is closely related to minimizing cross-entropy because the teacher’s entropy does not depend on the student’s parameters:

DKL(P∥Q)=H(P,Q)−H(P)D_{\mathrm{KL}}(P\|Q) = H(P,Q)-H(P)DKL​(P∥Q)=H(P,Q)−H(P)

When PPP is fixed, minimizing DKL(P∥Q)D_{\mathrm{KL}}(P\|Q)DKL​(P∥Q) and minimizing H(P,Q)H(P,Q)H(P,Q) produce the same optimal student distribution. The two losses differ only by the constant H(P)H(P)H(P) from the student’s point of view.

Central Relationships

The main concepts can be organized as follows:

  • Information content: The surprise of one event, −log⁡2(p)-\log_2(p)−log2​(p).

  • Entropy: The average information content under one distribution, H(P)H(P)H(P).

  • Cross-entropy: The average cost of using QQQ to encode data generated by PPP, H(P,Q)H(P,Q)H(P,Q).

  • KL divergence: The excess cost caused by using QQQ instead of the optimal distribution PPP, DKL(P∥Q)D_{\mathrm{KL}}(P\|Q)DKL​(P∥Q).

  • Language-model loss: The average negative log-probability assigned to observed next tokens.

  • Compression: A way to turn probabilities into approximate bit lengths.

  • Distillation: Training one model to match the full predictive distribution of another.

The shared mathematical principle is that probability distributions encode both predictions and compression costs. A model that assigns high probability to what actually occurs is simultaneously a better predictor and a more efficient compressor.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent