WordPiece › A Worked Example: Tracing the Training Algorithm
Language AI HandbookAll chapters WordPiece › A Worked Example: Tracing the Training AlgorithmOne section · Sections in this chapter5 / 13 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Let us work through a minimal numerical example to make the training algorithm concrete. We will use a tiny corpus and small vocabulary to trace exactly how WordPiece scores differ from BPE scores.
Suppose our training corpus contains four words with the following frequencies:
| Word | Count |
|---|---|
| low | 5 |
| lower | 2 |
| newest | 6 |
| wider | 3 |
Step 1: Initialization. We split each word into characters with ## prefixes:
| Word | Initial split |
|---|---|
| low | l, ##o, ##w |
| lower | l, ##o, ##w, ##e, ##r |
| newest | n, ##e, ##w, ##e, ##s, ##t |
| wider | w, ##i, ##d, ##e, ##r |
Step 2: Compute token counts. Adding up occurrences across the corpus (multiplied by word frequencies):
l: 5 + 2 = 7 (from "low" and "lower")##o: 5 + 2 = 7##w: 5 + 2 + 6 = 13 (from "low", "lower", "newest")##e: 2 + 6 + 6 + 3 = 17 (from "lower": 1, "newest": 2, "wider": 1)##r: 2 + 3 = 5n: 6##s: 6##t: 6w: 3 (from "wider")##i: 3##d: 3
Total token count: 7 + 7 + 13 + 17 + 5 + 6 + 6 + 6 + 3 + 3 + 3 = 76
Step 3: Score the candidate pairs. For the pair (##e, ##r), which appears in "lower" (count 2) and "wider" (count 3), making 5 occurrences:
Now compare this to the pair (##e, ##w) in "newest" (count 6 but ##e appears twice in "newest", specifically the first ##e precedes ##w):
where .
Even though ##e followed by ##w appears 6 times (more often than ##e followed by ##r), the WordPiece score favors ##er because ##r is much less common than ##w. When ##e and ##r appear together, it is more informative: the relatively rare ##r is almost exclusively found after ##e. A BPE tokenizer would have chosen (##e, ##w) at this step because 6 > 5. WordPiece chooses (##e, ##r) because the PMI score is higher.
Step 4: After the merge. We add ##er to the vocabulary. The words "lower" and "wider" now have splits l ##o ##w ##er and w ##i ##d ##er. We recompute all scores and continue until we reach our target vocabulary size.
This example illustrates the core intuition: WordPiece prefers merges where the pair is mutually informative, not just frequently co-occurring in absolute terms.