Tokenisation and byte pair encoding: how text is cut into pieces
10 questions on the weaknesses of word and character dictionaries, sub-word tokens, byte pair encoding merges and why the vocabulary size is fixed in advance.
- 10 questions
- Stopwatch
- Instant feedback
Sample questions
Play now ↑Answers stay hidden. Start the quiz to play them.
Sample 1
A tiny text reads 'peck peck peck pea'. Which adjacent pair of characters is the most frequent, and so the first to be merged?
- 'pe', which occurs 4 times
- 'ck', which occurs 3 times
- 'ea', which occurs once
How it works
- One question per screen. An answer is final once sent.
- You see right away whether you were right, with a short explanation.
- The stopwatch runs: on equal scores, the faster player ranks first.
- Only your best attempt counts, so replaying is never risky.
About this quiz
Before a model can read text, someone has to decide what the basic units are. Words seem the natural choice, but a fixed list of words stumbles over names, typos, new coinages, punctuation and computer code. Characters never stumble, but they bury the structure of words and make every sentence very long. The solution used by modern language models is in between: break text into tokens, small chunks that may be whole common words, pieces of rarer words or single characters. This quiz explains why that compromise works and how a popular recipe, byte pair encoding, builds the set of tokens from data.
You will start with the weaknesses of the two obvious options. A dictionary of words cannot cope with anything outside it. A dictionary of characters forces the network to learn to assemble words, and it needs far more sequential steps for the same text. You will then see how sub-word tokens give the best of both, with common words kept whole and rare ones assembled from pieces, and how related forms of a word can share a piece. The same idea lets one system handle program code and, with a suitable scheme, other kinds of data.
The core of the quiz is the merging procedure. You start with a vocabulary of single characters, look through a body of text for the most frequent adjacent pair of tokens, and make that pair a new token, then repeat. You will carry out a step on a tiny made-up text, see the rule that stops words from being glued together, and think about when the process stops. The quiz asks what the vocabulary looks like at the start and as merges accumulate, and why its final size is chosen beforehand.
There are several common misconceptions. Tokens are not the same as words, and a token is often only part of a word. The set of tokens is learned from a body of text and then fixed. And the process is a pre-processing step that comes before the network sees any data, not something the network learns during training.
The quiz suits learners who have met the term token and want to know where tokens come from, and practitioners who wonder why a model splits a word oddly. It needs only simple counting. There are 10 questions, each with an explanation, and it takes about four minutes. Tokenisation is easy to overlook, yet it affects a great deal about how a model behaves. It shapes what the model finds easy, such as common words and standard code, and what it finds awkward, such as rare names, unusual spellings or long numbers, which are split into many small pieces. It also sets the length of every input, which in turn sets the cost of processing it. Knowing how the pieces are made explains many odd behaviours that otherwise seem arbitrary.
Questions and answers
›How long does the quiz take?
About 3 to 4 minutes for 10 questions, at roughly 20 seconds each. An explanation follows each answer, with the counting for the merge example shown.
›Is this the same as the quiz about tokens and the context window?
No. That quiz looks at tokens from the point of view of someone using a language model, for example how many fit in a conversation. This one looks at how the tokens are created, which is a question about the training of the model's front end.
›What is wrong with using a fixed dictionary of words?
It cannot cope with words that are not in the dictionary or that are misspelled, and it takes no account of punctuation or other character sequences such as computer code. Any new word becomes an unknown symbol, which wastes information that the letters contain.
›What is wrong with using characters?
It discards the semantically important structure of words, so the network has to learn to reassemble words from letters. It also needs many more sequential steps for a given text, which raises the computational cost. A representation between characters and words avoids both difficulties.
›What is a token?
It is a small group of characters. Tokens can be whole common words, fragments of longer words or individual characters that can be assembled into rarer words. Variations of a word, such as different verb endings, can share a common token, so their representations are related.
›How does byte pair encoding work?
It starts with a list of individual characters as tokens. It then searches a body of text for the most frequent pair of adjacent tokens and replaces it with a new token, adding it to the list. To avoid merging words, no new token is formed from two tokens if the second begins with a space. The process repeats.
›When does the merging stop?
The total number of tokens is chosen in advance as a compromise between character-level and word-level representations. At the start there are as many tokens as characters, and each merge adds one, so the process is stopped when the chosen size is reached. If it went on long enough, the tokens would become the words of the text.
›Can tokens represent things other than ordinary words?
Yes. Because tokens are built from characters, the scheme can handle punctuation, numbers, computer code and other character sequences, and it is also used to prepare other kinds of data, such as images, for the same models.
›Why do models sometimes split words in odd ways?
The splits come from statistics of the text used to build the token list. A word that was frequent in that text may be a single token, while a rare word is built from several pieces. The pieces do not have to match what a person would call syllables or meaningful parts.