Best on a larger screen — the animation and controls have more room to breathe.
Inputs

BPE Training

BPE training learns a subword vocabulary from raw text. Starting with every word split into single characters, Byte-Pair Encoding repeatedly counts adjacent symbol pairs across the corpus (weighted by each word's frequency), merges the most frequent pair into a new token, and records that merge as a ranked rule. Those ranked rules become the tokenizer.

The ampersand began as an e–t ligature for the frequent Latin word et. BPE training applies the same idea using frequency counts: it finds the most common adjacent symbol pair, combines it into one symbol, and repeats. Start with this lesson to see where the rules come from, then open the companion Tokenization lesson, which applies a merge table at real GPT-2 scale (50,257 entries) to encode any text you type. This lesson uses a small corpus so the whole table fits on screen; the mechanism is identical at either size.

How BPE Training Works

The corpus is a list of words, each with a frequency. Every word begins as a row of single characters. Each iteration makes three moves:

  1. Count every adjacent pair of symbols across all words, weighting by word frequency. A pair inside a word seen 12 times contributes 12 to the tally, not 1.
  2. Pick the most frequent pair.
  3. Merge it everywhere it appears across the whole corpus at once, and record the rule.

The recorded rules are ordered: the first merge learned is the highest-priority rule, and that ordering is part of the trained tokenizer because the encoder later replays rules by rank. The loop runs for a fixed number of merges here; in production you stop when the vocabulary reaches a target size.

BPE Training Step by Step

The default run learns 4 merges from this corpus:

wordfrequency
hug10
pug5
pun12
bun4
hugs5
  1. Start. Every word splits into characters: h u g, p u g, p u n, b u n, h u g s.
  2. Merge 1. The tally counts (u,g) in hug (10) + pug (5) + hugs (5) = 20, the highest count. The algorithm creates ug and merges it in every word that contains it: h ug, p ug, p u n, b u n, h ug s.
  3. Merge 2. Recount on the new symbols. (u,n) scores pun (12) + bun (4) = 16 and wins, giving h ug, p ug, p un, b un, h ug s. (h,ug) scored 15, one behind, so frequency weighting decided the winner.
  4. Merges 3 and 4 repeat the pattern: (h,ug) wins at 15 (from hug ×10 and hugs ×5), then (p,un) wins at 12 (from pun alone). The corpus now reads hug, p ug, pun, b un, hug s.
  5. Done. The learned rules, in priority order, are ug, un, hug, pun. Together with the seven base characters b g h n p s u, they form an 11-token vocabulary.

In the middle of the canvas, the tally panel ranks candidate pairs on each count beat and lights the winner. On the following merge beat, that pair fuses simultaneously in every word that contains it across the corpus. The growing list on the right is the merge table, the same kind of artifact that the Tokenization lesson applies at GPT-2 scale.

How Many Merges Should BPE Learn?

The default run learns four merges, each with a single clear winner. Setting the merge count to 5 introduces a tie: (hug,s) and (p,ug) both score 5, so frequency alone does not pick a winner. The animation gives this tie its own beat, lighting both candidates side by side at 5 before applying the tie-break rule. This implementation breaks ties alphabetically, so (hug,s) wins and hugs joins the table as rule #5. Different training implementations use different fixed tie orders; what matters is that the rule is fixed and deterministic so repeated training runs over the same corpus learn the same vocabulary.

Production training chooses a target vocabulary size (a hyperparameter, typically tens of thousands of tokens) and keeps merging until base symbols plus learned merges reach it; the number of merges follows from that budget. Too few merges leave common words split into characters, while too many fill the vocabulary with rare corpus-specific strings. Production systems may also use much larger corpora, byte-level or other base symbols, pre-tokenization boundaries, and reserved special tokens around the same count, pick, merge, and record loop.

Edge Cases

  • Frequency ties. Set the merge count to 5 to watch (hug,s) and (p,ug) tie at 5 and resolve by the alphabetical rule. Real tokenizers settle every tie with a deterministic rule so the learned table stays reproducible.
  • A pair spans many words. The count of 20 for (u,g) comes from three different words. Counting is corpus-wide, and word frequencies add.
  • Merging fuses every occurrence. When a rule fires, it replaces the pair in all words at once rather than stopping at the first match.
  • Order is part of the model. Learning the same merges in a different order can cause the encoder to produce different tokenizations whenever applicable merges compete. The ranking ships with the rules.
  • Merged symbols participate in later counts. Once ug is formed, it behaves like any other symbol and enters later pairs such as (h,ug). Merges compose.

Common Mistakes

  • Counting pairs unweighted. Word frequency belongs in the count, not in a tie-breaker. Unweighted, merge 2 is a tie because (u,n) and (h,ug) each appear in two words; only the weighting (16 vs. 15) settles it, and dropping weights changes the learned table.
  • Merging inside one word only. A rule is global. Fusing the pair in a single word leaves the corpus inconsistent and corrupts all subsequent counts.
  • Confusing training with encoding. Training produces the ranked rules from a corpus, whereas encoding replays them on new text. The two lessons separate those two stages.
  • Expecting whole words as tokens. With the default four-merge budget, even the corpus word hugs is not a single token; the fifth merge makes it one. The vocabulary holds only the pieces the loop had budget to learn, which is why the encoder later renders hugs as hug + s.

A Note on Simplification

This lesson uses a five-word teaching corpus with 36 total word occurrences, characters as base symbols, and an alphabetical tie-break. Real BPE training uses the same count, pick, merge, and record loop with implementation-specific base symbols, pre-tokenization boundaries, overlap handling, and reserved tokens, stopping at a target vocabulary size instead of a fixed merge count.