liveBook · Manning

Chapter 2: Working with text data

What you will learn in chapter 2 of Build a Large Language Model (From Scratch)

This introduction explains why text data must be prepared before pretraining an LLM and previews the chapter’s data pipeline.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This introduction explains why text data must be prepared before pretraining an LLM and previews the chapter’s data pipeline.

Key terms: pretraining, next-word prediction task, training dataset

2.1 Understanding word embeddings

This section explains why LLMs need embeddings, what word embeddings represent, and how their training and dimensionality affect their use.

Key terms: raw text, categorical, embedding, continuous-valued vectors, Word2Vec, context, word embeddings, pretrained models, input layer, updated during training

2.2 Tokenizing text

This section builds a simple tokenizer that separates words and punctuation in a short story, then explains choices about capitalization and whitespace.

Key terms: tokens, tokenization, embeddings, re.split, whitespace, punctuation characters, special characters, capitalization, proper nouns, common nouns

2.3 Converting tokens into token IDs

The section builds a vocabulary to map text tokens to integer IDs, uses a tokenizer to encode and decode text, and shows why a vocabulary built from limited training text can fail on new input.

Key terms: vocabulary, token IDs, embedding vectors, encode, decode, inverse vocabulary, SimpleTokenizerV1, unknown words, training set

2.4 Adding special context tokens

This section adds tokens for unknown words and boundaries between unrelated texts to a simple tokenizer, then contrasts those choices with other special tokens and GPT tokenization.

Key terms: SimpleTokenizerV2, <|unk|>, vocabulary, byte pair encoding, subword units, <|endoftext|>, separator token, independent documents, [BOS], [EOS]

2.5 Byte pair encoding

This section introduces BPE tokenization, shows how to use it through tiktoken, and explains why it can represent unfamiliar words.

Key terms: BPE, subword tokens, out-of-vocabulary words, <|unk|>, vocabulary, subwords, frequency cutoff, tiktoken, encode, decode

2.6 Data sampling with a sliding window

This section shows how to turn tokenized text into batches of input–target pairs for next-token training using a sliding window.

Key terms: input–target pairs, input tokens, targets, token IDs, GPTDatasetV1, Dataset, DataLoader, max_length, input tensor, target tensor

2.7 Creating token embeddings

This section shows how an embedding layer turns token IDs into continuous vectors by looking up rows in a trainable weight matrix.

Key terms: embedding, continuous vector representation, embedding weights, backpropagation, embedding layer, weight matrix, token ID, vocab_size, output_dim, lookup operation

2.8 Encoding word positions

This section explains why LLM inputs need positional information and shows how to add learned absolute positional embeddings to token embeddings.

Key terms: token embeddings, self-attention mechanism, position information, absolute positional embeddings, relative positional embeddings, context_length, pos_embeddings, input_embeddings

2.9 Summary

This chapter summary connects tokenization, training-pair creation, and embeddings into a pipeline that prepares text for an LLM.

Where this is used later

Concepts explained in this chapter

← Chapter 1: Understanding large language models Chapter 3: Coding attention mechanisms →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.