Level 9 · PractitionerLesson 9.2 38 min read 10 question quiz 12 marks of written practice

Sequence Models: Predicting the Next Word

What a language model does

A language model assigns probabilities to text. Its core job is to answer: given the words so far, how likely is each possible next token?

Next-token predictionThe words The keys to the cabinet go into a model, which outputs a probability for each possible next token: are 62 percent, is 21 percent, were 8 percent, in 3 percent, was 2 percent, and everything else 4 percent. A note explains the loop: choose one, such as are, add it to the text, and predict again, which is how every chatbot writes its answers. The model must look back past cabinet to keys to choose the plural are.A language model predicts a probability for every next tokenThekeystothecabinetmodelare62%is21%were8%in3%was2%…4%Then: choose one (say “are”), add it to the text, and predict again.“The keys to the cabinet are …” → next prediction → next → … That loop writes every chatbot answer.
To choose “are”, the model must remember that the subject was “keys”, not “cabinet”.

To generate text, a model repeats one simple loop:

  1. Predict the probability of every possible next token.
  2. Choose one, usually by sampling from those probabilities.
  3. Add it to the text, and go back to step 1.

Generating one token at a time, each time feeding the output back in as input, is called autoregressive generation. Every chatbot works this way: an essay is hundreds of next-token predictions in a row.

N-gram models: counting what comes next

The simplest language model just counts. A bigram model estimates the chance of each word from the word before it:

Bigram probability

P(next word | previous word) = count(previous, next) ÷ count(previous)

In the sentence set "the cat sat on the mat . the dog sat on the log . the cat saw the dog . the dog saw the cat on the mat .", the word "the" appears 9 times before another word:

After "the"CountProbability
cat30.33
dog30.33
mat20.22
log10.11

An n-gram model looks at the previous n − 1 words. Longer contexts predict better but become hopelessly sparse: most five-word sequences never appear in any training text at all, so the counts are zero. Smoothing tricks help, but n-gram models can never use more than a few words of context.

Perplexity: how surprised is the model?

Language models are compared by perplexity, which measures how surprised a model is by real text:

Perplexity

perplexity = e to the power of the average cross-entropy loss per token

A perplexity of 20 means the model is, on average, as uncertain as if it were choosing uniformly among 20 equally likely words. Lower is better. On a standard benchmark of news text, the best n-gram models scored perplexities well over 100; neural language models brought that down to the teens and below.

Recurrent neural networks

A recurrent neural network (RNN) reads a sequence one token at a time and carries a hidden state, a vector that acts as its memory of everything so far.

The RNN update

new hidden state = tanh(W × old hidden state + U × current input + b)

A recurrent neural network, unrolledOn the left, a recurrent cell labelled RNN takes an input x and loops its hidden state h back into itself. It equals the unrolled version on the right: five copies of the same cell in a row, reading the inputs the, cat, sat, on, the, one at a time. An orange hidden state, h1 to h4, passes from each copy to the next, and each copy predicts the next token: cat, sat, on, the, mat. The same weights are used at every step.A recurrent network, unrolled: the same cell at every stepRNNhxₜ=RNNthecath₁RNNcatsath₂RNNsatonh₃RNNontheh₄RNNthematinputs, one token at a timepredicted next tokensorange: the hidden state, the network's memory, passed forward with the same weights each step
The hidden state is the network's memory of everything so far.

The same weights W and U are used at every step, so an RNN can handle sequences of any length. Drawn "unrolled", it looks like a very deep network with one layer per word, and it is trained with backpropagation through that unrolled graph, called backpropagation through time.

Why plain RNNs forget

The gradient from a word 50 steps back passes through 50 multiplications by W and 50 tanh slopes. Just as in deep networks (Level 6), it usually vanishes, so a plain RNN struggles to connect "The keys to the cabinet, which I left in the kitchen last week, ___" back to "keys" when choosing between "is" and "are".

LSTMs: learning what to remember

The long short-term memory network (LSTM), invented by Sepp Hochreiter and Jürgen Schmidhuber in 1997, adds a separate cell state, a memory line running along the sequence that information can travel on almost unchanged, plus three gates that learn what to do with it:

An LSTM cellA schematic LSTM cell. A thick teal line, the cell state or long-term memory, runs across the top. It passes a red multiply node, controlled by the forget gate, a sigmoid deciding what to erase, then a green add node, where new information enters: the input gate, a sigmoid deciding what to write, multiplied by a candidate from a tanh layer. At the right, the cell state goes through tanh and is multiplied by the output gate, a sigmoid deciding what to reveal, to give the new hidden state h. Along the bottom runs the previous hidden state and the input, feeding all four boxes.Inside an LSTM cell: a memory line and three gatescell state: long-term memoryprevious hidden state hₜ₋₁ and input xₜforget gateσ: what to eraseinput gateσ: what to writecandidatetanh: new infooutput gateσ: what to reveal××+tanh×hₜthe memory line is changed only by × (forget) and + (add), so gradients travel along it far better than through a plain RNN
Each gate outputs numbers between 0 (block) and 1 (let through).
GateQuestion it answers
Forget gateWhich parts of the memory should be erased?
Input gateWhich new information should be written in?
Output gateWhich parts of the memory should be passed on now?

Each gate is a sigmoid layer outputting values between 0 (block) and 1 (let through), multiplied with the information it controls. Because the cell state is updated by addition rather than repeated multiplication, gradients survive over hundreds of steps. The simpler GRU (2014) merges some of the gates and works about as well.

LSTMs powered the 2016 version of Google Translate, speech recognition in phones and much more, until transformers replaced them.

Sequence to sequence, and the bottleneck

For translation, a sequence-to-sequence model (2014) uses two RNNs:

  • an encoder reads the whole source sentence and squeezes it into its final hidden state, one fixed-size vector;
  • a decoder generates the translation word by word, starting from that vector.
The sequence-to-sequence bottleneckAn encoder of four blue cells reads the, black, cat, sleeps, and passes its state along to a single red circle, one vector, labelled the bottleneck. A decoder of four pink cells starts from that vector and writes le, chat, noir, dort. Dashed amber curves of different thicknesses show attention: while writing noir, the decoder looks back at every encoder state, most strongly at black.Sequence to sequence: everything squeezed through one vectorenctheencblackenccatencsleeps1 vecthe bottleneckdecledecchatdecnoirdecdortattention (2014): while writing “noir”, look back at every encoder state, most strongly at “black”encoder reads the sourcedecoder writes the translation
Attention removed the bottleneck, and then replaced the RNNs entirely.

The weakness is obvious from the picture: the meaning of the entire sentence, however long, must pass through one vector, the bottleneck. Translation quality fell sharply for long sentences.

In 2014 Dzmitry Bahdanau, Kyunghyun Cho and Yoshua Bengio let the decoder look back at every encoder state, weighting them by relevance at each step. They called it attention. Three years later, a team at Google asked what would happen if you kept the attention and threw the recurrence away. That is Level 10.

Hands-on: language models from scratch

A bigram model that writes

Python
import random
from collections import Counter

text = ("the cat sat on the mat . the dog sat on the log . the cat saw the dog . "
        "the dog saw the cat on the mat .").split()
pairs = Counter(zip(text, text[1:]))
starts = Counter(text[:-1])

def next_word_probs(word):
    return {b: c / starts[word] for (a, b), c in pairs.items() if a == word}

print("after 'the':", {w: round(p, 2) for w, p in next_word_probs("the").items()})
print("after 'sat':", next_word_probs("sat"))

random.seed(3)
word, sentence = "the", ["the"]
for _ in range(14):
    probs = next_word_probs(word)
    word = random.choices(list(probs), weights=list(probs.values()))[0]
    sentence.append(word)
print("generated:", " ".join(sentence))

Every pair of neighbouring words is plausible, yet the whole makes little sense, because a bigram model only ever sees one word back.

Perplexity

Python
import math
from collections import Counter

train = ("the cat sat on the mat . the dog sat on the log . the cat saw the dog . "
         "the dog saw the cat on the mat .").split()
vocab = set(train)
pairs = Counter(zip(train, train[1:]))
starts = Counter(train[:-1])

def prob(prev, word):
    # add-one smoothing, so unseen pairs are unlikely but not impossible
    return (pairs[(prev, word)] + 1) / (starts[prev] + len(vocab))

def perplexity(sentence):
    words = sentence.split()
    loss = -sum(math.log(prob(a, b)) for a, b in zip(words, words[1:])) / (len(words) - 1)
    return math.exp(loss)

for s in ["the cat sat on the mat", "the dog sat on the log", "mat the on sat cat the"]:
    print(f"{s:24} perplexity {perplexity(s):5.2f}")

A recurrent memory

This one-number RNN state remembers that it saw a "1", and that memory fades with every step, which is the forgetting problem in miniature.

Python
import math

w_hidden, w_input = 0.7, 1.5
h = 0.0
sequence = [1, 0, 0, 0, 0, 0, 1, 0, 0]
for t, x in enumerate(sequence):
    h = math.tanh(w_hidden * h + w_input * x)
    print(f"step {t}: input {x}  hidden state {h:.3f} " + "#" * int(h * 30))

Try this

  1. Train the bigram model on a paragraph of your own and generate from it.
  2. Change w_hidden to 0.95 and then 0.3. How long does the memory last now?

Level check

  • Explain what a language model predicts and how autoregressive generation works
  • Calculate bigram probabilities from counts
  • Explain what perplexity measures and whether lower or higher is better
  • Describe how an RNN carries a hidden state, and why plain RNNs forget
  • Describe the three gates of an LSTM and why its cell state helps
  • Explain the bottleneck in sequence-to-sequence models and how attention began

Key terms you must be able to define

Language model
A model that assigns probabilities to text, usually by predicting the next token.
Autoregressive generation
Producing text one token at a time, feeding each output back in as input.
N-gram
A sequence of n words; an n-gram model predicts a word from the n − 1 before it.
Sparsity
Most possible word sequences never appear in training data, so their counts are zero.
Perplexity
e to the power of the average cross-entropy per token: how many choices the model is effectively unsure between.
Recurrent neural network (RNN)
A network that processes a sequence step by step, carrying a hidden state forward.
Hidden state
The vector an RNN passes from step to step as its memory of the sequence so far.
Backpropagation through time
Training an RNN by backpropagating through its unrolled steps.
LSTM
Long short-term memory: an RNN with a cell state and gates that learn what to forget, store and output.
Sequence to sequence
An encoder network that reads an input sequence and a decoder that generates an output sequence.
Bottleneck
Forcing all of a sequence's information through one fixed-size vector.

Flashcards

DefineLanguage model
AnswerA model that assigns probabilities to text, usually by predicting the next token.
DefineAutoregressive generation
AnswerProducing text one token at a time, feeding each output back in as input.
DefineN-gram
AnswerA sequence of n words; an n-gram model predicts a word from the n − 1 before it.
DefineSparsity
AnswerMost possible word sequences never appear in training data, so their counts are zero.
DefinePerplexity
Answere to the power of the average cross-entropy per token: how many choices the model is effectively unsure between.
DefineRecurrent neural network (RNN)
AnswerA network that processes a sequence step by step, carrying a hidden state forward.
DefineHidden state
AnswerThe vector an RNN passes from step to step as its memory of the sequence so far.
DefineBackpropagation through time
AnswerTraining an RNN by backpropagating through its unrolled steps.

Go further: notes for future AI engineers

Teacher forcing and exposure bias

When training a sequence model, the input at each step is the real previous token from the training text, not the model's own guess. This is called teacher forcing, and it makes training fast and parallel. But at generation time the model must eat its own outputs, including its mistakes, which it never practised: a mismatch called exposure bias. A single odd word can push the model into territory unlike anything it trained on, which is one reason long generations can drift.

Why RNNs were slow to train

An RNN must compute step 2 after step 1, step 3 after step 2, and so on, so a 1,000-token sequence takes 1,000 sequential steps that cannot run in parallel, leaving most of a GPU idle. The transformer's attention compares all positions at once, turning a sequence into one big matrix multiplication. That parallelism, more than any other single factor, is why transformers could be trained on vastly more data and replaced RNNs almost everywhere after 2017.

Misconceptions to avoid

Avoid these
  • Thinking a language model looks up answers. It predicts likely next tokens, one at a time.
  • Believing higher perplexity is better. Lower means the model is less surprised by real text.
  • Assuming an RNN uses different weights at each step. The same weights are reused at every step.
  • Thinking LSTMs have no limit to their memory. They remember far longer than plain RNNs, but still struggle with very long texts.
  • Forgetting that a bigram model ignores everything except the previous word.
Fun fact

Claude Shannon generated surprisingly readable fake English in 1948 by choosing each word based on the word before it, using statistics he counted by hand from books. It was the first language model.

Knowledge check

0 of 10 answered
1What does a language model predict?
2What is autoregressive generation?
3'the' appears 10 times before another word, followed by 'cat' 4 times. What is P(cat | the) in a bigram model?
4Why can n-gram models not use long contexts?
5Model A has perplexity 12 on a test text and model B has 45. Which is better?
6What does an RNN carry from one step to the next?
7Why do plain RNNs struggle with long sentences?
8What does the forget gate in an LSTM do?
9What was the bottleneck in early sequence-to-sequence translation?
10Who first generated text with word statistics, in 1948?
Score 0 / 10

Check you could explain this

12 marks
1Explain how a language model generates a paragraph of text.4 marks
Explain

Mark scheme0 / 4
  • LIt predicts a probability for every possible next token
  • LIt chooses / samples one token
  • LThe chosen token is added to the input / context
  • LRepeated one token at a time until finished: autoregressive
Model answer

Given the text so far, the language model predicts a probability for every token in its vocabulary being the next one. It then chooses one token, usually by sampling from those probabilities. The chosen token is appended to the text, and the longer text becomes the input for the next prediction. Repeating this loop one token at a time, which is called autoregressive generation, builds up the whole paragraph.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.

Think this answer or mark scheme is wrong? Report it

2Compare a bigram model with a recurrent neural network as language models.4 marks
Compare

Mark scheme0 / 4
  • LA bigram model counts pairs of words / uses only the previous word
  • LIt suffers from sparsity / cannot use long context
  • LAn RNN carries a hidden state summarising all previous words
  • LRNNs generalise through learned weights but forget over long sequences / vanishing gradients
Model answer

A bigram model simply counts how often each word follows each other word, so it predicts the next word from only the previous one, and it cannot handle combinations it never saw because their counts are zero. A recurrent neural network instead reads the sequence one token at a time and carries a hidden state vector that summarises everything so far, so in principle it can use the whole context, and its learned weights let it generalise to new combinations. However, plain RNNs suffer from vanishing gradients, so in practice they forget words more than a few dozen steps back, which LSTMs improved.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.

Think this answer or mark scheme is wrong? Report it

3Explain the bottleneck problem in sequence-to-sequence models and how attention addressed it.4 marks
Explain

Mark scheme0 / 4
  • LThe encoder compresses the whole input into one fixed-size vector
  • LInformation is lost for long sentences
  • LAttention lets the decoder look at all encoder states
  • LWeighted by how relevant each is at each step
Model answer

In a sequence-to-sequence model the encoder reads the whole source sentence and squeezes it into its final hidden state, a single fixed-size vector, which is all the decoder receives. However long the sentence, its meaning must pass through that one vector, so information is lost and translation quality falls for long sentences. Attention removes the bottleneck by letting the decoder look back at every one of the encoder's hidden states at each step, weighting them by how relevant each is to the word being produced, so it can focus on the right part of the source.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.

Think this answer or mark scheme is wrong? Report it

Machine mark 0 / 12