liveBook · Manning

Fine-tuning

As explained in Transformers in Action

Fine-tuning is the adaptation of a pretrained model by further optimizing its parameters for a specific task or behavior.

Fine-tuning adapts a pretrained model to a narrower task or behavior by continuing optimization on task-specific examples. In supervised fine-tuning (SFT), labeled examples supply target responses, and training adjusts model parameters to reduce prediction error. This sits between broad pretraining and any later preference-alignment phase: SFT teaches the model the kinds of responses its role requires; alignment can further shape those responses around human preferences. section 5.2.1

A practical SFT workflow has five parts:

  • Choose a pretrained model and establish a baseline by generating responses before training. section 7.4 section 7.5
  • Prepare supervised examples in the format the model will see. Instruction data can use instruction, input, and response fields; chat data can use system, user, and assistant messages. section 5.2.1 section 7.4
  • Configure optimization for the available compute. One Mistral-7B workflow loads a 4-bit model and adds LoRA adapters so training updates only a small portion of it; a Gemma translation workflow uses batch size, gradient accumulation, and memory-saving settings. section 5.2.1 section 7.5
  • Train with TRL's SFTTrainer, then save the resulting model or checkpoint. section 7.4 section 7.5
  • Compare outputs before and after training on held-out or fixed prompts. For the prosocial adaptation, a further check computes perplexity on an evaluation slice. section 7.4 section 7.5

Specializing a small model for translation

For English–Spanish translation, filter and sample OPUS-100 sentence pairs, then format each pair as a chat exchange: a system instruction specifies translation, the user supplies English, and the assistant supplies Spanish. Fine-tune Gemma 3 270M on these examples and rerun the initial translation prompts. The shown outputs improve on some baseline failures, but still include an inaccurate translation; inspect actual generations rather than treating completion of training as proof of quality. section 7.5

Listing 7.16 Running SFT with TRL on chat-formatted samples
torch_dtype = model.dtype
args = SFTConfig(
    output_dir=checkpoint_dir,
    max_length=512,
    packing=False,
    num_train_epochs=3,            
    per_device_train_batch_size=4,
    gradient_checkpointing=False,
    optim="adamw_torch_fused",
    logging_steps=10,
    save_strategy="epoch",
    eval_strategy="epoch",
    learning_rate=learning_rate,
    fp16=(torch_dtype == torch.float16),
    bf16=(torch_dtype == torch.bfloat16),
    lr_scheduler_type="cosine",
    push_to_hub=False,
    report_to="tensorboard",
    dataset_kwargs={
        "add_special_tokens": False,
        "append_concat_token": True,
    },
)

trainer = SFTTrainer(
    model=model,
    args=args,
    train_dataset=train_built,
    eval_dataset=eval_built,
    processing_class=tokenizer,
)

trainer.train()
trainer.save_model()

The chat-data training listing shows how SFTConfig sets epochs, batch size, learning rate, evaluation, and checkpointing before SFTTrainer runs training. section 7.4

Fine-tuning can target behavior as well as a conventional task. A prosocial dialogue workflow builds chat examples with an empathy-oriented system message, trains Gemma 3 270M for three epochs, and compares its replies with those of the starting model. The aim is a more suitable conversational stance, not merely a different answer format. section 7.4

How Transformers in Action covers it

How deep it goes
Works through data formatting, trainer configuration, training, and output checks; does not derive the optimization mathematics.
Examples
Uses Alpaca instruction data, ProsocialDialog conversations, and OPUS-100 English–Spanish sentence pairs.
What it assumes
Builds on a pretrained model while walking through dataset preparation and trainer setup rather than requiring a model trained from scratch.
What you can do after
Apply it via TRL's SFTTrainer to specialize a pretrained model and compare its generations with a baseline.
How it connects
Connects pretraining, supervised fine-tuning, LoRA, preference alignment, chat templates, and perplexity.

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 Transformers in Action. The section links point to the parts of the book it draws on.