Hugging Face Transformers: Fine-Tune a Pretrained Model
IntermediateRun, measure and fine-tune a small model that sorts customer messages, all on a CPU.
You build
A text classifier that reads a short message from a bank customer about their card and names which of ten requests it is, made by fine-tuning the small pretrained DistilBERT model on real labelled customer messages from the Banking77 dataset.
You need
- Python: functions, lists, dictionaries, loops, and running a script from a terminal
- The basics of deep learning in PyTorch: what a tensor, a neural network, a loss and a training loop are, and having trained and measured a small network yourself. The course Deep Learning with PyTorch teaches all of it and is one way to get there.
To build it on your own machine
This is optional and comes with a plan. The course pack brings the starting files, a ready container and a check for every lesson. You need only Docker and git, plus your own AI coding agent if you want a tutor.
What you learn
The model already reads English. You teach it your labels in minutes.
You take a small pretrained transformer from the Hugging Face Hub and adapt it to one job: reading a bank customer's message about their card and naming the request. You see what the model already knows, how a tokenizer turns text into numbers and how scores become a label. Then you measure the model on real labelled messages, fine-tune it in a few minutes on a laptop CPU, judge the result on a test set and use it. You need Python and the basics of training a network in PyTorch. No GPU and no account.
- Pretrained Models and the Hub. A model someone else trained on a huge amount of text, published with its card and licence, downloaded by name and reused instead of trained from nothing.
- From Text to a Label. What happens between a sentence and an answer: a tokenizer turns text into token IDs, the model turns them into vectors, a small head turns one vector into a score per class.
- Measure Before and After. Labelled data split three ways, a baseline taken before any training, and a final judgement on a test set with results per class and a look at the mistakes.
- Fine-Tuning as Adaptation. A short, seeded training run that keeps what the model learned from pretraining and adds your classes, with far less data and time than training from random weights.
Lessons
Run a Pretrained Model from the Hub
Pretrained model, The Hugging Face Hub, Model card and licence, Pipeline
Tokenizers: Turn Text into Token IDs
Tokens and vocabulary, Subword tokenization, Special tokens, Token IDs and attention mask
From Token IDs to a Predicted Label
Encoder model, Body and head, A model class for the task, Logits and probabilities
Load and Split a Labelled Dataset
Dataset repositories, Splits, columns and class labels, Filtering and mapping, A seeded, stratified split
Measure the Model Before You Train It
Batches and dynamic padding, Accuracy, Baseline, Measure first
Fine-Tune the Model on Your Data
Fine-tuning, The Trainer, The settings that matter, Metrics during training
Judge the Fine-Tuned Model on the Test Set
The held-out test result, Precision and recall, F1 and the macro average, Confusion matrix
Use Your Fine-Tuned Model on New Messages
What a saved model holds, Loading from a folder, A pipeline with your model, Confidence
Where it fits
Course 4 of 5 on the Machine Learning Engineer path.