과적합(Overfitting)은 모델이 훈련 데이터에는 잘 맞지만 새로운 데이터에서 성능이 나쁜 현상이다. 모델이 훈련 데이터의 노이즈까지 학습한 경우 발생한다.
과적합 vs 과소적합
과소적합 (Underfitting):
Train Loss: 높음
Test Loss: 높음
→ 모델이 너무 단순
적절한 적합:
Train Loss: 낮음
Test Loss: 낮음
→ 이상적인 상태
과적합 (Overfitting):
Train Loss: 매우 낮음
Test Loss: 높음
→ 모델이 너무 복잡
방지 방법
데이터 증강 (Data Augmentation)
python
from torchvision import transforms
transform = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.RandomRotation(10),
transforms.ColorJitter(brightness=0.2)
])
드롭아웃 (Dropout)
python
nn.Sequential(
nn.Linear(256, 128),
nn.ReLU(),
nn.Dropout(p=0.5), # 학습 중 50% 뉴런 랜덤 비활성화
nn.Linear(128, 10)
)
정규화 (Regularization)
python
# L2 정규화 (Weight Decay)
optimizer = torch.optim.Adam(model.parameters(),
lr=0.001, weight_decay=1e-4)
# Batch Normalization
nn.BatchNorm1d(256)
조기 종료 (Early Stopping)
python
# 검증 손실이 더 이상 줄지 않으면 학습 중단
if val_loss > best_val_loss:
patience_counter += 1
if patience_counter >= patience:
break
관련 개념
참고문헌
- •Srivastava et al. (2014). Dropout: A Simple Way to Prevent Neural Networks from Overfitting