Part I · Prediction
Ch 1. Predicting by counting
What usually comes next?
The simplest way to predict the next word is to look at the word you just read, and remember what came after it in the past. If little was followed by lamb once, by dog once, and by star once, then after little each of those has a one in three chance.
That’s a bigram modelbigram model: A model that predicts the next word using only the one word before it. Ch 1: bi because it only ever looks at two words, the current one and the next.[1][1][1] Building one takes nothing but counting. Below, the training text is six nursery rhymes. Step through it (or press Play) to watch the counts fill in, one pair of words at a time.
From counts to probabilities
Once everything is counted, each word’s counts turn into chances: after little, the three followers each get one in three. Pick a word in the widget to see its chances as bars. To make up text, start with a word, pick the next one using those chances, and repeat.
Once everything is counted, each row turns into probabilities: divide each count by the row’s total. That’s all the model knows. Pick a word in the widget to see its row as bars.
To generate text, start with a word, spin for the next one using those probabilities, then repeat with the new word. Claude Shannon generated English this way in 1948, choosing each word based only on the one before it.[2][2]
The counting rule
As a formula, the bigram model’s chance for the next word is
where is the word just read, is a candidate next word, is how many times that pair appears in the training text, and is how many times appears with any word after it. For little, and , so . These are the chances that make the training text as likely as possible.[1]
Real n-gram models look further back (a trigram uses the last two words) and deal with pairs they never saw. Here, any unseen pair gets a chance of 0, and since a sentence’s chance is its words’ chances multiplied, one new pair makes the whole sentence impossible. Smoothing moves a little probability onto unseen pairs. The simplest version adds one to every count:
where is the number of different words.[1]
Counting also stops scaling. With GPT-2’s 50,257 tokens, a table of every pair would have about 2.5 billion cells, and a table of every triple about 127 trillion. Bengio and colleagues called this the curse of dimensionality, and proposed learning to compute a prediction instead of looking one up.[3]
Why the output is bad
Press Generate a few times. The output borrows phrases from the rhymes, but it wanders between them: the cow jumped over the clock struck one, the lamb was sure to go. Every pair of words appeared in the training text, but the sentence as a whole makes no sense.
The problem is that the model only ever looks one word back. After the, it has no idea whether it is in the rhyme about the cow, the mouse, or the lamb, so it picks a follower of the from all of them.
Recipe card
Recap
- Prediction means answering “what usually comes next?”
- A bigram model answers it by counting which word follows which, then turning the counts into probabilities.
- It only looks one word back, so its output is locally plausible but wanders.
- : the pair’s count over the first word’s count.
Next question: the model gives probabilities, but something has to decide which word to actually use. How does that choice work, and can we control it?
Sources
- Daniel Jurafsky and James H. Martin (2025). N-gram Language Models, in Speech and Language Processing (3rd ed. draft). Stanford University. Accessed October 23, 2026.
Sources
- Daniel Jurafsky and James H. Martin (2025). N-gram Language Models, in Speech and Language Processing (3rd ed. draft). Stanford University. Accessed October 23, 2026.
- Claude E. Shannon (1948). A Mathematical Theory of Communication. Bell System Technical Journal 27(3).
Sources
- Daniel Jurafsky and James H. Martin (2025). N-gram Language Models, in Speech and Language Processing (3rd ed. draft). Stanford University. Accessed October 23, 2026.
- Claude E. Shannon (1948). A Mathematical Theory of Communication. Bell System Technical Journal 27(3).
- Yoshua Bengio et al. (2003). A Neural Probabilistic Language Model. Journal of Machine Learning Research 3.