-
Notifications
You must be signed in to change notification settings - Fork 10
/
useful_loss_functions.py
112 lines (98 loc) · 3.8 KB
/
useful_loss_functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
from typing import Optional
import torch
import torch.nn as nn
import torch.nn.functional as F
class FocalLoss(nn.modules.loss._WeightedLoss):
def __init__(
self, weight: Optional[torch.Tensor] = None, gamma: int = 2, reduction: str = "mean"
):
"""
Version of (Weighted) focal loss for multi-class classification with CE loss
Taken from https://arxiv.org/abs/1708.02002
Args:
weights (Optional, torch.Tensor): a tensor of alpha parameter to balance class weights
gamma (int): focus parameter.
higher gamma => more "easy" examples with low loss is discounted
when gamma == 0, focal loss is equivalent to CE Loss
defaults to 2 based on findings
reduction (str): reduction strategy for CE loss
"""
super().__init__(weight=weight, reduction=reduction)
self.gamma = gamma
self.weight = weight
self.reduction = reduction
def forward(self, input: torch.Tensor, target: torch.Tensor) -> torch.Tensor:
ce_loss = F.cross_entropy(input, target, reduction=self.reduction, weight=self.weight)
pt = torch.exp(-ce_loss)
focal_loss = ((1 - pt) ** self.gamma * ce_loss).mean()
return focal_loss
class PolyCELoss(nn.modules.loss._WeightedLoss):
def __init__(
self,
weight: Optional[torch.Tensor] = None,
reduction: str = "mean",
label_smoothing: float = 0.0,
epsilon: float = 1.0,
) -> None:
"""
Pytorch implementation of Poly Cross Entropy Loss from
https://arxiv.org/abs/2204.12511v1
This version uses logits for input, don't use softmaxed input
"""
super().__init__(weight=weight, reduction=reduction)
self.weight = weight
self.reduction = reduction
self.label_smoothing = label_smoothing
self.epsilon = epsilon
if self.reduction not in ["mean", "sum", "none"]:
raise ValueError(
f'Unsupported reduction: {self.reduction},\
available options are ["mean", "sum", "none"].'
)
def forward(self, input: torch.Tensor, target: torch.Tensor) -> torch.Tensor:
"""
input must be logits, not probabilities
"""
probs = torch.softmax(input, dim=1)
pt = (target * probs).sum(dim=1)
ce_loss = F.cross_entropy(
input,
target,
reduction="none",
weight=self.weight,
label_smoothing=self.label_smoothing,
)
poly_loss = ce_loss + self.epsilon * (1 - pt)
if self.reduction == "mean":
poly_loss = poly_loss.mean()
elif self.reduction == "sum":
poly_loss = poly_loss.sum()
else:
poly_loss = poly_loss.unsqueeze(dim=1)
return poly_loss
class PolyFocalLoss(nn.modules.loss._WeightedLoss):
def __init__(
self,
weight: Optional[torch.Tensor] = None,
gamma: int = 2,
reduction: str = "mean",
epsilon: float = 1.0,
) -> None:
"""
Pytorch implementation of Poly Focal Loss from
https://arxiv.org/abs/2204.12511v1
Adjusted for multiclass classification
"""
super().__init__(weight=weight, reduction=reduction)
self.focal_loss = FocalLoss(weight, gamma=gamma, reduction=reduction)
self.epsilon = epsilon
self.gamma = gamma
def forward(self, input: torch.Tensor, target: torch.Tensor) -> torch.Tensor:
"""
input must be logits, not probabilities
"""
probs = torch.softmax(input, dim=1)
pt = (target * probs).sum(dim=1)
fl = self.focal_loss(input, target)
poly_fl = fl + self.epsilon * (1 - pt) ** (self.gamma + 1)
return poly_fl.mean()