In this lesson: how you teach a small pretrained model your own labels (fine-tuning), how you find out whether it really learned, why more training stops helping once the model knows its training sentences, and why it learns your words, not your meaning. Everything here was run on a tiny model, bert-tiny, on 2 CPUs. Read the lesson and take the 5-question quiz: each question has a hint if you get stuck, and its explanation shows once you get it right (all of them at full marks). Pass the quiz to finish the lesson and earn its badge.
1. An untaught model: a coin toss
We gave bert-tiny a "worried / excited" output and asked about four sentences before teaching it anything. Before you look: what will it say about each, and how sure? Here is what it said (the run took about 7 seconds on 2 CPUs):
| Sentence | Really | It said | How sure |
|---|
| I have three tests tomorrow and I have not studied. | worried | worried | 0.50 |
| We won the basketball match after school! | excited | worried | 0.54 |
| I left my lunch at home. | worried | worried | 0.53 |
| The summer holiday starts on Friday. | excited | worried | 0.53 |
All four came out "worried", at about 0.50–0.54: a coin toss, "right" on two only because two really are worried. As it loaded, the model reported its worried / excited layer as missing from the file: it was newly made. (The model gives each label a score; the two scores are turned into chances that add up to 1, and the bigger one is its answer.)
Untaught: “worried” for all four, about 0.5
2. A trained body, a new head
bert-tiny's 4,386,178 parameters are the numbers inside it. Almost all of them (the model's body) were trained on lots of English (bert-tiny knows only English), mainly by filling in hidden words, so they hold patterns of how words are used; but nobody ever showed it what "worried" means. We added a classification head: one small layer that turns the sentence's 128 numbers into 2 scores, one per label, with 258 parameters (128×2+2) set at random. Random head, random answers.
A trained body plus a new, random head
3. Fine-tuning: nudge, check, repeat
Fine-tuning teaches the whole model your labels with examples:
- Run a batch of 8 labelled sentences through the model.
- Measure the loss: how surprised the model is by the right labels (this way of measuring it is called cross-entropy; low when it gives the right label a high chance).
- Work out which way each of the 4 million numbers should move to lower the loss (the gradient), then move each a tiny bit (the learning rate, 10−4, that is 0.0001). That is one step, once for every batch (a last, smaller batch counts too).
One pass over all the training sentences is an epoch. Our run: 32 school sentences we wrote (16 excited, 16 worried); the first four of each, §1's four among them, are held out as a test set and never trained on, the other 24 train it. Each epoch the sentences are shuffled into new batches. While it learns, the model also ignores a few random numbers each time (dropout), so it can't lean on any one of them; to score, it uses them all. Accuracy is the share of sentences it labels right. We scored it every 5 epochs:
| before | epoch 5 | epoch 10 | epoch 15 | epoch 20 |
|---|
| training accuracy | 0.54 | 0.71 | 1.00 | 1.00 | 1.00 |
| test accuracy | 0.50 | 0.50 | 0.88 | 0.88 | 0.88 |
The 20 epochs took about 2 seconds. The test score stopped rising at epoch 10.
Batch, loss, gradient, step: epoch after epoch
4. The only honest score: held out
Accuracy on the training sentences says how well the model memorised them; what matters is new sentences, so the test set is kept aside. 24 training sentences took the test score from 0.50 to 0.88 (7 of 8).
A line that sounds right and isn't: "Once the training accuracy reaches 100 %, the model will be just as accurate on new sentences." At epoch 10 the training score is 1.00 and the test score 0.88. Only the held-out set tells you about new sentences.
Training 1.00, test 0.88: only held out counts
5. More epochs, or more data?
With a bigger set (our own 40 worried and 40 excited school sentences, 60 for training and 20 held out), we averaged the test score over 10 runs, each from a different random start (a different seed). More epochs on the same 60 sentences:
| 5 epochs | 15 epochs | 40 epochs |
|---|
| training accuracy | 0.85 | 1.00 | 1.00 |
| test accuracy | 0.79 | 0.88 | 0.91 |
Epochs helped while the model was still learning. Once it had its training sentences all right, at 15 epochs, 25 more epochs moved the test score by 0.03, less than one sentence. More sentences of the same kind did barely better: at 40 epochs, 20 / 40 / 60 training sentences gave 0.89 / 0.91 / 0.91. Extra sentences that repeat a pattern can even make it worse; what helps is sentences unlike the ones it has (§6). Past a training score of 1.00, there was little left for those sentences to teach.
Training longer can also hurt. Overfitting is when, later in the same run, the training score stays perfect (or keeps rising) while the held-out score falls: the model learns these exact sentences instead of the pattern. We didn't see it here (0.88–0.91 from 15 to 40 epochs), and you only see it on held-out sentences.
Watch the noise: with 20 test sentences, one sentence is 0.05, and single runs differing only in seed scored 0.65 to 0.95. Compare averages over several seeds before you call one setting better.
Past training 1.00, more adds little
6. It learns your words, not your meaning
Trained on all 80 of our sentences, it calls "I am not worried about the test." worried, at 0.95–0.96 (over three seeds). In our data, "not" (5 times) and "worried" (6 times) appear only in worried sentences; asked about single words, it called "not" on its own worried at 0.87–0.94, but "test" only at 0.26–0.42. So it learned that "worried" and "not" mean worried, not what "not worried" means. A shortcut is a cue that separates your labels in your data without meaning the label. After 40 epochs, some held-out sentences were still often wrong. "I scored a goal in PE today." (wrong in 6 to 9 of 10 runs): most likely because "PE" appeared only in worried training sentences (our reading: we didn't test "PE" alone). "I passed my maths exam!" (wrong in 5 of 10; with 20 or 40 training sentences it was never wrong, only once the 60 held six worried ones about exams). Just as often missed, "I am worried about moving to a new school." (6 of 10), even though it contains "worried", fits no story we could find: a model's mistakes don't always have a tidy reason.
The model learns whatever separates your labels in your data, including things you didn't mean, so "after training, the model knows what worried means" is wrong. More epochs didn't fix it (those sentences were still wrong at 40); only examples that break the pattern can, such as excited sentences that use "not" ("I could not sleep, I was so excited!") or "worried" ("I was worried, but we won!").
Fine-tuning a bigger model, in any language, works the same way: labelled examples, a loss, small nudges, then new sentences you write to break your data's patterns.
It learned “not” and “worried” mean worried
Do it yourself in the ML Lab Mood Sorter, step Trick it.