TensorFlow, Interactively

advanced · ~30 min

Transfer Learning

Take a model pretrained on millions of images and fine-tune it on a tiny dataset in seconds, right in the browser.

This module builds on Training Dynamics Lab. Feel free to jump ahead anyway.

Training a network from scratch on a handful of examples usually fails — there just isn't enough signal to learn good features. But a network trained on millions of images has already learned something useful along the way: its early layers detect edges, textures, and simple shapes, and its later layers combine those into increasingly task-specific concepts. Transfer learning reuses that early, general-purpose knowledge instead of relearning it from zero.

The standard recipe: take a pretrained model, chop off its final classification layer (the part that's specific to its original task), bolt on a fresh layer for your task, and train. You have two choices for the reused part — the backbone:

  • Feature extraction — freeze the backbone completely. Only the new head trains. Fast, and works well when your dataset is tiny and similar in kind to what the backbone originally saw.
  • Fine-tuning — unfreeze some or all of the backbone too, usually at a much lower learning rate than the head. Lets the features adapt to your task, at the cost of needing more data to avoid wrecking what the backbone already knew.
🔍 Deep dive: Why frozen features generalize from so little data

A randomly-initialized network has to discover and combine useful features from scratch, and with only a handful of examples it will happily memorize them instead — there's no pressure to find features that generalize. A pretrained backbone has already done the expensive part: its features were shaped by a much larger, more diverse dataset, so they tend to be broadly useful even for a task it never saw. Freezing it means the only thing left to learn is a much simpler problem — a linear-ish combination of already-good features — which needs far fewer examples to get right.

The playground below can't download a real pretrained vision model into your browser tab, so it simulates the same mechanics honestly: a small network is first trained on a large, unrelated 2D task (standing in for "pretraining on ImageNet"), then its first two layers — the backbone — are reused on a completely different, tiny target dataset. Toggle the backbone between frozen and fully trainable and watch how much faster (and better) the frozen version generalizes from the same few points.

Production note

In Keras, freezing is just base_model.trainable = False before compiling — as long as you recompile after changing it, since trainable is baked into the compiled training step. Fine-tuning afterward means flipping it back to True, recompiling with a low learning rate (often 10-100x smaller than what trained the head), and continuing training. Skipping the learning-rate drop is the single most common way to ruin a pretrained backbone.

Beginner tip

If the frozen-backbone curve in the playground doesn't obviously beat "from scratch" on every single run, that's expected — both are training on a handful of points, so there's real run-to-run noise. Reset a few times and look at the pattern across runs, not any one of them.

Playground

BackboneOnly 12 labeled target-task points — few-shot on purpose.
Step 0frozen val acc: 0% · from-scratch val acc: 0%
  • from scratch
  • frozen backbone
steptraining loss