0Pricing
Learn AI with Python · Lesson

Fine-tuning: Unfreezing and Retraining

base_model.trainable = True, layer-by-layer unfreezing, differential learning rates.

Fine-tuning: Unfreezing and Retraining is a free Learn AI with Python lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

When to Fine-Tune

After feature extraction trains your head, fine-tuning squeezes out extra accuracy by letting the top of the pretrained base adapt to your data. Do it only once the head is stable, otherwise large gradients from a random head can damage the base.

Unfreezing the Base

Set base.trainable = True to make weights updatable again. By itself this unfreezes every layer, which is usually too aggressive.

base.trainable = True
print("Trainable layers:", sum(l.trainable for l in base.layers))

Freeze the First N Layers

Early layers hold generic edge/texture detectors you want to keep. Re-freeze the first N layers and only fine-tune the deeper, task-specific ones.

base.trainable = True
freeze_until = 100   # keep first 100 layers frozen

for layer in base.layers[:freeze_until]:
    layer.trainable = False
for layer in base.layers[freeze_until:]:
    layer.trainable = True

Why a Very Low Learning Rate

Fine-tuning needs a tiny learning rate such as 1e-5. The pretrained weights are already good; you only want gentle nudges. A large rate causes catastrophic forgetting, erasing the ImageNet knowledge you came for.

Recompile After Unfreezing

You must recompile after changing trainable flags so Keras rebuilds which weights get gradients. Set the low learning rate here.

from tensorflow.keras.optimizers import Adam

model.compile(
    optimizer=Adam(learning_rate=1e-5),
    loss="categorical_crossentropy",
    metrics=["accuracy"]
)

The Two-Phase Process

  1. Phase 1 (feature extraction): base frozen, train head at ~1e-3 until validation accuracy plateaus.
  2. Phase 2 (fine-tuning): unfreeze top layers, recompile at ~1e-5, continue training.

Splitting it this way stabilizes training and protects the pretrained features.

Continuing Training in Phase 2

Resume training from where phase 1 stopped using initial_epoch so logs and schedules stay continuous.

history2 = model.fit(
    train_ds,
    validation_data=val_ds,
    epochs=20,
    initial_epoch=10   # phase 1 ended at epoch 10
)

Learning Rate Scheduler

A scheduler decays the learning rate over time so updates shrink as the model converges. ReduceLROnPlateau automatically lowers it when validation loss stalls.

from tensorflow.keras.callbacks import ReduceLROnPlateau

lr_cb = ReduceLROnPlateau(
    monitor="val_loss", factor=0.5,
    patience=2, min_lr=1e-7
)
model.fit(train_ds, validation_data=val_ds,
          epochs=20, callbacks=[lr_cb])

BatchNorm Caution

ResNet and EfficientNet contain BatchNormalization layers that track running statistics. When fine-tuning, keeping BN layers frozen (or in inference mode) often gives more stable results, because updating their statistics on a small dataset can hurt accuracy.

Watch for Overfitting

Fine-tuning adds many trainable parameters, so overfitting risk rises. Combat it with data augmentation, dropout, and EarlyStopping on validation loss.

from tensorflow.keras.callbacks import EarlyStopping

stop = EarlyStopping(monitor="val_loss",
                     patience=4,
                     restore_best_weights=True)

Putting It Together

A robust fine-tuning run: train head, unfreeze top layers, freeze BN, recompile at 1e-5, then train with a scheduler and early stopping. Always compare validation accuracy before and after to confirm fine-tuning actually helped.

Quick Check

Test your fine-tuning knowledge.

Recap

You learned to fine-tune by setting base.trainable=True, re-freezing the first N layers, recompiling at a tiny learning rate (1e-5), and running a two-phase process. A learning rate scheduler and early stopping keep it stable, and BatchNorm layers deserve care. Next: lightweight models for edge deployment.

Frequently asked questions

Is the “Fine-tuning: Unfreezing and Retraining” lesson free?

Yes — the full text of “Fine-tuning: Unfreezing and Retraining” is free to read here on the web, and the Learn AI with Python course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.

What will I learn in “Fine-tuning: Unfreezing and Retraining”?

base_model.trainable = True, layer-by-layer unfreezing, differential learning rates. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Learn AI with Python?

No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Fine-tuning: Unfreezing and Retraining” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Learn AI with Python lesson?

Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Transfer Learning Concepts and Strategies
  2. Using VGG16 and ResNet50 as Base Models
  3. Fine-tuning: Unfreezing and Retraining
  4. MobileNet and EfficientNet for Edge Deployment
← Back to Learn AI with Python