Sub topic 9
The Unigram Language Model Tokeniser
This chapter assumes that you know what tokens and vocabularies are, and that you can follow basic frequency calculations and Python loops. You should also understand that a word can be split into several subword pieces.
The basic idea
A unigram language model tokeniser starts with a large collection of possible pieces. Each piece receives a probability, such as:
lowlow erlowererestnewnewest
The model chooses a segmentation, which is a sequence of pieces covering the input. Unlike BPE, it does not build one fixed sequence of merges. Unlike WordPiece, it does not simply select the longest matching piece.
The probability of a segmentation is usually the product of its piece probabilities:
P(pieces) = P(piece1) × P(piece2) × ...
The preferred segmentation is the one with the greatest probability. In practice, systems use log probabilities, so multiplication becomes addition.
A useful distinction is that “unigram” describes the probability model for pieces. It does not mean that the input must contain single-character tokens.
Training with expectation-maximisation
Training usually begins with a seed vocabulary containing many substrings. The algorithm repeatedly performs two broad steps:
- Expectation: Estimate how often each possible piece appears across likely segmentations of the training text.
- Maximisation: Update each piece's probability from those expected counts.
The model then removes pieces that contribute little to the corpus likelihood. It repeats this process until the vocabulary reaches the requested size.
The training objective considers multiple possible segmentations instead of committing immediately to one. This makes the method more flexible when several splits are plausible.
Encoding with dynamic programming
For new text, the encoder searches for the best path through the characters. At each position, it considers every vocabulary piece that starts there. It stores the best score for each prefix and extends those scores when a valid piece is found.
This method is called dynamic programming because it reuses the best results for shorter prefixes instead of recalculating them. If the text is unaffable, the encoder might compare:
un+aff+ableun+affableuna+ffable
It returns the valid path with the highest total log probability.
A simplified scoring recurrence is:
best[end] = max(best[start] + log P(piece))
where piece covers the text from start to end.
Deterministic and sampled segmentation
The usual encoder returns the highest-probability segmentation. Some unigram tokenisers can also sample segmentations according to their probabilities. Sampling exposes different valid splits of the same text during training.
For example, a model might sometimes represent a word as one common piece and sometimes as two less common pieces. This can act as a form of data augmentation, although it also makes training less deterministic.
Comparison with BPE
| Property | BPE | Unigram model |
|---|---|---|
| Vocabulary construction | Adds frequent pair merges | Starts large and prunes pieces |
| Encoding | Applies an ordered merge list | Searches over candidate segmentations |
| Alternatives | Usually one fixed result | Can support sampling |
| Main trade-off | Simple and fast | More flexible but more expensive |
A unigram tokeniser still needs a fallback strategy for text that has no matching piece. Character or byte pieces can provide coverage. SentencePiece commonly uses this approach to train unigram or BPE models directly from raw text.
Exercise
Why does a unigram tokeniser keep several possible segmentations during training instead of selecting one immediately?
Show answer
Because the training text may support several plausible splits. Estimating their expected contributions lets the model learn probabilities before pruning weaker pieces.