liveBook · Manning

Chapter 5: Pretraining on unlabeled data

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

The chapter introduction connects evaluating generated text to LLM training and explains what trainable weight parameters are and how PyTorch exposes them.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduction connects evaluating generated text to LLM training and explains what trainable weight parameters are and how PyTorch exposes them.

Key terms: model evaluation, quality of the generated text, optimizing the LLM, weights, weight parameters, parameters, .weight, model.parameters(), biases

5.1 Evaluating generative text models

This section previews a recap of text generation, model evaluation, and training and validation loss calculations.

5.1.1 Using GPT to generate text

Sets up a GPT model for text generation, traces how text becomes tokens and back, and explains why its untrained output needs numerical evaluation.

Key terms: tokenizer, token IDs, logits, generate_text_simple, text_to_token_ids, token_ids_to_text, GPT_CONFIG_124M, context length, 256 tokens, 1,024 tokens

5.1.2 Calculating the text generation loss

This section derives next-token generation loss from target-token probabilities, shows how PyTorch computes it, and relates it to perplexity.

Key terms: targets, token IDs, logits, softmax, probas, argmax, target token IDs, softmax probability, likelihood, log probabilities

5.1.3 Calculating the training and validation set losses

The section prepares training and validation batches, computes cross-entropy loss over each set, and explains what the initial losses indicate.

Key terms: The Verdict, public domain, 5,145 tokens, training set, validation set, data loaders, max_length, context length, input data (x), cross entropy loss

5.2 Training an LLM

The section presents a simple GPTModel pretraining loop, shows how to monitor training, and interprets the resulting losses and generated text.

Key terms: train_model_simple, epoch, batch, gradients, loss, evaluate_model, generate_and_print_sample, start_context, evaluation mode, training loss

5.3 Decoding strategies to control randomness

The section revisits simple GPT text generation and explains why selecting the highest-probability token produces the same output from the same starting context.

Key terms: generate_text_simple, token, probability score, start context, inference, evaluation mode, dropout, CPU, GPU

5.3.1 Temperature scaling

This section explains how sampling next tokens from probabilities adds variety to generated text and how temperature scaling changes that variety.

Key terms: greedy decoding, torch.argmax, multinomial, probability scores, temperature scaling, logits, softmax, sharper distributions, more uniform distribution

5.3.2 Top-k sampling

This subsection explains how top-k sampling limits next-token choices to the most likely tokens while retaining probabilistic sampling.

Key terms: top-k sampling, logits, negative infinity, softmax, multinomial function, temperature scaling, probabilistic sampling

5.3.3 Modifying the text generation function

The section combines temperature and top-k sampling in a new text-generation function and compares its output with the earlier simple function.

Key terms: temperature sampling, top-k sampling, generate function, generate_simple function

5.4 Loading and saving model weights in PyTorch

This section explains how to save and reload GPT model weights, prepare a loaded model for inference, and preserve optimizer state when continuing pretraining.

Key terms: state_dict, torch.save, GPTModel, model weights, model.eval(), evaluation mode, inference, dropout, optimizer state, AdamW

5.5 Loading pretrained weights from OpenAI

This section shows how to download OpenAI’s pretrained GPT-2 weights, configure a matching GPTModel, transfer the weights, and check the result by generating text.

Key terms: pretrained weights, settings, params, download_and_load_gpt2, model_configs, GPT_CONFIG_124M, NEW_CONFIG, 1,024-token length, bias vectors, GPTModel

5.6 Summary

Summarizes how LLMs generate tokens, how pretraining is assessed and performed, and why loading pretrained weights can be a practical alternative.

Where this is used later

Concepts explained in this chapter

← Chapter 4: Implementing a GPT model from scratch to generate text Chapter 6: Fine-tuning for classification →

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.