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.
Practice
Solution
Step 1: Understand fine-tuning purpose
Fine-tuning adjusts a pre-trained model slightly to new data without losing learned knowledge.Step 2: Reason about learning rate effect
A smaller learning rate makes small changes, preserving useful features learned before.Final Answer:
To make gentle updates and avoid destroying learned features -> Option CQuick Check:
Small learning rate = gentle fine-tuning [OK]
- Using a large learning rate that overwrites learned features
- Ignoring the need to freeze layers before fine-tuning
- Assuming learning rate affects batch size
Solution
Step 1: Identify typical fine-tuning learning rates
Fine-tuning usually uses smaller rates like 0.0001 to avoid big weight changes.Step 2: Compare given learning rates
0.0001 is smaller than 0.001, 0.01, and 0.1, making it suitable for fine-tuning.Final Answer:
optimizer = tf.keras.optimizers.Adam(learning_rate=0.0001) -> Option AQuick Check:
Smaller learning rate = 0.0001 [OK]
- Choosing a learning rate too large for fine-tuning
- Confusing learning rate with batch size
- Not using tf.keras.optimizers.Adam correctly
base_model.trainable = True optimizer = tf.keras.optimizers.Adam(learning_rate=0.01) model.compile(optimizer=optimizer, loss='sparse_categorical_crossentropy', metrics=['accuracy']) history = model.fit(train_data, epochs=3)What is the likely outcome of using this learning rate during fine-tuning?
Solution
Step 1: Analyze learning rate size
Learning rate 0.01 is relatively large for fine-tuning, which risks big weight changes.Step 2: Predict training behavior
Large learning rate can cause unstable training and overwrite useful pre-trained features.Final Answer:
The model may have unstable training and lose pre-trained knowledge -> Option BQuick Check:
Large learning rate = unstable fine-tuning [OK]
- Assuming higher learning rate always improves training speed
- Ignoring the effect of unfreezing the base model
- Thinking loss function controls learning rate impact
base_model.trainable = True optimizer = tf.keras.optimizers.Adam(learning_rate=0.1) model.compile(optimizer=optimizer, loss='categorical_crossentropy') history = model.fit(train_data, epochs=5)What is the main issue and how to fix it?
Solution
Step 1: Identify learning rate problem
Learning rate 0.1 is very high for fine-tuning, causing unstable or no learning.Step 2: Suggest fix
Lowering learning rate to 0.0001 allows gentle updates and loss decrease.Final Answer:
Learning rate is too high; reduce it to 0.0001 -> Option AQuick Check:
High learning rate blocks loss decrease [OK]
- Changing loss function unnecessarily
- Freezing base model after unfreezing
- Increasing epochs without fixing learning rate
Solution
Step 1: Understand fine-tuning best practice
Freeze base model first to train new layers safely with higher learning rate.Step 2: Fine-tune with smaller learning rate after unfreezing
Unfreeze base model and use smaller learning rate to gently adjust pre-trained weights.Final Answer:
Freeze the base model, train the new layers with a higher learning rate, then unfreeze and fine-tune with a smaller learning rate -> Option DQuick Check:
Freeze first, then fine-tune with small learning rate [OK]
- Using large learning rate from start causing unstable training
- Training all layers from scratch wasting pre-trained knowledge
- Not freezing base model before fine-tuning
Ready to prove it?
Pass the test and this pattern moves unknown
