liveBook · Manning

Vocabulary

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

A mapping between unique text tokens and unique integer identifiers used to encode and decode text.

A vocabulary maps each unique text token—words, punctuation, or special markers—to a unique integer identifier. Token IDs provide the intermediate representation before embedding vectors and let a model encode input text and decode generated numbers back into text. section 2.3

Build and use a vocabulary through this data flow:

  • Tokenize the training text, remove duplicates, sort the tokens, and enumerate them to establish the vocabulary size and token-to-ID mapping. section 2.3
  • Maintain an inverse mapping from integer IDs to tokens so generated IDs can be decoded into text. section 2.3
  • Add <|unk|> for tokens absent from the vocabulary and <|endoftext|> to separate unrelated documents. section 2.4

Python vocabulary and token IDs

A vocabulary built from the sample text contains 1,130 entries. Adding <|endoftext|> and <|unk|> increases it to 1,132. SimpleTokenizerV2 encodes "Hello, do you like tea?" by assigning 1131 to the unknown word Hello, and decodes that ID as <|unk|>. section 2.3 section 2.4

Figure 2.6 We build a vocabulary by tokenizing the entire text in a training dataset into individual tokens. These individual tokens are then sorted alphabetically, and duplicate tokens are removed. The unique tokens are then aggregated into a vocabulary that defines a mapping from each unique token to a unique integer value. The depicted vocabulary is purposely small and contains no punctuation or special characters for simplicity.
figure

Shows how tokenized training data becomes a sorted set of unique tokens and then a token-to-integer vocabulary. section 2.3

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

How deep it goes
Defines construction and inverse lookup, then implements encoding, decoding, unknown handling, and document separators without descending into embeddings.
Examples
Uses sorted Python dictionaries, Edith Wharton text, SimpleTokenizerV1 and V2, unknown words, and concatenated independent texts.
What it assumes
Assumes tokenized text and teaches the mapping from tokens to IDs before embedding vectors.
What you can do after
Apply it via Python to build vocabularies and encode or decode text with unknown and document-boundary tokens.
How it connects
Connects tokenization, token IDs, embedding vectors, tokenizer encoding, tokenizer decoding, and special tokens.

Also explained in

Compare how 13 books explain vocabulary →

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.