3.4.11 · D4Convolutional Neural Networks

Exercises — Transfer learning and fine-tuning

3,128 words14 min readBack to topic

This page is a self-test ladder for the parent topic. Work each problem before opening its solution. The rungs climb from L1 (just recognise the words) to L5 (design and defend a full pipeline).

Every symbol used below is defined the first time it appears. If you have not met a term, the linked prerequisite note builds it: 3.4.1-Introduction-to-CNs, 3.2.3-Gradient-descent-and-backpropagation, 3.2.5-Regularization-techniques, 3.4.7-Batch-normalization, 3.4.8-Residual-connections, 4.1.3-Data-augmentation.


Level 1 — Recognition

Goal: can you name the pieces and match them to definitions?

Exercise 1.1

A network is written as .

Here is one input image, is the full bag of numbers the network can adjust (its weights), and is the network's output. In plain words, what is and what is ? Which part is usually copied from a model trained on a big dataset?

Recall Solution
  • is the feature extractor (the "base"): the stack of convolutional layers that turns raw pixels into a compact description — think "there is a vertical edge here, fur texture there."
  • is the head: the small set of final layers that maps that description to the answer we actually want (e.g. "this is species #47").
  • The base is copied (pretrained) from the big-dataset model. The head is new, because our labels are new.

Exercise 1.2

Match each of the three strategies to its one-line description. (Recall a "block" is one named convolutional stage such as conv5_x, defined at the top of the page.)

Strategies: (A) Feature extraction, (B) Fine-tuning, (C) Full fine-tuning.

Descriptions:

  1. Unfreeze all layers; train everything end-to-end with small, layer-wise learning rates.
  2. Freeze the whole base; train only the new head.
  3. Unfreeze the last few blocks; train them plus the head with a reduced learning rate.
Recall Solution
  • A → 2 (freeze base, train only head).
  • B → 3 (unfreeze last few blocks).
  • C → 1 (unfreeze everything).

The word "freeze" means: set trainable = False, so gradient descent never updates those weights.

Exercise 1.3

True or false, with a one-sentence reason:

"Early convolutional layers (near the input) are the most task-specific and the least transferable."

Recall Solution

False. Early layers learn generic low-level features — edges, colours, simple textures — which appear in almost every visual task, so they are the most transferable. It is the late layers (near the output) that specialise to the source task's exact classes.


Level 2 — Application

Goal: plug numbers into the formulas from the parent note.

Exercise 2.1

The parent note states the heuristic The symbol means "grows in proportion to." Read it as: if two setups have the same dataset size, the one with more trainable parameters needs proportionally more data to avoid overfitting. It is a rule of thumb, not an equation: there is a hidden constant so that (roughly) , but depends on the architecture, task difficulty, and how much regularization you add, so nobody memorises a number for it. You use comparatively: to say "setup X is worse than setup Y," the unknown cancels and only the ratio of the two right-hand sides matters. "Sample complexity" here is a rough proxy for how much data you need per trainable parameter before the model stops overfitting — bigger ratio means more overfitting risk.

A ResNet-style model has total parameters. You freeze the base and keep only a head of trainable parameters. Your dataset has images.

Compute the ratio (a) if you trained all parameters, and (b) with only the head trainable. By what factor did freezing shrink the ratio?

Recall Solution

(a) All trainable: .

(b) Head only: .

Factor of improvement: .

Notice the hidden constant never appeared: because we took a ratio of two sample-complexity estimates, cancels. That is the only rigorous way to use a relation quantitatively — compare, never plug in an absolute number. Freezing the base cut the overfitting-risk ratio by , so the frozen base acts as strong regularization.

Exercise 2.2

Global Average Pooling. The base outputs a block of numbers of shape — read this as " channels, each channel being a grid." GAP collapses each channel to a single number by averaging its grid:

(a) What is the output length (how many numbers) after GAP? (b) For channel , suppose its grid contains the value in exactly cells and everywhere else. What is ?

Recall Solution

(a) One number per channel, so the output has numbers. The spatial is averaged away.

(b) The grid has cells; hold the value and the other hold . Sum . Divide by :

Exercise 2.3

Fine-tuning learning rate. The parent rule is Here the learning rate (eta) is the step size in gradient descent: bigger means bolder steps.

You trained the head with . Give the recommended range for the fine-tuning learning rate.

Recall Solution
  • Divide by : .
  • Divide by : .

So . A common single choice is .


Level 3 — Analysis

Goal: reason about why a design choice helps, using the geometry of the loss surface.

Exercise 3.1

Differential (layer-wise) learning rates use

(a) If , compute and . (b) In one sentence each, explain why early layers get the smallest rate and the head the largest.

Figure — reading the schematic below. The picture is a deliberately simplified 1-D cross-section of the loss landscape, drawn to contrast step sizes, not to depict a real network. Read every element as follows:

  • Horizontal axis = the value of one representative weight (imagine collapsing all the millions of weights onto a single left–right dial).
  • Vertical axis = the loss , i.e. how wrong the model currently is; lower is better.
  • The lavender curve is a "valley": the lowest point is the good minimum, marked by the butter dot labelled good minimum.
  • The short mint arrow on the left is one gradient-descent step for the early layers: its small length shows the small step size — barely a nudge, so the good features are preserved.
  • The long coral arrow on the right is one step for the head: its large length shows the large step size — a bold move, because the head starts random and must travel far.
  • The length of each arrow equals its learning rate (step size); direction is "downhill toward lower loss." Real loss surfaces live in millions of dimensions — this single slice merely lets us see "small step vs. big step."
Figure — Transfer learning and fine-tuning
Recall Solution

(a) . .

(b)

  • Early layers: they already encode near-universal features (edges/colours) that are correct for almost any vision task — so we want tiny nudges only. Look at the short mint arrow on the left of the figure.
  • Head: it started from random weights and knows nothing about our labels, so it needs big steps to reach a good state fast — the long coral arrow on the right.

Exercise 3.2

Catastrophic forgetting as a distance. The parent note wants the base to stay close to its pretrained point: The symbol is the ==Euclidean () norm==: for a vector it is the ordinary straight-line length you get from the Pythagorean theorem, extended to dimensions. So is the straight-line distance between the new weight vector and the pretrained weight vector — literally "how far, in total, did all the weights move together." We want it (much less than one) so the base barely budges.

Suppose after one gradient step a single weight moved from to using .

(a) What was the size of the gradient for that weight? (Use with .) (b) If instead (ten times bigger) with the same gradient, where would the weight land, and how far did it move?

Recall Solution

(a) Rearrange: .

(b) New position: . It moved ten times farther than the move in (a).

For a single weight the norm of the change is just vs. ; the small- case keeps that distance far below , the large- case starts drifting. This makes the danger concrete: same gradient, the learning rate, the drift away from the good pretrained value. Keep small to keep the drift small.


Level 4 — Synthesis

Goal: combine multiple ideas into a coherent design.

Exercise 4.1

You have three projects. For each, pick (A) feature extraction, (B) partial fine-tuning, or (C) full fine-tuning, and justify in two sentences. (Recall "block" = one named convolutional stage, e.g. conv5_x, per the definition at the top.)

  1. labelled photos of dog breeds. Source: ImageNet (which contains many dog breeds).
  2. labelled retinal (eye) scans, disease classes. Source: ImageNet natural photos.
  3. labelled satellite tiles, land-use classes. Source: ImageNet.
Recall Solution
  1. A — feature extraction. Tiny dataset () and very similar to the source (dogs are in ImageNet). The frozen base already extracts everything relevant; only a head is needed. Unfreezing would overfit images fast.
  2. B — partial fine-tuning. Medium dataset (1k–100k) but a domain shift (medical vs. natural). Keep the early edge/texture stages frozen (still useful, since retinal images still contain edges and textures) and unfreeze only the last block(s) — e.g. conv5_x — so the highest-level features can re-specialise to the appearance of retinas.
  3. C — full fine-tuning. Large dataset (k) and a big domain gap (top-down satellite vs. natural photos). Enough data to safely move all layers with differential, small learning rates for maximum performance.

Exercise 4.2

Order the pipeline. Arrange these five steps into the correct sequence and give the one-line reason for the first step's placement. ("Last block(s)" here means the final one or two named stages such as conv5_x.)

  • (i) Reduce learning rate by .
  • (ii) Train the head with the base frozen for a few epochs.
  • (iii) Load pretrained base, freeze it, attach a fresh head.
  • (iv) Unfreeze the last block(s).
  • (v) Continue training base + head jointly.
Recall Solution

Correct order: (iii) → (ii) → (iv) → (i) → (v).

  • (iii) build the model with frozen base + new head;
  • (ii) warm up the head so it is no longer random;
  • (iv) unfreeze the last block(s);
  • (i) lower the learning rate;
  • (v) train jointly.

Why (ii) — head warm-up — must come before unfreezing: a freshly-initialised head emits large, meaningless gradients. If the base is unfrozen while the head is still random, those junk gradients flow back and corrupt the pretrained features (catastrophic forgetting) before the head ever becomes useful.


Level 5 — Mastery

Goal: quantify a full pipeline end-to-end and defend it with numbers.

Exercise 5.1 — Bird species pipeline

Recreate the parent note's example. Source: ResNet-style base on ImageNet. Target: bird species, images each.

(a) Total target images? (b) The base outputs shape ; after GAP and a layer, how many weights connect GAP-output to that dense layer? (Weights only, ignore biases: it is .) (c) The final classification layer is fed by the features. How many weights there (again )? (d) Feature-extraction gave validation accuracy; fine-tuning the last block raised it to . State the absolute improvement in percentage points.

Recall Solution

(a) images — a medium dataset, so fine-tuning is appropriate.

(b) GAP produces numbers; connecting to units: weights.

(c) weights.

(d) percentage points. The gain comes from the last block (conv5_x) re-learning high-level bird-part features that generic ImageNet layers did not specialise for.

Exercise 5.2 — Effective regularization check

Continuing 5.1: the new head's trainable weights are (dense-512) (dense-200) , plus a small BatchNorm and bias count we round to a total head of trainable parameters. Total model parameters .

(a) In feature-extraction mode, what fraction of parameters is trainable (as a percentage)? (b) The parent note says heads are typically of the total. Does this fall in range?

Recall Solution

(a) .

(b) Yes — sits comfortably inside the band. Only about in parameters is learnable, which is exactly the strong-regularization effect that lets images train the model without severe overfitting.


Recall Quick self-check reveals

What does freezing the base do to the trainable-parameter ratio, and why does that help small datasets? ::: It shrinks the ratio dramatically (e.g. in Ex 2.1), acting as strong regularization so the model cannot overfit a tiny dataset. Why warm up the head before unfreezing the base? ::: A random head emits large junk gradients that would corrupt pretrained features (catastrophic forgetting) if the base were already unfrozen. Why use layer-wise learning rates with ? ::: Early layers hold near-universal features needing only tiny nudges; the head starts random and needs bold steps. What is a "block" in a ResNet-style network? ::: One of the four named residual convolutional stages (conv2_xconv5_x) that the network treats as a single freeze/unfreeze unit.