Notes

Unigram Language Model Tokenization

Most subword tokenizers are built bottom-up: start from characters and glue the frequent pairs together. Unigram tokenisation runs the other way — begin with far too many candidate pieces, then throw away the ones the data can manage without.

Pruning instead of merging
The method treats segmentation as a probabilistic model: every subword carries a probability, and a candidate split of a word scores as the product of its pieces — a unigram language model over subwords. Training starts from a large seed vocabulary and loops:

  • estimate the piece probabilities with EM, treating each word's segmentation as hidden;
  • score every piece by how much corpus likelihood would be lost if it were removed;
  • drop the least useful fraction, then repeat until the target vocabulary size is reached.

Encoding a new word then uses Viterbi search over the lattice of possible splits, not a fixed replay of merge rules.

What this buys over BPE
Every word has many scored segmentations, so the tokenizer can sample among them instead of always taking the best. That is subword regularisation, which acts as data augmentation and makes translation models markedly more robust to spelling noise and rare words. The scores also make the vocabulary's cost explicit — you can see which pieces earn their place. It is the default algorithm in SentencePiece, and so the tokenizer behind T5, ALBERT and XLNet, where it consumes raw text with spaces marked and needs no separate pre-tokeniser.

The trade-off
Training is heavier than counting merges, and encoding needs a lattice search rather than a lookup, so each sentence costs more to process. In exchange the vocabulary is chosen by an explicit objective rather than by greedy frequency, and every string in an open vocabulary still gets a principled, scored segmentation.

Unigram language model tokenization builds a subword vocabulary by pruning rather than merging: it starts from a large candidate set, estimates each piece's probability, and repeatedly discards the pieces whose removal costs the least corpus likelihood. Encoding picks the highest-probability segmentation by Viterbi search, so a word has many scored splits — which makes subword regularisation by sampling possible. It is SentencePiece's default and underlies the tokenizers of T5, ALBERT and XLNet.

Imagine assembling a box of rubber stamps for printing any word you might ever need. One approach starts with single letters and glues the popular pairs together. This one does the opposite: it begins with a huge pile of candidate stamps and keeps throwing out the ones it can manage without, checking after each cull that it can still print real text neatly.

What survives is a compact set of word pieces, each with a score showing how likely it is. Because a word can be stamped out in several different ways, the software can choose the most likely one — or deliberately vary it while learning, which helps it cope with odd spellings and words it has never met before.