Same topic, related formats. Practice these next.
Same topic, related formats. Practice these next.
Compare the Unigram Language Model tokenizer (used in SentencePiece) to BPE in terms of: (1) training direction (additive vs. subtractive), (2) the optimization objective, and (3) the key capability that Unigram's probabilistic output enables during model training.
BPE merges up from characters by frequency; Unigram LM prunes down from a large vocabulary by corpus likelihood, and its probabilistic output enables stochastic tokenization as training-time augmentation.
Imagine two ways to settle on a useful set of word-pieces. One way starts with single letters and keeps gluing the most popular pair together, like 'th' then 'the'. The other starts with a huge pile of every chunk imaginable and keeps asking, which chunk would I miss the least, then throws it out, until the pile is the right size. The second way, Unigram LM, has a bonus. It remembers that a word can be cut in several valid ways and even how likely each cut is. During training it uses different cuts on different passes, which teaches the model to handle the same word no matter how it is sliced. The first way always cuts the same way every time.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
5 min: additive vs subtractive + likelihood objective over segmentations + E-step forward-backward + M-step prune + stochastic tokenization + alpha and inference determinism.
import sentencepiece as spm
sp = spm.SentencePieceProcessor()
sp.load("llama_tokenizer.model") # a Unigram model
text = "tokenization"
# Deterministic Viterbi path: same result every call
print("viterbi:", sp.encode(text, out_type=str))
# Stochastic sampling: different valid splits across calls
for _ in range(4):
print("sample:", sp.encode(text, out_type=str,
enable_sampling=True, alpha=0.2, nbest_size=-1))Real products, models, and research that use this idea.
What an interviewer would ask next. Try answering before peeking at the approach.
Red flags and common mistakes that signal junior thinking. Click to expand.
Reversing the directions. BPE is additive and merges up; Unigram LM is subtractive and prunes down. They reach the same vocabulary size from opposite ends.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.