liveBook · Manning

Chapter 6: Fine-tuning for classification

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

The chapter introduces classification fine-tuning as adapting a pretrained LLM to a specific task, using spam detection as its example.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduces classification fine-tuning as adapting a pretrained LLM to a specific task, using spam detection as its example.

Key terms: pretrained LLM, fine-tuning for classification, spam, not spam

6.1 Different categories of fine-tuning

This section contrasts instruction and classification fine-tuning and explains when each approach is appropriate.

Key terms: instruction fine-tuning, classification fine-tuning, class labels, predefined classes, computational resources

6.2 Preparing the dataset

This section prepares an SMS spam dataset for classification fine-tuning by balancing its classes, encoding its labels, and splitting and saving the data.

Key terms: SMSSpamCollection.tsv, pandas DataFrame, ham, spam, undersample, balanced dataset, integer class labels, token IDs, GPT vocabulary, random_split

6.3 Creating data loaders

This section prepares variable-length spam messages as uniform token sequences and uses PyTorch data loaders to batch them with class labels.

Key terms: truncate, pad, uniformly sized, SpamDataset, GPT-2 tokenizer, <|endoftext|>, 50256, max_length, context length, data loaders

6.4 Initializing a model with pretrained weights

This section loads pretrained GPT weights, checks that the model still generates coherent text, and shows why it needs fine-tuning for spam classification.

Key terms: GPTModel, download_and_load_gpt2, load_weights_into_gpt, pretrained weights, text generation utility function, coherent text, model weights, pretraining, instruction fine-tuning, classification fine-tuning

6.5 Adding a classification head

The section adapts a pretrained GPT model for spam classification by replacing its output layer, selecting layers to fine-tune, and using the last token’s output to represent the input text.

Key terms: out_head, output layer, output nodes, classes, freeze, requires_grad, last transformer block, final LayerNorm, output tensor, last output token

6.6 Calculating the classification loss and accuracy

This section defines how to turn last-token model outputs into spam predictions and how to calculate accuracy and cross-entropy loss for classification fine-tuning.

Key terms: last token, argmax, softmax, spam, not spam, classification accuracy, calc_accuracy_loader, cross-entropy loss, calc_loss_batch, calc_loss_loader

6.7 Fine-tuning the model on supervised data

The section fine-tunes a pretrained LLM for spam classification, then uses loss and accuracy to judge training progress and generalization.

Key terms: train_classifier_simple, examples_seen, classification accuracy, training loss, validation loss, overfitting, epochs, eval_iter, training accuracy, validation accuracy

6.8 Using the LLM as a spam classifier

This section uses a fine-tuned GPT model to classify new messages as spam or not spam, then saves it for later reuse.

Key terms: classify_review, token IDs, integer class label, class name, spam, not spam, torch.save, save the model, loaded

6.9 Summary

This chapter summary recaps how a pretrained LLM is adapted, trained, and evaluated for text classification.

Where this is used later

Concepts explained in this chapter

← Chapter 5: Pretraining on unlabeled data Chapter 7: Fine-tuning to follow instructions →

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.