Loss function
정리
개념
- 모델이 예측한 값과 실제 정답 사이의 차이를 수치고 계산하는 함수
- 값이 작을수록 모델이 예측을 잘 했다는 의미이며,
- 이 값을 최소화하도록 학습하는 것을 최적화(=optimization)라고 함
카테고리별 Loss function
카테고리별 Loss function 요약
- 회귀: MSE, MAE, Huber, Log-Cosh
- 분류: Cross Entropy, NLL, Hinge, Focal, Label Smoothing
- 임베딩: Cosine, Contrastive, Triplet, InfoNCE
- 검출·세그멘테이션: IoU, Dice, Tversky, GIoU/DIoU/CIoU
1. 회귀 (Regression)
(1) Mean Squared Error (MSE)
LMSE=n1i=1∑n(yi−y^i)2
(2) Mean Absolute Error (MAE)
LMAE=n1i=1∑n∣yi−y^i∣
(3) Huber Loss (Smooth L1)
Lδ(y,y^)={21(y−y^)2,δ∣y−y^∣−21δ2,∣y−y^∣≤δotherwise
- 작은 오차는 MSE, 큰 오차는 MAE처럼 동작.
(4) Log-Cosh Loss
L(y,y^)=i=1∑nlog(cosh(y^i−yi))
2. 분류 (Classification)
(1) Cross Entropy Loss
LCE=−i=1∑Cyilog(p^i)
- 가장 널리 쓰이는 분류 손실. Softmax와 함께 사용.
(2) Negative Log Likelihood (NLL)
LNLL=−logp^(y)
- 단일 정답 클래스의 확률을 직접 penalize. (Softmax+NLL = CE)
(3) Hinge Loss
Lhinge=i=1∑nmax(0,1−yiy^i)
(4) Focal Loss
LFL=−α(1−p^t)γlog(p^t)
- 어려운 샘플에 집중, 클래스 불균형 대응.
- pt=p^(y).
(5) Label Smoothing Cross Entropy
L=−i=1∑C((1−ϵ)yi+Cϵ)log(p^i)
3. 임베딩 / Metric Learning
(1) Cosine Embedding Loss
L={1−cos(x1,x2),max(0,cos(x1,x2)−m),y=1y=−1
- 같은 쌍(positive)은 가깝게, 다른 쌍(negative)은 멀게.
(2) Contrastive Loss
L=(1−y)21D2+y21max(0,m−D)2
- D=∥x1−x2∥, y=0 같은 클래스, y=1 다른 클래스.
(3) Triplet Loss
L=max(0,d(a,p)−d(a,n)+α)
- Anchor–Positive는 가깝게, Anchor–Negative는 멀게.
(4) InfoNCE Loss
L=−log∑j=1Nexp(sim(x,xj)/τ)exp(sim(x,x+)/τ)
- Contrastive Learning에서 자주 사용, 여러 negative 대비 positive를 구분.
4. 객체 검출 · 세그멘테이션 (Detection & Segmentation)
(1) IoU Loss (Jaccard Loss)
LIoU=1−∣y∪y^∣∣y∩y^∣
(2) Dice Loss
LDice=1−∣y∣+∣y^∣2∣y∩y^∣
(3) Tversky Loss
L=1−∣y∩y^∣+α∣y∖y^∣+β∣y^∖y∣∣y∩y^∣
- FP/ FN 비중 조절 가능 (Dice의 일반화).
(4) GIoU / DIoU / CIoU Loss
(Detection bounding box 용 개선된 IoU)
LGIoU=1−IoU+∣C∣∣C−(A∪B)∣
- C: 두 박스를 포함하는 최소 영역.
- 위치 정렬과 수렴 속도를 개선.