11. Pretraining, Fine-tuning and Transfer Learning
Learn why modern NLP rarely starts from random weights.
01 Learning objectives
By the end of this module, you should be able to explain the concept in plain language, connect it to a use case, and recognize when it is — and is not — appropriate.
-
Objective 1
Understand pretraining objectives.
-
Objective 2
Understand task fine-tuning.
-
Objective 3
Know when to freeze, fine-tune or prompt.
02 Pretraining
Large models first learn broad language patterns from very large corpora using self-supervised objectives such as masked-token prediction or next-token prediction.
03 Fine-tuning
Fine-tuning adapts pretrained weights to a downstream task using a smaller labeled dataset.
04 Hugging Face example
from transformers import AutoTokenizer, AutoModelForSequenceClassification
name = "distilbert-base-uncased"
tokenizer = AutoTokenizer.from_pretrained(name)
model = AutoModelForSequenceClassification.from_pretrained(
name,
num_labels=6
)
batch = tokenizer(
["My account shows an unauthorized debit."],
padding=True,
truncation=True,
return_tensors="pt"
)
logits = model(**batch).logits
05 Pitfalls and nuances
These are the mistakes most likely to create a wrong mental model or a weak implementation.
-
Pitfall 1
Fine-tuning with too high a learning rate.
-
Pitfall 2
Ignoring class imbalance and label quality.
-
Pitfall 3
Evaluating on data too similar to training examples.
06 Checkpoint questions
Try answering these without looking back at the module. If the explanation is clear in your own words, you are ready to continue.
-
Check 1
What knowledge is transferred during fine-tuning?
-
Check 2
Why is a tokenizer model-specific?