task 에 맞는 loss 골라 — input 계약 읽기
대부분 'model 이 학습 안 함' 스토리가 loss function 과 model output 사이 mismatch 로 추적. 발견하면 fix 가 보통 한 줄 변경.
Classification
nn.CrossEntropyLoss— multi-class. Input: raw logit shape(N, C); target: int64 class index shape(N,). 내부에서 numerical 안정성 위해 log_softmax + NLL 결합 — 전에 softmax X.nn.BCEWithLogitsLoss— binary OR multi-label. Input: raw logit; target: {0, 1} 의 float. 안정성 위해 sigmoid + BCE 결합 — 전에 sigmoid X.nn.NLLLoss— multi-class 인데 logit 대신 log-probability 기대. log_softmax 이미 적용했을 때만.
Regression
nn.MSELoss— mean squared error. regression default. 제곱 때문에 outlier 에 무겁게 penalize.nn.L1Loss— mean absolute error. outlier 에 더 robust.nn.SmoothL1Loss/nn.HuberLoss— 0 근처 L2, 꼬리 L1. noisy regression 의 'best of both'.
덜 흔하지만 유용
nn.KLDivLoss— 두 distribution 사이 KL divergence. knowledge distillation 에 사용.nn.CosineEmbeddingLoss— similarity-based learning (face verification, embedding similarity).nn.TripletMarginLoss— anchor/positive/negative triple 의 metric learning.