Transfer learning: keep the representation, change the task
Transfer learning reuses knowledge learned on one task or dataset for another. A pretrained image encoder can supply features for a new classifier; a language model can be adapted to a domain.
A useful starting point is to freeze the pretrained representation and train only a new output layer. Fine-tuning later allows some or all of the existing weights to change too.
Build a new head on a pretrained encoder
This TensorFlow/Keras example downloads MobileNetV2 weights and builds a three-class classifier for 224×224 RGB images with pixel values in 0–255. Supply your own labelled training data to model.fit.
from tensorflow import keras
base = keras.applications.MobileNetV2(
include_top=False, weights="imagenet", pooling="avg"
)
base.trainable = False
inputs = keras.Input(shape=(224, 224, 3))
scaled = keras.applications.mobilenet_v2.preprocess_input(inputs)
features = base(scaled, training=False)
outputs = keras.layers.Dense(3)(features)
model = keras.Model(inputs, outputs)
model.compile(
optimizer=keras.optimizers.Adam(1e-3),
loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
)
The preprocessing matches the pretrained encoder. training=False keeps its batch-normalization behaviour fixed while the new head learns.
A frozen encoder makes the first stage cheaper, but its representation must contain information useful for the new task. Unfreezing with a smaller learning rate can adapt it further; recompiling is necessary after changing Keras trainability.