Foundations to Frontier
Tests

Character, Word and Subword Tokenization

A language model reads tokens, not text, and the choice of token sets a basic trade-off. Word tokens give short sequences but a huge vocabulary that still misses new words (out-of-vocabulary, OOV). Character tokens never miss a word but make sequences several times longer, which is expensive for attention and makes each step carry less meaning. Subword tokens (BPE, WordPiece, Unigram) sit in between: frequent words stay whole, rare words split into reusable pieces, and nothing is unknown. This page covers the three granularities, OOV handling, fertility (tokens per word), vocabulary size versus sequence length, and their costs in embeddings and attention. In the exercises you write the three tokenisers, then compare them on text built from stems and suffixes. Even the full 2224-word training vocabulary missed 4.1% of test words, and a 500-word vocabulary missed 20%. Characters needed 7.0 tokens per word. BPE with 1500 merges had no unknown tokens at 1.13 tokens per word, and split unseen words into about two pieces.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Choosing the pieces a model reads

A model can read text letter by letter, word by word, or in pieces in between. Whole words are efficient, but no dictionary has every word: new names, typos and words like “unlaunchable” will be missing. Letters never go missing, but sentences become very long. Subword pieces such as “launch” + “able” get the best of both: common words stay whole, and rare words are spelled from familiar parts. The demo counts how many tokens one sentence needs under each choice.

Language lab
"the newest rockets flew lower": 29 characters, 5 words, 20 subword tokens after 6 BPE merges (t·h·e ne·we·st r·o·c·k·e·t·s f·l·e·w lo·we·r). Subwords sit between: no unknown words, shorter than characters.
Why engineers care

Tokenisation decides what a model can even see: part numbers, call signs, chemical formulas and code identifiers are often split into odd pieces, which affects accuracy and the cost of running the model.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Word-level tokenisers struggle with…
  2. 2. Character-level tokenisation makes sequences…
  3. 3. Subword tokenisers split rare words into…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Word level: vocabulary = the V most frequent training words; everything else becomes <unk>. Zipf’s law means a long tail of rare words, so OOV never reaches zero.
  • Character (or byte) level: tiny vocabulary, no OOV, but about 5–7 tokens per English word. Self-attention cost grows with the square of length, so 6× longer sequences cost about 36× more attention.
  • Subword level: a vocabulary of 30k–250k pieces learned from data. Frequent words are single tokens, rare words split, e.g. “unlaunchable” → “un”, “launch”, “able”.
  • Fertility: tokens per word, a measure of efficiency (about 1.3 for English with GPT-style tokenisers, often 2–4 for under-represented languages).
  • Costs: embedding and output layers scale with vocabulary size (50k × 768 = 38.4M parameters); sequence length drives compute and context usage.
📝 Quick check (Level 1)
0/3 answered
  1. 1. OOV rate with one unknown token in four:
  2. 2. Attention cost of 6× longer sequences is about…
  3. 3. Embedding parameters for 50,000 tokens × 768 dims:

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Tokenisers in code

Exercise 1.

  • Implement character and word tokenisers.
  • Implement greedy longest-match subword segmentation, which never produces an unknown token.

Practice.

  • Use the tokenizer that ships with a pretrained model (AutoTokenizer.from_pretrained), never a different one.
  • Train your own with Hugging Face tokenizers or SentencePiece when building a model from scratch or for a very different domain.
  • Measure fertility on your own data; domain terms (part numbers, formulas) can split badly.
  • Count costs in tokens, not words or characters: context limits and API prices are per token.
from transformers import AutoTokenizer
tok = AutoTokenizer.from_pretrained("gpt2")
ids = tok("The rocket was unlaunchable.")["input_ids"]
print(len(ids), tok.convert_ids_to_tokens(ids))   # subword pieces, 'Ġ' marks a leading space
🐍 Exercise 1: three tokenisers
Implement character, word and greedy longest-match subword tokenisers.
📝 Quick check (Level 2)
0/3 answered
  1. 1. With a pretrained model, use…
  2. 2. Context limits and prices are counted in…
  3. 3. A library for training tokenizers:

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

The trade-off. For a corpus of N characters, total tokens fall as the vocabulary grows, while the vocabulary-dependent parameters and the sparsity of rare tokens grow. Subword vocabularies of 30k–100k are a compromise found empirically to work well.

Why subwords generalise. Languages build words from reusable parts (stems, affixes, compounds). A subword vocabulary learned by compression (BPE) or likelihood (Unigram) tends to recover such parts, so an unseen word is composed from pieces the model has learned. This is the open-vocabulary property Sennrich and colleagues introduced for translation in 2016.

Byte fallback. Even subword vocabularies can meet unseen characters (new scripts, emoji). Byte-level tokenisers (F2-4) start from the 256 bytes, so every string is representable.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Larger vocabularies mean…
  2. 2. Subwords generalise because words are built from…
  3. 3. Open-vocabulary subword translation was introduced by…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

How do word, character and subword vocabularies trade coverage against length?

History.

  • R. Sennrich, B. Haddow and A. Birch, “Neural machine translation of rare words with subword units” (ACL 2016).
  • Y. Wu et al., “Google’s neural machine translation system” (2016), with WordPiece.

Exercise 2.

  • Data: words formed from about 400 stems (Zipfian) plus one of 10 suffixes (−, s, ed, ing, er, ers, ly, ness, able, less); 20,000 training tokens (2224 word types) and 5000 test tokens.
  • Tokenisers:
    • word level, keeping the 500, 2000 or all training words;
    • character level;
    • BPE with 50, 300 or 1500 merges.
tokeniser vocabulary test OOV rate tokens per word tokens per unseen word
words, top 500 500 0.201 1.00 —
words, top 2000 2000 0.055 1.00 —
words, all 2224 0.041 1.00 —
characters (+ end marker) 22 0 7.00 7.00
BPE, 50 merges 72 0 2.93 5.00
BPE, 300 merges 322 0 1.66 3.15
BPE, 1500 merges 1522 0 1.13 2.07
  • Word vocabularies never close the gap: even keeping every training word leaves 4.1% of test words unknown, because rare stem–suffix combinations keep appearing.
  • Characters cover everything but cost 7 tokens per word, about 38× more attention than BPE with 1500 merges.
  • BPE gets both: with a vocabulary smaller than the word list it has no OOV and only 1.13 tokens per word. Unseen words split into about two pieces, which is what stem + suffix would give.
  • Seeds: seed 1 gave the same pattern (OOV 0.040 for all words, 1.14 tokens per word for BPE with 1500 merges).
🐍 Exercise 2 (research): how do word, character and subword vocabularies trade coverage against length?
Compare word vocabularies of several sizes, characters and BPE with several merge counts on held-out text: OOV rate and tokens per word.

Tokenisation in AI, robotics and aerospace

  • Technical text: checking how part numbers, units and call signs are split before fine-tuning a model on maintenance or ATC data.
  • Multilingual operations: fertility differs by language, which affects cost and context use.
  • Design lesson: measure tokens per word on your own data, and remember that every token costs compute.

References

  1. R. Sennrich, B. Haddow and A. Birch (2016). “Neural machine translation of rare words with subword units.” Proc. ACL.
  2. Y. Wu et al. (2016). “Google’s neural machine translation system: bridging the gap between human and machine translation.” arXiv:1609.08144.
  3. S. J. Mielke et al. (2021). “Between words and characters: a brief history of open-vocabulary modeling and tokenization in NLP.” arXiv:2112.10508.