XP: 0
Part B · Module 5

Predicting the Next Word

Words are vectors now — so how did the first language models actually generate text? Meet the bigram: predict the next word from the current one. Simple, clever… and gloriously broken. Let's play with it and watch it fail.
The first idea

A bigram: guess the next word from the last word

One of the earliest language models was the bigram. The idea is beautifully simple: given one word, predict the word most likely to come next. Chain those predictions and you generate a whole sentence — one word at a time.

Layman example — phone keyboard autocomplete

You've used a bigram thousands of times: the next-word suggestions above your phone keyboard. Type "good" and it offers "morning / luck / night." It's just guessing the most common word to follow the last one you typed. Tap-tap-tap and it writes a sentence for you — often a nonsensical one. That's a bigram.

🎮 Game 1

Build a sentence, one prediction at a time

You're the bigram. Start with a word, then keep picking the next word from the model's top suggestions (with their probabilities). Watch your sentence grow — and notice how quickly it drifts into nonsense, because each choice only looks at the single previous word.

The next-word chain
Tap a suggestion to add it. The model only sees your last word.
the
most likely next words after "the":
How it decides

Where the probabilities come from: counting

The earliest bigram, Maximum Likelihood Estimation (MLE), isn't clever at all — it just counts. It scans a big pile of text and tallies: "after the word cat, how often does each word appear?" Those counts become probabilities.

P( next | "cat" ) = count("cat" → next) ÷ count("cat" → anything)
# how often 'next' followed 'cat', divided by how often 'cat' appeared
Greedy vs. random

Two ways to pick: greedy decoding always takes the single highest-probability word (deterministic, but can get stuck in loops), or sample randomly weighted by the probabilities (adds variety — this is temperature from Module 3!).

🎮 Game 2

Count the corpus yourself

Here's a tiny training text. Click a word to highlight every place it appears, and the model counts what came right after it — turning raw counts into next-word probabilities, exactly like MLE.

MLE: counts → probabilities
Pick a word to see what usually follows it in the training text.
what follows "the" →
Why it breaks

The fatal flaws of bigrams

Bigrams feel magical for two words and fall apart for two sentences. Here's why — and these failures are exactly what pushed the field toward everything that came after.

Flaw 1 — no memory past ONE word

A bigram only ever sees the single previous word. It has zero context beyond that. So it forgets the subject, the topic, everything older — which is why chained bigram text wanders into gibberish.

Flaw 2 — it ignores meaning-flipping words like "not"

"I do not like this" and "I do like this" mean the opposite — but a bigram treats "not" as just another word to count. It has no understanding, only statistics. Try the demo below.

🎮 Game 3

Watch "not" get ignored

Two sentences that mean opposite things. A meaning-aware model sees the flip; a bigram, only counting word-pairs, treats them almost the same. Press the button and compare.

"not" — the word bigrams can't handle
Sentiment of two opposite sentences, judged two ways.
"I do like this movie"
true meaning: positive 👍
"I do not like this movie"
true meaning: negative 👎
🎮 Game 4

The greedy loop trap

Greedy decoding always grabs the most likely word. But if "mat" is most likely after "the," and "the" is most likely after "mat"… you get an infinite loop. Press Generate (greedy) and watch it get stuck — then try Generate (random) to escape.

Greedy gets stuck · random breaks free
Same model, two decoding strategies.

This "greedy loop" is literally the problem in your notes that pushed models from bigrams → trigrams (look at the last two words) — and eventually to models that look at everything.

The first upgrade

Trigrams: look at the last two words

The obvious fix for "no context past one word" is: look at two words instead of one. That's a trigram — it predicts the next word from the previous pair. This small change fixes a surprising amount: it catches "not like" as a unit, and it breaks many greedy loops (because "the mat" and "mat the" lead to different places).

The n-gram family

bigram = last 1 word → next.   trigram = last 2 words → next.   4-gram = last 3, and so on. The general name is an n-gram: use the previous n−1 words to predict word n. More context = better predictions… but also exponentially more combinations to store.

Layman example — hearing half a sentence

If a friend just says "bank", you have no idea what they mean (money? river?). But "river bank" — now you know. A bigram only heard the last word; a trigram heard a bit more, so it guesses better. It's the difference between catching one word of a conversation vs. catching the last few.

🎮 Game 5

Bigram vs. Trigram — see the window grow

Same sentence, two models. Flip between bigram (looks at 1 word) and trigram (looks at 2 words) and watch how much of the sentence each one "sees" when predicting the next word — and how the prediction improves.

How wide is the model's window?
The highlighted words are all the model gets to look at.
given the highlighted context, the model predicts:

Notice: with the phrase "do not", the trigram's two-word window captures the "not" together with the verb — so it can flip the meaning. The bigram, seeing only one word, still can't.

🎮 Game 6

Trigrams escape the greedy loop

Remember Game 4, where the bigram got stuck in the → mat → the → mat…? A trigram remembers the pair it's in, so "the mat" and "mat the" predict different next words — and the loop breaks. Compare them side by side.

Same start, two models
Watch the bigram loop while the trigram keeps moving.
BIGRAM (1-word memory)
TRIGRAM (2-word memory)
The next step

Neural-network n-grams: smarter, but still short-sighted

Since words are vectors, we can replace the counting with a small neural network that predicts the next word. This is a real upgrade over pure counting:

Counting bigram (MLE)

  • If a pair never appeared → probability zero
  • Doesn't scale to big vocabularies
  • Treats "dog" and "cat" as totally unrelated

Neural-network bigram

  • Non-zero: can predict unseen pairs
  • Scales much better to large vocabularies
  • Captures similarity ("dog" ≈ "cat" via embeddings)

Instead of a lookup table of counts, the neural version runs the word through a tiny network: embed → hidden layer → softmax → next-word probabilities. It's the exact machine from Modules 1–3, now applied to words.

🎮 Game 7

Inside a neural next-word predictor

Pick an input word and press Predict. Watch it become an embedding (Module 4), flow through a hidden layer (Module 2's neurons), and come out as softmax probabilities (Module 3) over the next word. Everything you've learned, working together.

word → embedding → hidden → softmax → next word
A real (tiny) feed-forward neural bigram.
🎮 Game 8

The neural superpower: it never saw this pair

Here's what counting can't do. The training text contains "the dog ran" but never "the cat ran". A counting bigram gives "cat ran" a probability of zero. But because the neural model knows dog ≈ cat (similar embeddings from Module 4), it predicts "ran" anyway. Test it.

"cat ran" — a pair never seen in training
Counting says impossible. The neural net generalises.
Counting bigram
P(ran | cat) = ?
Neural bigram
P(ran | cat) = ?
But the core problem remains

Even a neural bigram still only looks at the last word. Trigrams stretch this to the last two words — better, but still tiny. The real question becomes: how do we give a model memory of everything that came before? That question is the whole rest of this course — RNNs (Module 6), then attention (Module 7).

🎯 Check your understanding

Quick challenge

1. What does a bigram use to predict the next word?
2. How does the counting bigram (MLE) get its probabilities?
3. Why does "I do not like this" fool a bigram?
4. What advantage does a neural-network bigram have over counting?
5. How is a trigram different from a bigram?
6. In an "n-gram", what does the n refer to?
7. Why can a neural bigram predict "cat ran" even if it never appeared in training?
Takeaway

What you learned

A bigram predicts the next word from the single previous word (counting = MLE), picked greedily (can loop) or randomly. Its flaws — no context past one word, and no understanding of "not" — push us to trigrams (look at the last two words), and generally n-grams (last n−1 words). Wider windows catch phrases like "do not" and break loops, but cost exponentially more storage. The neural versions swap counting for a tiny network (embed → hidden → softmax), gaining unseen-pair prediction and similarity (dog≈cat). Yet even trigrams only see a few words — the real goal is a model that remembers everything. That's what's coming.

Next up → Module 6: Reading the mood (sentiment analysis) — a gentle, tangible NLP task before we tackle the big stuff. Then Part C: memory (RNNs) and the attention breakthrough.