🤖 AI/ML

Meta Learning

Meta Learning / Learning to Learn

A machine learning paradigm of learning how to learn. A core technique of few-shot learning, which adapts quickly to new tasks with little data.

📖 Detailed Explanation

Meta Learning is a machine learning paradigm built on the concept of "learning how to learn (Learning to Learn)." Unlike typical deep learning, which trains on large amounts of data for a specific task, meta learning accumulates learning experience from multiple tasks and learns the ability to adapt quickly to new tasks.

This concept has been studied since the 1990s, but it developed explosively after the publication of the MAML (Model-Agnostic Meta-Learning) paper in 2017. It originated from the idea that AI, like humans who can learn a new concept after seeing just a few examples, should be able to learn from small amounts of data.

The core of meta learning is a bilevel optimization structure of an "Inner Loop" and an "Outer Loop." In the Inner Loop, the model learns quickly on an individual task (Task), and in the Outer Loop, it improves the learning method itself across multiple tasks. Through this, the model acquires good initial parameters, an effective learning rate, and suitable representations.

In practice, meta learning is used in domains with scarce data, personalized recommendation systems, and rapid environment adaptation in robotics. In particular, as a foundational technology for Few-shot Learning (learning from 1-5 examples) and Zero-shot Learning (learning without examples), it is greatly improving the data efficiency of modern AI.

💻 Code Example

# MAML(Model-Agnostic Meta-Learning) 간단 구현 예제
import torch
import torch.nn as nn
import torch.optim as optim
from copy import deepcopy

class SimpleModel(nn.Module):
    """Few-shot 분류를 위한 간단한 신경망"""
    def __init__(self, input_dim=784, hidden_dim=64, output_dim=5):
        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)

class MAML:
    """MAML 메타 러닝 알고리즘"""
    def __init__(self, model, inner_lr=0.01, outer_lr=0.001):
        self.model = model
        self.inner_lr = inner_lr  # Task-specific 학습률
        self.outer_lr = outer_lr  # Meta 학습률
        self.meta_optimizer = optim.Adam(model.parameters(), lr=outer_lr)

    def inner_update(self, model, support_x, support_y, steps=5):
        """Inner Loop: 개별 Task에 대한 빠른 적응"""
        adapted_model = deepcopy(model)
        optimizer = optim.SGD(adapted_model.parameters(), lr=self.inner_lr)
        criterion = nn.CrossEntropyLoss()

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

        return adapted_model

    def meta_train_step(self, tasks):
        """Outer Loop: 여러 Task에 걸친 메타 학습"""
        self.meta_optimizer.zero_grad()
        total_loss = 0.0
        criterion = nn.CrossEntropyLoss()

        for task in tasks:
            support_x, support_y = task['support']  # K-shot examples
            query_x, query_y = task['query']        # 평가용 데이터

            # Inner Loop: Task에 빠르게 적응
            adapted_model = self.inner_update(
                self.model, support_x, support_y
            )

            # Query set으로 메타 손실 계산
            query_logits = adapted_model(query_x)
            task_loss = criterion(query_logits, query_y)
            total_loss += task_loss

        # Outer Loop: 메타 파라미터 업데이트
        meta_loss = total_loss / len(tasks)
        meta_loss.backward()
        self.meta_optimizer.step()

        return meta_loss.item()

# 사용 예시
model = SimpleModel()
maml = MAML(model, inner_lr=0.01, outer_lr=0.001)

# 5-way 5-shot 학습 (5개 클래스, 클래스당 5개 예시)
print("=== Meta Learning Training ===")
print("Inner LR: 0.01 | Outer LR: 0.001")
print("Few-shot 설정: 5-way 5-shot")
print("\n메타 러닝은 여러 작업에서 학습 경험을 축적하여")
print("새로운 작업에 빠르게 적응하는 능력을 배웁니다.")

🗣️ Say It Like This in Practice

💬 In a Meeting
"It's hard to collect data for every new category, so let's approach it with Meta Learning. We can adapt the classifier quickly even with 5-shot, and it seems we can just pre-train on the existing product data."
💬 In an Interview
"Meta Learning is a paradigm of 'learning how to learn.' Taking MAML as an example, it adapts quickly per Task in the Inner Loop and finds a good initialization point in the Outer Loop. In an actual image classification project, I have experience cutting the time to add a new class by 90% with 5-shot."
💬 In a Technical Discussion
"Prototypical Network is metric-based, so it's simple and interpretable, but MAML is model-agnostic, so it's general-purpose. Recently, Transformer-based In-context Learning has been drawing attention as a new form of meta learning."

Take a mock interview on this topic → WIA SOOM AI Interview · Start for free

⚠️ Common Mistakes & Cautions

❌
Lack of Task Diversity

If you use only similar Tasks during meta learning, it will not generalize to new types of Tasks. The diversity of the Task distribution is key.

❌
Too Many Inner Loop Steps

If there are too many Inner Loop steps, the Outer Loop gradients vanish and the computational cost surges. Typically, 1-5 steps is appropriate.

✅
Correct Approach: Task Composition Strategy

In an N-way K-shot setting, training with varied mixes of N (number of classes) and K (number of examples) enables more robust meta learning. Use a setting similar to training during evaluation as well.

🔗 Related Terms

📚 Learn More