liveBook · Manning

Byte pair encoding

As explained in Grokking AI Algorithms, Second Edition

Byte-pair encoding is a tokenization technique that iteratively merges frequent adjacent symbol pairs to form a vocabulary of subword tokens.

Byte-pair encoding (BPE) builds subword tokens by repeatedly merging the most frequent adjacent token pair. It starts by splitting training text into characters and marking word endings, helping distinguish a sequence inside a word from a complete word. section 11.9

A simplified BPE pass follows this loop:

  • Merge every occurrence of the most frequent pair into one token. section 11.9
  • Repeat counting and merging until reaching a vocabulary size or a minimum frequency threshold. section 11.9
  • Stop before rare merges create unintuitive tokens such as picturesorconversations. section 11.9

A small merge sequence

For the training paragraph, i followed by n is initially the most frequent pair, so occurrences become in. After recounting, e followed by r becomes the next merge; later merges produce tokens such as ic, th, ti, and ing. A resulting vocabulary contains both individual characters and learned subwords.

How Grokking AI Algorithms, Second Edition covers it

How deep it goes
Works through repeated frequency counts and merges, then stops short of a complete tokenizer implementation.
Examples
Uses a training paragraph, character-marked words, frequency tables, successive merges, and a Python tokenizer listing.
What it assumes
Starts from character splitting and defines bigrams as adjacent token pairs before introducing vocabulary growth.
What you can do after
Build it from scratch with a simplified Python loop that counts pairs, merges tokens, and enforces vocabulary stopping rules.
How it connects
Connects bigrams, end-of-word markers, tokenization, and vocabulary construction.

Also explained in

Compare how 11 books explain byte pair encoding →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking AI Algorithms, Second Edition. The section links point to the parts of the book it draws on.