liveBook · Manning

Subword tokenization

As explained in Build a Large Language Model (From Scratch)

A token fragment smaller than a full word, used by subword tokenizers to represent rare or unseen words.

A subword unit is a token fragment smaller than a full word. Byte pair encoding (BPE) uses subwords, and individual characters when necessary, to represent unfamiliar words without replacing them with an unknown token. section 2.5

In the GPT pipeline, BPE converts text into subword tokens, token IDs, and then embedding vectors that neural networks can process. This lets the tokenizer and model handle text containing words absent from training data. section 2.9

BPE builds and uses subword units by:

  • Starting with individual characters in the vocabulary. section 2.5
  • Iteratively merging frequent character combinations into subwords and frequent subwords into words. section 2.5
  • Breaking unknown words into subwords or characters so they remain decodable. section 2.5

Unknown-word round trip

A BPE tokenizer can encode “someunknownPlace” or “Akwirw ier” as sequences of token IDs, including subwords and characters, then decode those IDs to reconstruct the original text. section 2.5

tokenizer = tiktoken.get_encoding("gpt2")

Inspect the GPT-2 tokenizer configuration used to produce subword token IDs. section 2.5

How Build a Large Language Model (From Scratch) covers it

How deep it goes
Defines BPE’s iterative merging process and demonstrates encoding, decoding, and unknown-word handling, but defers a detailed implementation.
Examples
Demonstrates “someunknownPlace,” “Akwirw ier,” character fallback, and tiktoken encode–decode round trips.
What it assumes
Assumes basic tokenization and introduces token IDs, embeddings, special tokens, and BPE-based unknown-word handling.
What you can do after
Apply it via tiktoken to encode unfamiliar words, inspect token IDs, and verify decoded text.
How it connects
Links subword units to byte pair encoding, tokenization, token IDs, embeddings, and unknown-word handling.

Also explained in

Compare how 7 books explain subword tokenization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Large Language Model (From Scratch). The section links point to the parts of the book it draws on.