Natural Language Processing · free preview
Subword Tokenization with Byte-Pair Encoding
Why vocabularies settle between words and characters, how Unicode text becomes bytes, and how byte-pair encoding learns merges and encodes new words.
1. Subword Tokenization with Byte-Pair Encoding
Subword Tokenization with Byte-Pair Encoding
2. BPE learns merges from counted pairs
BPE learns merges from counted pairs
3. Training and applying a BPE tokenizer
Training and applying a BPE tokenizer
4. UTF-8 byte length
How many bytes does UTF-8 use to encode the single character 日?
5. Encoding an unseen word
With the same trained merge list ['s+_', 't+i', 'ti+d', 'tid+e', 'h+a'], encode the word "harnt", which never appeared in the training corpus.
6. The first BPE merge
A BPE trainer runs on this word-frequency corpus (each word ends with the end-of-word marker _), hared (x2), hars (x4), tideed (x2), tides (x6).
Which adjacent symbol pair is merged first?
7. Effect of the first merge
Starting from the same corpus (hared (x2), hars (x4), tideed (x2), tides (x6), with the end-of-word marker _), the trainer performs the first merge s+_. After this merge, across all words in the corpus, in how many token positions does the new symbol s_ occur? (Weight each word by its frequency.)
8. Encoding after early merges
BPE is trained on (hared (x2), hars (x4), tideed (x2), tides (x6), end-of-word marker _) for exactly 5 merges, producing the ordered merge list ['s+_', 't+i', 'ti+d', 'tid+e', 'h+a'].
Encoding replays the merges in order. Write the resulting tokenization of the word "tides" as symbols joined with + (include the end-of-word marker _ as its own symbol if it stands alone).
Practice this interactively
Free account · instant grading · spaced review that schedules itself.
Start this course for free