Principles of AI · Unit 1: Inside the Machine · Activity 1.1.1

Deep Dive: Next-Word Dice

🔬 Deep Dive
This is your textbook for this topic. Take your time. Read it more than once.
Principles of AI · 1.1.1Concept

What Is a Language Model?

Type "see you" on your phone and the keyboard offers a few ways to finish the thought. That is the smallest everyday version of a language model: a program that looks at the text so far, estimates how likely each possible next word is, and picks one. A chatbot does the same thing over and over. It picks a word, adds it to the text, predicts again, and keeps going until the reply is finished.

Models don't read in whole words, exactly. They split text into tokens: chunks that are often a short word, a piece of a longer word, or a punctuation mark. In your dice models, every word and every full stop is one token. The text a model learns from is its training data. Everything a language model "knows" comes from patterns in that text.

🔑A language model doesn't look anything up. It estimates what usually comes next, then picks. Hold on to that. Almost everything strange a chatbot does follows from it.

The practice text

To keep every number checkable by hand, this page uses a tiny practice text of our own. In class you'll use the text printed on your gameboards, but the ideas are identical.

Practice text · training data for this page
the robot rolls to the door .
the robot beeps at the cat .
a cat sits on the robot .
the cat likes the door .
a robot sleeps .
the cat naps .
the robot waits by the door .
42 tokens in all · 17 different tokens (the full stop counts as one) · this is a practice text, not the text on your class gameboards.
Principles of AI · 1.1.1Concept⚠ Watch Out

Model 1 — Equal Chance

The simplest possible model gives every token the same chance on every roll. With 17 different tokens in the practice text, each one has a 1-in-17 chance each time, no matter what came before. It uses the training data for exactly one thing: the list of tokens it's allowed to say. The result is word salad, which is the point. A model with no sense of which words are common or which words go together can't write anything that sounds like language.

Chance of each token = 1 ÷ (number of different tokens)

"Random" is not the same as "equal chance"

Equal chance sounds easy: just roll some dice. But if you add dice together, the totals are not equally likely. There are many ways to make a total in the middle and only one way to make the smallest or largest. Try the dice below.

Pick a dice setup. Each bar is one possible total, and its height is how likely that total is. If a board gives each word its own total, a word is only as likely as its total.

fair share3524.3%0
50 possible totals · fair share = 2.00% each
most likely: 27–28 at 4.25% · least likely: 0.02%
Most likely total is 204× as likely as the least likely.
⚠️The hidden favorite. Suppose a gameboard lists its words alphabetically and gives each word one possible total from a D12 plus two D20s. The words in the middle of the alphabet would come up far more often than the words at either end, so the "equal chance" model would secretly have favorites. To make a fair roll you need every outcome equally likely: one die, or a D10 and a D00 together (0–99).
Principles of AI · 1.1.1ConceptSkill

Model 2 — Word Frequency

Model 2 starts using the training data properly: it counts. If a token makes up 24% of the text, the model picks it about 24% of the time. That is a probability learned from data, and it's the first real step from random noise toward language. To roll on it with dice, you turn each count into a share of the die's faces.

faces = (count ÷ total tokens) × faces on the die

How often each token appears in the practice text. Model 2 rolls on exactly this.

the
10/42 · 23.8%
.
7/42 · 16.7%
robot
5/42 · 11.9%
cat
4/42 · 9.5%
door
3/42 · 7.1%
a
2/42 · 4.8%
at
1/42 · 2.4%
beeps
1/42 · 2.4%
by
1/42 · 2.4%
likes
1/42 · 2.4%
naps
1/42 · 2.4%
on
1/42 · 2.4%
rolls
1/42 · 2.4%
sits
1/42 · 2.4%
sleeps
1/42 · 2.4%
to
1/42 · 2.4%
waits
1/42 · 2.4%
ExampleGuided Example — From Counts to Dice Faces

Using the practice text, how many results on a 100-result roll (a D10 and a D00 together) should belong to the token "the"?

Step 1 — Count the token
Count how many times the appears in the practice text: 10.
⚠️What Model 2 gets wrong: it knows which tokens are common, but nothing about which tokens belong together. Expect plenty of "the" and plenty of full stops, in any order. Each roll ignores the one before it.
Principles of AI · 1.1.1ConceptSkill

Model 3 — Context Chains

Real sentences aren't piles of common words. Which word comes next depends on the word before it. After "the" you might expect "robot" or "door", almost never a full stop. Model 3 builds that in: instead of one table for the whole text, it has one follower table for every token, listing only the tokens that came right after it in the training data.

The idea is named for Andrey Markov, a Russian mathematician who, in the early 1900s, counted how often letters followed other letters in a famous Russian poem and showed that the next letter depends on the one before it. A Markov chain is a system that moves from one state to the next, with probabilities that depend only on where it is right now. In your game, the "state" is the last word you wrote.

Next token ~ follower table of the token just written
ExampleGuided Example — Build the Follower Table for "the"

Using the practice text, build the follower table for the token “the” and turn it into faces on a D20.

Step 1 — Find every “the”
"the" appears 10 times in the practice text.

Try all three models

Roll a few sentences with each model. Then peek at the tables underneath and find the table that produced your last word.

Pick a model, then roll a sentence. Each word comes from one roll of a 100-faced die. Roll several sentences with each model and compare what you get. (Model 3 starts with a word from Model 2, then chains. Every sentence stops at a full stop or after 14 tokens.)

Press "Roll a sentence" to start.
TokenCountChanceFaces on a 100-roll
robot440.0%1–40
cat330.0%41–70
door330.0%71–100
🔑Model 3 can only say pairs it has seen. Every pair of neighboring tokens in its output appeared somewhere in the training text. That gives you a test: if a sentence contains a pair that never appears in the text, Model 3 could not have written it. You'll use that test in the Dice Check.

Better, but it still can't hold a thought

Model 3 looks back exactly one token. So it can chain neat-looking pairs into a sentence that forgets how it started. For example, every neighboring pair in the cat likes the robot beeps at the door . appears in the practice text, yet the sentence falls apart halfway through. By the time it writes "beeps", the model has forgotten that "the cat likes" came first.

Principles of AI · 1.1.1Concept

Same Start, Different Ending

A deterministic algorithm gives the same output every time you give it the same input. A calculator always says 2 + 2 = 4. A probabilistic system uses chance, so the same input can lead to different outputs. Your dice models are probabilistic: start from "the" three times and you might get "robot", then "door", then "cat".

Deterministic: same input → same output

Probabilistic: same input → output chosen by probability

A chatbot works the same way. At each step it has a probability for every possible next token and picks from them, so the same question can come back worded differently. Many tools have a setting, often called temperature, that controls how adventurous the picks are. At very low settings the chatbot almost always takes the most likely token and becomes close to deterministic.

💡If you ask a chatbot the same question twice and get two different answers, nothing is broken. Chance is part of the design. It's also a good reason to check an answer instead of trusting one run.
Principles of AI · 1.1.1Concept

From Dice to LLM

Your dice models and the model behind a chatbot are in the same family: both estimate a probability for each possible next token and pick one. The differences are enormous.

Your dice modelsA real LLM
Training dataA few sentencesA huge share of books, websites, code and more — trillions of tokens for the largest models
TokensWhole words and full stopsChunks of text: short words, pieces of longer words, punctuation
What it looks atNothing (Models 1–2) or just the one word before (Model 3)Thousands of earlier tokens at once, and far more in recent models
Where the knowledge livesA table someone counted by handBillions of numbers (parameters, also called weights) adjusted during training
How it learnsCount onceGuess the next token, compare with the real text, nudge the numbers — repeated an enormous number of times
Changes while you use it?No — the table never changesNo — the weights stay fixed during your chat

Why a real model can't just use a bigger table

A model with a vocabulary of about 50,000 tokens needs 50,000 rows for a one-token context. For two tokens of context there are 50,000 × 50,000 = 2.5 billion possible pairs, and for a context of a thousand tokens the number of possible histories is far beyond anything a table could hold. Real models skip the table. They compress patterns into their weights, which lets them make a sensible guess even for text they have never seen.

🔑Fixed after training. When you chat with an AI it can feel like it is learning from you. The model's weights are not changing. What changes is the text it is shown. Some apps save notes about you and add them to the conversation, but that is extra text for the model to read, not new learning. For scale: GPT-3 (2020) had 175 billion parameters, and newer models are bigger or don't publish their size.