Transfer Learning
Learn how to use pre-trained models for efficient deep learning. This is a foundational concept in artificial intelligence and machine learning that professional developers rely on daily. The explanations below are written to be beginner-friendly while covering the depth and nuance that comes from real-world AI/ML experience. Take your time with each section and practice the examples
What is Transfer Learning?
Transfer Learning involves using a pre-trained neural network model on a new task, leveraging learned features to improve performance and reduce training time.. This is an essential concept that every AI/ML developer must understand thoroughly. In professional development environments, getting this right can mean the difference between code that works reliably and code that breaks in production. The following sections break this down into clear, digestible pieces with practical examples you can try immediately
Key Concepts
- Pre-trained Models: Models trained on large datasets like ImageNet
- Fine-tuning: Adjusting pre-trained weights for a specific task
- Feature Extraction: Using pre-trained layers as feature extractors
- Domain Adaptation: Applying models to related but different domains
Implementation
import tensorflow as tf
from tensorflow import keras
import numpy as np
# Load a pre-trained model (e.g., MobileNetV2)
base_model = keras.applications.MobileNetV2(
input_shape=(224, 224, 3),
include_top=False,
weights='imagenet'
)
# Freeze the base model
base_model.trainable = False
# Generate sample image data
np.random.seed(42)
X = np.random.randn(100, 224, 224, 3) # 100 images, 224x224 pixels, 3 channels
y = np.random.randint(0, 5, 100) # 5 classes
y = tf.keras.utils.to_categorical(y, 5)
# Split the data
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Create the model
model = keras.Sequential([
base_model,
keras.layers.GlobalAveragePooling2D(),
keras.layers.Dense(128, activation='relu'),
keras.layers.Dropout(0.2),
keras.layers.Dense(5, activation='softmax')
])
# Compile the model
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
# Train the model
history = model.fit(X_train, y_train,
epochs=5,
batch_size=32,
validation_split=0.2,
verbose=1)
# Evaluate the model
test_loss, test_accuracy = model.evaluate(X_test, y_test, verbose=0)
print(f"Test accuracy: {test_accuracy:.4f}")