Applications of Machine Learning

Lecture 13 · Animations

Transfer learning

Two toys for two of today's decisions: whether to train from scratch or probe a pretrained backbone, and how fast to let pretrained weights move. They are toys. The networks are small, the inputs are points in the plane, and the "pretrained" backbone was trained a moment ago, in your browser, on a related synthetic task. They show the mechanism the lecture measured on Flowers102, not its numbers.

All data on this page is synthetic, generated in your browser from a fixed seed, so Reset always gives back the same picture.

1A frozen backbone against training from scratch

The new task has three classes: which of three rings a point lies in. There are only four labelled points per class (the large markers). On the left, a linear head trained on the features of a frozen backbone. That backbone, a $2\to 32\to 8$ ReLU network, was pretrained on 600 labelled points of a different, 12-class task. On the right, the same architecture trained from scratch on the same twelve points. Shading is each model's predicted class; dashed circles are the true boundaries. Each step is one epoch for both.

The mathematics

The backbone maps a point to features $\mathbf h(\mathbf x) = \operatorname{ReLU}\bigl(W_2\operatorname{ReLU}(W_1\mathbf x + \mathbf b_1) + \mathbf b_2\bigr) \in \mathbb R^8$. The probe fits only the head, scores $\mathbf t = V\mathbf h(\mathbf x) + \mathbf c$ and softmax, by minimising the cross-entropy on the labelled points. The trainable parameters are

$$\begin{aligned}\text{probe:}\quad & 8\cdot 3 + 3 = 27,\\ \text{scratch:}\quad & (2\cdot 32 + 32) + (32\cdot 8 + 8) + 27 = 387.\end{aligned}$$

With $W_1, W_2$ frozen, $\mathbf h(\mathbf x)$ never changes, so it is computed once per point. The probe is then softmax regression on a table, and its loss is convex in $(V, \mathbf c)$. Both models use Adam with $\eta = 0.05$ on full batches. The pretraining task is "which ring and which quadrant", 12 classes. Its alternative, "which of 8 angular sectors", never needs the distance from the centre.

Watch for
  • The probe's regions are rings within a few dozen epochs, and its test accuracy ends near 90%. The backbone already separates points by radius, because its own task needed that, so twelve labels only have to say which ring is which.
  • From scratch, training accuracy reaches 100% while test accuracy stays near 50%. Twelve points pin down 387 parameters badly: the boundaries pass the training points and wander everywhere else. That is variance: the fitted boundary depends on which twelve points were drawn.
  • The probe never runs the backbone again. Its epochs cost a $3\times 8$ product per point.
Try this
  1. Switch the backbone to one pretrained on angular sectors. Its features describe direction and discard distance, and the probe falls to about 40%, below training from scratch. This is the lecture's "when transfer fails": the probe tells you so in seconds.
  2. Raise the labels to 32 per class. From scratch now reaches about 90%, close to the probe. Transfer pays most where labels are scarce.
  3. Drop to 2 labels per class. Both models suffer, the probe less.

2Unfreezing, and how fast

The backbone from the first widget, now unfrozen, on the same twelve labels. Three runs start from the same pretrained weights and the same fresh head. The first keeps the backbone frozen. The second lets it move at a tenth of the head's learning rate. The third lets it move at the head's own rate. On the left, the regions of the run you choose. On the right, test accuracy on the new task. Below, how well each backbone can still do the old task, read through the old head kept aside.

The mathematics

One optimiser, two parameter groups: the head moves with $\eta_{\text{head}} = 0.05$ and the backbone with $\eta_{\text{body}}$, which is $0$, $0.005$ or $0.05$. That is all a differential learning rate is. How far the backbone has moved is measured as in Lecture 9:

$$\text{drift} = \frac{\lVert W - W^{(0)}\rVert}{\lVert W^{(0)}\rVert},$$

over both backbone weight matrices together. The old-task accuracy runs the current backbone under the old 12-class head, which nobody trains any more, on the 600 points it was pretrained on. While the backbone is frozen, that accuracy cannot change.

Watch for
  • At the head's rate, the backbone drifts by more than half its own size. Its old-task accuracy collapses, from about 100% to below 40%. The features it came with have been overwritten, and its new-task accuracy ends near 70%, well below the frozen run.
  • At a tenth of the rate, the drift stays near 5%, the old task survives at close to 90%, and the new-task accuracy ends within two points of the frozen run. It gets there sooner, but it does not end higher.
  • By the end, nothing beats the frozen backbone. With four labels per class that is the lecture's first row: probe only, because fine-tuning has little left to buy and something to lose.
Try this
  1. Show the same rate run's regions while it plays, and watch the rings deform as the backbone bends its features towards twelve points. Then replay with rate ÷ 10, whose rings hold.
  2. Read the drift column at the end. The ranking of new-task accuracy follows it in reverse.