🤖 AI/ML

Meta-Learning

Meta-Learning

Learning how to learn. The underlying technique of few-shot learning.

📖 Detailed Explanation

Meta-Learning is an AI technique that "learns how to learn," designed so that a model can quickly adapt to new tasks. Traditional machine learning trains from scratch for a specific task, whereas meta-learning accumulates learning experience across diverse tasks and solves new tasks efficiently.

The core idea of meta-learning is learning from a "task distribution." For example, MAML (Model-Agnostic Meta-Learning) finds initial parameters that can adapt to a new task with just a few gradient updates. This is the key technique behind Few-shot Learning, which achieves high performance with only a handful of examples.

Meta-learning is broadly divided into three approaches: 1) Optimization-based (MAML, Reptile) - learns an initialization that allows fast adaptation, 2) Metric-based (Prototypical Networks, Siamese Networks) - learns an effective embedding space, 3) Model-based (Memory-Augmented Neural Networks) - learning that uses external memory.

In practice, meta-learning is used in domains with scarce data, recommendation systems that need fast personalization, and robot control adapting to new environments. Its value shines especially in fields with high labeling costs, such as healthcare and drug discovery.

💻 Code Example

Here is an example implementation of MAML-style meta-learning:

import torch
import torch.nn as nn
from torch.optim import Adam
from copy import deepcopy

class MAMLModel(nn.Module):
    def __init__(self, input_dim, hidden_dim, output_dim):
        super().__init__()
        self.net = 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, x):
        return self.net(x)

def maml_inner_loop(model, support_x, support_y, inner_lr=0.01, inner_steps=5):
    """태스크별 내부 루프: 빠른 적응"""
    adapted_model = deepcopy(model)
    optimizer = Adam(adapted_model.parameters(), lr=inner_lr)
    criterion = nn.CrossEntropyLoss()

    for _ in range(inner_steps):
        pred = adapted_model(support_x)
        loss = criterion(pred, support_y)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

    return adapted_model

def maml_outer_loop(model, tasks, outer_lr=0.001):
    """메타 학습: 좋은 초기화를 찾음"""
    meta_optimizer = Adam(model.parameters(), lr=outer_lr)
    criterion = nn.CrossEntropyLoss()
    meta_loss = 0

    for task in tasks:
        support_x, support_y, query_x, query_y = task
        adapted_model = maml_inner_loop(model, support_x, support_y)
        pred = adapted_model(query_x)
        meta_loss += criterion(pred, query_y)

    meta_optimizer.zero_grad()
    meta_loss.backward()
    meta_optimizer.step()

    return meta_loss.item() / len(tasks)

# 5-way 5-shot 분류 예제
model = MAMLModel(input_dim=784, hidden_dim=64, output_dim=5)
print(f"메타러닝 모델 준비 완료: {sum(p.numel() for p in model.parameters())} 파라미터")

📊 Performance & Cost

Item Standard learning Meta-Learning
Required samples Thousands to tens of thousands 1-10 (Few-shot)
New task adaptation time Hours to days Seconds to minutes
Meta-training cost - 2-8 GPUs, 1-7 days
Memory usage 1x 2-4x (second-order gradients)
Omniglot 5-way 5-shot ~85% 99.5% (MAML++)

🗣️ Say It Like This in Practice

"This domain has little data, so how about approaching it with meta-learning?"
Use this when proposing an appropriate solution in a situation where it is hard to obtain labeled data.

"If we learn the initialization with MAML, adapting to new customers will be faster."
Use this when developing B2B services that need fast fine-tuning.

"Let's test N-way K-shot classification performance with Prototypical Networks."
This is an expression used when discussing the design of Few-shot Learning experiments.

⚠️ Common Mistakes & Cautions

1. Task distribution mismatch in meta-training: If the distribution of the tasks used for meta-training differs from the tasks you will actually apply it to, performance drops sharply. Design the domain carefully.

2. Second-order gradient memory blowup: MAML's second-order derivative computation uses a lot of memory. Approximate it with First-order MAML (FOMAML) or reduce the batch size.

3. Securing enough tasks: Effective meta-learning requires hundreds to thousands of diverse tasks. Consider task augmentation techniques as well.

4. Too many inner-loop steps: If you train too much in the inner loop, meta-knowledge is lost. Usually 1-10 steps is appropriate.

🔗 Related Terms

  • Few-shot Learning: A technique that learns from only a few examples, and a major application area of meta-learning
  • Transfer Learning: A method of applying a pre-trained model to a new task
  • Zero-shot Learning: A technique that recognizes new classes without any training examples
  • Hyperparameter Optimization: Optimizes training settings, similar to meta-learning
  • Neural Architecture Search: An AutoML technique that learns the network structure itself

📚 Learn More