The corpus word counts are: hug , pug , pun , bun , hugs . Starting from single characters, what are the first three merges of byte-pair encoding (BPE), done by hand? How are the words "bugs" and "mug" then tokenised?
Sequence models and transformers, Tokenisation
Level L2 Elementary. Length Medium. Nature Mat Mathematical.
What's being asked
Whether you can count adjacent pairs weighted by word frequency, recount after each merge, and apply the learned merges to new words.
Key idea
BPE builds a vocabulary by repeatedly merging the most frequent adjacent pair of symbols. A pair's count is the sum of the counts of the words it appears in. New words are tokenised by applying the learned merges in the order they were learned.
Steps
- Initial vocabulary: . Words: h u g (10), p u g (5), p u n (12), b u n (4), h u g s (5).
- Pair counts: (h,u) ; (u,g) ; (p,u) ; (u,n) ; (b,u) ; (g,s) . Merge 1: (u,g) "ug".
- Recount, because the merge changes the neighbouring pairs: (h,ug) ; (p,ug) ; (p,u) ; (u,n) ; (b,u) ; (ug,s) . Merge 2: (u,n) "un".
- Recount: (h,ug) ; (p,un) ; (p,ug) ; (ug,s) ; (b,un) . Merge 3: (h,ug) "hug". The vocabulary now also contains ug, un and hug.
- Tokenise "bugs": b u g s (merge 1) b ug s; merges 2 and 3 do not apply. Result: [b, ug, s].
- Tokenise "mug": m is not in the base vocabulary, so the result is [UNK, ug], where UNK is the unknown token. Byte-level BPE (as in GPT-2) avoids unknown tokens by starting from the 256 byte values.
Common mistakes
- Counting each pair once per distinct word, instead of weighting by word frequency.
- Not recounting after a merge. Pairs that overlap the merged one change.
- Tokenising new words by greedy longest match against the vocabulary, instead of replaying the merges in order. The two can differ.
How long your answer should be
Half a page; 4–5 minutes.
Last checked on 4 December 2026.
Read this
Previous in Sequence models and transformers: A language model assigns probabilities , , and to the four tokens of a test sentence (each conditioned on the tokens before it). What is the perplexity? What is the perplexity of a model that is uniform over a vocabulary of size ?
Next in Sequence models and transformers: What are the two gates of a GRU and what does each control? How is a GRU different from an LSTM?