liveBook · Manning

Fine-tuning

As explained in Deep Learning with Python, Third Edition

Adapting a pretrained model to a new task by continuing training, usually with a smaller learning rate or on additional data.

A pretrained model already knows useful patterns, but its predictions may not fit your task. Fine-tuning continues training that model on task-specific data, adjusting some or all of its learned representations. For a small dogs-versus-cats dataset, for example, an ImageNet-pretrained Xception network offers a starting point instead of learning visual features from scratch. section 8.3.2 section 8.3

For image classification, use this order:

  • Add a classifier to the pretrained convolutional base, freeze the base, and train the new classifier first. Unfreezing immediately lets large errors from the randomly initialized classifier damage the pretrained representations. section 8.3.2
  • Then unfreeze the base and train it jointly with the classifier. Keep BatchNormalization layers frozen. With a large base, consider unfreezing only its upper layers: lower layers encode more generic features, while upper layers are more specialized. section 8.3.2
  • Compile with a small learning rate, monitor validation loss, save the best checkpoint, and evaluate that saved model on test data. section 8.3.2
Listing 8.31 Fine-tuning the model
model.compile(
    loss="binary_crossentropy",
    optimizer=keras.optimizers.Adam(learning_rate=1e-5),
    metrics=["accuracy"],
)

callbacks = [
    keras.callbacks.ModelCheckpoint(
        filepath="fine_tuning.keras",
        save_best_only=True,
        monitor="val_loss",
    )
]
history = model.fit(
    augmented_train_dataset,
    epochs=30,
    validation_data=validation_dataset,
    callbacks=callbacks,
)

The Xception training listing uses Adam at 1e-5, augmented training images, validation data, and a checkpoint selected by validation loss. section 8.3.2

The same idea applies beyond images. For IMDb sentiment classification, a pretrained RoBERTa backbone gains a binary classification head. Its first token’s output supplies one representation for the whole review; the resulting classifier is trained and evaluated on labeled reviews. Match the tokenizer’s pretrained sequence format, including start, end, and padding tokens, before training. In this example, one epoch reaches about 93% test accuracy, at a higher computational cost than a simpler bigram classifier. section 15.4.3 section 15.4.4

For a language model, instruction fine-tuning uses instruction–response pairs as further training data. Consistent markers distinguish the instruction from the response, and training can still use next-token prediction. This nudges output toward desired responses; it does not relearn language from zero. Handwritten responses provide supervised fine-tuning, but collecting them is slow and expensive. Reinforcement learning with human feedback builds on that baseline using ranked responses and a reward model. Fine-tuning can improve instruction following, but it is not a cure for hallucinations. section 16.3.2 section 16.4.1 section 16.3.1

How Deep Learning with Python, Third Edition covers it

How deep it goes
Works through freeze–train–unfreeze steps, classifier construction, preprocessing, and training listings; treats batch normalization details as a later topic.
Examples
Uses Xception for dogs versus cats, RoBERTa for IMDb sentiment, and Gemma with instruction–response pairs.
What it assumes
Assumes access to a pretrained model and task data, while teaching the freeze–train–unfreeze sequence explicitly.
What you can do after
Apply it via Keras and KerasHub to adapt pretrained classifiers and train a language model on instruction–response pairs.
How it connects
Connects feature extraction, data augmentation, pretrained Transformers, instruction fine-tuning, and reinforcement learning with human feedback.

Also explained in

Compare how 37 books explain fine-tuning →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with Python, Third Edition. The section links point to the parts of the book it draws on.