Part IV: Training and Adapting

Chapter 14: Fine-Tuning Fundamentals

"The secret of getting ahead is getting started. The secret of fine-tuning is knowing when to stop."

Finetune Finetune, Wisely Restrained AI Agent
Fine-Tuning Fundamentals chapter illustration
Figure 14.0.1: Fine-tuning is teaching an old (very large) dog new tricks, one carefully chosen gradient step at a time.

Chapter Overview

Pre-trained language models are powerful general-purpose tools, but they often fall short on specialized tasks that require domain-specific knowledge, a particular output style, or strict formatting. Fine-tuning bridges this gap by adapting a pre-trained model to your specific use case through additional training on curated data. The result is a model that retains its broad language understanding while gaining the ability to excel at your particular task.

This chapter covers the complete fine-tuning workflow from first principles. You will learn when fine-tuning is the right approach (and when prompting or RAG is a better alternative), how to prepare high-quality training data in the correct format, and how to run supervised fine-tuning with Hugging Face TRL. The chapter also covers API-based fine-tuning through providers like OpenAI and Google, fine-tuning for embedding and classification tasks, and strategies for adapting models to handle longer contexts.

By the end of this chapter, you will be able to make informed decisions about when to fine-tune, prepare datasets in standard formats, execute training runs with appropriate hyperparameters, monitor training progress, and adapt models for specialized tasks including classification, representation learning, and long-context processing.

Big Picture

Fine-tuning transforms a general-purpose LLM into a specialist for your domain. This chapter covers the full workflow: data preparation, training configuration, catastrophic forgetting mitigation, and evaluation. It provides the foundation for the parameter-efficient methods in Chapter 15 and alignment techniques in Chapter 17.

Learning Objectives

Prerequisites

Sections

What's Next?

In the next chapter, Chapter 15: Parameter-Efficient Fine-Tuning (PEFT), we explore parameter-efficient methods like LoRA and QLoRA that let you adapt large models on modest hardware.