N-Grams
The words immediately before a position give us clues about what might come next. An n-gram model uses counts of short sequences to make that prediction, with adjustments for sequences missing from its training data.
15 sections · 15 questions · 3 coding challenges
The arrow keys move between sections too.
Could a Short Memory Capture Some of a Language's Structure?
BOTIE
Shannon's 1948 communication paper illustrated increasingly structured approximations to English by incorporating dependencies between successive symbols and words. Short-context models offered a manageable way to represent some of the order in language.
An n-gram is a consecutive group of a chosen length. Counting these groups preserves local order, whether the units are characters or words. The representation remains limited by its window: distant relationships fall outside it, and longer groups quickly become rare. This explains both the usefulness of short recurring phrases and the need to handle sequences that were absent from training.
Sources for this history
One Previous Word May Not Be Enough
After reading spread butter on the, we can make a reasonable guess about what might come next. One simple way for a computer to practise this is to count what followed similar text before.
An n-gram model records short consecutive token sequences. It uses those counts to estimate what may follow a limited recent context.
Choose the units before counting runs
First decide what counts as a token. An n-gram then groups consecutive tokens; it does not choose the original token boundaries. Here we use words so the grouping is easy to see.
Tokens
- spread
- butter
- on
- bread
Local counts provide a transparent prediction baseline
The detailed lesson calculates conditional probabilities from counts, then examines sampling and evaluation.
The model's context length is a deliberate limit. Its predictions reflect the training corpus and fallback rules, rather than an understanding of everything earlier in the document.
Explore the full example
Compare predictions with shorter and longer contexts. Inspect an unseen continuation and observe how smoothing changes its probability.
…
Part 1. Counting Runs, Not Cutting Text
1. What this method is for
The pages on either side of this one decide where the pieces of a text are, splitting on spaces by the simplest rule there is and the Unicode boundaries by a standard written to cover every writing system at once. This page starts after that decision has been made. It takes the pieces as given and asks a different question, which is how often each run of adjacent pieces occurs, and what those counts let you say about a word you have not read yet.
Context can help a speech recogniser choose between words that sound similar. A count-based language model supplies one source of that contextual evidence.
Predicting the next token is also a training task for autoregressive neural language models. Those models learn prediction parameters instead of relying on an explicit table of n-gram counts. The counting version gives us a transparent starting point for the shared prediction problem.
A run of n adjacent pieces is called an n-gram. One word is a unigram, two a bigram, three a trigram, and above that people usually say the number. The pieces do not have to be words, and character n-grams are common, though everything on this page is counted over words.
Runs have to be counted rather than words because a table of how often each word occurs contains no information about order at all. It gives exactly the same answer for a sentence and for the same sentence with its words reversed, and step 19 measures that coming out equal to the last digit on six real sentences. Everything a model of this kind knows about which word follows which is in the runs of two or more.
Keep in mind
2. The running sentence as runs of words
Take the sentence this section of the site carries throughout, Dr. Alvarez didn’t expect the low-cost re-analysis. Split on spaces and it is seven words, and the last of them is re-analysis. with the full stop attached, because a space splitter has no reason to take the stop off. Slide a window of three along those seven and you get the trigrams. Slide a window of two and you get the bigrams.
The widget below cuts it at four widths. The chip that matters in each row is the last one, since that is the word the run predicts, and everything to its left is what the prediction is made from.
…
Keep in mind
3. Framing the edges
A sentence has a beginning and an end, and both have to be written down or the counting cannot see them. The first word of a sentence has nothing before it, so if the model is to predict it at all there must be something standing in for the place where the sentence starts. A model reading two words back needs two such markers, one for each empty position, which is why the count of them changes with the width of the window.
The end matters for a different reason. A marker after the last word is predicted exactly like a word, so the model learns which words tend to end a sentence, and a model that has learned that is a model that knows when to stop. Without it the probabilities of all the sentences of a given length would sum to one separately for each length, and the model would have no way of preferring a short sentence to a long one.
The start markers are the mirror image. They are never predicted, only ever read back from, since the beginning of a sentence is where the model starts rather than somewhere it can arrive. A run whose last position is a start marker is not counted at all.
P(sentence) = P(w₁ | start) · P(w₂ | w₁) · … · P(end | wₙ)What the framing adds
The running sentence is seven words. Framed for a window of two it becomes nine positions, one start marker and one end marker, and produces eight windows. Framed for a window of three it becomes ten positions, two start markers and one end marker, and still produces eight windows. The window count does not change with the width, because every framing is built so that each of the eight predicted positions, the seven words and the end, has a full context to be predicted from.
Keep in mind
4. How many runs there could be, and how few there are
Everything on this page from here on is fitted to forty-four short sentences from the world the running sentence belongs to, reports and analyses and samples arriving late. They hold 326 word occurrences and 62 distinct words, so with the end marker and one stand-in for anything unseen the model has 64 words it is allowed to answer with.
Sixty-four words allow 4,096 different pairs and 262,144 different triples. The forty-four sentences produced 141 pairs and 202 triples. That is the ordinary condition of the method rather than a shortage of text, and it gets worse as the window widens, since the number of possible runs is multiplied by the vocabulary each time while the number of positions in the corpus stays where it was.
| words per run | distinct runs seen | runs the vocabulary allows | seen exactly once |
|---|---|---|---|
| 1 | 63 | 64 | 9 of 63 |
| 2 | 141 | 4,096 | 41 of 141 |
| 3 | 202 | 262,144 | 109 of 202 |
| 4 | 236 | 16,777,216 | 164 of 236 |
| 5 | 248 | 1,073,741,824 | 185 of 248 |
Every width sees the same 370 predicted positions, the 326 words and the 44 end markers. The last column is the one that hurts, since by three words per run more than half of everything the corpus taught, it taught exactly once.
The 63 distinct single words are the 62 words of the corpus plus the end marker; the stand-in is in the vocabulary without ever having been counted, which is a detail that comes back in Part 3.
In a model
Keep in mind
Part 2. From Counts to a Prediction
5. The assumption that makes the question answerable
The probability of a sentence is the probability of its first word, times the probability of its second given the first, times the probability of its third given the first two, and so on to the end. That is exact and it is useless, because by the fourth or fifth word the thing being conditioned on is a phrase that has never been written before, so there is nothing to count and no estimate to make.
P(w₁ … wₘ) = Π over i of P(wᵢ | w₁ … wᵢ₋₁)The n-gram model throws away all of that history except the last few words. A model of width three believes that the probability of a word given everything before it equals the probability of that word given the two words immediately before, and nothing else about the sentence matters. This is the Markov assumption, and it is false about language, since what a sentence is about survives across any number of words.
It is useful anyway, because it turns an unanswerable question into a table of counts, and a table of counts can be built from any text at all and read in constant time.
P(wᵢ | w₁ … wᵢ₋₁) ≈ P(wᵢ | wᵢ₋ₙ₊₁ … wᵢ₋₁)The width n is the whole trade and it is the only real dial the method has. A model of width one knows which words are common and nothing else. A model of width three knows which words follow which pairs and has seen very few of the pairs a new text will contain. Part 5 measures where the trade lands here.
Keep in mind
6. One count divided by another
With the history cut down to a fixed length, the estimate is the obvious one. Count how often the run occurred, count how often its first words stood before anything at all, and divide. Nothing about it is a choice; it is the estimate that makes the training text as probable as it can be, which is why it is called the maximum-likelihood estimate.
P(word | context) = C(context word) ⁄ C(context)Three sentences are enough to see the whole thing. The report was late, the report was short, a summary was late. Nine words can be predicted once the end marker and the stand-in are counted, and there are fifteen predicted positions in all, twelve words and three ends.
…
One sentence by hand
The word the started two of the three sentences, so the probability of the after the start marker is 2 ⁄ 3. The followed by report happened twice out of the two times the stood before anything, so that step is 1. Report was is the same, and was late happened twice out of the three times was stood before anything, so that step is 2 ⁄ 3. Late ended the sentence both times it occurred, so the last step is 1 again.
P(the report was late) = (2⁄3)(1)(1)(2⁄3)(1) = 4⁄9 = 0.4444Five steps went into that product, the four words and the end, so the score reported in Part 5 is the fifth root of 9 ⁄ 4, which is 1.1761.
Keep in mind
7. The same rule on a larger text
Fit the same rule to the forty-four sentences, ask it to read the running sentence, and every step is one division. Two of the sentence’s words are not in the vocabulary at all, since nothing in the training text contains didn’t or expect, and both are replaced by the one stand-in the model keeps for that purpose before anything is looked up.
| reading back | predicting | times seen | probability |
|---|---|---|---|
| start | Dr. | 7 of 44 | 0.1581 |
| Dr. | Alvarez | 7 of 7 | 0.9570 |
| Alvarez | the stand-in | 0 of 7 | 0.0007 |
| the stand-in | the stand-in | 0 of 0 | 0.0156 |
| the stand-in | the | 0 of 0 | 0.0156 |
| the | low-cost | 1 of 46 | 0.0217 |
| low-cost | re-analysis. | 2 of 4 | 0.4641 |
| re-analysis. | end | 3 of 3 | 0.9051 |
The running sentence read by a two-word model, with a little smoothing applied so that the zeroes of the next Part do not swallow the table. Three of the eight runs never occurred in training.
Two rows are worth staring at. Dr. was followed by Alvarez every one of the seven times it occurred, and the model very nearly says so. The two rows reading 0.0156 are the model with nothing at all to go on, since the stand-in never occurred in training and so never stood before anything, and 0.0156 is one divided by the 64 words the model is allowed to answer with.
Keep in mind
8. A whole answer, not one number
Asking the probability of one word is asking one entry of a larger answer. For any context the model has a number for every word it knows, the numbers sum to one, and it is only together that they say anything. The largest is the model’s guess. How evenly the rest are spread is how unsure it is, and that spread has a standard measurement in bits, with two to the power of the bits being the number of equally likely words the answer is worth.
bits = −Σ over words of P(word) log₂ P(word)…
Four contexts are worth comparing there. At the start of a sentence the model has 44 observations and only five words ever began one, so the answer is the narrowest of the four at 4.9775 bits. After the it has 46 observations spread over eighteen different words and reads 5.6283. After Dr. it has seven observations all agreeing, which ought to be the narrowest answer of all and is instead the widest at 5.8117, for a reason Part 4 is about.
And after a word it never saw it has nothing, so every word of the vocabulary gets the same number and the answer is exactly 6 bits, which is 64 equally likely words.
Keep in mind
Check yourself
Questions on Parts 1 and 2
Question 1 of 5
A seven-word sentence gives eight predicted positions whether the model reads one word back or two.
Part 3. The Zero
9. A run that never occurred
Go back to the three sentences. The word summary never followed the, so the count of that pair is zero, so the probability of summary given the is zero. Now ask for the probability of the sentence the summary was late. Every other step of it is perfectly ordinary and one step is zero, and a product with a zero in it is zero.
The model is not saying that sentence is unlikely. It is saying the sentence cannot happen, which is a claim of a different kind, and a false one about a sentence a reader has just read. The same thing happens to a report was short, whose every word and every idea is in the three sentences and whose pair a report is not.
Two sentences the three cannot contain
The report was late, which the corpus holds, comes out at 0.4444 and scores 1.1761. The summary was late and a report was short both come out at exactly zero, and the score in Part 5, which is a probability inverted and rooted, is therefore infinite for both. Under the repair of the next Part the same two sentences come back finite, at 5.0321 and 5.8064 against the held sentence’s 3.7965, which is the ordering a reader would have wanted all along.
Keep in mind
10. How much of a new text is new
How often that happens is a measurable thing rather than a fear. Take six sentences the model was never fitted to, frame them, and count how many of their runs are runs the training text never produced. There are 53 predicted positions across the six.
| words per run | runs never seen | of 53 | share |
|---|---|---|---|
| 1 | 3 | 53 | 0.057 |
| 2 | 10 | 53 | 0.189 |
| 3 | 21 | 53 | 0.396 |
| 4 | 30 | 53 | 0.566 |
| 5 | 33 | 53 | 0.623 |
At three words per run, two fifths of a new text is made of runs this corpus never contained. Each of those would be a zero, and any one of them is enough.
The single-word row is the only one that is small, and even there three words of the new text were never seen. Everything above one word is a model that would refuse most of what it is shown. This is the ordinary condition of the method rather than a symptom of a small corpus, and it is worse at the widths that would otherwise be the most informative.
Keep in mind
11. Why the estimate does this on purpose
Dividing one count by another is not a careless choice that a more careful one would improve on. It is the estimate that maximises the probability of the training text, and it is the best possible estimate by that standard. Every scrap of probability it gives to a run it never saw would have to be taken from a run it did see, and that would lower the number it was maximising.
So the failure is exactly the success viewed from outside. On the forty-four sentences it was fitted to, a two-word model scores 2.9970 and a five-word model 1.6025, both far better than anything else on this page. On the six sentences it was not fitted to, the same models have no score at all, at every width from one to five.
›Why this works
Write the probability of the training text as a product over contexts, and for one context the terms are the counts of each following word as exponents on that word’s probability. Maximising that product subject to the probabilities summing to one gives each word its own count over the total, and gives zero to anything with a count of zero, because raising a probability of zero to the power of zero costs the product nothing while giving that word any probability at all takes it from the words that do have counts.
Keep in mind
Part 4. Taking From the Seen and Giving to the Unseen
12. Adding one to every count
The repair every method here is a version of is to pretend the counting saw a little more than it did. Before dividing, add the same constant to every count, and add the matching amount to the denominator so that the numbers still sum to one, which means adding the constant once for every word the model is allowed to answer with. A run that never occurred now has a small numerator instead of nothing.
P(word | context) = ( C(context word) + k ) ⁄ ( C(context) + k V )V there is the size of the vocabulary and k is how many times every word is pretended to have followed every context. Setting k to one is the oldest version, and it is Laplace’s rule of succession from the Essai philosophique sur les probabilités of 1814, which is where anyone reading about this for the first time starts.
What adding one does to the three sentences
The vocabulary is nine words. The probability of the after the start marker was 2 ⁄ 3 and becomes 3 ⁄ 12, which is 0.25. The probability of report after the was 1 and becomes 3 ⁄ 11, which is 0.2727. And the probability of summary after the was 0 and becomes 1 ⁄ 11, which is 0.0909. The sentence the summary was late is now possible.
Keep in mind
13. What adding one costs a certainty
Adding one sounds modest until you notice that it is added once per word of the vocabulary, so the denominator grows by the size of the vocabulary while a typical numerator grows by one. When the context is rare that swamps the evidence completely.
In the forty-four sentences, Dr. occurs seven times and is followed by Alvarez all seven. The counts are as certain as counts get. Adding one leaves the probability of Alvarez at 0.1127, because the denominator went from 7 to 71, and the answer as a whole costs 5.8117 bits, which is very close to the 6 bits of knowing nothing at all.
| times every word is pretended to have followed | P(Alvarez after Dr.) | bits the answer costs |
|---|---|---|
| nothing added | 1.0000 | 0.0000 |
| 0.001 | 0.9911 | 0.1268 |
| 0.005 | 0.9570 | 0.5132 |
| 0.01 | 0.9175 | 0.9037 |
| 0.1 | 0.5299 | 3.8076 |
| 0.5 | 0.1923 | 5.5341 |
| 1 | 0.1127 | 5.8117 |
Seven observations, all agreeing. Adding one to every count leaves the model less than an eighth as sure as its evidence, and nearly as unsure as a model with no evidence at all.
Keep in mind
14. A fraction rather than one, chosen by measurement
Nothing requires the constant to be one. Any positive number works, and a fraction takes less from what was seen while still leaving nothing at zero. Which fraction is a question with an answer, and the answer is found by trying them and scoring each on text the counts were not taken from.
Six sentences are held back for the choosing and six more are held back from that too, so the number reported is a number nothing selected. The indigo curve below is the choosing text and the amber curve is the reporting text; the ring marks the lowest point of the indigo curve, which is the setting a person would take away.
…
At two words the chosen constant is 0.005, two hundred times smaller than the textbook one. It scores 8.9626 on the reporting text where adding one scores 23.6179, so the textbook setting is more than two and a half times worse than a setting nobody had to be clever to find. The same shape holds at three, four and five words, with the chosen constant drifting up as the counts thin out.
Keep in mind
15. The two ends of the dial
The constant has a limit in each direction, and both limits are models we have already met. Send it towards zero and the added amount stops mattering beside the real counts, so the answer approaches one count divided by another and the zeroes come back. Send it upwards and the real counts stop mattering beside the added amount, so every word of the vocabulary gets the same number whatever stands before it.
| times every word is pretended to have followed | score on the reporting text |
|---|---|
| 1 | 23.62 |
| 10 | 49.41 |
| 100 | 61.70 |
| 1,000 | 63.75 |
| 100,000 | 64.00 |
The vocabulary is 64 words, and a model that has been pretended into knowing nothing scores exactly that. The number a model has to beat is the size of its own vocabulary.
The upper limit is more useful than it looks, because it is the yardstick. A model over 64 words that knows nothing scores 64, and any real model has to come in under that to have earned anything. It also has a sting in it, which Part 5 comes back to, since a model over a smaller vocabulary starts from a smaller number without having done anything better.
Keep in mind
16. What this page does not carry
Adding a constant is the baseline rather than the state of the art, and it has been known to be a poor one since Gale and Church argued in the early 1990s that it hands unseen events far more probability than they deserve. Everything measured above is a demonstration of their point on a small scale.
The rules that replaced it are named here without being drawn, because this page measures only what it can show. The Good-Turing estimate uses how many runs were seen exactly once to estimate how much probability the unseen runs should share. Interpolation mixes the estimate from a wide window with the estimate from a narrower one, so a triple nobody has seen falls back on the pair inside it.
Kneser and Ney’s rule refines the fallback by asking, of the lower-order estimate, not how often a word occurred but in how many different contexts it occurred, which is what stops a word like Francisco from looking common when it only ever appears after one other word.
Keep in mind
Check yourself
Questions on Parts 3 and 4
Question 1 of 5
A pair with a count of zero makes the model say the sentence containing it is unlikely.
Part 5. Judging a Model on Text It Was Not Fitted To
17. Perplexity, and the same number in bits
There is one way of scoring a model of this kind and it is the obvious one. Take text the model has never seen, ask it for the probability of each word in turn given what came before, and multiply. A model that gives the actual text a high probability is a model that would have guessed well.
Multiplying thousands of numbers below one underflows, and the result depends on how long the text is, so the reported figure is the geometric mean of the inverses. Ask over how many equally likely words a model would have to be guessing to do this well, and that number is the perplexity. A model with one chance in a hundred at every word has a perplexity of 100, a model certain of every word has 1, and lower is better because a smaller number of effective choices is a better-informed guess.
perplexity = exp( −(1⁄M) Σ over positions of ln P(word | context) )The same quantity in bits per word is the cross-entropy, and perplexity is two to that power. A reader of the literature meets both and neither is more fundamental. On the six reporting sentences a two-word model with one added scores 4.5618 bits per word, and two to that power is 23.6179, which is the perplexity quoted above.
Keep in mind
18. The score against the width
Now the trade of Part 2 can be measured. Fit the same corpus at every width from one to five, score each on the six reporting sentences, and see where the curve turns.
…
The grey line falls all the way, from 35.06 at one word to 1.6025 at five, and it is the score on the sentences the counts came from, so it is measuring how much of the corpus the model has memorised. The indigo line, on text nothing was fitted to, turns immediately. One word scores 46.73, two words 8.9626, and from there it rises through 13.00, 20.09 and 23.40. Two words is the best this corpus supports, and every wider window is worse despite having strictly more information available to it.
The reason is Part 3’s table. At three words per run, two fifths of the reporting text is runs that never occurred, so most of what a three-word model says comes from the constant rather than from the counts. The extra context it could use is context it has almost never seen.
In a model
Keep in mind
19. What the second word buys
Step 1 claimed that counting words one at a time carries no information about order. That is checkable rather than arguable. Take the six reporting sentences, reverse the words of each, and score both versions with the same model.
| words per run | the sentences as written | the same words reversed |
|---|---|---|
| 1 | 57.5683 | 57.5683 |
| 2 | 8.9626 | 1715.1308 |
| 3 | 14.0173 | 111.8429 |
A one-word model gives the two versions the same number to the last digit, since the words it multiplies are the same words in a different order. A two-word model is 191 times more surprised by the reversed text than by the original.
The equality in the first row is exact and has to be, because a product does not depend on the order of its factors and the factors are the same set of words either way. Every one of the 53 positions is scored from the same table however the sentence is arranged, so the two columns could not have differed.
The third row is the part worth noticing. A three-word model is less offended by the scrambling than a two-word model, at 111.8429 against 1715.1308, and the reason is the smoothing rather than any weaker grasp of order. Most of what a three-word model says about any text at all comes from the constant, so it was never very confident about the original either.
Keep in mind
20. When two perplexities cannot be compared
A perplexity is a number about one model reading one text with one vocabulary, and moving any of those three moves the number for reasons that have nothing to do with how good the model is. The vocabulary is the one that catches people, because a smaller vocabulary makes every guess easier without making any guess better informed.
This corpus can be made to show it. Fitted to eleven of the forty-four sentences the model has a vocabulary of 36 words, and a model over 36 words that knows nothing scores 35.9998. Fitted to all forty-four it has 64 words, and knowing nothing there scores 63.9975. The ignorant model looks nearly twice as good on the smaller vocabulary, and it is the same model.
| sentences fitted to | words in the vocabulary | positions of the reporting text it calls unfamiliar | its score | score of knowing nothing |
|---|---|---|---|---|
| 11 | 36 | 15 of 53 | 52.03 | 36.00 |
| 22 | 48 | 9 of 53 | 21.49 | 48.00 |
| 33 | 59 | 5 of 53 | 11.93 | 59.00 |
| 44 | 64 | 3 of 53 | 8.96 | 64.00 |
The last two columns have to be read together. A model whose score is above the score of knowing nothing over its own vocabulary has learned nothing useful, and the eleven-sentence model, at 52.03 against 36.00, is in that position.
That first row is worth naming plainly. A two-word model fitted to eleven sentences, at the constant chosen for the full corpus, is worse on the reporting text than a model that gives every word of its vocabulary the same probability. The counting only starts to pay once there is enough text for the counts to mean something, and on the way there it can be beaten by the crudest thing available.
Keep in mind
Part 6. Writing Instead of Reading
21. Drawing a word instead of scoring one
The answer in step 8 is a distribution, and there are two things to do with a distribution. One is to look up the probability of a word somebody else wrote, which is everything above. The other is to draw a word from it at random in proportion to the numbers, write that word down, slide the window along, and draw again. Stop when the end marker is drawn.
This is worth doing because it makes the model’s opinion visible in a way a score does not. A perplexity of 8.9626 tells you a model is better than one at 23.6179 and tells you nothing about what either of them thinks English is.
…
A one-word model produces the right words in no order, since it has no notion of order to have. Two words gives phrases that hold together for three or four words and then wander. Three gives sentences that are mostly grammatical, and by five and six the sentences are grammatical because they are the training sentences.
Keep in mind
22. Flattening or sharpening the draw
The draw can be reshaped before a word is taken. Raise every probability to the power of one over a temperature and renormalise, and the answer becomes more or less peaked without any word being ruled in or out. Below a temperature of one the likely words become likelier and the text more predictable; above one the tail comes up and the text more surprising. At a temperature of exactly one the draw is the model’s own answer untouched.
At a temperature of a quarter a three-word model draws The analysis of the samples was expected to agree, which is one of the forty-four sentences word for word. At a temperature of one the same seed gives The analysis of the samples. first summary delay. reviewed cost. rewrote, which is the smoothing showing through, since a little probability was spread over every word and the draw occasionally takes it.
Keep in mind
23. What a wide window turns out to have learned
The bar chart above is the measurement rather than the impression. Draw fifty sentences at each width with no smoothing at all and count how many come back as one of the forty-four training sentences word for word. At one word it is 0 of 50. At two it is 2, at three 10, at four 37, at five 45, and at six all fifty.
A six-word model on this corpus draws nothing that was not already written down, which is the same fact as the grey line in step 18 falling to 1.6025, seen from the other side. What it has learned is the forty-four sentences, and its excellent score on those sentences is a report of that and not of anything else.
Keep in mind
Part 7. Where the Method Stops Being Defined
24. A dependency the window cannot reach
The Markov assumption is not an approximation that more data improves. It is a statement about what the model is allowed to condition on, and anything outside that window is absent from the description altogether rather than approximated badly.
Here is a corpus in which that can be watched. Six sentences, each of the form the samples that somebody read were late or the sample that somebody read was late, so the verb agrees with the subject every single time and the subject stands five words before the verb. There is nothing ambiguous about the corpus and nothing statistical about the rule.
| words the model reads back | P(were) after “the samples that Dr. Alvarez read” | P(was) |
|---|---|---|
| 1 | 0.5 | 0.5 |
| 2 | 0.5 | 0.5 |
| 3 | 0.5 | 0.5 |
| 4 | 0.5 | 0.5 |
| 5 | 1.0 | 0.0 |
The deciding word is five back. At four words of context the model splits its answer exactly in half, and no amount of further text of this kind would move it, since the word that settles the question is not in the description.
Widening the window until it reaches is not a general answer. Every extra word multiplies the number of possible contexts by the size of the vocabulary, which is Part 1’s table, so the width that would reach a dependency of ten words describes a space no corpus can populate. English has agreements and references that reach much further than that, and a fixed window cannot be widened to cover them without the counting collapsing first.
Keep in mind
25. The counts are one text’s opinions
A model of this kind has no content beyond the text it counted. It does not know that Dr. is usually followed by a surname; it knows that in these forty-four sentences it was followed by Alvarez seven times out of seven, and it will say so about any text you show it. Every regularity it reports is a regularity of one corpus, including the ones that are accidents of that corpus.
The measurement in step 20 is the same point in another form. Changing which sentences were counted changed the vocabulary from 36 words to 64, changed how many of a new text’s 53 predicted positions were recognised at all from 38 to 50, and changed the score from 52.03 to 8.96. None of that is the method working better or worse; it is the model being a different model because the text was different.
This is not peculiar to counting runs. Anything fitted to a corpus inherits it. It is worth stating here because the counting is so direct that the inheritance can be looked up entry by entry, which is more than can be said for most of the models on this site.
Keep in mind
26. A rule the data cannot supply
The deepest gap is the one Part 4 measured without naming. The counts say how often each run occurred. They cannot say how much probability the runs that did not occur ought to share, because that is a question about events the counting has no observations of, and no amount of counting produces observations of them.
So a smoothing rule is an assumption brought in from outside, and the choice among rules is not settled by the data. Adding one assumes every unseen run is as plausible as every other, which is why a certainty of seven out of seven falls to 0.1127. Choosing 0.005 instead assumes far less, and it was chosen by scoring candidate values on further text, which is a measurement of the consequence rather than a derivation of the rule. Somebody had to decide what to sweep over and which text to sweep on.
That the sweep found 0.005 at two words and 0.02 at four is itself the evidence. An amount that followed from the counts would not have to be searched for, and it would not have moved by a factor of four when the only thing that changed was how many words the model reads back.
Keep in mind
27. Where the arithmetic runs out
Beside the three limits above there are cases where the definition simply has nothing to say, and it is worth knowing which is which. Each row below is a fact about the mathematics rather than a matter of taste, and several of them carry a choice that somebody has to make and that changes the answer.
| the case | what the method says |
|---|---|
| a context that never occurred | the estimate is a count over a total and the total is zero, so the quotient is undefined rather than small. Adding a constant makes it defined and makes it flat, one over the vocabulary for every word alike, which is 0.0156 here and carries no information about the context at all. |
| a run that occurred with a context that did | defined, and zero. That is a real number rather than an absence, and it is the number that makes a whole text impossible, which is the difference between this row and the one above. |
| any text containing one such run | its probability is a product with a zero in it, so the probability is zero, the log probability is negative infinity and the perplexity is infinite. Infinite is the correct answer rather than an error, since the model really is claiming the text cannot occur. |
| a word outside the vocabulary | the model has no entry for it, so something must be decided. Either the vocabulary is closed and the word becomes a single stand-in, which is what happens here, or the model refuses the text. Closing it makes every text scorable and makes the score depend on how many words were folded into the stand-in. |
| the stand-in itself never occurring in training | then the model has never seen an unfamiliar word and has no opinion about what surrounds one, so both of the running sentence’s unfamiliar words are answered from the smoothing alone. Relabelling the rarest training words as the stand-in before counting is the repair, and it costs those words their own entries. |
| a smoothing constant of exactly zero | the rule becomes one count over another, so it is defined wherever the context occurred and undefined where it did not. Every positive constant is defined everywhere, so zero is a boundary rather than a small value. |
| a window wider than the sentence | defined, because the framing supplies as many start markers as the width asks for, so a sentence of two words under a six-word window is read entirely out of markers. The counts it produces are about the framing rather than about the language. |
| no end marker in the framing | the model can never predict that a sentence has finished, so generation does not terminate and the probabilities of the sentences of each length sum to one separately. A distribution over sentences of all lengths needs the end to be an event. |
| an empty text | there are no predicted positions, so the average in the exponent is a division by zero and the perplexity is undefined. A text of no words is not a text with a perfect score. |
| two perplexities over different vocabularies | not comparable, and the direction of the error is predictable. A model that knows nothing scores the size of its own vocabulary, measured here at 36.00 over 36 words and 64.00 over 64, so the smaller vocabulary flatters every model built on it. |
| the smoothing constant chosen on the text the score is quoted from | the score is then a best-of-many rather than an estimate, and it is optimistic. Choosing on separate text is the repair; here the chosen constant scores 4.07 where it was chosen and 8.96 where it was not, and at one word per run it scores 46.73 against adding one’s 43.06, so the choice made on one text was the wrong choice for the other. |
Keep in mind
Check yourself
Questions on Parts 5 to 7
Question 1 of 5
A model with one chance in a hundred at every word has a perplexity of 100. What is the number saying?
Try it with Botie
Build with Botie
Write and run Python