C.W.K.
Stream
Lesson 04 of 07 · published

Fine-Tuning

~9 min · transfer

Level 0Keras Apprentice
0 XP0/97 lessons0/20 achievements
0/120 XP to next level120 XP to go0% complete

Step 2: unfreeze the top, retrain with a tiny learning rate

Once the head is trained and your accuracy has settled, fine-tuning squeezes out the rest. You flip base_model.trainable = True and then re-freeze everything except the last handful of layers. Why only the top? The early layers hold the universal edge/texture detectors you want to keep; the later layers hold the ImageNet-specific concepts that are worth gently bending toward your task.

The learning rate is the whole game

The single most important number here is the learning rate, dropped to roughly 1e-5 — about 100× lower than normal training. Your pretrained weights are already near a good solution; a normal-sized LR would take huge steps and wreck them (catastrophic forgetting). The tiny LR nudges them just enough to specialize without unlearning. Fine-tune for ~10–20 more epochs and watch validation accuracy — if it stops improving or starts overfitting, stop. The Code section walks the exact freeze/recompile/fit sequence.

Code

Unfreeze the top layers and fine-tune at 1e-5·python
# Unfreeze the top layers of the base model
base_model.trainable = True

# Freeze everything except the top 20 layers
for layer in base_model.layers[:-20]:
    layer.trainable = False

# CRITICAL: Re-compile with a very low learning rate
model.compile(
    optimizer=keras.optimizers.Adam(1e-5),  # 100x lower!
    loss="categorical_crossentropy",
    metrics=["accuracy"],
)

# Continue training (~10-20 more epochs)
model.fit(train_ds, epochs=20, validation_data=val_ds)

External links

Exercise

Take your feature-extraction model. Unfreeze the last 30 layers of the backbone. Re-compile with learning_rate=1e-5. Train 3 more epochs. Compare validation accuracy before/after.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.