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

Training 중 Metric

~18 min · metrics, logging, monitoring

Level 0Curious
0 XP0/73 lessons0/11 achievements
0/120 XP to next level120 XP to go0% complete

항상 log 해야 할 metric

최소: step 당 training loss, epoch 당 validation loss, epoch 당 validation accuracy (또는 task 적절 metric), step 당 learning rate. 이 4 개로 거의 모든 거 디버깅 가능.

추천 추가: step 당 gradient norm (exploding gradient 잡음), per-class accuracy (imbalance 잡음), example/sec throughput (data-loading regression 잡음).

팁: Metric 그려. Log file 의 숫자는 forensic, 그림은 diagnostic. One-off 면 Jupyter cell 의 matplotlib OK, 진지한 일이면 W&B 또는 TensorBoard.

Epoch average vs running average

Training loss 는 smoothed running average 로 그리는 게 좋음 — single-step loss 는 너무 noisy. 0.99 * old + 0.01 * new 가 fine default. Validation loss 는 epoch 당 한 번이고 raw 값 그려.

Accuracy 만 믿지 마

Accuracy 는 imbalance, calibration, edge-case failure 숨김. 항상 봐: classification 에 class 당 precision/recall, regression 에 MAE/RMSE/quantile error, generation 에 BLEU/ROUGE/exact-match, validation set 의 confusion matrix epoch 당.

원칙: 한 숫자만 보면 다른 모든 숫자에 놀람. Model 이 'accuracy 에서 fine 해 보이는데' 소수 class 에서 깨진 첫 순간이 팀과의 신뢰 한 주 비용.

Code

Per-step logging with a smoothed train loss·python
smooth_loss = None
for step, (xb, yb) in enumerate(train_loader):
    opt.zero_grad()
    loss = loss_fn(model(xb.to(device)), yb.to(device))
    loss.backward(); opt.step()

    val = loss.item()
    smooth_loss = val if smooth_loss is None else 0.99 * smooth_loss + 0.01 * val
    if step % 50 == 0:
        print(f"step {step:6d}  loss {val:.4f}  smooth {smooth_loss:.4f}  lr {opt.param_groups[0]['lr']:.2e}")
Per-class accuracy·python
import torch

@torch.inference_mode()
def per_class_accuracy(model, loader, num_classes, device):
    model.eval()
    correct = torch.zeros(num_classes); total = torch.zeros(num_classes)
    for xb, yb in loader:
        xb, yb = xb.to(device), yb.to(device)
        preds = model(xb).argmax(dim=-1)
        for c in range(num_classes):
            mask = (yb == c)
            correct[c] += ((preds == yb) & mask).sum().cpu()
            total[c]   += mask.sum().cpu()
    return (correct / total.clamp(min=1)).tolist()

External links

Exercise

Training loop 에 per-class accuracy logging 추가. Imbalanced dataset (또는 의도적으로 한 class downsample) 에 돌려. 희귀 class 의 accuracy 가 평균에서 벌어지는 거 봐. 이제 aggregate metric 이 거짓말하는 이유 이해.

Progress

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

댓글 0

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

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