Finish this sentence.
How you
You said are. Everyone says are. Now try this one.
How much
Here the answers spread out. Is, does, time, longer are all reasonable. You did not pick one answer. You held several, ranked.
That ranking is the subject of Part III. A model that can produce it is a language model. The rest of this book is a series of steadily more capable ways to build one.
Notice how much you brought to a two word prompt.
Lexical knowledge told you which words exist and which are common. Syntactic rules told you that How are needs a subject next. Semantic cues ruled out How are cement. World knowledge told you that people greet each other this way. Pragmatics told you the sentence was a greeting rather than a question about health.
Five kinds of knowledge, none of them written down anywhere, all applied in under a second.
The engineering question is how to put that box on a computer. For thirty years the field tried to write the five knowledge sources down as rules. It did not work, for the reasons Chapter 1 gave.
This chapter takes the other road. Do not encode the knowledge. Count how often things happened, and let the counts stand in for all five.
That sounds far too crude to work. It works very well. Where it fails, it fails in ways that point straight at what comes next.
A language model assigns a probability to a sequence of words. That is the whole definition.
Pause on how strange it is. Nothing in it mentions grammar, meaning, or truth. A language model is a probability distribution over strings.
Yet this one idea is the foundation of everything after. The neural models of Chapter 11, the recurrent networks of Chapter 12, and the large language models of Chapter 17 all answer the same question. Given what has been said so far, what comes next, and with what probability? Only the machinery changes.
Historically, because many language technologies reduce to choosing between candidate word sequences.
A speech recogniser hears a signal that fits both “recognise speech” and “wreck a nice beach”. It must pick one. A translation system generates a dozen candidate renderings and must rank them. A spelling corrector must decide whether “their going home” was meant to be “they’re going home”.
In every case the winning move is the same. Prefer the candidate the language model scores higher.
Today the motivation is more direct. A modern large language model is a language model in exactly this chapter’s sense. It produces its output one next word distribution at a time.
We want the joint probability of a sentence . The chain rule of probability factorises it exactly, with no approximation:
Equation (10.1) converts one impossible problem into merely difficult ones. The impossible problem was a distribution over all sentences. The difficult one is a distribution over the next word, given a history.
It also shows why a language model is a generative model. Sample from the first factor, append the word, condition on it, and sample again. Repeat until you draw the stop symbol. You have written a sentence.
The difficulty that remains is the history. The context can be arbitrarily long, and almost every particular history is one nobody has ever seen.
No corpus contains reliable statistics for the exact prefix “The results of the 2026 municipal elections in Tenkasi suggest”. It has appeared once, in this book.
The n-gram model’s answer is bold. Forget the past.
More precisely, assume the next word depends only on the previous words:
With , a bigram model, each word depends only on its predecessor. With , a trigram model, on the previous two. With , a unigram model, on nothing at all.
This is a Markov assumption, and it is plainly false. In “The book that the committee rejected was short” the verb agrees with book, eight words away.
The assumption is not made because it is true. It is made because it buys something we cannot otherwise have: contexts short enough to recur.
The history “of the” appears thousands of times in a modest corpus. Statistics conditioned on it are trustworthy. The history in the Tenkasi example appears once, and statistics conditioned on it are worthless.
So the whole art of n-gram modelling is one trade off. Longer context means a sharper prediction and thinner evidence. Shorter context means a blunter prediction and firmer evidence.
How do we get the conditional probabilities? By the most honest method available. Counting.
The maximum likelihood estimate of a bigram probability is the fraction of times the context was followed by the word in question:
That is all. No optimisation, no training loop. Read the corpus, keep two counters, divide.
We will use three sentences for the whole chapter, so that every number can be verified by hand.
the cat sat the cat ran the dog sat
The word the occurs three times. Twice it is followed by cat, once by dog. So
and every other continuation of the has probability zero. Hold on to that zero. It becomes the villain of the chapter.
Before any two implementations of Equation (10.3) can agree, three bookkeeping conventions must match. These sound like pedantry. Each one changes the answers, and Project 4 checks them exactly.
Each sentence is padded with
start symbols <s> and one stop symbol
</s>.
The start padding gives the first real word a context, so a bigram model can speak of .
The stop symbol does something deeper. It makes sentence length part of the model.
Without it the model only ever assigns mass to continuing, never to stopping. The probabilities of all sentences of all lengths then sum to more than one. With it, ending a sentence is an event like any other.
A model trained on our three sentences will one day meet the word elephant.
The remedy is a designated symbol <unk>. Every
word absent from the training vocabulary maps to it, and
<unk> is an ordinary vocabulary item. So the
vocabulary of our corpus is
Note what is not in
.
The start symbol is context only. It is never predicted, so it never
needs a probability, and it must never be mapped to
<unk>.
Probabilities, and later perplexity, are computed over the
predicted tokens. That means the words of the sentence plus the
one </s>. The start padding is never scored.
State these three and Equation (10.3) is reproducible to the last decimal. Omit them and two correct programs will disagree.
Our model believes and .
The first claim is merely overconfident. The second is fatal. Under the chain rule a single zero factor drives the whole sentence to probability zero, and its perplexity to infinity.
Yet “the dog ran” is perfectly good English. It just failed to appear in three sentences of training data. The model has confused unseen with impossible.
The remedy is to smooth. Shave a little mass off what we saw and hand it to what we did not. The simplest scheme is add- smoothing, which pretends every vocabulary item was seen extra times in every context:
The
in the denominator is exactly what makes the probabilities sum to one
again. That is why the vocabulary convention matters, and why
</s> and <unk> have to be counted
in
.
Set and , and look at every continuation of the. The context was seen times.
| next word | count | MLE | add-1 | what changed |
|---|---|---|---|---|
| cat | 2 | 0.6667 | 0.3000 | lost |
| dog | 1 | 0.3333 | 0.2000 | lost |
| the | 0 | 0.0000 | 0.1000 | gained |
| sat | 0 | 0.0000 | 0.1000 | gained |
| ran | 0 | 0.0000 | 0.1000 | gained |
</s> |
0 | 0.0000 | 0.1000 | gained |
<unk> |
0 | 0.0000 | 0.1000 | gained |
| total | 3 | 1.0000 | 1.0000 |
Both columns sum to one, as they must. The two seen words gave up between them, and the five unseen words received each.
The counts did not change. The belief did.
Now the same table for the context dog, which was seen only once.
| next word | count | MLE | add-1 |
|---|---|---|---|
| sat | 1 | 1.0000 | 0.2500 |
| cat | 0 | 0.0000 | 0.1250 |
| dog | 0 | 0.0000 | 0.1250 |
| the | 0 | 0.0000 | 0.1250 |
| ran | 0 | 0.0000 | 0.1250 |
</s> |
0 | 0.0000 | 0.1250 |
<unk> |
0 | 0.0000 | 0.1250 |
| total | 1 | 1.0000 | 1.0000 |
This is the repair we wanted. has risen from to , so “the dog ran” is no longer impossible.
It is also where add- shows its weakness. Moving from to is not a shave. It is a scalping, and the evidence for it was a single observation.
The effect scales badly. With a realistic vocabulary of tens of thousands, the denominator is dominated by . Nearly all the mass then goes to events nobody has ever seen.
So in practice one uses a small , tuned on held out data. Or one of the better schemes in Section 1.8.
The principle never changes. Every language model, up to the largest, must reserve belief for things it has not seen.
Two colleagues each hand you a language model. Which is better?
The models are distributions, so let the data decide. The better model is the one that finds held out text less surprising. That is to say, it assigns the text higher probability.
Raw probabilities are unwieldy, because they shrink exponentially with length. So we normalise. The average negative log probability per predicted token is the model’s cross-entropy on the text:
measured in bits per token. Its exponential is the perplexity:
Here
counts the predicted tokens. The words plus </s>, by
the convention above.
Perplexity has a clean reading. It is the model’s effective branching factor, the size of the uniform distribution that would be equally surprised.
A perplexity of means the model is as uncertain at each step as if it were choosing among equally likely words. Lower is better. A model that knew the text by heart would score .
And the zero we worried about gives , hence perplexity . That is the formal statement of why unsmoothed models are unusable.
Take the bigram model with
and score the sentence the cat sat. Padded, it is
<s> the cat sat </s>, so
there are four predicted tokens.
Each row applies Equation (10.4) with .
| predicted | context | count | of | ||
|---|---|---|---|---|---|
| the | <s> |
3 | 3 | ||
| cat | the | 2 | 3 | ||
| sat | cat | 1 | 2 | ||
</s> |
sat | 2 | 2 | ||
| sum | |||||
The count column is and of is . Every probability is .
Now finish it:
So the model is about as unsure at each step as if it were choosing uniformly among words. The vocabulary holds . On a corpus this small, that is a respectable score.
Two details in that table are worth naming. The first row scores
the in the context <s>, which is why the
padding exists. The last row scores </s>, which is
why
and not
.
Perplexity gives us a way to pick . Try several and keep the best.
The catch is which text you measure on. Here are two sentences. The cat sat is in the training data. The dog ran is not, though every word of it is, and only the bigram is new.
| PP on the cat sat | PP on the dog ran | |
|---|---|---|
| 0.01 | 1.3466 | 4.3297 |
| 0.02 | 1.3767 | 3.7788 |
| 0.05 | 1.4649 | 3.3211 |
| 0.10 | 1.6050 | 3.2047 |
| 0.15 | 1.7374 | 3.2408 |
| 0.20 | 1.8628 | 3.3166 |
| 0.50 | 2.4982 | 3.8394 |
| 1.00 | 3.2568 | 4.4721 |
| 2.00 | 4.1902 | 5.1845 |
The two columns tell opposite stories.
The seen column falls all the way down. Less smoothing always looks better on text the model has memorised, and it would keep improving as .
The held out column is a U. It bottoms at and rises on both sides.
Both sides of that U have a cause. Too little smoothing and the single unseen bigram is crushed towards zero probability. Too much and the seen bigrams are robbed to pay for events that never happen.
Tuning on training text would have chosen , which scores on new text against the best available . That is times worse, from a decision that looked correct at the time.
Perplexity is only comparable across models that share a vocabulary and a tokenisation. A model with a smaller vocabulary has an easier problem, and the subword tokenisers of Chapter 5 change itself.
Perplexity also measures surprise, not usefulness. It correlates with downstream quality and does not guarantee it. Both cautions return with force in Chapter 21.
Everything so far suggests a simple way to improve the model. Use a longer context. If bigrams are good, trigrams should be better.
They are, for a while. Then the arithmetic stops you.
An n-gram model needs one number per history and word. There are possible histories, and within each one free choices, since the last probability is fixed by the others:
That exponent is the whole problem.
| 1,000 | |||||
| 10,000 | |||||
| 50,000 |
Take the bottom right cell. A 5-gram model over a word vocabulary has about parameters.
Now count what could fill them. A corpus of a trillion tokens contains at most a trillion distinct 5-grams, one per position. So at most one cell in can hold a nonzero count.
The counts do not merely get thin. Almost the entire table is empty. No corpus that will ever be assembled can change that.
Three consequences follow, and the lectures name them.
Data sparsity. Most n-grams have zero or tiny counts, so their probabilities cannot be estimated reliably.
Overfitting. The model latches onto the exact phrases in the training text and generalises to nothing else.
Computational cost. Storing and searching the table grows with the same exponent.
The framework can be pushed further than add-, and three techniques matter.
Pruning discards n-grams with low counts. It saves space and removes estimates that were never trustworthy.
Interpolation mixes the orders. When the trigram evidence is thin, lean on the bigram. When the bigram fails too, lean on the unigram:
subject to the one constraint . The weights say how much to trust each level of context, and they are tuned on held out data like any other hyperparameter.
Backoff does the same job with a switch rather than a blend. Use the trigram if you have seen it, otherwise drop an order and try again.
Take a four sentence corpus, so that the trigram counts can run out while the bigram counts survive.
the cat sat on the mat the cat sat on the rug
the dog sat on the mat a cat ran to the mat
Set , and , and take four queries in decreasing order of evidence. All three estimates below are unsmoothed, so the failures are visible.
| query | mixed | counts | |||
|---|---|---|---|---|---|
| 1.0000 | 1.0000 | 0.1071 | 0.9107 | tri 2, bi 3 | |
| 0.6667 | 0.4286 | 0.1071 | 0.5393 | tri 2, bi 3 | |
| 0.0000 | 0.2857 | 0.1071 | 0.0964 | tri 0, bi 2 | |
| 0.0000 | 0.0000 | 0.0357 | 0.0036 | tri 0, bi 0 |
Read the four rows as a staircase.
Row one has full evidence at every order. The trigram cat sat on was seen twice and never continued any other way. So all three estimates are high, and so is the mixture.
Row two shows what the extra context buys. The trigram is sharper than the bigram, against , because on the narrows the field more than the alone.
Row three is the one that matters. The trigram on the cat never occurred, so , and a pure trigram model would call the phrase impossible. The bigram has seen the cat twice and says . The mixture returns and the sentence survives.
Row four is the last resort. The word ran never follows the anywhere in this corpus, so both higher orders fail. Only the unigram is left. The answer is small. What matters is that it is not zero.
Kneser-Ney smoothing is the state of the counting art, and it refines the lowest order term. What matters for an unseen context is not how often a word occurs but in how many distinct contexts it does. The word Francisco is common yet occurs almost only after San, so it deserves very little unseen-context mass. These refinements carried speech recognition and translation for two decades.
Try it yourself.
code/worked_examples/ngram_lm.pyproduces every table in this chapter, using the same conventions the autograder enforces.--countsprints the two smoothing tables,--perplexitythe four row calculation,--alphathe U shape,--paramsthe parameter explosion,--interpolatethe staircase, and--unkwhat happens to a word the model has never met.
Smoothing repairs the zeros. Interpolation repairs thin evidence. Neither touches the two real blindnesses, and both are structural.
The first is the Markov assumption. The model cannot know that book, eight words back, governs the verb it is about to emit. Eight words back does not exist for it. Raising does not solve this, because the parameter table of Equation (10.7) explodes long before reaches eight.
The second is that words are opaque strings. Suppose the corpus contains the cat sat many times and the dog only once. A counter has no way to let cat lend statistical strength to dog, because to a counter they are two unrelated symbols.
Chapter 9 already built the cure for the second problem. Words became vectors, and similar words came out near each other. What was missing was a way to put those vectors to work on prediction.
That is exactly what the next chapter does. It keeps the task, the corpus, the smoothing instinct and the perplexity yardstick, and replaces the count table with a network.
cover this material with more smoothing variants and worked examples. remains the definitive empirical comparison of smoothing methods. treat the statistical foundations in depth. introduce the smoothing method that held the record for years. The class-based n-grams of are an early attempt to share statistical strength between related words, which is the problem Chapter 11 solves properly. is where the entropy of a text was first proposed as something measurable, and it is still worth reading.
Counting, by hand. On the corpus the cat
sat / the cat ran / the dog sat, with the
padding, <unk> and
conventions of this chapter, compute
and
first by maximum likelihood and then with add-one smoothing. Confirm the
move for the first and the
move for the second. Say in one sentence which convention makes the
second answer nonzero.
Perplexity to four decimals. For the same add-one
bigram model, compute the perplexity of the cat sat. The
predicted tokens are the, cat, sat and
</s>, so
.
Show the four conditional probabilities and their logs, and recover
.
Then recompute with
,
by wrongly leaving out </s>, and report how far off
you land.
Implement perplexity(). Write a
function that takes a trained n-gram model and a token sequence and
returns its perplexity. It must handle the trigram case as well as the
bigram, map out of vocabulary tokens to <unk>, and
score </s>. Test it against your hand answer
above.
Find the U yourself. Train bigram models on the three sentence corpus with ranging from to . Plot perplexity on the cat sat and on the dog ran. Explain why one curve is monotone and the other has a minimum, and state which one you would use to choose in a real project.
Calibrate before you measure. Write three short sentences and rank them, before computing, from least to most surprising under your model. Then compute their perplexities. Where your ranking was wrong, name the feature of the training counts you had mis-weighted.
Count the parameters. Using Equation (10.7), compute the parameter count for at . Then estimate how many distinct n-grams a corpus of one billion tokens could possibly contain. At which does the corpus stop being able to fill even one cell in a thousand?
Interpolate. On the four sentence corpus of Section 1.8, verify the four rows of the staircase table by hand. Then change the weights to and recompute. Which row changes most, and why is it the row where the higher orders had failed?