liveBook · Manning

Fine-tuning

As explained in Generative AI in Action

Task-specific fine-tuning is the adaptation of a pretrained model using data targeted at a particular task.

Fine-tuning adapts a pretrained model to a specific task by continuing training on a smaller, specialized dataset. Training adjusts model parameters so the model performs better on that task without starting from scratch. Uses include sentiment analysis, question answering, and domain-specific generation. section 2.8.8 section 9.1.1

Treat fine-tuning as a later option, not the default response to weak output. Try prompt engineering first; add retrieval-augmented generation (RAG) when answers need grounding in your data. These approaches can remain useful after fine-tuning. section 9.0 section 9.2

A practical workflow is:

  • Define the task, identify where the base model falls short, and establish evaluation criteria and a baseline. section 9.2
  • Choose a suitable model and method. Full fine-tuning updates all parameters; low-rank adaptation (LoRA) trains a smaller set while leaving pretrained weights frozen. section 9.1.1 section 9.6.1
  • Curate representative, high-quality examples and separate training, validation, and test data. Validate the dataset format and use validation results to watch for overfitting. section 9.2.1 section 9.3.1
  • Train, compare the result with the base model and task baseline, then iterate before deploying for inference. Use qualitative and quantitative checks suited to the task. section 9.2 section 9.2.1

Teaching a model a required output format

Suppose a chat assistant must answer with emoji codes that Microsoft Teams renders, rather than ordinary emoji characters. Example JSONL training records pair a user message with the desired assistant response, such as “I've been working nonstop and need a break.” → “(tired)”. An Azure OpenAI workflow uploads the file, submits a GPT-3.5 Turbo fine-tuning job, and deploys the resulting model for testing. The example is not a guarantee of correct output: some fine-tuned answers still require a revised system prompt. section 9.3 section 9.3.1 section 9.3.3 section 9.4.1

Listing 9.1 JSONL example
{"messages": [{"role": "system", "content": "You're a chatbot that only responds with emojis!"}, {"role": "user", "content": "I heard the koala habitat was destroyed by the fire."}, {"role": "assistant", "content": "(sadkoala)" }]}

{"messages": [{"role": "system", "content": "You're a chatbot that only responds with emojis!"}, {"role": "user", "content": "I've been working nonstop and need a break."}, {"role": "assistant", "content": "(tired)" }]}

{"messages": [{"role": "system", "content": "You're a chatbot that only responds with emojis!"}, {"role": "user", "content": "I just finished reading an amazing book!"}, {"role": "assistant", "content": "(like)" }]}

Inspect the JSONL message records to see how system instructions, user inputs, and desired assistant outputs are represented for the emoji task. section 9.3.1

Budget for curated data, compute, evaluation, and repeated runs. Fine-tuning can be expensive, can overfit, and can cause catastrophic forgetting of earlier capabilities. A first fine-tuned model may perform worse than the base model; deploy only after testing the behavior that matters for the task. section 2.8.8 section 9.2 section 9.8

How Generative AI in Action covers it

How deep it goes
Works through model choice, dataset preparation, training jobs, evaluation, and deployment, with a high-level account of parameter-efficient methods.
Examples
Uses JSONL chat examples and Azure OpenAI training jobs to teach a GPT-3.5 Turbo model Microsoft Teams emoji-code responses.
What it assumes
Assumes a pretrained model and an identified task, while introducing dataset splits, validation, and the main training stages.
What you can do after
Apply it via Azure OpenAI to prepare data, submit a fine-tuning job, evaluate the result, and deploy a task-specific model.
How it connects
Connects fine-tuning to transfer learning, prompt engineering, retrieval-augmented generation, supervised fine-tuning, and low-rank adaptation.

Also explained in

Compare how 37 books explain fine-tuning →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Generative AI in Action. The section links point to the parts of the book it draws on.