Advanced Topics
Meta-Learning — Learning to Learn from Few Examples
Discover meta-learning algorithms that enable models to learn new tasks quickly with minimal data. The key to few-shot learning and rapid adaptation.
- MAML — Model-Agnostic Meta-Learning for fast adaptation
- Prototypical Networks — Learning metric spaces for classification
- Reinforcement Learning — Meta-learning with reward signals
"The most important skill is learning how to learn."
📋 Prerequisites
- ● Deep Learning: Neural networks, backpropagation, gradient descent, loss functions
- ● Optimization: Convex/non-convex optimization, learning rate schedules
- ● Transfer Learning: Pre-trained models, fine-tuning concepts
- ● Python & PyTorch: Model training, autograd, custom datasets
- ● Probability: Probability distributions, Bayes' theorem, expectation
🎯 Learning Objectives
Meta-Learning — Learning to Learn
Meta-learning trains models to learn new tasks quickly from few examples.
Meta-Learning Concept
Key Formulas Reference
Key Formulas — Meta-Learning
Find initialization that minimizes loss across all tasks
Adapt to task i using support set
Update initialization using query set losses
Classify by nearest prototype c_k = mean(f(x_k))
Ignore second-order terms for faster computation
The Formal Framework
MAML Algorithm
MAML Algorithm Details
Prototypical Networks
Few-Shot Learning Scenarios
Python Implementation Example
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchmeta.utils.prototype import get_prototypes, prototypical_distance
class PrototypicalNetwork(nn.Module):
def __init__(self, input_dim, hidden_dim, output_dim):
super().__init__()
self.encoder = nn.Sequential(
nn.Linear(input_dim, hidden_dim),
nn.ReLU(),
nn.Linear(hidden_dim, hidden_dim),
nn.ReLU(),
nn.Linear(hidden_dim, output_dim)
)
def forward(self, support_images, support_labels, query_images):
# Encode support and query images
support_embeddings = self.encoder(support_images)
query_embeddings = self.encoder(query_images)
# Compute prototypes (class centroids)
n_way = support_labels.unique().shape[0]
prototypes = torch.zeros(n_way, support_embeddings.shape[-1])
for i, c in enumerate(support_labels.unique()):
mask = support_labels == c
prototypes[i] = support_embeddings[mask].mean(dim=0)
# Compute distances to prototypes
distances = torch.cdist(query_embeddings, prototypes)
# Negative distances for softmax (closer = higher probability)
log_probs = F.log_softmax(-distances, dim=-1)
return log_probs
class MAMLModel(nn.Module):
def __init__(self, input_dim, hidden_dim, output_dim, lr=0.01, inner_steps=5):
super().__init__()
self.model = nn.Sequential(
nn.Linear(input_dim, hidden_dim),
nn.ReLU(),
nn.Linear(hidden_dim, output_dim)
)
self.lr = lr
self.inner_steps = inner_steps
def inner_adapt(self, support_x, support_y):
fast_weights = list(self.model.parameters())
for _ in range(self.inner_steps):
logits = self.model(support_x)
loss = F.cross_entropy(logits, support_y)
grads = torch.autograd.grad(loss, self.model.parameters())
fast_weights = [w - self.lr * g for w, g in zip(fast_weights, grads)]
return fast_weights
def forward(self, x, fast_weights=None):
if fast_weights is None:
return self.model(x)
# Forward pass with adapted weights
return F.linear(x, fast_weights[0], fast_weights[1])
# Episodic training loop
def meta_train(model, tasks, num_episodes=1000, inner_lr=0.01, outer_lr=0.001):
optimizer = torch.optim.Adam(model.parameters(), lr=outer_lr)
for episode in range(num_episodes):
task = tasks.sample()
support_x, support_y = task.support
query_x, query_y = task.query
# Inner loop: adapt to task
fast_weights = model.inner_adapt(support_x, support_y)
# Outer loop: evaluate on query set
query_logits = model(query_x, fast_weights)
meta_loss = F.cross_entropy(query_logits, query_y)
optimizer.zero_grad()
meta_loss.backward()
optimizer.step()
Real-World Applications
🌍 Real-World Applications of Meta-Learning
1. Medical Image Diagnosis
Rare diseases have very few training examples. Meta-learning trains on common diseases first, then adapts to rare conditions with just 5-10 labeled scans. Used for skin cancer detection, retinal disease classification, and pathology image analysis across hospitals.
2. Robotic Manipulation
Robots use meta-learning to quickly adapt to new objects, environments, and tasks. A robot trained with MAML on grasping common objects can learn to grasp novel objects with just 5-10 demonstrations, enabling rapid deployment in warehouses and homes.
3. Drug Discovery
Meta-learning accelerates drug discovery by enabling models trained on many molecular property prediction tasks to quickly adapt to new chemical compounds with limited experimental data, reducing the time and cost of identifying promising drug candidates.
4. Natural Language Processing
Few-shot text classification for sentiment analysis, intent detection, and language detection. Companies deploy meta-learned models that adapt to new languages or domains with just 5-10 labeled examples per class, enabling rapid internationalization.
5. Autonomous Driving
Self-driving cars use meta-learning to quickly adapt to new weather conditions, road types, and geographic regions. The model learns to generalize across diverse driving scenarios and rapidly fine-tune to local conditions in new cities.
6. Personalized Recommendation
E-commerce platforms use meta-learning to build personalized recommendation models that adapt to individual users with just a few interactions. The meta-learned initialization captures general user behavior patterns, enabling rapid personalization.
Common Mistakes & How to Avoid Them
⚠️ Common Mistakes & How to Avoid Them
- 1Insufficient Meta-Training Tasks:
Using too few tasks during meta-training causes poor generalization. Use at least 100+ diverse tasks. More diverse tasks → better meta-learning performance.
- 2Mismatch Between Meta-Train and Meta-Test:
If meta-test tasks are too different from meta-training tasks, adaptation fails. Ensure task distribution overlap. Use the same N-way K-shot protocol consistently.
- 3Overfitting Inner Loop:
Running too many inner loop steps on support data causes overfitting. Use 1-5 inner steps and ensure query set is sufficiently large for reliable meta-gradient estimation.
- 4Ignoring Task Distribution:
All meta-learning assumes tasks come from the same distribution. If test tasks are from a different distribution, performance degrades significantly. Use domain-invariant features.
- 5Choosing Wrong Meta-Learning Approach:
Optimization-based (MAML) works well for regression/RL. Metric-based (Prototypical) is better for classification. Model-based (Memorization) for sequential tasks. Choose based on your task type.
Interview Questions
💬 Interview Questions — Meta-Learning
Q1: What is the difference between transfer learning and meta-learning?
Transfer learning pre-trains on a large source task and fine-tunes on a target task. Meta-learning trains across many tasks to learn how to adapt quickly. Transfer learning is task-specific; meta-learning learns a general adaptation strategy. Meta-learning typically requires more meta-training tasks but enables faster adaptation.
Q2: How does MAML find a good initialization?
MAML uses bi-level optimization: the inner loop adapts to each task with a few gradient steps, and the outer loop optimizes the initialization based on how well adaptation works. The key insight is that some initializations are better for adaptation than others — MAML finds one that can quickly reach good task-specific solutions.
Q3: Why do Prototypical Networks work well?
Prototypical Networks learn a distance metric where similar images are close and different images are far apart. The prototype (class mean) is a robust representation of each class. Classification by nearest prototype is simple yet effective. The learned metric space generalizes well to new classes not seen during meta-training.
Q4: What is episodic training?
Episodic training simulates few-shot scenarios during meta-training. Each episode randomly samples a task, splits data into support and query sets, adapts on support, and evaluates on query. This directly optimizes for few-shot performance, unlike standard training which optimizes for many-shot performance.
Q5: When should you use MAML vs Prototypical Networks?
MAML is model-agnostic and works for any task (classification, regression, RL) but is computationally expensive due to second-order gradients. Prototypical Networks are simpler, faster, and often better for classification tasks. Use MAML for complex tasks or when the loss function matters; use Prototypical Networks for standard classification benchmarks.
Q6: What is few-shot learning?
Few-shot learning is the problem of learning from very limited labeled examples (typically 1-5 per class). It's a specific setting where standard deep learning fails due to insufficient data. Meta-learning, metric learning, and data augmentation are common approaches to solve few-shot learning.
Q7: Can meta-learning be combined with self-supervised learning?
Yes! Self-supervised pre-training learns general representations from unlabeled data, then meta-learning adapts these representations for few-shot tasks. This combination is powerful: self-supervised learning provides good features, and meta-learning provides fast adaptation. Methods like "Self-Supervised Meta-Learning" combine both paradigms effectively.
Practice Exercise
🏋️ Practice Exercise — Prototypical Network Implementation
Challenge:
Build a Prototypical Network for 5-way 5-shot image classification on Omniglot dataset:
- Load Omniglot dataset and create episode sampler (5-way 5-shot)
- Implement the Prototypical Network with Convolutional encoder
- Train using episodic training with 10,000 episodes
- Evaluate accuracy on 1000 test episodes
- Compare Euclidean vs cosine distance metrics
Starter Code:
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchmeta.datasets import Omniglot
from torchmeta.utils.data import BatchMetaDataSampler
class ConvEncoder(nn.Module):
def __init__(self, hidden_dim=64):
super().__init__()
self.conv1 = nn.Conv2d(1, 64, 3, padding=1)
self.conv2 = nn.Conv2d(64, 64, 3, padding=1)
self.conv3 = nn.Conv2d(64, 64, 3, padding=1)
self.conv4 = nn.Conv2d(64, hidden_dim, 3, padding=1)
self.pool = nn.MaxPool2d(2)
def forward(self, x):
x = F.relu(self.conv1(x))
x = self.pool(x)
x = F.relu(self.conv2(x))
x = self.pool(x)
x = F.relu(self.conv3(x))
x = self.pool(x)
x = F.relu(self.conv4(x))
return x.mean(dim=[2, 3]) # Global average pooling
def euclidean_distance(a, b):
return torch.cdist(a, b)
def cosine_distance(a, b):
a_norm = F.normalize(a, dim=-1)
b_norm = F.normalize(b, dim=-1)
return -torch.mm(a_norm, b_norm.t())
# Your implementation here
Comparison Table
📊 Meta-Learning Approaches Comparison
| Approach | MAML | Prototypical Networks | Reptile | Transfer Learning |
|---|---|---|---|---|
| Type | Optimization-based | Metric-based | Optimization-based | Feature-based |
| Task Types | Any (classification, regression, RL) | Classification primarily | Any (simpler than MAML) | Task-specific fine-tuning |
| Adaptation Speed | Fast (1-5 gradient steps) | Instant (nearest neighbor) | Fast (few gradient steps) | Slow (full fine-tuning) |
| Computational Cost | High (2nd-order gradients) | Low (forward pass only) | Low (1st-order only) | Medium (fine-tuning) |
| Model Agnostic | Yes | No (learns encoder) | Yes | Yes |
| Few-Shot Performance | Good | Excellent | Good | Moderate |
| Best For | Complex tasks, RL | Few-shot classification | Simple tasks, efficiency | When many labeled examples |
Key Takeaways
📌 Key Takeaways — Meta-Learning
- ▸ Meta-learning enables few-shot learning — adapt to new tasks with 5 examples
- ▸ MAML finds initialization θ for fast gradient-based adaptation
- ▸ Prototypical Networks learn metric spaces — classify by nearest prototype
- ▸ Episodic training simulates few-shot scenarios: support + query sets
- ▸ Bi-level optimization: Outer loop optimizes initialization, inner loop adapts to task
- ▸ Applications: robotics, personalization, drug discovery, NLP
- ▸ Transfer learning is simpler but less flexible
- ▸ Neural architecture search is meta-learning for architectures
- ▸ FOMAML, Reptile, ANIL are practical alternatives to full MAML
- ▸ N-way K-shot is the standard evaluation protocol
- ▸ Task diversity is crucial for meta-learning generalization
- ▸ Self-supervised pre-training + meta-learning is a powerful combination
- ▸ Metric-based methods are faster and simpler than optimization-based
What to Learn Next
-> Self-Supervised Learning — Pre-training Revolution Learn about self-supervised learning — pre-training revolution.
-> Transfer Learning — Pre-trained Models Complete Guide Learn about transfer learning — pre-trained models complete guide.
-> Neural Networks Fundamentals — Perceptrons to Deep Learning Learn about neural networks fundamentals — perceptrons to deep learning.
-> Model Evaluation — Metrics, Cross-Validation and Selection Learn about model evaluation — metrics, cross-validation and selection.
-> AutoML — Automated Machine Learning Learn about automl — automated machine learning.
-> ML System Design — Architecture and Production Patterns Learn about ml system design — architecture and production patterns.
Further Reading
📚 Further Reading
- 📄 Finn et al., "Model-Agnostic Meta-Learning for Fast Adaptation of Deep Networks" (2017) — The foundational MAML paper
- 📄 Snell et al., "Prototypical Networks for Few-shot Learning" (2017) — Prototypical Networks paper
- 📄 Nichol et al., "Reptile: A Scalable Metalearning Algorithm" (2018) — Simple and efficient meta-learning
- 📄 Vinyals et al., "Matching Networks for One Shot Learning" (2016) — Attention-based few-shot learning
- 📖 "Meta-Learning" by Chelsea Finn's PhD Thesis (2018) — Comprehensive meta-learning overview
- 🔗 Torchmeta Library: https://github.com/triechelt/torchmeta
- 🔗 Learn2Learn: https://github.com/learnables/learn2learn