Sub topic 1
Why Fine-tune a Pretrained Transformer
Prerequisites
This chapter assumes that you know Python, the purpose of training and test sets, and the basic idea of a neural network. You should also know that an embedding represents an item, such as a word, as a vector of numbers. The later chapters use PyTorch tensors and the Hugging Face transformers and datasets libraries.
The basic idea
A transformer is a neural network architecture that processes sequences, such as sentences. It uses attention to compare each token with other tokens in the sequence. This helps it represent context. For example, the word “great” can contribute differently in “a great film” and “not a great film.”
A pretrained transformer has already learned general language patterns from a large collection of text. Pretraining usually does not teach the model your exact labels. Instead, it teaches the model useful representations of words, syntax, and relationships between parts of a sentence.
Fine-tuning means continuing training on a smaller, labeled dataset for a specific task. For sentiment classification, your examples might contain a review and one of two labels: NEGATIVE or POSITIVE. Fine-tuning adjusts the pretrained model so that its representations support this decision.
The process looks like this:
Why not train from scratch?
Training from scratch requires a large dataset, substantial compute, and careful optimization. A randomly initialized model must learn both general language structure and the classification task. A small labeled dataset usually does not contain enough information for this.
Fine-tuning starts with useful parameters. The model may already recognize negation, common grammatical patterns, and relationships between words. The labeled dataset then needs to teach the model only how those patterns relate to your labels.
| Approach | Data requirement | Training cost | Typical use |
|---|---|---|---|
| Train from scratch | Very large | High | Building a new general language model |
| Fine-tune all parameters | Moderate labeled data | Moderate to high | Best task performance when resources allow |
| Use a frozen encoder | Smaller labeled data | Lower | Quick baselines or limited compute |
Fine-tuning is transfer learning: knowledge learned for one purpose is reused for another purpose. The source task is general language modeling. The target task is your classification problem.
What changes during fine-tuning?
A transformer used for classification normally has two parts:
- An encoder converts the input tokens into contextual representations.
- A classification head converts a selected representation into class scores.
The class scores are called logits. For two labels, the head produces two logits. A softmax function can turn them into probabilities, although the training loss can work directly with logits.
During full fine-tuning, the optimizer updates the encoder and the classification head. The learning rate is usually small because the encoder already contains useful knowledge. Large updates can damage that knowledge, a problem sometimes called catastrophic forgetting.
The classification head starts with task-specific parameters, often randomly initialized. It therefore needs enough training to learn the relationship between the encoder's output and the labels.
A practical decision
Use fine-tuning when you have labeled examples and need a model for a defined task, such as sentiment, topic, intent, or toxicity classification. Start with a pretrained encoder that matches your language and input type.
Compare a fine-tuned model with a simple baseline. If a keyword rule or linear model performs nearly as well, the transformer may not justify its additional cost. If the transformer performs better, inspect its validation results and its mistakes before deploying it.
The next step is to turn raw text into the token IDs that the pretrained transformer expects. Tokenization, truncation, and padding determine what information reaches the model.
Exercise
Why can fine-tuning work well with fewer labeled examples than training a transformer from scratch?
Show answer
Pretraining has already taught the transformer general language patterns. Fine-tuning mainly adapts those existing representations and adds a mapping from them to the task's labels.