Continual Learning
Learning that continuously acquires new knowledge while retaining existing knowledge
Learning that continuously acquires new knowledge while retaining existing knowledge
Continual Learning is a machine learning paradigm in which an AI model learns new data and tasks sequentially while retaining previously learned knowledge. Also known as Lifelong Learning or Incremental Learning, it is a core technology for building AI systems that can adapt to continual changes in the real world.
In traditional deep learning, training on new data causes "Catastrophic Forgetting," a phenomenon in which existing knowledge is rapidly lost. Since McCloskey and Cohen first discovered this problem in 1989, it has been regarded as a fundamental limitation of neural networks, and various studies are underway to solve it.
There are three main approaches. The Replay approach stores or generates past data and trains on it together with new data. The Regularization approach (EWC, SI) limits changes to important parameters. The Architecture approach expands the network for each new task or allocates separate subnetworks.
In practice, Continual Learning is applied to real-time updates of recommendation systems, adaptation of fraud detection models to new patterns, learning of new environments in autonomous driving, and integration of new disease data in medical AI. It is especially important in environments where storing past data is difficult due to data privacy, and it is also connected to the problem of updating the knowledge of LLMs.
import torch
import torch.nn as nn
from copy import deepcopy
class EWC:
"""Elastic Weight Consolidation - Continual Learning 기법"""
def __init__(self, model: nn.Module, dataloader, device, lambda_ewc: float = 1000):
self.model = model
self.lambda_ewc = lambda_ewc
self.device = device
# 이전 태스크 학습 후 파라미터 저장
self.params_old = {n: p.clone().detach()
for n, p in model.named_parameters() if p.requires_grad}
# Fisher Information Matrix 계산 (파라미터 중요도)
self.fisher = self._compute_fisher(dataloader)
def _compute_fisher(self, dataloader) -> dict:
"""Fisher Information 근사 계산"""
fisher = {n: torch.zeros_like(p)
for n, p in self.model.named_parameters() if p.requires_grad}
self.model.eval()
for data, target in dataloader:
data, target = data.to(self.device), target.to(self.device)
self.model.zero_grad()
output = self.model(data)
loss = nn.functional.cross_entropy(output, target)
loss.backward()
for n, p in self.model.named_parameters():
if p.requires_grad and p.grad is not None:
fisher[n] += p.grad.data ** 2
# 평균화
for n in fisher:
fisher[n] /= len(dataloader)
return fisher
def penalty(self) -> torch.Tensor:
"""EWC 패널티 항 계산 - 기존 손실에 추가"""
loss = 0
for n, p in self.model.named_parameters():
if p.requires_grad:
# 중요한 파라미터일수록(Fisher 높음) 변화에 큰 페널티
loss += (self.fisher[n] * (p - self.params_old[n]) ** 2).sum()
return self.lambda_ewc * loss
# 사용 예시
def train_with_ewc(model, new_task_loader, ewc=None, epochs=10):
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
for epoch in range(epochs):
for data, target in new_task_loader:
optimizer.zero_grad()
output = model(data)
loss = nn.functional.cross_entropy(output, target)
# EWC 패널티 추가 (이전 태스크 지식 보존)
if ewc is not None:
loss += ewc.penalty()
loss.backward()
optimizer.step()
return model
# Task 1 학습 후 EWC 초기화
# ewc = EWC(model, task1_loader, device)
# Task 2 학습 (이전 지식 보존하며)
# model = train_with_ewc(model, task2_loader, ewc)
"Retraining the recommendation model entirely every day is too costly. If we use Continual Learning to train only on new data, and prevent catastrophic forgetting with EWC or a Replay buffer, we can reduce training time by 80%."
"The core challenge of Continual Learning is the stability-plasticity dilemma. To learn new knowledge well (plasticity), parameters need to change a lot, but to retain existing knowledge (stability), parameters need to change less. EWC balances this based on importance."
"Because of GDPR, it is difficult to retain customer data for long. If we train on synthetic data created with the Generative Replay approach, we can retain past knowledge without the originals. It also lets us avoid personal data issues."
Take a mock interview on this topic → WIA SOOM AI Interview · Start for free
When a new task conflicts with existing tasks, performance on both can degrade. Analyze task similarity, and consider a separate model for tasks that differ significantly.
Performance on previous tasks must be measured continuously so that forgetting can be detected early. Maintain a benchmark test set and evaluate regularly.
The Replay approach requires storing past data, and the Architecture approach increases model size. Choose a technique that fits your system constraints.