Chapter 8: Statistical Language Models
This chapter covers the language models that came before deep learning, and the first neural one. They matter for two reasons. Historically, they are where the central problem of language modelling first shows up: almost every context you meet at test time was never seen in training, so a model must spread probability to things it has no direct evidence for. Practically, they stay useful. The count-based model you build here becomes the perplexity baseline every later model in the book must beat, a draft model for speculative decoding, and the corpus pipeline’s perplexity filter in the TinyStories capstone.
Concepts from other sections (13)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- Perplexity: , the geometric mean of ; the effective number of choices per token. Read more: 17.2 Perplexity, bits per byte, NLL accumulator in Mathematics
- Speculative decoding: drafting several tokens cheaply and verifying them with one target forward pass, keeping the longest acceptable prefix. Read more: 23.9 Speculative decoding in the engine
- n-gram model: a language model that conditions each token only on the previous tokens. Read more: 8.1 n-gram language model with interpolated modified Kneser-Ney
- modified Kneser-Ney: Kneser-Ney with three discounts per order (, , ) estimated from counts of counts. Read more: 8.1 n-gram language model with interpolated modified Kneser-Ney
- skip-gram: a model that predicts the context words around each center word. Read more: 8.3 word2vec skip-gram with negative sampling, and the PPMI-SVD baseline
- negative sampling: replacing the softmax with logistic classification of the real context against words drawn from a noise distribution. Read more: 8.3 word2vec skip-gram with negative sampling, and the PPMI-SVD baseline
- Pointwise mutual information (PMI): for a single pair; positive above chance, for unseen pairs. Read more: 17.3 Mutual information, PMI, and PPMI in Mathematics
- embedding table: the matrix whose row is the learned vector for token , shared across every window. Read more: 8.2 Bengio’s neural probabilistic language model
- Bits per byte (bpb): , total information divided by the UTF-8 length; comparable across tokenizers. Read more: 17.2 Perplexity, bits per byte, NLL accumulator in Mathematics
- safetensors: a checkpoint format of an 8-byte header length, a JSON header, and raw little-endian row-major tensor bytes. Read more: 6.1 Byte bigram: counts, logits, safetensors v0
- continuation count: , the number of distinct symbols seen immediately before . Read more: 8.1 n-gram language model with interpolated modified Kneser-Ney
- direct path: the optional linear map from the embeddings straight to the logits, in parallel with the hidden layer. Read more: 8.2 Bengio’s neural probabilistic language model
- scatter-add: an update that accumulates every contribution to a repeated index (
np.add.at). Read more: 8.3 word2vec skip-gram with negative sampling, and the PPMI-SVD baseline
The chapter tells one story in three steps. It opens with counts: an n-gram model with interpolated modified Kneser-Ney smoothing, the strongest of the classic smoothers, which discounts every count and redistributes the mass by how many contexts a word continues rather than how often it occurs. It then replaces counts with learned vectors: Bengio’s 2003 neural probabilistic language model gives every token an embedding and an MLP over a fixed window, generalizes to unseen n-grams through similar embeddings, and is the first model you train with your own autograd on real text. Finally, it isolates the embeddings themselves: word2vec (skip-gram with negative sampling) learns them without a language model around them, and a PPMI-SVD baseline shows that SGNS implicitly factorizes a shifted PMI matrix, which you can also build and factor explicitly.
Learning objectives
- Explain why maximum-likelihood counts fail on new text, and implement interpolated modified Kneser-Ney so that every distribution is proper.
- Build, train, score, and sample from a neural probabilistic language model using the layers and training loop from Part 0, reproducibly from a seed.
- Derive and implement skip-gram with negative sampling with hand-written sparse gradients.
- Build PPMI-SVD embeddings and evaluate any embedding table by word similarity and analogies.
- Compare count-based and neural models on perplexity and bits per byte.
Before you begin
Section titled “Before you begin”You should have worked through Foundations, whose tensor, layers, training loop, and safetensors format are used directly here, and Tokenizers. From Mathematics, the chapter draws on SVD, Eckart-Young low rank, and least squares, inner products, projections, cosine similarity, and top-k, and mutual information, PMI, and PPMI, with the problem sets on linear algebra, part b and information theory, part b as practice.
In this chapter
Section titled “In this chapter”- n-gram language model with interpolated modified Kneser-Ney. Builds Kneser-Ney smoothing from the zero-probability problem one fix at a time: interpolation, continuation counts, the
<s>rule, and three data-estimated discounts per order, with a scalar and a vectorized query path that agree exactly. - Bengio’s neural probabilistic language model. Replaces the count table with an embedding table, a
tanhhidden layer, and a direct path, trained with the Part 0 loop; it beats the 4-gram’s perplexity on the story corpus and is saved as a model directory the zoo loads. - word2vec skip-gram with negative sampling, and the PPMI-SVD baseline. Derives SGNS and its three-line gradient, the noise and subsampling distributions, and sparse scatter-add SGD, then builds the explicit PPMI-SVD factorization and evaluates both by Spearman’s and analogies.
Further reading
Section titled “Further reading”- Jurafsky and Martin, Speech and Language Processing, chapter 3; Chen and Goodman, An Empirical Study of Smoothing Techniques for Language Modeling (1998); KenLM.
- Bengio et al., A Neural Probabilistic Language Model (JMLR 2003); Mikolov et al., Distributed Representations of Words and Phrases; Levy and Goldberg, Neural Word Embedding as Implicit Matrix Factorization (NeurIPS 2014).