Skip to main content

Training

  • Audience years 5 and up
  • Duration 20 min
  • Runs on grid, ledger, cutouts

Key idea: Language models learn by counting patterns in text and tracking which words follow other words.

Used in My First Language Model and How AI writes stories and Under the hood and Shaping a model — the lessons that include this module.

Choose your method: this module works with a grid (paper and dice), a ledger (tally sheets and a cup of counters) or cutouts (printed tokens). Choose whichever suits your materials.

Build a Bigram model A model that predicts the next word from one previous word. It's what you build in the fundamental modules: each row of your grid says what can follow a single word. View in glossary that tracks which words follow which other words in text.

You will need

  • some text (e.g. a few pages from a kids book, but it can be anything)
  • pen or pencil
  • a printed grid template (or any grid paper)

For each group of four or five:

  • a short text to train on, printed
  • blank ledger sheets, one per person
  • a pen each

The How AI writes stories printouts have six short training texts, with every word boxed so the reader can see whether it starts a new row, and the blank sheets to go with them.

For each pair (or group) of students:

  • printed token cutouts (use the CLI to generate these from your text)
  • a clear table or flat surface to spread the cutouts out on
  • scissors

Your goal

Produce a Grid A sheet of grid paper with the vocabulary written down the side and across the top. Training fills each cell with tally marks for how often the row's word was followed by the column's word; generation reads a row and rolls against it. It's the original LLMs Unplugged apparatus. View in glossary that captures the patterns in your input text data. This grid is your bigram language model. Stretch goal: keep training your model on more input text.

Fill a set of Ledger A sheet with one row per word, tally marks for each word that followed it, and a coloured box around each tally column. Generation uses a cup of coloured counters, one per tally mark: draw a counter, read the word beside its colour. The cup does the weighted sampling. View in glossary with the word pairs in your text. The filled-in sheets are your bigram language model. Stretch goal: keep training your model on more input text.

Spread your printed Cutouts Printed cards, one per word pair in the text, each showing a previous word in a box and the word that followed it. Spread on a table they are the model: to generate, find a card whose boxed word matches your current word and read off its next word. Common pairs appear on more cards, so picking by eye samples in proportion. View in glossary out on a table. Each cutout shows a next word together with its previous word (the word that came before it). The whole spread of cutouts is your language model—every (previous, next) pair from your training text is sitting somewhere on the table.

Key idea

Language models learn by counting patterns in text. Training Building a model by counting patterns in text. When you read through a passage and tally which word follows which, you are training a model. The cutouts, booklets and search sheets are models that the generator has already trained for you from a text. View in glossary means building a model (filling out the grid) to track which words follow other words.

Language models learn by counting patterns in text. On a ledger, means giving every word a row, and tallying beside it each word that came next in the text.

The CLI has already done the bigram counting for you. Each cutout shows a next word together with the word it follows in the training text. If spot follows see 40% of the time in the training text, then 40% of the cutouts with see as the previous word will have spot as the next word. Whichever matching cutout your eye lands on, you’ve sampled in proportion to the original distribution, with no dice or probability tables.

Algorithm

  1. Preprocess your text
    • convert everything to lowercase
    • treat words and single punctuation marks (. , ! ? ; :) as separate “ Token A single unit of text that the model works with. In our activities each word and punctuation mark is a token. Modern LLMs use subword tokens, so a long or unusual word may be split into several. View in glossary ”; ignore paired punctuation like quotes and brackets, and ignore whitespace
  2. Set up your grid
    • take the first word from your text
    • write it in both the first row header and first column header of your grid
  3. Fill in the grid one word pair at a time
    • find the row for the first word (in your training text) and the column for the second word
    • add a tally mark in that cell (if the word isn’t in the grid yet, add a new row and column for it)
    • shift along by one word (so the second word becomes your “first” word) and repeat until you’ve gone through the entire text
  1. Preprocess your text
    • convert everything to lowercase (except “I”)
    • treat words and single punctuation marks (. , ! ? ; :) as separate words
  2. Read the text two words at a time, and for each pair:
    • find the row for the first word; no row yet? start one on the next spare row, and say the word out loud so the group knows who has it
    • find the box for the second word on that row; not there yet? write it in the next empty box (the boxes fill from the left: red, blue, green, yellow)
    • add a tally mark in that box
    • move along by one: the second word starts the next pair
  3. Keep going until you reach the end of the text
  1. Cut out the tokens from your printed sheets
    • each cutout shows a next word preceded by its previous word; every word has its own colour, and previous words appear inside a matching coloured box
  2. Spread the cutouts out on a table
    • face up, no overlap if you can manage it
    • the spread is your trained model

Optional extension: see “Group into piles” below.

Example

Before you try training a model yourself, work through this example to see the algorithm in action.

Original text: “See Spot run. See Spot jump. Run, Spot, run. Jump, Spot, jump.”

Preprocessed text: see spot run . see spot jump . run , spot , run . jump , spot , jump .

After the first two words (see spot) the model looks like:

Token see spot run . jump ,
see  |    
spot       
run       
.       
jump       
,       

After the full text the model looks like:

Token see spot run . jump ,
see  ||    
spot   | |||
run    || |
. | | | 
jump    || |
,  ||| | 

Original text: “See Spot run. See Spot jump. Run, Spot, run. Jump, Spot, jump.”

Preprocessed text: see spot run . see spot jump . run , spot , run . jump , spot , jump .

After the first pair (see spot) the ledger has one row:

see
spot
red
blue
green
yellow

After the full text it has a row for every word, in alphabetical order:

,
spot
red
run
blue
jump
green
yellow
.
see
red
run
blue
jump
green
yellow
jump
.
red
,
blue
green
yellow
run
.
red
,
blue
green
yellow
see
spot
red
blue
green
yellow
spot
run
red
jump
blue
,
green
yellow

Each row’s boxes fill in the order the words appear. After spot, run came first and took the red box; , came third, and with two marks it’s the most common word after spot.

For the text see spot run . see spot jump . run , spot , run . jump , spot , jump .:

After cutting and spreading, the table contains every adjacent (previous → next) pair from the text:

  • see→spot × 2
  • spot→run × 1, spot→jump × 1, spot→, × 2
  • run→. × 2, run→, × 1
  • .→see × 1, .→run × 1, .→jump × 1
  • ,→spot × 2, ,→run × 1, ,→jump × 1
  • jump→. × 2, jump→, × 1

Each entry is a physical cutout on your table. spot→, shows up twice, so two cutouts on the table have spot as the previous word and , as the next word, weighting generation towards that pair.

Previous word Next words
see spot spot
spot run jump , ,
run . , .
. see run jump
jump . , .
, spot run spot jump

Optional extension: group into piles

Once your students have got the hang of the loose-on-table flow, you can introduce grouping as an optimisation:

  1. Sort the cutouts into piles, one pile per unique previous word
  2. Label each pile with that word

Generation is faster when you go straight to the pile for your current word instead of scanning the whole table, just as a computer stores a language model in a hash table. The model holds the same counts, rearranged for faster lookup.

Instructor notes

Icebreaker questions

If this is the group’s first hands-on activity, these prompts surface what students already think about language models. Depending on your learning context, they work as either “call out your answer” or “discuss with your neighbour and share-back” questions.

  • why is a language model called a “language model”? What does it mean to “model language”?
  • what’s the best/clearest explanation you’ve ever heard of how LLM (Large Language Model) A language model trained on a very large amount of text, with billions of parameters. The hand-built models in these lessons are tiny language models; ChatGPT, Claude and Gemini are large ones, built on the same principles at a far greater scale. View in glossary (e.g. ChatGPT, Claude) work? What’s the weirdest explanation you’ve ever heard?
  • when was the first Language model A system that predicts what text comes next, using patterns learned from training data. Your hand-built grid, a cutouts spread, a booklet, and a room holding search sheets are all language models. View in glossary ever created? How similar/different was it to modern LLMs?
  • activity: get everyone to stand up, then have them sit down if they’ve never used ChatGPT, Claude, or a similar LLM. Then ask if they’ve used it in the last month/week/day/hour/5mins. At the end, everyone should be sitting down.

Keep this brief so students can get into the activity.

Discussion questions

  • what can you tell about the input text by looking at the filled-out bigram model grid?
  • how does including punctuation as “words” help with sentence structure?
  • are there any other ways you could have written down this exact same model?
  • how could you use this model to generate new text in the style of your input/training data?
  • what can you tell about the input text from the rows with the most marks?
  • why do the boxes have to fill from the left, with no gaps?
  • are there any other ways you could have written down this exact same model?
  • how could you use these sheets to generate new text in the style of your training text?
  • what can you tell about the input text by looking at which (previous, next) pairs show up most often in the spread?
  • why does see→spot appear twice while run→, appears only once?
  • how does including punctuation as separate help capture sentence structure?
  • what would happen if you trained on more text? How would the spread change?
  • how could you use these cutouts to generate new text in the style of your training data?

Troubleshooting

  • “Do I add a new row/column for every word?” No. Each new word gets a new row and column only the first time you see it. After that, find the existing row and column and add a tally mark.
  • “Two of us started a row for the same word.” The marks are now split across two rows, so neither count is right. Add one row’s marks into the other and cross the spare row out. Saying each new word out loud as you claim its row stops this happening.
  • “We skipped a box.” Move the word into the empty box. In generation a counter’s colour names a box, so a gap makes the colours point at the wrong words.
  • “We need a fifth box.” The training texts are written so that no word is ever followed by more than four different words, so four boxes are always enough. A fifth means a word has been written into the row twice.
  • “Some cutouts are duplicates—is that a mistake?” No, repeated cutouts are expected. If see→spot appears twice in the training text, you should see two see→spot cutouts on the table. The repetition is what makes common pairs easier to spot during generation.

Connection to current LLMs

This counting process is exactly what happens during the “ ” phase of language models:

  • Training data The text a model is trained on. In our activities this is the passage you tally, or the book a set of cutouts, a booklet or a set of search sheets was generated from. Modern LLMs are trained on trillions of words from books, websites and other sources. View in glossary : your paragraph vs trillions of words from the internet
  • learning/training process: hand counting vs automated counting by computers
  • storage: your paper model vs billions of Parameters The numbers a model stores to encode what it has learned. Each cell of your grid, holding the count for one word pair, is a parameter. A modern LLM has billions of them, and a frontier model on the order of a trillion. View in glossary in memory

“Training” a language model means counting patterns in text. Your hand-built model contains the same type of information that current LLMs store, at a vastly smaller scale.

Comparison to grid method

A ledger and a grid hold the same model:

  • a row of the ledger is the grid’s row for that word with the empty cells left out
  • a tally beside spot on the row for see is the tally in row see, column spot of the grid
  • the printed picture-book sets keep only each word’s four most common followers, so every word fits on one row of four boxes however long the book

Comparison to grid method

The cutouts spread and the grid method produce equivalent models:

  • a tally mark in row X, column Y of the grid corresponds to one cutout on the table whose previous word is X and whose next word is Y
  • both capture the same “what follows what” relationships
  • cutouts make the weighting tangible: some outcomes are more likely because there are more cutouts to pick from

Interactive widget

Step through the training process at your own pace. Enter your own text or use the example, then press Play or Step to watch the model being built.

Training text
Training text (tokenised)
thecatsatonthemat.
Model
Tokenthecatsatonmat.
the​​​​​​
cat​​​​​​
sat​​​​​​
on​​​​​​
mat​​​​​​
.​​​​​​

The training widget counts the same pairs into a grid: choose Grid above to step through it.

Step through the training process at your own pace. Enter your own text or use the example, then press Play or Step to watch the cutouts being placed.

Training text
Training text (tokenised)
thecatsatonthemat.
Model