1

From text to tokens

Clean the raw text, chop it into tokens, and give every distinct token an ID number.

Hover or tap a token to see it light up everywhere it appears. Tap empty space to reset.

1Our corpus: four messy sentences
Module 1, step 1: Our corpus: four messy sentences

This tiny text is the only data in the whole course. Module 6 trains on it and module 7 tests on it. Cleaning first makes “The”, “the” and “WHERE”/“where” the same.

Also called normalisation.

2Chop it into tokens
Module 1, step 2: Chop it into tokens

A token is one piece the model reads. Here a token is a word or a punctuation mark.

Also called tokenization. This toy one splits by words.

3Give every distinct token an ID
Module 1, step 3: Give every distinct token an ID

Collect each different token once. That list is the vocabulary: 10 tokens here.

Vocabulary size V = 10.

4Our sentence as ID numbers
Module 1, step 4: Our sentence as ID numbers

From now on the model never sees letters, only these numbers.

Token IDs, length 4: [9, 3, 8, 2] → target 7.

5One sentence, five questions
Module 1, step 5: One sentence, five questions

Every token in the text is a “guess what comes next” question. We follow question 4.

This is next-token prediction.

6Real models: pieces of words
Module 1, step 6: Real models: pieces of words

Splitting isn't a fixed rule like “every 4 letters”. It is learned from text: start from single letters (or bytes), merge the most frequent neighbouring pair into a new token, and repeat until the vocabulary reaches a chosen size. The four merges above are exactly that, run on our corpus.

Merges computed on our four sentences. Method: byte-pair encoding (BPE). The learned merge list is the tokenizer.

7One word, more than one token
Module 1, step 7: One word, more than one token

Pieces that often appear together earn their own token, so a word can be one token or several. “playing” becomes “play” + “ing”.

These are sub-word tokens. Each piece gets its own ID, just like the words in step 4.

Training doesn't care what a token is Whole words or word pieces, the model always does the same job: guess the ID of the next token. The loss in module 6 is the same too: −log p(the true next token), also called cross-entropy.

Caveat: the formula is identical, but loss numbers from different tokenizers aren't directly comparable, because they count different units. A word split into 3 pieces is 3 predictions; as one whole-word token it is 1. So “average loss per token” measures something different for each tokenizer. To compare, divide the total loss over the same text by its number of characters or bytes (e.g. bits per byte).

Our text is now numbers: “where did the cat” → [9, 3, 8, 2], and the right answer “sat” is 7. The sentence isn't perfect English. That's fine: a model learns whatever text it's fed.