Loss Function
bi_tempered_logistic_loss(activations, labels, t1, t2, label_smooth=0.0, num_iters=5, reduction='mean')
Bi-Tempered Logistic Loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
activations
|
Tensor
|
A multidimensional tensor with last dimension |
required |
labels
|
Tensor
|
Tensor with the same shape and dtype as activations (one-hot encoded), or a long tensor with one dimension less (class indices). |
required |
t1
|
float
|
Temperature 1 (< 1.0 for boundedness of loss). |
required |
t2
|
float
|
Temperature 2 (> 1.0 for tail heaviness, < 1.0 for finite support). |
required |
label_smooth
|
float
|
Label smoothing parameter, between 0 and 1. |
0.0
|
num_iters
|
int
|
Number of iterations to run the normalization method. |
5
|
reduction
|
str
|
Specifies reduction method to apply to output: 'none', 'mean', or 'sum'. |
'mean'
|
Source code in pytorch_optimizer/loss/bi_tempered.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | |
BCEFocalLoss
Bases: Module
BCEFocal loss function with probability input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alpha
|
float
|
Weighting factor for class imbalance, commonly set to 0.25. |
0.25
|
gamma
|
float
|
Focusing parameter to reduce loss contribution of easy examples. |
2.0
|
label_smooth
|
float
|
Smoothness constant to regularize target labels. |
0.0
|
eps
|
float
|
Small epsilon to avoid numerical instability. |
1e-06
|
reduction
|
str
|
Specifies reduction type to apply to output: 'none', 'mean' or 'sum'. |
'mean'
|
Source code in pytorch_optimizer/loss/focal.py
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 | |
BCELoss
Bases: Module
Binary Cross Entropy loss with label smoothing and probability input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label_smooth
|
float
|
Smoothness constant to soften target labels. |
0.0
|
eps
|
float
|
Small epsilon to avoid numerical instability. |
1e-06
|
reduction
|
str
|
Specifies the reduction to apply to the output; 'none' | 'mean' | 'sum'. |
'mean'
|
Source code in pytorch_optimizer/loss/cross_entropy.py
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |
BinaryBiTemperedLogisticLoss
Bases: Module
Bi-Tempered Logistic Loss for Binary Classification.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t1
|
float
|
Temperature 1 (< 1.0 for boundedness of the loss). |
required |
t2
|
float
|
Temperature 2 (> 1.0 for tail heaviness, < 1.0 for finite support). |
required |
label_smooth
|
float
|
Label smoothing parameter between 0 and 1. |
0.0
|
ignore_index
|
Optional[int]
|
Specifies a target value that is ignored and does not contribute to the input gradient. |
None
|
reduction
|
str
|
Specifies the reduction to apply to the output: 'none', 'mean', or 'sum'. |
'mean'
|
Source code in pytorch_optimizer/loss/bi_tempered.py
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | |
BiTemperedLogisticLoss
Bases: Module
Bi-Tempered Log Loss.
Reference
https://github.com/BloodAxe/pytorch-toolbelt/blob/develop/pytorch_toolbelt/losses/bitempered_loss.py
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t1
|
float
|
Temperature 1 (< 1.0 for boundedness). |
required |
t2
|
float
|
Temperature 2 (> 1.0 for tail heaviness, < 1.0 for finite support). |
required |
label_smooth
|
float
|
Label smoothing parameter between 0 and 1. |
0.0
|
ignore_index
|
Optional[int]
|
Index to ignore during loss calculation. |
None
|
reduction
|
str
|
Type of reduction to apply to output, e.g. 'mean', 'sum', or 'none'. |
'mean'
|
Source code in pytorch_optimizer/loss/bi_tempered.py
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | |
DiceLoss
Bases: _Loss
Dice loss for image segmentation task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode
|
ClassMode
|
Loss mode - 'binary', 'multiclass', or 'multilabel'. |
'binary'
|
classes
|
Optional[List[int]]
|
List of classes to include in loss computation. Defaults to all classes. |
None
|
log_loss
|
bool
|
If True, loss is computed as |
False
|
from_logits
|
bool
|
If True, assumes input is raw logits. |
True
|
label_smooth
|
float
|
Smoothness constant for dice coefficient numerator and denominator. |
0.0
|
ignore_index
|
Optional[int]
|
Label to ignore during loss computation. |
None
|
eps
|
float
|
Small epsilon for numerical stability. |
1e-06
|
Source code in pytorch_optimizer/loss/dice.py
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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | |
FocalCosineLoss
Bases: Module
Focal Cosine Loss function with logits input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alpha
|
float
|
Weighting factor for class imbalance. |
1.0
|
gamma
|
float
|
Focusing parameter to reduce loss contribution from easy examples. |
2.0
|
focal_weight
|
float
|
Weight of the focal loss component in the combined loss. |
0.1
|
reduction
|
str
|
Specifies the reduction to apply to the output: 'none', 'mean', or 'sum'. |
'mean'
|
Source code in pytorch_optimizer/loss/focal.py
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 | |
FocalLoss
Bases: Module
Focal Loss function with logits input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alpha
|
float
|
Weighting factor for class imbalance. |
1.0
|
gamma
|
float
|
Focusing parameter to down-weight easy examples and focus training on hard negatives. |
2.0
|
Source code in pytorch_optimizer/loss/focal.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | |
FocalTverskyLoss
Bases: Module
Focal Tversky Loss with logits input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alpha
|
float
|
Weight for false negatives in Tversky index. |
0.5
|
beta
|
float
|
Weight for false positives in Tversky index. |
0.5
|
gamma
|
float
|
Focusing parameter that shapes the loss to focus more on hard examples. |
1.0
|
smooth
|
float
|
Smoothing factor to avoid division by zero. |
1e-06
|
Source code in pytorch_optimizer/loss/focal.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | |
get_supported_loss_functions(filters=None)
Return list of available loss function names, sorted alphabetically.
:param filters: Optional[Union[str, List[str]]]. wildcard filter string that works with fmatch. if None, it will return the whole list.
Source code in pytorch_optimizer/loss/__init__.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | |
JaccardLoss
Bases: _Loss
Jaccard loss for image segmentation.
Reference: https://github.com/BloodAxe/pytorch-toolbelt
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode
|
str
|
Loss mode, one of 'binary', 'multiclass', or 'multilabel'. |
required |
classes
|
Optional[List[int]]
|
List of classes to include in the loss computation, defaults to all classes if None. |
None
|
log_loss
|
bool
|
If True, loss is computed as -log(jaccard); otherwise, 1 - jaccard. |
False
|
from_logits
|
bool
|
If True, input is raw logits, which will be converted to probabilities. |
True
|
label_smooth
|
float
|
Label smoothing constant. |
0.0
|
eps
|
float
|
Small number to prevent division by zero. |
1e-06
|
Source code in pytorch_optimizer/loss/jaccard.py
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 113 114 115 116 117 118 119 | |
LDAMLoss
Bases: Module
Label-Distribution-Aware Margin (LDAM) Loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_class_list
|
List[int]
|
List of the number of samples per class. |
required |
max_m
|
float
|
Maximum margin (the |
0.5
|
weight
|
Optional[Tensor]
|
Optional class weights for re-weighting. |
None
|
s
|
float
|
Scaling factor for logits. |
30.0
|
Source code in pytorch_optimizer/loss/ldam.py
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 | |
LovaszHingeLoss
Bases: Module
Binary Lovasz hinge loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
per_image
|
bool
|
compute the loss per image instead of per batch. |
True
|
Source code in pytorch_optimizer/loss/lovasz.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | |
soft_dice_score(output, target, label_smooth=0.0, eps=1e-06, dims=None)
Get soft dice score.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output
|
Tensor
|
Predicted segmentation probabilities. |
required |
target
|
Tensor
|
Ground truth segmentation masks. |
required |
label_smooth
|
float
|
Label smoothing factor to avoid zero denominators. |
0.0
|
eps
|
float
|
Small epsilon for numerical stability. |
1e-06
|
dims
|
Optional[Tuple[int, ...]]
|
Dimensions over which to reduce when computing score. |
None
|
Source code in pytorch_optimizer/loss/dice.py
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 | |
soft_jaccard_score(output, target, label_smooth=0.0, eps=1e-06, dims=None)
Get soft Jaccard score.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output
|
Tensor
|
Predicted segments (probabilities or logits). |
required |
target
|
Tensor
|
Ground truth segments. |
required |
label_smooth
|
float
|
Label smoothing factor to avoid zero denominators. |
0.0
|
eps
|
float
|
Small epsilon for numerical stability. |
1e-06
|
dims
|
Optional[Tuple[int, ...]]
|
Dimensions to reduce over when computing the score. |
None
|
Source code in pytorch_optimizer/loss/jaccard.py
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 | |
SoftF1Loss
Bases: Module
Soft-F1 loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
beta
|
float
|
The beta parameter in the F-beta score, balancing precision vs recall. |
1.0
|
eps
|
float
|
Small epsilon value to avoid division by zero during calculation. |
1e-06
|
Source code in pytorch_optimizer/loss/f1.py
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 | |
TverskyLoss
Bases: Module
Tversky Loss with logits input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alpha
|
float
|
Weight of false positives. |
0.5
|
beta
|
float
|
Weight of false negatives. |
0.5
|
smooth
|
float
|
Small constant to avoid division by zero. |
1e-06
|
Source code in pytorch_optimizer/loss/tversky.py
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 | |