liveBook · Manning

Fine-tuning

As explained in Hugging Face in Action

Fine-tuning is the adaptation of a pretrained model by continuing training on data for a particular task or domain.

Fine-tuning adapts a pretrained model to a particular task or domain by continuing training with a smaller, domain-specific dataset. Reusing learned features can reduce the data and computational resources needed compared with training from scratch. section 6.1

Listing 6.6 Setting up the TrainingArguments and Trainer classes for fine-tuning
from transformers import Trainer, TrainingArguments

training_args = TrainingArguments(  #1
    output_dir = "./results",  #2
    eval_strategy = "epoch",  #3
    save_strategy = "epoch",  #4
    learning_rate = 2e-5,  #5
    per_device_train_batch_size = 16,  #6
    per_device_eval_batch_size = 16,  #7
    num_train_epochs = 3,  #8
    weight_decay = 0.01,  #9
    logging_dir = "./logs",  #10
    logging_steps = 10,  #11
    save_steps = 500,  #12
    load_best_model_at_end = True,  #13
)

trainer = Trainer(  #14
    model = model,
    args = training_args,
    train_dataset = tokenized_datasets["train"],
    eval_dataset = tokenized_datasets["test"],
)

trainer.train()  #15

Configure Hugging Face training with an output directory, evaluation and saving schedules, learning rate, batch sizes, epochs, weight decay, logging, and best-model loading, then call trainer.train(). section 6.1.5

A practical fine-tuning workflow includes:

  • Load and inspect a labeled dataset, then tokenize it for model input. section 5.0 section 6.1.1
  • Load a compatible pretrained model with AutoModelForSequenceClassification and set the number of task labels. section 6.1.4
  • Choose a device, including CPU, CUDA, or Apple MPS, and move the model to it. section 6.1.4
  • Train on the training split, evaluate on the test split, save the model and tokenizer, and inspect evaluation loss and runtime metrics. section 6.1.5

Restaurant-review sentiment

Fine-tune DistilBERT on the yelp_polarity dataset, whose reviews carry label 0 for negative sentiment and 1 for positive sentiment. After training, load the saved model to classify new reviews; demonstrated examples produce positive and negative predictions with 0.99 confidence. section 6.1.1 section 6.1.6

How Hugging Face in Action covers it

How deep it goes
Explains dataset preparation, model and device setup, training configuration, evaluation, saving, and inference without deriving optimization mathematics.
Examples
Uses yelp_polarity reviews, DistilBERT sequence classification, training logs, saved checkpoints, and positive or negative review predictions.
What it assumes
Assumes access to a pretrained model and labeled data while introducing dataset loading, tokenization, device selection, and Trainer configuration.
What you can do after
Apply it via Hugging Face Trainer to train, evaluate, save, reload, and use a task-specific classification model.
How it connects
Connects pretrained models, tokenization, domain-specific datasets, sequence classification, Trainer, TrainingArguments, MPS, and CUDA.

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