BERT Fine-Tuning

Machine Learning
Introduced by
Devlin, J. et al.
Year
2019
Type
Transfer learning (fine-tuning a pre-trained transformer)

What does it do?

BERT fine-tuning takes a BERT model pre-trained on large text corpora, as published by Devlin and colleagues at NAACL in 2019, and continues training it on a small labelled dataset for a target task such as text classification, named-entity recognition (NER) or question answering.

It is a form of transfer learning: the general language knowledge acquired during pre-training is adapted to the task rather than learned anew. How much labelled data is needed depends on the task and is discussed under whenToUse.

The intuition behind the method

Training a language model from scratch requires very large text collections and substantial computation. A pre-trained BERT model has already learned representations of language that reflect how words are used in context, so the practical question becomes how to adapt them. Fine-tuning updates the pre-trained weights, together with a new task-specific output layer, using your labelled examples. Because the starting point already encodes general language structure, the task data mainly has to teach the task-specific mapping, which is why a modest labelled set can sometimes be enough.

History

BERT was published by Devlin, Chang, Lee and Toutanova at NAACL 2019. The paper showed that deep bidirectional transformers pre-trained on large corpora could be fine-tuned for a range of language-understanding tasks. Sun, Qiu, Xu and Huang's 2019 paper, 'How to Fine-Tune BERT for Text Classification?', examines how the fine-tuning procedure is applied to classification.

When to use it

Choose BERT fine-tuning for text-based tasks such as classification, named-entity recognition or question answering, when you have labelled examples of that task and a pre-trained BERT model suited to your language and domain. The amount of labelled data needed depends on the task, the number of labels and the class balance. The registry's minimum sample size for this method is 50 labelled examples. As a rough practitioner heuristic, several hundred examples or more give more stable results, and overfitting becomes a greater concern when the set is smaller than about 500 examples. Below the registry minimum, results should be treated with particular caution. In that range, compare BERT against a simple classical baseline, such as TF-IDF features with a linear classifier, on the same held-out data before relying on it. Avoid relying on BERT fine-tuning when labelled data amounts to only a handful of examples. Plan for GPU access if training time matters.

Limitations and considerations

  • With small labelled sets (roughly below 500 examples, as a rough heuristic that varies with task and class balance), fine-tuning is prone to overfitting, so its outputs can be unreliable.
  • Below the registry minimum of 50 labelled examples, results should be interpreted with great caution.
  • Training is considerably more computationally demanding than classical text models, and a GPU is recommended.
  • Results depend on a suitable pre-trained BERT model being available for the language and domain.
  • BERT accepts a fixed maximum input length, so longer documents must be truncated or split, which can discard text relevant to the label.

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • Transfer from pre-training means a usable task model can be built from far less labelled data than training a language model from scratch would require.
  • Contextual, bidirectional representations let the same word be read differently depending on the surrounding text.
  • One pre-trained backbone can be reused with different output heads, such as classification, NER and question answering.
  • Avoids the very large text and compute budget needed for pre-training.

Applications

  • Text classification such as sentiment analysis and topic labelling.
  • Named-entity recognition (NER) over text.
  • Question answering and span extraction.
  • Adapting a general pre-trained language model to a specific domain with limited labelled data.

How it works

How it works

1

Start from a BERT model that has been pre-trained on large text corpora. Its bidirectional representations already encode general language structure, so the new task does not have to learn that from scratch.

2

Attach an output layer suited to the task. For sentence-level classification this is a layer applied to the [CLS] vector, as in the formula given for this method; for NER a token-level layer is used, and for question answering a span layer. The formalised expression covers only the sentence-level case.

3

Train the combined model on your labelled task dataset, updating the pre-trained weights and the new output layer so that the model specialises to the task. Before training, split the labelled data into training, validation and test subsets. The validation subset guides the choice of training length and learning settings, and the test subset is used only for the final report. Track training and validation loss across epochs: a training loss that keeps falling while the validation loss rises or stalls signals overfitting. Because the network is large, training is normally run on a GPU.

Formula
\hat{y} = \text{softmax}(W \cdot h_{\text{[CLS]}} + b)

Common pitfalls

  • Fine-tuning on too few labelled examples and trusting the resulting metrics.
  • Skipping a held-out evaluation set, so overfitting on a small dataset goes unnoticed.
  • Reporting accuracy alone on imbalanced classes, which can hide poor performance on minority labels.
  • Reaching for BERT without a simple classical baseline on the same data for comparison.
  • Training for too many epochs on a small dataset, which amplifies overfitting.
  • Reporting a single run on a small dataset. Results can vary noticeably with the random seed, so repeat runs or use cross-validation.

Frequently asked questions

How much labelled data do I need?

There is no fixed threshold. The registry gives 50 labelled examples as the minimum sample size for this method. As a rough practitioner heuristic, several hundred examples or more give more reliable results, and overfitting becomes a concern below about 500 examples. The right amount depends on the task, the number of labels and the class balance, so check performance on a held-out set. With very few examples, compare against a simple classical baseline before relying on BERT.

Do I need a GPU?

A GPU is recommended because the BERT network is large and training on a CPU is generally much slower. The training length is a setting to tune against a held-out validation set.

Why fine-tune instead of training from scratch?

BERT's pre-training has already learned general bidirectional language representations. Fine-tuning reuses them, so a usable task model typically needs far less labelled data and compute than training a model from zero.

What tasks can BERT fine-tuning handle?

By attaching a task-specific output head you can use the same pre-trained backbone for classification, named-entity recognition, and question answering, among other text tasks.

How do I know whether the model is overfitting?

Compare training loss with validation loss across epochs. If training loss keeps decreasing while validation loss rises or stalls, the model is memorising the training examples. Stop training earlier, use fewer epochs, or add more labelled data. On small datasets, repeated runs or cross-validation give a more stable picture.

Related methods

Sources

  1. Devlin, J., Chang, M.-W., Lee, K. & Toutanova, K. (2019). BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding. NAACL.

    Open DOI reference
  2. Sun, C., Qiu, X., Xu, Y. & Huang, X. (2019). How to Fine-Tune BERT for Text Classification. CCL.

    Open DOI reference