C.W.K.
Stream
Lesson 03 of 08 · published

Optimizer — SGD, Adam, AdamW, Per-Group 설정

~14 min · optimizer, sgd, adam, adamw, weight_decay

Level 0Tensor 호기심
0 XP0/62 lessons0/13 achievements
0/120 XP to next level120 XP to go0% complete

실제로 필요한 세 optimizer

  • SGD (with momentum) — 고전. 싼 메모리, 잘 이해됨, 적절한 lr scheduling 으로 vision task 에서 Adam 과 동등하거나 이김. CIFAR / ImageNet replication 의 default.
  • Adam — first/second gradient moment 의 running estimate 통한 per-parameter adaptive lr. task 잘 모를 때 좋은 default.
  • AdamWdecoupled weight decay 의 Adam. Transformer, fine-tune, 대부분 non-vision deep learning 의 실제 modern 표준. 2026 paper 의 'Adam' 은 거의 항상 AdamW.

왜 AdamW, Adam 아님

Adam 의 'weight_decay' 인자가 gradient 에 항 추가했었음. AdamW 가 weight decay 를 gradient update 적용 — parameter 의 L2 regularization 과 수학적 등가, 경험적으로 더 나은 행동. modern training setup 에 AdamW 선호.

Per-parameter-group 설정

optimizer 가 flat parameter list OR 각자 설정 가진 parameter group list 받음. 고전 사용: pretrained backbone 에 낮은 lr, 새 head 에 높은 lr. 또는: bias / LayerNorm parameter 에 weight decay 없음 (Transformer convention).

Code

세 optimizer, 세 맛·python
import torch
import torch.nn as nn
import torch.optim as optim

model = nn.Sequential(nn.Linear(100, 50), nn.ReLU(), nn.Linear(50, 10))

# SGD with momentum and weight decay — vision classic
opt_sgd = optim.SGD(model.parameters(), lr=1e-1, momentum=0.9, weight_decay=1e-4)

# Adam — adaptive, decoupled-weight-decay-FREE (the bad version)
opt_adam = optim.Adam(model.parameters(), lr=1e-3, betas=(0.9, 0.999))

# AdamW — adaptive AND decoupled weight decay (the good version)
opt_adamw = optim.AdamW(model.parameters(), lr=1e-3, betas=(0.9, 0.999), weight_decay=0.01)
Per-group LR — pretrained backbone + fresh head·python
import torch.optim as optim
import torch.nn as nn
from torchvision.models import resnet18, ResNet18_Weights

backbone = resnet18(weights=ResNet18_Weights.DEFAULT)
backbone.fc = nn.Linear(backbone.fc.in_features, 5)   # new head

# Lower LR for pretrained, higher for new head
optimizer = optim.AdamW([
    {'params': [p for n, p in backbone.named_parameters() if 'fc' not in n], 'lr': 1e-5},
    {'params': backbone.fc.parameters(), 'lr': 1e-3},
], weight_decay=0.01)

for i, g in enumerate(optimizer.param_groups):
    print(f"group {i}: lr={g['lr']}, n_params={sum(p.numel() for p in g['params']):,}")
bias / LayerNorm 에 weight decay 없음 — Transformer convention·python
import torch.optim as optim
import torch.nn as nn

model = nn.TransformerEncoder(nn.TransformerEncoderLayer(512, 8, batch_first=True), num_layers=6)

# Standard Transformer training trick — exclude bias and norm params from weight decay
decay, no_decay = [], []
for name, p in model.named_parameters():
    if not p.requires_grad: continue
    if p.dim() < 2 or any(k in name for k in ('bias', 'norm', 'embedding')):
        no_decay.append(p)
    else:
        decay.append(p)

optimizer = optim.AdamW([
    {'params': decay, 'weight_decay': 0.01},
    {'params': no_decay, 'weight_decay': 0.0},
], lr=1e-4)

print(f"decay: {sum(p.numel() for p in decay):,}, no_decay: {sum(p.numel() for p in no_decay):,}")

External links

Exercise

어떤 작은 model 든. 같은 toy data 에 다른 optimizer 로 100 step 세 번 train (SGD lr=1e-2 momentum=0.9, Adam lr=1e-3, AdamW lr=1e-3 weight_decay=0.01). loss curve plot. AdamW 와 Adam 이 처음엔 거의 동일해 보임; SGD 는 LR tuning 없이 뒤처짐.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.