AI Hints
Home›Fine-Tuning LLMs Tutorial›Training with the Trainer
Fine-Tuning LLMs · Lesson 31 of 120

Trainer and TrainingArguments

The training loop, done for you.

⏱ 2 min read▶ Video at 49:49🎓 Free
Watch this lesson in the Fine-Tuning LLMs courseWatch on YouTube ↗Full Fine-Tuning LLMs course

Remember gradient descent? Loss, backward, step, repeated thousands of times, in batches. Writing that loop yourself is easy to get wrong. The Trainer class does it all for you.

  • Loss, backward, step
  • Thousands of times
  • The Trainer does it for you

Key training arguments

Training arguments holds the settings. Output dir is where results go. Num train epochs is how many passes. Per device train batch size is the batch size. Learning rate is the step size. And eval strategy epoch means, test the model after every epoch.

ArgumentMeaning
output_dirwhere results are saved
num_train_epochspasses over the data
per_device_train_batch_sizeexamples per batch
learning_ratesize of each step
eval_strategy="epoch"test after each epoch

train.py

Here's train dot p y. It loads the model, like before. Then the training arguments. Two epochs, batches of sixteen, and a learning rate of five times ten to the minus five. The accuracy function compares the model's top guess with the right label. Finally, the Trainer brings everything together. The model, the settings, the train and test data, a data collator, and our metric.

train.pypython
import numpy as np
from transformers import (AutoModelForSequenceClassification, DataCollatorWithPadding,
                          Trainer, TrainingArguments)
from prep import tok, data, MODEL, label2id, id2label

model = AutoModelForSequenceClassification.from_pretrained(
    MODEL, num_labels=5, id2label=id2label, label2id=label2id)
args = TrainingArguments(output_dir="intent-model", num_train_epochs=2,
                         per_device_train_batch_size=16, learning_rate=5e-5,
                         eval_strategy="epoch", logging_strategy="epoch",
                         save_strategy="no", report_to="none")

def accuracy(pred):
    return {"accuracy": (np.argmax(pred.predictions, -1) == pred.label_ids).mean()}

trainer = Trainer(model=model, args=args, train_dataset=data["train"],
                  eval_dataset=data["test"], data_collator=DataCollatorWithPadding(tok),
                  compute_metrics=accuracy)

The data collator

One new word. A data collator builds each batch. Our messages have different lengths. Data collator with padding pads each batch only to its longest message. That's faster than padding everything to one fixed length.

keyvalue
Data collatorbuilds each batch
With paddingpads to the longest in the batch
Benefitless wasted work
Used bythe Trainer

Summary

Let's recap. Training arguments holds the settings. The Trainer runs the whole training loop. And a data collator builds each batch. Next, let's press go.

Key takeaways
  • TrainingArguments
  • Trainer
  • DataCollatorWithPadding
Next up: Train It — our first fine-tuning run