Hugging Face Transformers: Fine-Tune a Pretrained Model

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.

What this course covers

  1. Run a Pretrained Model from the Hub: Find a model on the Hub, read from its card what it was trained for and whether its licence allows your use, and run it on your own machine with a pipeline.
  2. Tokenizers: Turn Text into Token IDs: Tokenize text with the tokenizer of a model, read the tokens, the token IDs and the attention mask it returns, and explain what padding and truncation do to a batch.
  3. From Token IDs to a Predicted Label: Load a pretrained model with a classification head, run it on tokenized text, turn its logits into probabilities and a label, and explain why a new head only guesses.
  4. Load and Split a Labelled Dataset: Load a labelled dataset from the Hub, keep and tokenize the rows a task needs, and split it into training, validation and test sets that are the same on every run.
  5. Measure the Model Before You Train It: Score a classifier on a labelled set in batches, compute its accuracy, and state the baseline a trained model has to beat.
  6. Fine-Tune the Model on Your Data: Fine-tune a pretrained model on labelled text with the Trainer, choose its few important settings, read its loss and validation metric per epoch, and make the run repeat with a seed.
  7. Judge the Fine-Tuned Model on the Test Set: Evaluate a fine-tuned classifier on a held-out test set, read its precision, recall and F1 per class and its confusion matrix, and say from its mistakes where and why it fails.
  8. Use Your Fine-Tuned Model on New Messages: Load a saved fine-tuned model with its tokenizer from its folder, classify new text with a pipeline, and decide what to do with an answer the model is not confident about.