ML Systems Lab Open interactive version →
Intermediate 29 min read pre-trainingtransfer learningBERTGPTfine-tuning

Pre-training & Transfer Learning

Masked LM, causal LM, BERT vs GPT objectives, feature extraction vs fine-tuning

The Transformer module closed on architecture — how attention, residuals, and Pre-LN normalisation combine into a stable, stackable block. None of that explains how the *weights inside* that architecture come to know anything about language before you've trained them on your task at all. That's a separate question, and it's the one this module answers.

You have 500 labelled radiology reports and need to classify them by findings. Train a model from scratch on those 500, and it has to learn *everything* at once — what "pulmonary" means, that "nodule" is worrying, that "no evidence of" flips the meaning, *and* the actual classification rule — all from 500 examples. It ends up memorising quirks that do not generalise, and test AUC lands at a dismal 0.61.

Now do one thing differently: start from PubMedBERT, a model already trained on 14 million medical papers, and fine-tune it on the *same* 500 reports. Test AUC: 0.87. Nothing about your labels or task changed. What changed is the *starting point* — PubMedBERT already knows medical language, so your 500 labels only have to teach it the final decision, not the entire vocabulary. This is transfer learning, and it is one of the highest-leverage ideas in modern ML.


What pre-training actually does

Picture the model's millions of weights as coordinates on a vast, foggy mountain range, where height measures how badly the model performs at language and low valleys are where it performs well. Training from scratch is being dropped at a *random* point in that fog with only a few hundred noisy update steps — as many as 500 labelled examples can drive — to feel your way toward a good valley; that's barely enough to get off the plateau you happened to land on, which is exactly why the from-scratch radiology model stalled at 0.61.

Pre-training is a helicopter ride to a known region of that range before you ever start hiking. You take a mountain of unlabelled text and make the model play fill-in-the-blank or predict-the-next-word, billions of times — this is *self-supervised* learning, since the "labels" (the missing or next word) come free from the text itself. To get good at that game the model is forced to internalise how language works — grammar, vocabulary, which words go together, domain structure — and all of that gets baked into its weights as coordinates, landing it at a "base camp" a short hike from a huge number of good valleys, including yours. Fine-tuning is that short hike: your 500 examples only have to nudge the model from base camp to the *particular* valley your task needs, not search the whole foggy range from a random drop point.


Two pre-training styles, two strengths

The *game* you make the model play shapes what it becomes. Masked language modelling (BERT) hides about 15% of the words and asks the model to fill them in using context on *both* sides — which produces rich, full-context representations, ideal for *understanding* tasks. Causal language modelling (GPT) predicts each next word from only the words *before* it — which is denser training (every token is a target) and lines up naturally with *generating* text. That is why understanding tasks lean on BERT-style models and generation leans on GPT-style ones.


The one danger: forgetting what it knew

Fine-tuning has a trap called catastrophic forgetting: hit the pre-trained model with a big learning rate on your small dataset and you *overwrite* the very knowledge that made it valuable, collapsing it onto your narrow task. The standard safety recipe is a *gentle* touch — a learning rate 10–100× smaller than pre-training used, only a few epochs, a short warmup, and a little weight decay — small enough steps that you *stay near* the pre-trained starting point instead of wandering off and erasing it.

Key points

Takeaway

Pre-training changes the optimization starting point, not just the weight scale — it places the model in a loss basin near representations that generalize, which is why 500 fine-tuning examples produce a 26-point AUC gain that no amount of regularization from random initialization can replicate.

Recap

Check your understanding

Q1. BERT masks 15% of tokens and predicts them. Why 15% and not 50% or 1%? Explain the tradeoff. Select the TWO correct statements.

Q2. GPT is trained with causal (autoregressive) language modelling, BERT with masked language modelling. Which is better for generation, and why can't you use BERT for generation directly?

Q3. What is catastrophic forgetting in neural networks, and why does it make sequential fine-tuning on multiple tasks difficult?

Try it interactively

ML Systems Lab is a free interview-prep platform for ML engineers — work through the full interactive module, quizzes, and drills.

Open ML Systems Lab →