- Introduced by
- Vaswani et al. (architecture); fine-tuning paradigm popularised by Howard & Ruder, Devlin et al.
- Year
- 2017–2019
- Type
- Transfer learning / supervised fine-tuning
- Family
- Deep learning / NLP / CV
What does it do?
Fine-tuning a Transformer adapts a large pre-trained model, such as BERT, GPT, or ViT, to a specific downstream task by continuing gradient-based training on a labelled target dataset.
It follows a two-stage pattern of pre-training followed by fine-tuning, and it can require far less task-specific data than training from scratch. Results depend on how well the pre-training domain matches the target, on the learning rate, and on the training schedule.
The intuition behind the method
A pre-trained Transformer has already learned representations of language or images from large corpora. Fine-tuning is like an experienced specialist who already knows a field being trained for a particular job. Rather than learning everything again, you add a small output head for your task and update the whole network with a low learning rate on your labelled examples. The pre-trained weights give a warm starting point, so a modest labelled set can be enough for usable performance on some tasks, which is hard to reach when a comparable network is trained from scratch on the same data.
History
The Transformer architecture was introduced by Vaswani et al. in 2017, replacing recurrent networks with self-attention as the main computation. Devlin et al. (2019) introduced BERT, a deep bidirectional Transformer pre-trained on large text corpora and then fine-tuned on downstream tasks, and reported strong results across eleven NLP benchmarks. The pre-train-then-fine-tune pattern became common in NLP and was later applied to computer vision through Vision Transformers.
When to use it
Use a fine-tuned Transformer when you have a labelled text, image, or multimodal task, need high accuracy, and the labelled set is too small to train a comparable network from scratch. It is well suited to NLP tasks such as classification, NER, and question answering, and increasingly to vision tasks through ViT. Prefer a domain-matched checkpoint when the target vocabulary differs from general corpora. Do not use it when the deployment environment cannot support large-model inference, for example edge devices with strict latency or memory limits and no quantisation option. Avoid it when the pre-training domain is drastically different from the target and no suitable checkpoint exists, or when reviewers or regulators require coefficient-level interpretability. For very small labelled sets, consider the parameter-efficient alternatives discussed in the FAQ.
Limitations and considerations
- Pre-trained Transformers range from about 110M to billions of parameters, so fine-tuning needs substantial GPU memory and compute.
- The fine-tuned network does not yield interpretable coefficients, which can conflict with reporting norms that expect coefficient estimates.
- Results vary across random seeds for head initialisation and data order, especially on small target sets, so a single run can be misleading.
- A learning rate that is too high or too many epochs can overwrite useful pre-trained weights, so these two settings have the largest influence on forgetting, although warmup and batch size also matter.
- The benefit depends on the match between pre-training data and the target domain; a mismatch can degrade performance markedly.
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- Pre-trained representations carry much of the task-relevant knowledge, so a labelled target set too small for training a network from scratch can still support a useful model.
- The same architecture and fine-tuning loop covers classification, sequence labelling, span extraction, and generation, with the output head and loss changing by task.
- Self-attention computes contextual representations over the whole input, so dependencies between distant tokens can be captured without the sequential processing of recurrent models.
- Public model hubs provide many domain-specific and multilingual checkpoints to start from, such as the domain models named in the checkpoint-selection step.
- Common libraries (Transformers, PyTorch Lightning) provide standard fine-tuning loops, which makes a setup easier to reproduce, although results still depend on the chosen settings.
Applications
- Text classification (sentiment analysis, topic categorisation, hate speech detection).
- Named entity recognition, part-of-speech tagging, and other sequence labelling tasks.
- Question answering and reading comprehension on domain-specific corpora.
- Image classification and object recognition via fine-tuned Vision Transformers.
- Biomedical and legal document analysis using domain-specific pre-trained checkpoints.
- Low-resource language tasks where labelled data is scarce but a multilingual checkpoint (mBERT, XLM-R) exists.
How it works
How it works
Choose a Transformer checkpoint whose pre-training domain matches the target task. For text tasks, models like BERT, RoBERTa, or GPT-2 pre-trained on large corpora are standard. For image tasks, Vision Transformer (ViT) variants pre-trained on ImageNet or CLIP are common. Domain-specific pre-trained models (e.g., BioBERT, LegalBERT) are preferable when the target domain vocabulary diverges from general-purpose corpora.
Append a lightweight task-specific head on top of the final transformer layer: a linear classification layer for sequence classification, a token-level linear layer for sequence labelling (NER, POS), or a span-extraction head for question answering. Only this head is randomly initialised; the transformer body carries pre-trained weights.
Tokenise the labelled target dataset using the model's original tokeniser to preserve vocabulary alignment. Apply truncation and padding to fit the model's maximum sequence length. Split into train and validation sets; stratify by class if classification labels are imbalanced. Compute class weights or apply oversampling if severe imbalance exists.
Train the entire model end-to-end on the target data with a small learning rate (typically 1e-5 to 5e-5 for BERT-family models) over a short schedule (2–5 epochs for most NLP tasks). Use a linear warmup over the first 10 % of steps, followed by linear decay. The formula in the mechanics panel is the cross-entropy objective used for classification with one-hot labels. Sequence labelling, span extraction, and generation objectives use different, task-specific losses. Monitor validation loss each epoch and stop early when it no longer improves, which limits overfitting. The learning rate and epoch budget strongly influence how much pre-trained knowledge is forgotten; warmup and batch size also matter.
Formula
\mathcal{L} = -\sum_{i} y_i \log \hat{y}_iEvaluate on a held-out test set using task-appropriate metrics: accuracy and macro-F1 for classification, span-level F1 for NER and QA, ROUGE or BERTScore for summarisation. Select the learning rate, epoch budget, and checkpoint on the validation split only, and use the test set once for the final report. Report the mean and standard deviation over several random seeds for each configuration, because fine-tuning results can be sensitive to initialisation, especially on small datasets, and differences between settings should be judged against that run-to-run variation.
Common pitfalls
- Using a learning rate that is too high (above 1e-4) causing catastrophic forgetting of pre-trained weights.
- Fine-tuning for too many epochs without early stopping, leading to overfitting on small target datasets.
- Reporting results from a single fine-tuning run without averaging over multiple seeds, masking high variance.
- Ignoring tokenisation alignment by using a different tokeniser than the one used during pre-training.
- Selecting a general-purpose checkpoint when a domain-specific model (e.g., BioBERT, SciBERT) is available and clearly more appropriate.
Frequently asked questions
How many labelled examples do I need to fine-tune a Transformer?
It depends on task complexity and the number of classes, but Transformers are data-efficient. Classification tasks often work well with a few hundred to a few thousand labelled examples. For very small datasets (below about 200 examples), few-shot or parameter-efficient methods such as LoRA or prompt tuning may be more stable.
Should I freeze the Transformer body and only train the head?
When enough labelled data is available, full end-to-end fine-tuning usually outperforms head-only training. Freezing the body mainly makes sense with very few examples or limited compute, at the cost of task-specific adaptation of the representations.
What learning rate should I use?
BERT-family models are sensitive to the learning rate. Values between 1e-5 and 5e-5 with a linear warmup over the first 10 % of steps are standard starting points. Rates above 1e-4 can cause catastrophic forgetting.
How do I choose between different pre-trained checkpoints?
Prefer domain-matched checkpoints (BioBERT for biomedical, LegalBERT for legal text, SciBERT for scientific text) when available. For general tasks, RoBERTa or DeBERTa are often stronger starting points than vanilla BERT. Compare candidates on a validation split drawn from your own data rather than relying on general rankings.
Is a fine-tuned Transformer interpretable enough for academic reporting?
Not in the traditional coefficient-reporting sense. However, attention visualisation, integrated gradients, and SHAP-based explanations can show which tokens drive predictions. These techniques are increasingly accepted in applied NLP research but may not satisfy reviewers in strictly confirmatory social-science contexts.
Related methods
Sources
Vaswani, A., Shazeer, N., Parmar, N., Uszkoreit, J., Jones, L., Gomez, A. N., Kaiser, L., & Polosukhin, I. (2017). Attention is all you need. Advances in Neural Information Processing Systems, 30.
Open publication referenceDevlin, J., Chang, M.-W., Lee, K., & Toutanova, K. (2019). BERT: Pre-training of deep bidirectional transformers for language understanding. Proceedings of NAACL-HLT 2019, 4171–4186.
Open publication reference