Part XI — Sequence Models: RNNs, LSTMs, Attention & The Transformer Block · Chapter 7

Tokenization & Byte-Pair Encoding (BPE)

Hook

Last chapter's word vectors assumed every word already had a slot in a fixed vocabulary. Real text is unbounded — new words, typos, names, made-up words appear constantly. Something has to decide, before any vector is even looked up, what counts as "one unit."

Intuition
n
e
w
e
s
t
</w>

"newest" split into individual characters — no merges applied yet

Start from raw characters and merge the single most frequent adjacent pair, over and over. Drag the slider forward and watch "newest" collapse from 7 individual characters down to just 3 pieces — each merge fusing together whatever pair showed up most often across the whole training vocabulary.

Formalize
  1. Byte-pair encoding, defined

    This algorithm is byte-pair encoding (BPE): starting from individual characters, repeatedly find the most frequent adjacent pair of symbols across the whole corpus and merge it into one new symbol, forming a growing vocabulary of subword pieces.

  2. Trained on just four words

    Trained on a corpus of just four words — low, lower, newest, widest — it discovers pieces like est</w> (a common suffix) and low (a common stem), simply because those pairs of symbols kept showing up together.

Play
low11
est</w>13

Type any word. These merge rules were learned from only four training words, yet they generalize: type "lowest" — a word the algorithm never saw — and it still gets cut cleanly into low + est</w>, reusing pieces learned from completely different words ("low" from low/lower, "est</w>" from newest/widest). That's the entire point of subword tokenization: no word is ever truly unknown, because it can always fall back to pieces — down to individual characters if it has to.

Worked example
  1. Start from characters

    Tokenizing "lowest" from scratch: l o w e s t /wl\ o\ w\ e\ s\ t\ \langle/w\rangle.

  2. Apply the learned merges in order
    1. e+sese{+}s\to es
    2. es+testes{+}t\to est
    3. est+/west/west{+}\langle/w\rangle \to est\langle/w\rangle
    4. l+olol{+}o\to lo
    5. lo+wlowlo{+}w\to low
  3. Result

    Exactly two tokens: lowlow and est/west\langle/w\rangle. Every merge rule came from low, lower, newest, and widest alone; "lowest" itself was never part of training.

Checkpoint

Type a word (using letters l, o, w, e, r, n, s, t, i, d) that gets cut using at least one merged subword — a piece bigger than a single character.

</w>10
Type a word to try it
Summary

Byte-pair encoding is exactly what GPT (short for Generative Pre-trained Transformer)-style models use in practice — trained on billions of words instead of four, producing tens of thousands of merge rules instead of six, but the algorithm is identical: start from characters, merge the most frequent pair, repeat. Every one of those pieces gets an integer ID, and that integer is what finally goes into the embedding lookup from the last chapter. With text now reliably turned into a sequence of IDs, the next chapters ask how a model reads that whole sequence at once — starting with attention.