Learning rate controls how much the model changes with each step. For fine-tuning, a smaller learning rate helps the model adjust gently to new data without forgetting what it learned before.
Learning rate for fine-tuning in TensorFlow
optimizer = tf.keras.optimizers.Adam(learning_rate=0.0001) model.compile(optimizer=optimizer, loss='sparse_categorical_crossentropy', metrics=['accuracy'])
The learning_rate value is usually smaller for fine-tuning than for training from scratch.
You can use different optimizers like Adam, SGD, or RMSprop with a custom learning rate.
optimizer = tf.keras.optimizers.Adam(learning_rate=0.00001) model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'])
optimizer = tf.keras.optimizers.SGD(learning_rate=0.0001, momentum=0.9) model.compile(optimizer=optimizer, loss='sparse_categorical_crossentropy', metrics=['accuracy'])
This example shows how to load a pre-trained model, freeze it, add a new head, and train with a small learning rate. Then it unfreezes the base model and fine-tunes with an even smaller learning rate.
import tensorflow as tf # Load a pre-trained MobileNetV2 model without the top layer base_model = tf.keras.applications.MobileNetV2(input_shape=(96, 96, 3), include_top=False, weights='imagenet') base_model.trainable = False # Freeze base model # Add new classification head inputs = tf.keras.Input(shape=(96, 96, 3)) x = base_model(inputs, training=False) x = tf.keras.layers.GlobalAveragePooling2D()(x) x = tf.keras.layers.Dense(10)(x) # 10 classes outputs = x model = tf.keras.Model(inputs, outputs) # Compile with a small learning rate for fine-tuning optimizer = tf.keras.optimizers.Adam(learning_rate=0.0001) model.compile(optimizer=optimizer, loss='sparse_categorical_crossentropy', metrics=['accuracy']) # Create dummy data import numpy as np x_train = np.random.random((20, 96, 96, 3)) y_train = np.random.randint(0, 10, 20) # Train the model history = model.fit(x_train, y_train, epochs=2, batch_size=5, verbose=2) # Unfreeze base model for fine-tuning base_model.trainable = True # Recompile with even smaller learning rate optimizer_finetune = tf.keras.optimizers.Adam(learning_rate=0.00001) model.compile(optimizer=optimizer_finetune, loss='sparse_categorical_crossentropy', metrics=['accuracy']) # Continue training history_finetune = model.fit(x_train, y_train, epochs=2, batch_size=5, verbose=2)
Start with a small learning rate to avoid destroying the pre-trained features.
After initial training, unfreeze some layers and use an even smaller learning rate for fine-tuning.
Monitor training metrics to check if the learning rate is too high or too low.
Use a smaller learning rate when fine-tuning to make gentle updates.
Freeze the base model first, then unfreeze and fine-tune with a smaller learning rate.
Choose learning rates carefully to balance learning speed and stability.