The procedure in the original paper (Section 3 of Schuster and Nakajima) is as follows.
- Initialize the vocabulary with the basic characters.
- For Japanese, the original paper starts with about 22,000 characters, covering kanji, hiragana, katakana, and ASCII (about 11,000 for Korean).
- Segment the training data with this vocabulary and build a language model.
- Among the candidate new symbols formed by combining two symbols in the current vocabulary, choose the one that most increases the likelihood of the training data when added to the vocabulary, and add it.
- Repeat steps 2–3 until the vocabulary reaches the chosen size or the likelihood gain falls below a chosen threshold.
Rebuilding the language model for every candidate costs O(K2) per round (K is the current vocabulary size). The original paper builds a 200,000-word vocabulary in a few hours on a single machine with techniques such as testing only pairs that actually occur in the training data, merging pairs that do not affect one another in a batch, and recounting only the affected counts.
Which Pairs Increase the Likelihood?
Choosing by language-model likelihood tends to select pairs that are strongly associated with each other, not merely frequent pairs. For example, the Japanese characters を (an object particle) and 学 ("learn") both occur often, so they also happen to be adjacent many times. Merging them into one symbol does little to improve the explanation of the text's probability. Conversely, if 学 is almost always followed by 習, making 学習 (gakushū, "learning") a single symbol explains the text much better with far fewer symbols.
Google has not released its WordPiece training program. Hugging Face's explanation presents, as an approximation of the likelihood gain, the approach of merging the pair that maximizes the following score.
score(a,b)=(count of symbol a)×(count of symbol b)count of pair ab
The denominator discounts for how frequent each symbol already is, so "pairs that often occur together" are chosen over "pairs of frequent symbols that happen to be adjacent."
Example: Five Merges on a Small Training Set
The left side of the figure at the top of the article splits the training data into characters and performs five merges with this score. The training words are Japanese: 機械学習 (kikai gakushū, "machine learning") 4 times, 機械 ("machine") 3 times, 深層 ("deep") 5 times, 学ぶ ("learn") 6 times, and を (object particle) 10 times. The example is kept in Japanese because the counts and scores below were computed from it. As in BERT, symbols from the second character of a word onward are prefixed with ##.
| Step | Pair merged | Pair count | Score |
|---|
| 1 | ##学 + ##習 | 4 | 4 ÷ (4 × 4) = 1/4 |
| 2 | 深 + ##層 | 5 | 5 ÷ (5 × 5) = 1/5 |
| 3 | 学 + ##ぶ | 6 | 6 ÷ (6 × 6) = 1/6 |
| 4 | 機 + ##械 | 7 | 7 ÷ (7 × 7) = 1/7 |
| 5 | 機械 + ##学習 | 4 | 4 ÷ (7 × 4) = 1/7 |
In step 1, the most frequent pair is "機 + ##械" (7 times), which BPE would merge first. However, "機" and "##械" each occur 7 times, so the score stays at 1/7. "##学 + ##習" occurs only 4 times, but "##学" and "##習" always occur together, so its score of 1/4 is the highest. In step 4, "機 + ##械" and "##械 + ##学習" are tied at 1/7, and the pair counted first is chosen.
BPE saves "the merge rules and their order," whereas BERT's WordPiece saves only the finished vocabulary. Splitting new text uses only the vocabulary, not the order of merges.