liveBook · Manning

Byte pair encoding

As explained in Learn Generative AI with PyTorch

A subword tokenization method adapted from data compression that merges frequent character pairs to represent text as tokens.

Byte-pair encoding (BPE) is a subword tokenization method adapted from data compression. It repeatedly merges the most frequent consecutive character pair, balancing vocabulary size against tokenized sequence length. section 11.2.1

Unlike character-level or word-level tokenization, BPE can represent common words or word fragments as single tokens while breaking uncommon words into smaller pieces. GPT-2 applies the resulting token indexes before word embedding. section 11.1.2 section 11.2.1

The practical BPE workflow is:

  • Convert text into subword tokens. section 11.2.1
  • Map each token to its corresponding integer index. section 11.2.1
  • Repeat frequent-pair merges until reaching a desired vocabulary size or gaining no useful merges. section 11.2.1

Tokenizing and decoding a sentence

Using the BPE tokenizer on “This is the original text.” produces six tokens: ['This', ' is', ' the', ' original', ' text', '.'], with indexes [1212, 318, 262, 2656, 2420, 13]. Decoding those indexes reconstructs the original sentence. section 11.2.1

from utils.bpe import get_encoder
  
example="This is the original text."                       #1
bpe_encoder=get_encoder()                                  #2
response=bpe_encoder.encode_and_show_work(example)
print(response["tokens"])                                  #3

Run the supplied Python example with get_encoder and encode_and_show_work to inspect both BPE tokens and their indexes. section 11.2.1

How Learn Generative AI with PyTorch covers it

How deep it goes
Defines iterative frequent-pair merging and its goals, but defers detailed workings to an existing tokenizer implementation.
Examples
Demonstrates tokenizing “This is the original text.”, inspecting indexes, and decoding them back into text.
What it assumes
Assumes basic NLP tokenization knowledge while contrasting character-level, word-level, and subword approaches.
What you can do after
Apply it via the supplied Python BPE tokenizer to encode text, inspect token indexes, and decode them.
How it connects
Connects subword tokenization with character-level tokenization, word-level tokenization, word embedding, and GPT-2.

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 Learn Generative AI with PyTorch. The section links point to the parts of the book it draws on.