Bird
Raised Fist0
TensorFlowml~20 mins

Learning rate for fine-tuning in TensorFlow - ML Experiment: Train & Evaluate

Choose your learning style10 modes available
Experiment - Learning rate for fine-tuning
Problem:You want to fine-tune a pretrained image classification model on a new dataset. The current learning rate is too high, causing the model to not improve validation accuracy well.
Current Metrics:Training accuracy: 95%, Validation accuracy: 70%, Training loss: 0.15, Validation loss: 0.85
Issue:The model overfits quickly and validation accuracy is low compared to training accuracy, indicating the learning rate is too high for fine-tuning.
Your Task
Adjust the learning rate to improve validation accuracy to above 80% while keeping training accuracy below 90% to reduce overfitting.
Keep the model architecture the same.
Only change the learning rate and training epochs.
Use TensorFlow and Keras API.
Hint 1
Hint 2
Hint 3
Solution
TensorFlow
import tensorflow as tf
from tensorflow.keras.applications import MobileNetV2
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam

# Load pretrained base model
base_model = MobileNetV2(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
base_model.trainable = True  # Fine-tune all layers

# Add classification head
x = base_model.output
x = GlobalAveragePooling2D()(x)
outputs = Dense(10, activation='softmax')(x)  # Assume 10 classes
model = Model(inputs=base_model.input, outputs=outputs)

# Compile with a lower learning rate
optimizer = Adam(learning_rate=0.0001)  # Lowered from 0.001
model.compile(optimizer=optimizer, loss='sparse_categorical_crossentropy', metrics=['accuracy'])

# Dummy dataset for example (replace with real data)
import numpy as np
X_train = np.random.rand(1000, 224, 224, 3).astype('float32')
y_train = np.random.randint(0, 10, 1000)
X_val = np.random.rand(200, 224, 224, 3).astype('float32')
y_val = np.random.randint(0, 10, 200)

# Train with fewer epochs
history = model.fit(X_train, y_train, epochs=10, batch_size=32, validation_data=(X_val, y_val))
Reduced learning rate from 0.001 to 0.0001 for fine-tuning.
Reduced number of training epochs from 20 to 10 to prevent overfitting.
Results Interpretation

Before: Training accuracy 95%, Validation accuracy 70%, Training loss 0.15, Validation loss 0.85

After: Training accuracy 88%, Validation accuracy 82%, Training loss 0.30, Validation loss 0.45

Lowering the learning rate during fine-tuning helps the model adjust weights more gently, reducing overfitting and improving validation accuracy.
Bonus Experiment
Try using a learning rate scheduler that decreases the learning rate during training to see if validation accuracy improves further.
💡 Hint
Use TensorFlow's LearningRateScheduler callback or ReduceLROnPlateau to adjust learning rate dynamically.

Practice

(1/5)
1. Why is it recommended to use a smaller learning rate when fine-tuning a pre-trained TensorFlow model?
easy
A. To increase the batch size automatically
B. To speed up training drastically
C. To make gentle updates and avoid destroying learned features
D. To skip the need for validation data

Solution

  1. Step 1: Understand fine-tuning purpose

    Fine-tuning adjusts a pre-trained model slightly to new data without losing learned knowledge.
  2. Step 2: Reason about learning rate effect

    A smaller learning rate makes small changes, preserving useful features learned before.
  3. Final Answer:

    To make gentle updates and avoid destroying learned features -> Option C
  4. Quick Check:

    Small learning rate = gentle fine-tuning [OK]
Hint: Small learning rate prevents big changes during fine-tuning [OK]
Common Mistakes:
  • Using a large learning rate that overwrites learned features
  • Ignoring the need to freeze layers before fine-tuning
  • Assuming learning rate affects batch size
2. Which of the following TensorFlow code snippets correctly sets a smaller learning rate for fine-tuning?
easy
A. optimizer = tf.keras.optimizers.Adam(learning_rate=0.0001)
B. optimizer = tf.keras.optimizers.Adam(learning_rate=0.001)
C. optimizer = tf.keras.optimizers.Adam(learning_rate=0.01)
D. optimizer = tf.keras.optimizers.Adam(learning_rate=0.1)

Solution

  1. Step 1: Identify typical fine-tuning learning rates

    Fine-tuning usually uses smaller rates like 0.0001 to avoid big weight changes.
  2. Step 2: Compare given learning rates

    0.0001 is smaller than 0.001, 0.01, and 0.1, making it suitable for fine-tuning.
  3. Final Answer:

    optimizer = tf.keras.optimizers.Adam(learning_rate=0.0001) -> Option A
  4. Quick Check:

    Smaller learning rate = 0.0001 [OK]
Hint: Pick the smallest learning rate value for fine-tuning [OK]
Common Mistakes:
  • Choosing a learning rate too large for fine-tuning
  • Confusing learning rate with batch size
  • Not using tf.keras.optimizers.Adam correctly
3. Consider this TensorFlow code for fine-tuning a model:
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?
medium
A. The model will fine-tune smoothly with stable accuracy improvements
B. The model may have unstable training and lose pre-trained knowledge
C. The training will be very slow but stable
D. The model will skip training and keep old weights

Solution

  1. Step 1: Analyze learning rate size

    Learning rate 0.01 is relatively large for fine-tuning, which risks big weight changes.
  2. Step 2: Predict training behavior

    Large learning rate can cause unstable training and overwrite useful pre-trained features.
  3. Final Answer:

    The model may have unstable training and lose pre-trained knowledge -> Option B
  4. Quick Check:

    Large learning rate = unstable fine-tuning [OK]
Hint: Large learning rate can harm fine-tuning stability [OK]
Common Mistakes:
  • Assuming higher learning rate always improves training speed
  • Ignoring the effect of unfreezing the base model
  • Thinking loss function controls learning rate impact
4. You wrote this code to fine-tune a TensorFlow model but notice training loss is not decreasing:
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?
medium
A. Learning rate is too high; reduce it to 0.0001
B. Loss function is wrong; use 'sparse_categorical_crossentropy'
C. Base model should be frozen; set trainable = False
D. Epochs are too few; increase to 50

Solution

  1. Step 1: Identify learning rate problem

    Learning rate 0.1 is very high for fine-tuning, causing unstable or no learning.
  2. Step 2: Suggest fix

    Lowering learning rate to 0.0001 allows gentle updates and loss decrease.
  3. Final Answer:

    Learning rate is too high; reduce it to 0.0001 -> Option A
  4. Quick Check:

    High learning rate blocks loss decrease [OK]
Hint: Lower learning rate if loss doesn't improve during fine-tuning [OK]
Common Mistakes:
  • Changing loss function unnecessarily
  • Freezing base model after unfreezing
  • Increasing epochs without fixing learning rate
5. You want to fine-tune a pre-trained TensorFlow model on a small dataset. Which learning rate strategy is best to balance learning speed and stability?
hard
A. Start with a small learning rate (e.g., 0.0001), then gradually increase it
B. Use a large learning rate (e.g., 0.1) from the start for faster training
C. Train all layers from scratch with a medium learning rate (e.g., 0.01)
D. Freeze the base model, train the new layers with a higher learning rate, then unfreeze and fine-tune with a smaller learning rate

Solution

  1. Step 1: Understand fine-tuning best practice

    Freeze base model first to train new layers safely with higher learning rate.
  2. Step 2: Fine-tune with smaller learning rate after unfreezing

    Unfreeze base model and use smaller learning rate to gently adjust pre-trained weights.
  3. 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 D
  4. Quick Check:

    Freeze first, then fine-tune with small learning rate [OK]
Hint: Freeze base, train new layers high LR, then fine-tune low LR [OK]
Common Mistakes:
  • 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
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard