6 Fine-tuning for classification

Keywords

ver. 1.0.0, 6_fine_tuning_for_classification

Turn a pretrained GPT‑2 into a compact, end‑to‑end text classifier (data prep, model adaptation, training, evaluation, and deployment)

This unit teaches how to adapt a pretrained GPT‑2 into a task‑specific text classifier. You will prepare and split a labeled SMS spam dataset, build a PyTorch Dataset that tokenizes and pads texts, load and selectively freeze pretrained GPT‑2 weights, replace the language-model head with a two‑unit classification head that reads the final token position, train with cross‑entropy and AdamW while tracking accuracy, and save a simple inference wrapper for new messages.

Learners convert a pretrained causal language model into a narrow supervised classifier end to end. Key skills and concrete results include:

  • Choosing the right adaptation approach: distinguish narrow classification (fixed label set) from instruction fine‑tuning (generalist behavior) and select classification for label prediction tasks.
  • Preparing labeled data: download the SMS Spam Collection, balance classes by undersampling, encode labels as integers, and split into train/validation/test subsets.
  • Building a batching pipeline: implement a PyTorch Dataset that uses the GPT‑2 BPE tokenizer to encode variable‑length messages, truncates to a fixed max length, pads to produce rectangular tensors, and is wrapped by DataLoader for efficient training.
  • Adapting pretrained weights: load the 124M GPT‑2 checkpoint, verify generation behavior, then freeze parameters you don’t want to train and unfreeze only the last transformer block and final LayerNorm so fine‑tuning is efficient.
  • Replacing the output head: swap the vocabulary projection (Linear(768, 50257)) for a Linear(768, 2) classification head and extract logits from the final token position (logits[:, -1, :]), since the causal model only has full-context information at the last token.
  • Training and evaluation mechanics: compute cross‑entropy loss on the last‑token logits against integer labels, implement an accuracy metric over a data loader, and run a simple training loop using AdamW; monitor loss and accuracy curves across epochs and training steps to verify convergence.
  • Inference and persistence: wrap the fine‑tuned model in a single‑message classification function that tokenizes, truncates, pads, and scores new texts, then save and reload the fine‑tuned weights for deployment.

By the end, learners can take a pretrained GPT‑2, convert it into a compact classifier for binary or small‑label tasks, run supervised fine‑tuning on a real dataset, evaluate performance, and persist a usable inference function for new inputs. This gives practical experience with tokenizer handling, selective weight updates, architectural head replacement, metric computation, optimizer setup (AdamW), and simple model deployment.

Materials

Source document

  • Building a Large Language Model (from scratch), Sebastian Raschka, 2024, Manning Books — Link — Page 191-225