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




































