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