Sub topic 7
WordPiece: choosing merges by a likelihood-style score
This chapter assumes basic Python, dictionaries, tuples, loops, and frequency counting. You should also know that a token is one input unit given to a language model.
Why WordPiece differs from BPE
WordPiece builds a vocabulary from smaller pieces, usually characters at the beginning. It repeatedly combines adjacent pieces. The important difference is how it chooses a combination:
- BPE chooses the most frequent adjacent pair.
- WordPiece chooses the pair with the strongest likelihood-style score.
- The score favors pairs that occur together often but whose parts are not already individually common.
A typical simplified score is:
\text{score}(a,b)=\frac{\text{count}(a,b)}
{\text{count}(a)\times\text{count}(b)}Here, count(a, b) counts how often the adjacent pair occurs. The denominator measures how common the two pieces are separately. A pair made from rare pieces can receive a high score when those pieces strongly associate.
Implementations differ in their exact probability estimates and vocabulary-building procedure. Treat this formula as a useful model of the decision, not as a promise that every library uses precisely these counts.
| Pair | Pair count | Part counts | Simplified score |
|---|---|---|---|
| `a` + `b` | 10 | 100 × 100 | 0.001 |
| `x` + `y` | 4 | 5 × 5 | 0.16 |
The second pair occurs less often, but its parts occur together much more strongly than chance would suggest.
A small training trace
Suppose the corpus contains these word representations:
low</w>
lower</w>
newest</w>The marker </w> records the end of a word. Initial symbols might be characters:
l o w </w>
l o w e r </w>
n e w e s t </w>The trainer counts adjacent pairs and evaluates their scores. If e and s are uncommon individually but frequently occur together in the corpus, e s may beat the more frequent pair l o.
After selecting a pair, the trainer replaces every matching adjacent occurrence with one new symbol. It then recalculates counts and scores because the vocabulary and neighboring pairs have changed.
This process continues until the target vocabulary size is reached or no useful merge remains.
Why the score matters
Frequency alone favors pieces that appear everywhere. WordPiece’s score asks a more selective question: which pair is unusually associated?
This can produce pieces that represent recurring linguistic units, such as ##ing or ##able, rather than only common letter pairs. The ## marker commonly indicates that a piece continues a word and cannot begin one.
WordPiece training and encoding are separate stages. Training learns the vocabulary. Encoding later applies that vocabulary, usually with greedy longest-match search. For example, with un, ##aff, and ##able, the word unaffable becomes:
un ##aff ##ableIf no valid piece matches a position, many WordPiece tokenisers return [UNK] for the entire word.
Practical limits
The score depends on the training corpus. A domain-specific corpus can create useful pieces for medical terms or programming identifiers, while a narrow corpus may leave ordinary text fragmented. The target vocabulary size also matters. A small vocabulary saves model parameters but produces longer sequences. A large vocabulary shortens common words but costs more memory and may waste entries on rare forms.
Exercise
Why might WordPiece select a less frequent pair instead of the most frequent pair?
Show answer
Its likelihood-style score also considers how common the two parts are separately, so a less frequent pair can win when its parts are unusually associated.