Spaces:
Sleeping
Sleeping
Download src/utils/losses.py from Harshilforworks/Forcast: direct link, hf CLI and curl.
- Browser
- Download file 1.13 kB
-
https://huggingface.co/spaces/Harshilforworks/Forcast/resolve/main/src/utils/losses.py
- Command line
-
hf download hf://spaces/Harshilforworks/Forcast/src/utils/losses.py
-
curl -L -o losses.py https://huggingface.co/spaces/Harshilforworks/Forcast/resolve/main/src/utils/losses.py
1.13 kB
| from __future__ import annotations | |
| import torch | |
| import torch.nn.functional as F | |
| from torch import Tensor | |
| from torch import nn | |
| class FocalLoss(nn.Module): | |
| """Binary focal loss on logits.""" | |
| def __init__(self, alpha: float = 0.25, gamma: float = 2.0, reduction: str = "mean") -> None: | |
| super().__init__() | |
| if reduction not in {"mean", "sum", "none"}: | |
| raise ValueError("reduction must be one of: mean, sum, none") | |
| self.alpha = alpha | |
| self.gamma = gamma | |
| self.reduction = reduction | |
| def forward(self, logits: Tensor, targets: Tensor) -> Tensor: | |
| targets = targets.float() | |
| bce = F.binary_cross_entropy_with_logits(logits, targets, reduction="none") | |
| probs = torch.sigmoid(logits) | |
| pt = probs * targets + (1.0 - probs) * (1.0 - targets) | |
| alpha_factor = self.alpha * targets + (1.0 - self.alpha) * (1.0 - targets) | |
| focal = alpha_factor * (1.0 - pt).pow(self.gamma) * bce | |
| if self.reduction == "mean": | |
| return focal.mean() | |
| if self.reduction == "sum": | |
| return focal.sum() | |
| return focal | |