5 episodes · follows chapter 3 of the textbook
N-gram Language Models
A language model assigns a probability to every possible sentence. This module builds the oldest working one, the n-gram model, from nothing: the chain rule of probability turns a sentence into a product of next-word predictions, the Markov assumption bounds the context, and counting (which we prove is maximum likelihood) fills the table. We then measure models with cross-entropy and perplexity (the same number every LLM training log prints), sample from them, watch them overfit, and repair their zeros with smoothing, ending in interpolated Kneser-Ney.
Objectives
Language
- Explain what a language model is as a probability distribution over sentences, and why next-word prediction is equivalent
- Explain why sparsity (most n-grams are never seen) is the central problem of count-based language modeling
- Evaluate a language model on held-out text honestly: train/dev/test discipline, <s> and </s>, <UNK>, and leakage
- Describe how n-gram models were used in speech recognition and machine translation, and where n-gram statistics are used in LLM work today (data filtering, contamination checks, infini-gram)
Math & data structures
- Chain rule of probability, conditional probability as a filtered count, the Markov assumption
- Maximum likelihood estimation, with an argument for why relative frequency maximizes likelihood
- Logarithms for probabilities: products become sums, underflow avoided
- Entropy, cross-entropy, and perplexity = 2^H as a weighted branching factor; the link to compression
- Count tables as nested hash maps and tries; memory and lookup cost; sparsity
- Smoothing as moving probability mass: add-k, interpolation (EM for the weights), backoff, absolute discounting, Kneser-Ney continuation counts
Code
- nlpcore.lm.ngram: counts, trie, vocabulary with <UNK>, MLE model with a shared LanguageModel interface
- nlpcore.eval.perplexity: the course-wide perplexity / bits-per-byte module
- nlpcore.lm.sampling_np: temperature, top-k/top-p preview, inverse-CDF sampling, Shannon's game
- nlpcore.lm.smoothing: add-k, interpolation with EM, stupid backoff, absolute discounting, (modified) Kneser-Ney
- A bigram model trained by gradient descent that converges to the counts (bridge to Logistic Regression and Text Classification and Transformers and Pretraining)
Episodes
- The Chain Rule of Probability and the Markov Bet34 min · 31 cues
Joint probability of a sentence, chain rule, Markov assumption as a cache with bounded context; Markov 1913 Eugene Onegin.
- Counting Is Learning: Maximum Likelihood30 min · 31 cues
MLE from counts, why relative frequency maximizes likelihood (derivation with Lagrange intuition or simple argument), n-gram tables as nested dicts/tries, train/dev/test.
- Perplexity, Entropy, and Cross-Entropy26 min · 31 cues
Perplexity as weighted branching factor; entropy as average bits (Huffman); cross-entropy; perplexity = 2^H; connection to LLM loss.
- Sampling, Shannon's Game, and Overfitting24 min · 31 cues
Sampling from an LM, Shannon's guessing game, temperature, overfitting and generalization, zero counts.
- Smoothing, Backoff, and Kneser-Ney (Appendix C)26 min · 32 cues
Add-one, add-k, interpolation, backoff, stupid backoff, absolute discounting, Kneser–Ney continuation counts; Jelinek, Kneser & Ney history.
Exercises
| Exercise | Tier | Ladder | Time |
|---|---|---|---|
| The chain rule of probability and the cost of the Markov bet | browser | Predict · Scratch | 45 min |
| Count tables (nested dict and trie) and the MLE n-gram model | browser | Scratch · Harden · Compose | 75 min |
| Counting is learning: gradient descent on a bigram logit table converges to the counts | browser | Predict · Scratch · Probe a claim | 60 min |
| Port the bigram learner to PyTorch and match NumPy to 1e-9 | gpu | Port | 40 min |
| The shared perplexity module: cross-entropy, bases, zeros, bits per byte, and compressors | browser | Scratch · Harden · Compose · Probe a claim | 60 min |
| Temperature sampling and Shannon's guessing game with character n-grams | browser | Predict · Scratch · Probe a claim | 60 min |
| Interpolated Kneser-Ney from scratch: continuation counts and absolute discounting | browser | Scratch · Compose | 90 min |
| Break it: six ways to get a wrong perplexity (add-one, discounts, EM split, <s>/</s>, leakage, vocabulary) | browser | Break it · Probe a claim | 90 min |
Labs
- The Chain Rule of Probability and the Markov Bet browser C03.01 · 50 min
- Counting Is Learning: Maximum Likelihood browser C03.02 · 70 min
- Perplexity, Entropy, and Cross-Entropy browser C03.03 · 60 min
- Sampling, Shannon's Game, and Overfitting browser C03.04 · 60 min
- Smoothing, Backoff, and Kneser-Ney (Appendix C) browser C03.05 · 90 min
Library contributions
Files in nlpcore this module teaches you to write.
Sources
- Y. Bengio, R. Ducharme, P. Vincent, C. Jauvin, A Neural Probabilistic Language Model, JMLR 2003
- T. Brants, A. C. Popat, P. Xu, F. J. Och, J. Dean, Large Language Models in Machine Translation, EMNLP-CoNLL 2007 · link
- S. F. Chen & J. Goodman, An Empirical Study of Smoothing Techniques for Language Modeling, ACL 1996 · arXiv:cmp-lg/9606011
- S. F. Chen & J. Goodman, An Empirical Study of Smoothing Techniques for Language Modeling, Harvard TR-10-98, 1998 · link
- K. W. Church & W. A. Gale, A comparison of the enhanced Good-Turing and deleted estimation methods for estimating probabilities of English bigrams, Computer Speech and Language, 1991
- Zhang, Lipton, Li, Smola, Dive into Deep Learning, §9.3 Language Models (n-grams, Laplace smoothing, perplexity) and §22.11 Information Theory · link · §9.3, 22.11
- I. J. Good, The population frequencies of species and the estimation of population parameters, Biometrika 40:237-264, 1953
- Goodfellow, Bengio, Courville, Deep Learning, §3.13 Information Theory (entropy, KL, cross-entropy), §5.5 Maximum Likelihood Estimation · link · §3.13, 5.5
- K. Heafield, KenLM: Faster and Smaller Language Model Queries, WMT 2011 · link
- F. Jelinek & R. L. Mercer, Interpolated estimation of Markov source parameters from sparse data, Pattern Recognition in Practice, 1980
- 'Every time I fire a linguist, the performance ... goes up', attributed to F. Jelinek
- S. M. Katz, Estimation of probabilities from sparse data for the language model component of a speech recognizer, IEEE Trans. ASSP, 1987
- R. Kneser & H. Ney, Improved backing-off for M-gram language modeling, ICASSP 1995
- J. Kun, A Programmer's Introduction to Mathematics (style exemplar: 'mathematics has a culture' notes; definitions as API design)
- L. Lee, 'I'm sorry Dave, I'm afraid I can't do that': Linguistics, Statistics, and Natural Language Processing circa 2001 · arXiv:cs/0304027
- J. Liu, S. Min, L. Zettlemoyer, Y. Choi, H. Hajishirzi, Infini-gram: Scaling Unbounded n-gram Language Models to a Trillion Tokens, 2024 · arXiv:2401.17377
- A. A. Markov (1913), statistical analysis of 20,000 letters of Pushkin's Eugene Onegin as a two-state (vowel/consonant) chain; English translation in Science in Context 19(4), 2006
- Experiments on predictability of word in context (ar5iv cs/0612136) · arXiv:cs/0612136
- Multimodal Shannon Game with Images · arXiv:2303.11192
- Isaac Newton, Opticks, 4th ed. (London, 1730); public domain; PGDP/Project Gutenberg transcription (file as shipped in Go's src/testdata)
- A. M. Sabatini, Markov reads Pushkin, again: A statistical journey into the poetic world of Evgenij Onegin · arXiv:2604.20221
- C. E. Shannon, A Mathematical Theory of Communication, Bell System Technical Journal 27:379-423, 1948
- C. E. Shannon, Prediction and Entropy of Printed English, Bell System Technical Journal 30:50-64, 1951
- Jurafsky & Martin, SLP3 draft (Aug 2026), Appendix C: Kneser-Ney Smoothing · link · §C
- Jurafsky & Martin, SLP3 draft (Aug 2026), Ch. 1 §1.6.1 (pretraining as a Shannon game), §1.9.2 (perplexity), p. 13 (Jelinek's group coined 'language model') · link · §1.6.1, 1.9.2
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft (Aug 19, 2026), Ch. 3 N-gram Language Models · link · §3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, Historical Notes
- LLMs Unplugged: Teaching Resources for a ChatGPT World · arXiv:2609.13183
- S. Merity, C. Xiong, J. Bradbury, R. Socher, Pointer Sentinel Mixture Models (WikiText), 2016 · arXiv:1609.07843