Natural Language Processing · Advanced level

Advanced level · Module 6: Transformer Architecture and Modern NLP · Lesson 3 of 4

Fine-Tuning a Pretrained Transformer

Learners fine-tune a small encoder model (DistilBERT-size) for the news or enquiry classification task with the Hugging Face Trainer on CPU or a free notebook GPU.

In this lesson you will

  • Fine-tune a pretrained transformer for classification
  • Compare it with earlier models

Key concepts

AutoModelForSequenceClassificationdatasets libraryTrainer and TrainingArgumentslearning rate and epochscompute budget

The lesson

Fine-tuning starts from a pretrained model and trains it briefly on labelled task data. With transformers: load AutoTokenizer and AutoModelForSequenceClassification.from_pretrained(name, num_labels=k), tokenise a datasets.Dataset with truncation, then train with Trainer(model, args=TrainingArguments(...), train_dataset=..., eval_dataset=...).

Use a small model, a short maximum length (for example 128 tokens), two or three epochs and a small learning rate (for example 2e-5). On a CPU this takes longer but is feasible for a few thousand examples; a free notebook GPU is faster when internet is available.

Add the fine-tuned model to the comparison table. Transformers often give the best English accuracy, but they are larger and slower, and gains on local-language text must be measured, not assumed.

Step by step

  1. Prepare the news data as a datasets.Dataset with train and test splits.
  2. Tokenise with truncation and fine-tune for two epochs.
  3. Evaluate macro F1 overall and on the local sample.
  4. Add accuracy, time and size to the comparison table.

Practice activity

Fine-tune a small transformer classifier and update the model comparison.

Expected result: A fine-tuned model with honest per-language results and practical costs.

Check your understanding

  1. Why use a small model and short maximum length?

    Show answer

    To make fine-tuning feasible on CPUs or free notebook GPUs.

  2. Does better English accuracy guarantee better local-language accuracy?

    Show answer

    No. It must be measured on local-language test data.

Further learning

Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.