Thursday, August 28, 2025

thumbnail

Transfer Learning with Pre-trained Models: A Practical Guide

 ๐Ÿง  Transfer Learning with Pre-trained Models: A Practical Guide

๐Ÿ“Œ What is Transfer Learning?


Transfer Learning is a machine learning technique where a pre-trained model (trained on a large dataset) is used as the starting point for a new, related task.


Instead of training a model from scratch, you "transfer" the learned features to a new problem — saving time, data, and compute resources.


✅ When to Use Transfer Learning


You have limited data for your task.


You want to train models faster.


You want to leverage state-of-the-art models (like BERT, ResNet, GPT, etc.).


Your problem is similar to the original training domain.


๐Ÿงฑ Types of Transfer Learning

1. Feature Extraction


Use a pre-trained model as a fixed feature extractor.


Only replace and train the final classification layer.


2. Fine-Tuning


Load a pre-trained model.


Unfreeze part (or all) of the model and retrain it on your data.


Requires more compute but gives better performance.


๐Ÿ”ง Step-by-Step: How to Apply Transfer Learning


Let’s break it down using a real-world example in computer vision and NLP.


๐Ÿ–ผ️ Example 1: Image Classification with ResNet (PyTorch)

Step 1: Load Pre-trained Model

import torch

import torchvision.models as models


model = models.resnet18(pretrained=True)


Step 2: Freeze All Layers (for feature extraction)

for param in model.parameters():

    param.requires_grad = False


Step 3: Replace Final Layer

import torch.nn as nn


num_classes = 3  # Change according to your dataset

model.fc = nn.Linear(model.fc.in_features, num_classes)


Step 4: Train the New Layer

optimizer = torch.optim.Adam(model.fc.parameters(), lr=0.001)

criterion = nn.CrossEntropyLoss()

# Now train using your data loader


๐Ÿงพ Example 2: Text Classification with BERT (Hugging Face Transformers)

Step 1: Install Libraries

pip install transformers datasets


Step 2: Load Pre-trained BERT

from transformers import BertTokenizer, BertForSequenceClassification


tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")

model = BertForSequenceClassification.from_pretrained("bert-base-uncased", num_labels=2)


Step 3: Tokenize Your Text Data

inputs = tokenizer("Your input text here", return_tensors="pt", padding=True, truncation=True)


Step 4: Fine-Tune on Your Dataset


You can use the Hugging Face Trainer API or write a custom training loop.


from transformers import Trainer, TrainingArguments


training_args = TrainingArguments(

    output_dir="./results",

    evaluation_strategy="epoch",

    per_device_train_batch_size=8,

    num_train_epochs=3,

)


trainer = Trainer(

    model=model,

    args=training_args,

    train_dataset=train_data,

    eval_dataset=eval_data,

)


trainer.train()


๐Ÿง  Common Pre-Trained Models

Domain Model Example Library

Computer Vision ResNet, VGG, EfficientNet torchvision

Natural Language Processing BERT, RoBERTa, GPT transformers

Audio/Speech Wav2Vec, Whisper torchaudio, transformers

Multimodal CLIP, DALL·E transformers, openclip

๐Ÿช„ Tips for Better Results


Start with feature extraction, then try fine-tuning if performance is low.


Use learning rate scheduling when fine-tuning.


Always monitor for overfitting, especially on small datasets.


For best results, normalize your inputs the same way the original model was trained.


๐Ÿ“ฆ Useful Tools & Libraries


๐Ÿค— Hugging Face Transformers


๐Ÿ”ฅ PyTorch


๐Ÿงช TensorFlow Hub


๐Ÿž️ torchvision


๐Ÿง  timm (PyTorch image models)


๐ŸŽฏ Real-World Use Cases


Classifying medical images with a fine-tuned ResNet


Sentiment analysis using fine-tuned BERT


Voice emotion detection using Wav2Vec


Legal or financial document classification


Custom chatbot with a fine-tuned GPT model

Learn AI ML Course in Hyderabad

Read More

Understanding the Vanishing Gradient Problem in Neural Networks

Building a Neural Network with PyTorch: A Beginner’s Guide

How to Use TensorFlow for Deep Learning Projects

The Role of Backpropagation in Neural Networks

Visit Our Quality Thought Training Institute in Hyderabad

Get Directions

Subscribe by Email

Follow Updates Articles from This Blog via Email

No Comments

About

Search This Blog

Powered by Blogger.

Blog Archive