# Training

> Build a bigram language model that tracks which words follow which other words in text.

<VariantToggle client:load options={["grid", "ledger", "cutouts"]} />

Build a <GlossaryTerm id="bigram-model">bigram language model</GlossaryTerm> that tracks which words follow which other words
in text.

<Variant is="grid">

![](../../assets/images/hero-grid-training.avif)

</Variant>

<Variant is="ledger">

![](../../decks/assets/bg-tally-training.avif)

</Variant>

<Variant is="cutouts">

![](../../assets/images/hero-cutouts-training.avif)

</Variant>

## You will need

<Variant is="grid">

- some text (e.g. a few pages from a kids book, but it can be anything)
- pen or pencil
- a printed [grid template](https://pdf.llmsunplugged.org/worksheets/grid.pdf) (or any grid
  paper)

</Variant>

<Variant is="ledger">

For each group of four or five:

- a short text to train on, printed
- blank [ledger sheets](https://www.llmsunplugged.org/tools/#ledgers), one per person
- a pen each

The [How AI writes stories](https://www.llmsunplugged.org/lessons/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.

</Variant>

<Variant is="cutouts">

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

<Callout type="tip" title="Pre-made token cutouts">

The [Materials](https://www.llmsunplugged.org/tools/#cutouts) page has ready-to-print token cutouts for several
texts, or you can [generate your own](https://www.llmsunplugged.org/tools/#generate-your-own) from any text.
Each PDF starts with a student-facing instructions page (anatomy of a cutout,
worked example, tips), so you can hand the printed pack straight to a group.
The CLI also supports a `--duplex` flag that prints the same cards on both
faces, so students never have to flip a face-down card.

</Callout>

</Variant>

## Your goal

<Variant is="grid">

Produce a <GlossaryTerm id="grid">grid</GlossaryTerm> 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.

</Variant>

<Variant is="ledger">

Fill a set of <GlossaryTerm id="ledger">ledger sheets</GlossaryTerm> 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.

</Variant>

<Variant is="cutouts">

Spread your printed <GlossaryTerm id="cutouts">token cutouts</GlossaryTerm> 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.

</Variant>

## Key idea

<Variant is="grid">

Language models learn by counting patterns in text. <GlossaryTerm id="training">Training</GlossaryTerm> means building a
model (filling out the grid) to track which words follow other words.

</Variant>

<Variant is="ledger">

Language models learn by counting patterns in text. On a ledger, <GlossaryTerm id="training">training</GlossaryTerm> means giving every word a row, and tallying beside it each word that came next in the text.

</Variant>

<Variant is="cutouts">

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.

</Variant>

## Algorithm

<Variant is="grid">

1. **Preprocess your text**
   - convert everything to lowercase
   - treat words and single punctuation marks (`.` `,` `!` `?` `;` `:`) as
     separate "<GlossaryTerm id="token">words</GlossaryTerm>"; 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

</Variant>

<Variant is="ledger">

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

</Variant>

<Variant is="cutouts">

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.

</Variant>

## Example

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

<Variant is="grid">

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:

<LmGrid tokens="see spot" vocabulary="see spot run . jump ," />

After the full text the model looks like:

<LmGrid tokens="see spot run . see spot jump . run , spot , run . jump , spot , jump ." />

</Variant>

<Variant is="ledger">

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:

<LmLedger tokens="see spot" />

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

<LmLedger tokens="see spot run . see spot jump . run , spot , run . jump , spot , jump ." />

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`.

</Variant>

<Variant is="cutouts">

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.

<LmCutouts tokens="see spot run . see spot jump . run , spot , run . jump , spot , jump ." />

</Variant>

<Variant is="cutouts">

## 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.

</Variant>

## 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 <GlossaryTerm id="llm">Large Language Models</GlossaryTerm> (e.g. ChatGPT, Claude) work? What's the _weirdest_ explanation you've ever heard?
- when was the first <GlossaryTerm id="language-model">language model</GlossaryTerm> 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

<Variant is="grid">

- 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?

</Variant>

<Variant is="ledger">

- 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?

</Variant>

<Variant is="cutouts">

- 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 <GlossaryTerm id="token">tokens</GlossaryTerm> 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?

</Variant>

### Troubleshooting

<Variant is="grid">

- **"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.

</Variant>

<Variant is="ledger">

- **"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.

</Variant>

<Variant is="cutouts">

- **"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.

</Variant>

### Connection to current LLMs

This counting process is exactly what happens during the "<GlossaryTerm id="training">training</GlossaryTerm>" phase of
language models:

- **<GlossaryTerm id="training-data">training data</GlossaryTerm>**: 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 <GlossaryTerm id="parameters">parameters</GlossaryTerm> 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.

<Variant is="ledger">

### 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

</Variant>

<Variant is="cutouts">

### 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

</Variant>

## Interactive widget

<Variant is="grid">

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.

<TrainingWidget client:visible />

</Variant>

<Variant is="ledger">

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

</Variant>

<Variant is="cutouts">

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.

<CutoutsTrainingWidget client:visible />

</Variant>

Source: <https://www.llmsunplugged.org/modules/training/>\
Licence: [CC-BY-SA-4.0](https://creativecommons.org/licenses/by-sa/4.0/)\
Cite as: Swift, B. (2026). _LLMs Unplugged: Understand how AI language models work by building one yourself._ https://doi.org/10.5281/zenodo.17403824
