Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

How a language model picks the next word

After this lesson: I can explain how a language model picks the next word and where it goes wrong.

55 min lesson2 commandsJupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

A confident answer that was wrong

A new member of staff asks a chatbot when the shop opens on Sundays. It answers at once, in a polite and certain sentence. The hours it gives are wrong.

Nothing broke. The model did exactly what it was built to do. This lesson shows what that is, with a model small enough to work by hand.

By the end of this lesson

Note

I can explain how a language model picks the next word and where it goes wrong.

Key words

WordWhat it meansExample
Language modela program that gives the probability of the next token
Tokena piece of text a model works with, a word or part of one
Probabilityhow likely something is, from 0 to 1
Bigrama pair of tokens side by sidethe shop
Training textthe text a model learns from
Made-up answerfluent text that is not true, also called a hallucination

The cells students fill in are left blank in this preview.

Text becomes tokens

A model does not read letters the way you do. It cuts text into tokens first. A large model's tokens are common words and pieces of rarer words, often with the space attached.

Our tiny model keeps it simple. It makes everything lower case, treats a full stop as a token, and splits on spaces.

Cut a sentence into tokens

Code: python
sentence = "The shop opens at nine. The shop closes at six."
# Lower case, and make the full stop a token of its own.
tokens = sentence.lower().replace(".", " .").split()
print(tokens)
print(len(tokens), "tokens")
# Add a sentence of your own. How many tokens now?

Next-word probabilities from counts

A bigram model learns one thing: which token tends to follow which. Go through the training text and count every pair of neighbours.

To get the probabilities after a word, divide each count by how often that word was followed by anything. bigram.py does this for the word "the" in a short text about the shop.

Count what follows the word the

Command
docker exec jupyter python3 steps/bigram.py

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Worked example: after the word the

Next tokenCountProbability
shop33 / 9 = 0.33
desk22 / 9 = 0.22
hour11 / 9 = 0.11
bikes, customer, door1 each0.11 each
Total91.00, give or take rounding

The cells students fill in are left blank in this preview.

Count the next word yourself

Code: python
# Count what follows one word in a short text.
text = "the bus is late . the bus is full . the train is late ."
words = text.split()
word = "is"
counts = {}
total = 0
for n in range(len(words) - 1):
    if words[n] == word:
        after = words[n + 1]
        if after in counts:
            counts[after] = counts[after] + 1
        else:
            counts[after] = 1
        total = total + 1
for after in counts:
    print(word, after, counts[after], "of", total)
# Change word to "the". Which word is most likely next?

Writing is choosing the next token again

To write, the model picks a next token, often at random weighted by the probabilities. Then it does it again from the new token, and again.

Nothing in that loop checks whether the sentence is true. A large model is a neural network that looks at thousands of earlier tokens, not one. It is trained on a huge amount of text, but it is still choosing likely next tokens.

Write four sentences from the counts

Command
docker exec jupyter python3 steps/generate.py

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A common mistake

Warning

Many people think a language model looks facts up and reports them. It produces likely next tokens. When the likely words are also true, the answer is right. When they are not, it is wrong in the same confident voice, so check anything that matters.

The training text decides what is likely

A model can only echo the patterns in its training text. Our text says "the shop" three times, so the model reaches for "shop" whatever the topic.

A large model does the same with people. If its text mostly pairs one job with one sex, or one country with one crime, it repeats that. This is bias from data, and it is often invisible in a single answer.

How to check an answer

Treat an answer as a draft from a fast writer who never checks. Ask where a fact comes from, then open that source yourself. A source that does not exist is a warning.

Work any number out again. Ask the same question a second way and compare. Never paste in private data, such as customers' names, unless your organisation allows that tool for it.

Going further

Copy generate.py's text into a notebook cell and make the model look at two earlier words instead of one. Count which word follows each pair, such as "the shop".

Generate four sentences with the same seed. Are they closer to the training text? Explain why a model that looks further back writes better text but can still make things up.

About this lab and its source: JupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.