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

Speaker 1Speaker 2

Language Trees via Compression

13:27
0:00-13:27
  1. Corey

    Today we're exploring a surprisingly deep connection: compression, cross-entropy, and language models. It begins with a 2002 paper called Language Trees and Zipping, which showed that general-purpose file compression could reveal structure between languages.

  2. Maria

    The setup is fascinating. Give a compressor a collection of text documents in different languages, and ask it to cluster them by language. You can even try to recover which languages are most closely related and reconstruct a tree of shared linguistic lineage, without giving the system any pre-built linguistic knowledge.

  3. Corey

    And this was before modern language models. The authors used gzip, essentially the same kind of tool used to make files smaller on a computer. For two documents, A and B, they appended a small piece of B to A, compressed that combined file, and compared its size with the size of A compressed by itself.

  4. Maria

    The intuition is that the compressor has mostly adapted to the patterns in A. If a snippet from B has similar linguistic patterns, it should compress well using what the compressor learned from A. If B is linguistically different, that snippet should require more space.

  5. Corey

    That gives a distance-like measure based only on co-compression. Remarkably, the measure was good enough to recover a tree of language lineage. The same general idea can also be used for tasks such as identifying the author of a document.

  6. Maria

    So the broader lesson is that compression engineering can solve problems that sound like they belong to machine learning or artificial intelligence. The mathematical concept underneath this example is cross-entropy, which is also central to how modern language models are trained.

  7. Corey

    Let's build cross-entropy from the basics of encoding messages as bits. Imagine messages made from four symbols: instructions telling a robot to move up, down, left, or right. Suppose up has probability one-half, down has probability one-fourth, and left and right each have probability one-eighth.

  8. Maria

    Because some symbols are more likely than others, an efficient code should use fewer bits for likely symbols and more bits for rare ones. In this example, up can use one bit, down two bits, and left and right three bits each.

  9. Corey

    The general rule is that the number of bits assigned to a symbol is approximately negative log base two of its probability. Claude Shannon called this negative-log quantity the information content of the event.

  10. Maria

    That negative log can feel strange at first, but it measures how many halvings it takes to get down to a given probability. A probability of one-half corresponds to one bit, one-fourth to two bits, and one-eighth to three bits.

  11. Corey

    Usually probabilities do not produce whole numbers of bits. That's not a problem. For a complete message, the information content is the sum of the information contents of its individual symbols, and an optimal encoding uses approximately that many total bits. Fractional information has a real meaning as an average.

  12. Maria

    The average information per symbol is called entropy. If a distribution is Q, an encoding optimized for Q assigns negative log base two of Q sub i bits to symbol i, and the average length is the weighted sum of each probability times its information content.

  13. Corey

    Now let's change the robot's behavior. Suppose the new distribution gives up and down probability one-eighth each, left probability one-fourth, and right probability one-half. But our encoder is still hard-coded for the old distribution.

  14. Maria

    Under the old code, up costs one bit and occurs one-eighth of the time, down costs two bits and also occurs one-eighth of the time, while left and right cost three bits and together occur three-fourths of the time. The average is one-eighth times one, plus one-eighth times two, plus three-fourths times three, which equals 2.625 bits per symbol.

  15. Corey

    That average is the cross-entropy of the original distribution relative to the new one. In plain language, it asks: if your compression scheme is optimized for one context, how well does it perform when the symbols actually come from another context?

  16. Maria

    Let's label the new, real distribution P and the original, coding distribution Q. The widths in the weighted sum come from P, because P tells us how often symbols actually occur. The heights come from negative log base two of Q sub i, because Q determines how many bits the code assigns.

  17. Corey

    So the cross-entropy is the sum over symbols of P sub i multiplied by negative log base two of Q sub i. It is different from the entropy of Q, where Q supplies both the frequencies and the code lengths.

  18. Maria

    A two-event example makes the order of the distributions clear. If Q is even, assigning one-half to each event, then each event has one bit of information. Even if P is skewed ninety-ten, the code still spends one bit on either event, so the cross-entropy remains one bit.

  19. Corey

    But reverse the roles. Let Q be the ninety-ten distribution, and let P be even. The code optimized for Q gives very short codes to the common event and longer codes to the rare event. When both events become equally likely, that mismatch is costly, and the cross-entropy is about 1.74 bits.

  20. Maria

    This shows that cross-entropy is not symmetric. Swapping P and Q changes the answer because P describes what actually happens, while Q describes the code or model being used.

  21. Corey

    There is also a key geometric fact. Fix P and vary Q. The cross-entropy reaches its minimum when Q equals P, and that minimum value is the entropy of P. In other words, the most efficient code for reality is the code whose assumed distribution matches reality.

  22. Maria

    You can visualize this with bars. Keep each bar's width fixed according to P, while Q controls the height through negative log Q sub i. As Q changes, the total bar area changes, but it cannot fall below the entropy of P. The lower bound occurs exactly when the bar heights correspond to P itself.

  23. Corey

    Now return to language trees and zipping. Compressing A by itself gives a compressor adapted to A's patterns. Appending a piece of B and measuring the additional compressed size asks how well a code optimized for A performs on material from B.

  24. Maria

    That is essentially the cross-entropy question. The document from one language acts like a sample from one distribution, and the extra compressed length for text from another language acts like an empirical estimate of cross-entropy.

  25. Corey

    It is only an approximation. The documents are samples rather than explicit probability distributions, and gzip does not achieve the theoretical Shannon limit. Its algorithm mainly finds repeated text sequences and replaces later occurrences with pointers to earlier ones.

  26. Maria

    Even so, the approximation can be useful. Whenever we want to quantify how different the patterns in one setting are from those in another, cross-entropy becomes relevant. In language comparison, the settings are different languages; in language modeling, the comparison is between a model's predictions and patterns in the training data.

  27. Corey

    A modern language model divides text into tokens, usually words or pieces of words. It takes a sequence of tokens as input and produces a probability distribution over possible next tokens.

  28. Maria

    To train it, we need a loss function that says whether its predictions are good or bad. Training then uses gradient descent and backpropagation to adjust the model's many parameters, lowering that loss over a huge collection of text.

  29. Corey

    For every position in a training example, the model predicts the next token. We then look at the token that actually appears and record the probability the model assigned to it. The loss for that prediction is the negative log of that probability.

  30. Maria

    The interpretation is information from the model's perspective. If the model assigns a high probability to the true next token, the token is unsurprising and contributes little loss. If it assigns a very low probability, the token is surprising and contributes a large loss.

  31. Corey

    The total training loss is the average of those negative-log probabilities across all tokens in the training data. In machine learning, natural logarithms are usually used instead of base-two logarithms. They differ only by a constant factor, and natural logs make derivatives cleaner for gradient-based optimization.

  32. Maria

    This objective is commonly called cross-entropy loss. One standard explanation says that the model's full predicted distribution is compared with a distribution that puts all its probability on the one true next token. The weighted sum then reduces to the negative log probability assigned to that token.

  33. Corey

    But there is a deeper reason the logarithm appears. Suppose the loss for one prediction is some decreasing function F of the probability Q assigned to the true answer. We want the average loss over many examples to be minimized exactly when the model's distribution matches the distribution found in the data.

  34. Maria

    Consider a pattern such as 'my name is' followed by different names. Let P represent how frequently each name occurs in the training data, and Q represent the model's probabilities. The average loss is a weighted sum: each data frequency P is multiplied by F of the model probability Q.

  35. Corey

    A constrained-optimization argument shows that, for this average to be minimized precisely when P and Q match, the derivative of F has to behave like a constant divided by Q. The function with that derivative is a logarithm, so the negative log is not merely a convenient choice; the desired matching property essentially forces it.

  36. Maria

    With F equal to negative log, the average loss is exactly cross-entropy between the statistics of the data and the model's output distribution. Minimizing it makes the model's predicted distribution approach the distribution represented in the training examples.

  37. Corey

    At the level of loss functions, pre-training is remarkably simple: for every token in a vast training set, take the negative log of the probability the model assigns to the actual next token, then average. The practical details, such as batching, optimizers, and the engineering needed to run at enormous scale, are substantial, but the basic objective is this.

  38. Maria

    There is another setting where cross-entropy appears more explicitly: distillation. Here, we have a large, capable model and want a smaller model to approximate its performance more efficiently.

  39. Corey

    At each position in the data, the small model produces a full probability distribution over possible next tokens. Instead of comparing it only with the one token that happened to occur, we compare that entire distribution with the large model's distribution and use their cross-entropy as the loss.

  40. Maria

    That provides much richer information than ordinary pre-training. Ordinary training gives full weight to the one observed next token, while distillation lets the large model communicate how it weighs all possible continuations.

  41. Corey

    The analogy is learning chess by watching one game versus learning from a stronger player who explains the relative quality of many possible moves. For the 'my name is' example, a single large-model prediction can communicate a sensible distribution over names, rather than requiring many examples before the smaller model discovers those patterns.

  42. Maria

    Now the connection to compression becomes tighter. The language-model loss is the average information per token from the model's perspective. If a predictor can be turned into a compressor, then the number of bits used for text can approximately equal the information that the model assigns to that text.

  43. Corey

    That means minimizing cross-entropy can be reframed as training the model to become an increasingly effective text compressor. The full construction of how to turn a predictor into a compressor requires a separate compression algorithm, but the shared quantity is already visible in the average information per token.

  44. Maria

    This is why the phrase 'compression is intelligence' becomes worth examining. Compression and prediction are linked because a good predictor assigns high probability to likely continuations, and high probability means low information and fewer bits in an ideal code.

  45. Corey

    Finally, let's discuss KL divergence. Cross-entropy is always at least as large as the entropy of P, with equality when P and Q are the same. The difference between cross-entropy and entropy has its own name: KL divergence.

  46. Maria

    In compression terms, KL divergence tells you how many bits per symbol you waste by using a code optimized for the wrong distribution. It is the unoptimized average bits per symbol minus the optimized average bits per symbol.

  47. Corey

    In machine learning, KL divergence acts somewhat like a distance between distributions. It is zero when the distributions match and grows as they differ, but it is not a symmetric distance: the KL divergence of one distribution relative to another is generally different from the reverse.

  48. Maria

    The compact KL formula can be understood as the difference between cross-entropy and entropy. That relationship is useful because it hides the longer weighted-sum interpretation while preserving the idea of measuring the penalty for using one distribution in place of another.

  49. Corey

    And it raises a natural question about distillation. If KL divergence behaves like a distance, should we use it instead of cross-entropy when training the small model to imitate the large one? Understanding that question requires checking exactly which terms depend on the small model and which are fixed by the large model.

  50. Maria

    So the main path we've followed is this: optimal coding leads to information measured by negative logarithms; using a code optimized for one distribution on another leads to cross-entropy; cross-entropy measures mismatch in language-compression tasks; and the same expression becomes the loss used to train and distill language models.

  51. Corey

    The surprising unification is that prediction, compression, language comparison, pre-training, distillation, and KL divergence are not isolated ideas. They are different views of how probability distributions describe patterns, and how costly it is when the distribution you use does not match the distribution reality provides.

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent