Exercises — WordPiece and SentencePiece
Before we begin, three symbols we will lean on constantly — defined in plain words so nothing is assumed:
We will also reuse PMI, the pointwise mutual information of a pair:
Level 4 leans on two words from Expectation Maximization. We define them here so this page stands alone:
Level 1 — Recognition
Exercise 1.1
Which algorithm uses the marker ▁ (U+2581) for whitespace, and which uses the ## prefix for non-initial pieces? State each.
Recall Solution
▁is used by SentencePiece. It treats raw text as a stream, so it must encode spaces as a visible symbol to stay reversible.##is used by WordPiece. It marks a piece that is not at the start of a word (e.g.play+##ing), because WordPiece is fed already-space-split words and never crosses word boundaries. Answer: SentencePiece →▁; WordPiece →##.
Exercise 1.2
True or false: WordPiece merges the pair with the highest raw frequency. If false, state the true rule in one sentence.
Recall Solution
False. That is Byte-Pair Encoding (BPE)'s rule. WordPiece merges the pair with the largest likelihood gain — i.e. frequency weighted by PMI, which rewards pairs that co-occur more than chance.
Level 2 — Application
Exercise 2.1
Vocabulary: ["un", "##happ", "##i", "##ness", "happiness"].
Tokenize "unhappiness" with WordPiece greedy longest-match. Give the token list and its length.
Recall Solution
Greedy longest-match starts at position 0 and shrinks the candidate until it hits the vocabulary. WordPiece looks up a word-initial piece bare (no prefix) and a non-initial piece with a ## prefix.
"unhappiness"…"unh"— none in vocab."un"— found (word-initial). Consume it.- Remaining string is
"happiness", but we are now non-initial, so lookups carry##:"##happiness"— not in vocab. Shrink:"##happ"— found. Consume it. - Remaining
"iness":"##iness"…"##ines"…"##ine"…"##in"— none."##i"— found. Consume it. - Remaining
"ness":"##ness"— found. Consume it. Output:["un", "##happ", "##i", "##ness"], length 4. Note there is no barehappinessreachable here: once we cross the first piece, every later lookup must carry##, and##happinessis not in the vocabulary — so greedy longest-match assembles the word from the##-prefixed pieces instead.
Exercise 2.2
SentencePiece vocab: ["▁", "▁H", "ello", "▁w", "orld"].
Tokenize "Hello world" and then detokenize your output back to a string.
Recall Solution
Tokenize: replace each space with ▁ → "▁Hello▁world". Segment left-to-right:
▁H | ello | ▁w | orld → ["▁H", "ello", "▁w", "orld"].
Detokenize: concatenate → "▁Hello▁world" → replace ▁ with a space → "Hello world".
The round trip is exact — that reversibility is the whole point of the ▁ marker.
Exercise 2.3
Compute the PMI-weighted likelihood gain of merging the pair (un, happy) given
, , , .
Recall Solution
Inside: , so the ratio is . . Thus (using natural log). Positive and large → this pair clings together far above chance (ratio ) → a strong merge candidate.
Level 3 — Analysis
Exercise 3.1
A pair (the, cat) is very frequent () but , , . Compute and explain whether WordPiece merges it before the rarer pair from Ex 2.3.
Recall Solution
. Ratio .
. .
Numerically this beats Ex 2.3's , so on gain alone WordPiece would merge the cat first. The teaching point: the per-occurrence PMI () is far weaker than un+happy's () — the cat only wins because it is enormously frequent. This is exactly why WordPiece is not pure frequency and not pure PMI: it is count × PMI. Contrast with raw-frequency Byte-Pair Encoding (BPE), which would merge the cat even if its PMI were near zero.
Exercise 3.2
Explain, in terms of the Out-of-Vocabulary Problem, why word-level tokenization fails on "antidisestablishmentarianism" while subword succeeds. Reference Morphology and Compositionality.
Recall Solution
A word-level vocabulary is a fixed finite list. Any unseen word maps to a single [UNK] token, destroying all information — that is the OOV problem. Subword tokenization keeps a vocabulary of reusable pieces (anti, dis, establish, ment, arian, ism). Because English morphology is compositional — meaning is built by stacking morphemes — the model reconstructs a rare word from familiar parts and never emits [UNK] for spellable text. Coverage becomes effectively unbounded with a bounded vocabulary.
Level 4 — Synthesis
Exercise 4.1 (SentencePiece Unigram E-step)
Corpus: the single word "dogs", count 1.
Vocabulary probabilities: .
Score every segmentation, normalize, and give the posterior weight of [dog, s] and [dogs].
Recall Solution
Step 0 — enumerate every segmentation systematically. A segmentation is a choice of "cut points" between the 4 letters d o g s. There are 3 possible internal cuts (after d, after o, after g), so at most raw splits. We keep only those whose every piece is in the vocabulary. Walk them by number of pieces:
- 1 piece:
[dogs]— in vocab ✓ - 2 pieces:
[d,ogs]✗(ogsabsent),[do,gs]✓,[dog,s]✓ - 3 pieces:
[d,o,gs]✓,[d,og,s]✗(ogabsent),[do,g,s]✓ - 4 pieces:
[d,o,g,s]✓
Six segmentations survive: [dogs], [do,gs], [dog,s], [d,o,gs], [do,g,s], [d,o,g,s].
E-step scoring — hold the current fixed; each segmentation's score is the product of its token probabilities (unigram assumption):
[dog, s][dogs][do, gs][do, g, s][d, o, gs][d, o, g, s]
Normalizer .
- Posterior of
[dog, s] - Posterior of
[dogs]
The bar chart shows where the posterior mass lands: the two "sensible" splits [dog, s] and [dogs] between them hold about of the belief, while the letter-by-letter splits are nearly ignored — exactly the behaviour that lets the M-step drown out useless tokens. The horizontal bars are ordered by posterior weight, with each bar's numeric weight printed at its tip and a coral arrow flagging the two dominant, linguistically sensible segmentations.

[dog, s] and [dogs] dominate.
Exercise 4.2 (M-step follow-through)
Using the posteriors from Ex 4.1, compute the expected soft count of token s and of token dog. (The word count is 1, so soft counts are just summed posterior weights of segmentations containing the token.)
Recall Solution
Token dog appears only in segmentation [dog, s]:
Token s appears in [dog, s], [do, g, s], and [d, o, g, s]:
With : .
The M-step would then renormalize all soft counts into new . Tokens like do, gs collect almost no mass → they are prime pruning candidates in the next round.
Level 5 — Mastery
Exercise 5.1 (Derive the merge formula)
From the unigram log-likelihood , derive that merging pair changes the likelihood by . Show every case (including where the pair does not occur).
Recall Solution
Assumption (state it up front). During this single-merge analysis we treat , , and as fixed constants — the probabilities that already hold before we decide to merge. We are only asking "what does this one merge do to , everything else held still?" (In a full run these probabilities are re-estimated after each merge, but that is a separate step and does not affect this local comparison.) Setup. Only occurrences of the adjacent pair change. Each such occurrence currently contributes two independent terms . Before merge — summed over all occurrences: After merge — each becomes one token : Difference: Degenerate cases:
- If (pair never adjacent): — nothing to merge, correctly ignored.
- If are independent (): ratio , , gain — merging a chance co-occurrence buys nothing.
- If (anti-correlated): gain negative — WordPiece would never pick it.
Exercise 5.2 (Design decision)
You must tokenize a corpus mixing English and Chinese (no spaces in Chinese). Choose between WordPiece and SentencePiece-Unigram and justify with two concrete technical reasons.
Recall Solution
Choose SentencePiece-Unigram. Reasons:
- No pre-tokenization needed. WordPiece requires whitespace-split input; Chinese has no word delimiters, so a whitespace pre-tokenizer would treat entire sentences as one "word" and fail. SentencePiece consumes the raw byte/character stream directly and is language-agnostic.
- Reversibility via
▁. Because spaces are encoded as▁, detokenization is exact for the English half (leading/trailing spaces preserved), while Chinese — having no spaces — reconstructs perfectly too. This is the exact design used by T5 and mT5. WordPiece (used in BERT Architecture) drops precise spacing and assumes space-delimited words.
Recall Quick self-check (clozes)
The WordPiece merge criterion is count times ::: PMI (log P(ab)/(P(a)P(b))) SentencePiece encodes whitespace with the symbol ::: ▁ (U+2581) The SentencePiece Unigram E-step assigns counts that are ::: soft / probabilistic across all segmentations The gain of merging an independent pair is ::: zero (log 1 = 0)