Skip to content

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.

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.

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.

  1. 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.
  2. Bengio’s neural probabilistic language model. Replaces the count table with an embedding table, a tanh hidden 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.
  3. 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 ρ\rho and analogies.