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.
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.
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.
Collect each different token once. That list is the vocabulary: 10 tokens here.
Vocabulary size V = 10.
From now on the model never sees letters, only these numbers.
Token IDs, length 4: [9, 3, 8, 2] → target 7.
Every token in the text is a “guess what comes next” question. We follow question 4.
This is next-token prediction.
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.
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.
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).