Transfer Learning
Learn how to use pre-trained models for efficient deep learning.
60 min•By Priygop Team•Last updated: Feb 2026
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.
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
Example
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}")Try It Yourself — Deep Learning & Neural Networks
Try It Yourself — Deep Learning & Neural NetworksHTML
HTML Editor
✓ ValidTab = 2 spaces
HTML|32 lines|1574 chars|✓ Valid syntax
UTF-8