classification
AsymmetricLoss(gamma_neg=4, gamma_pos=0, m=0.05, eps=1e-08, disable_torch_grad_focal_loss=False, apply_sigmoid=True)
¶
Notice - optimized version, minimizes memory allocation and gpu uploading, favors inplace operations.
Parameters:
-
gamma_neg
(
float) –gamma for negative samples
-
gamma_pos
(
float) –gamma for positive samples
-
m
(
float) –bias value added to negative samples
-
eps
(
float) –epsilon to avoid division by zero
-
disable_torch_grad_focal_loss
(
bool) –if True, disables torch grad for focal loss
-
apply_sigmoid
(
bool) –if True, applies sigmoid to input before computing loss
Source code in quadra/losses/classification/asl.py
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 | |
forward(x, y)
¶
Compute the asymmetric loss.
Parameters:
-
x
(
torch.Tensor) –input logits (after sigmoid)
-
y
(
torch.Tensor) –targets (multi-label binarized vector)
Returns:
Source code in quadra/losses/classification/asl.py
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 | |
FocalLoss(alpha, gamma=2.0, reduction='none', eps=None)
¶
Criterion that computes Focal loss.
According to :cite:lin2018focal, the Focal loss is computed as follows:
.. math::
\text{FL}(p_t) = -\alpha_t (1 - p_t)^{\gamma} \, \text{log}(p_t)
Where
- :math:
p_tis the model's estimated probability for each class.
Parameters:
-
alpha
(
float) –Weighting factor :math:
\alpha \in [0, 1]. -
gamma
(
float) –Focusing parameter :math:
\gamma >= 0. -
reduction
(
str) –Specifies the reduction to apply to the output:
'none'|'mean'|'sum'.'none': no reduction will be applied,'mean': the sum of the output will be divided by the number of elements in the output,'sum': the output will be summed. -
eps
(
Optional[float]) –used.
Shape
- Input: :math:
(N, C, *)where C = number of classes. - Target: :math:
(N, *)where each value is :math:0 ≤ targets[i] ≤ C−1.
Example
N = 5 # num_classes kwargs = {"alpha": 0.5, "gamma": 2.0, "reduction": 'mean'} criterion = FocalLoss(**kwargs) input = torch.randn(1, N, 3, 5, requires_grad=True) target = torch.empty(1, 3, 5, dtype=torch.long).random_(N) output = criterion(input, target) output.backward()
Source code in quadra/losses/classification/focal.py
190 191 192 193 194 195 | |
forward(input_tensor, target)
¶
Forward call computation.
Source code in quadra/losses/classification/focal.py
197 198 199 | |