Build a tiny tokenizer
Before a model can use token IDs, a tokenizer learns a vocabulary from a text corpus. Change each step below, then train a small BPE example to see what it learns.
The four models in Token Lab already have trained tokenizers and model embeddings. This separate classroom example first builds a simplified, character-based BPE tokenizer, then lets you train a tiny model for its local token IDs.
1 Text corpus
A corpus is the collection of examples the tokenizer learns from. Repetition helps it discover useful pieces.
62 characters of your own text · maximum 4,0002 Normalize
Decide whether forms such as “Cat” and “cat” should be treated alike before learning vocabulary.
The cat sat on the mat. The cat wore a hat. The cat saw a rat.
3 Pre-tokenize
The pre-tokenizer chooses chunks that BPE cannot merge across. The GPT-2-style option attaches a leading space to many chunks. Spaces appear as ␠, and line breaks as ↵. This demo does not reproduce GPT-2’s byte conversion.
The␠cat␠sat␠on␠the␠mat.↵The␠cat␠wore␠a␠hat.↵The␠cat␠saw␠a␠rat.4 Target vocabulary size
The starting vocabulary needs one ID for each character in this corpus, plus an unknown-character ID. The target allows additional pieces learned through merges.
<unk>↵␠.acehmnorstTw5 Train BPE
The trainer counts adjacent pairs inside each chunk, merges the most frequent pair, and repeats until it reaches the target or runs out of repeated pairs.
What the tokenizer learned
20 pieces · 4 learned merges
The target vocabulary size was reached.
Merge history
| Step | Pair | New piece | Count |
|---|---|---|---|
| 1 | a + t | at | 7 |
| 2 | h + e | he | 4 |
| 3 | ␠ + c | ␠c | 3 |
| 4 | ␠c + at | ␠cat | 3 |
Final vocabulary
<unk>↵␠.acehmnorstTwathe