code: def focal_loss_with_logits(
      output: torch.Tensor,
      target: torch.Tensor,
      gamma: float = 2.0,
      alpha: Optional[float] = 0.25,
      reduction: str = "mean",
      normalized: bool = False,
      reduced_threshold: Optional[float] = None,
      eps: float = 1e-6,
      ignore_index=None,
      activation: str = "sigmoid",
      softmax_dim: Optional[int] = None,
  ) -> torch.Tensor:
      """Compute binary focal loss between target and output logits.
  
      See :class:`~pytorch_toolbelt.losses.FocalLoss` for details.
  
      Args:
          output: Tensor of arbitrary shape (predictions of the model)
          target: Tensor of the same shape as input
          gamma: Focal loss power factor
          alpha: Weight factor to balance positive and negative samples. Alpha must be in [0...1] range,
              high values will give more weight to positive class.
          reduction (string, optional): Specifies the reduction to apply to the output:
              'none' | 'mean' | 'sum' | 'batchwise_mean'. '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. Note: :attr:`size_average`
              and :attr:`reduce` are in the process of being deprecated, and in the meantime,
              specifying either of those two args will override :attr:`reduction`.
              'batchwise_mean' computes mean loss per sample in batch. Default: 'mean'
          normalized (bool): Compute normalized focal loss (https://arxiv.org/pdf/1909.07829.pdf).
          reduced_threshold (float, optional): Compute reduced focal loss (https://arxiv.org/abs/1903.01347).
          activation: Either sigmoid or softmax. If `softmax` is used, `softmax_dim` must be also specified.
  
      References:
          https://github.com/open-mmlab/mmdetection/blob/master/mmdet/core/loss/losses.py
      """
      output = output.float()
      target = target.float()
  
      if activation == "sigmoid":
          p = torch.sigmoid(output)
      else:
          p = torch.softmax(output, dim=softmax_dim)
  
      ce_loss = F.binary_cross_entropy_with_logits(
          output, target, reduction="none"
      )
      pt = p * target + (1 - p) * (1 - target)
  
      # compute the loss
      if reduced_threshold is None:
          focal_term = (1.0 - pt).pow(gamma)
      else:
          focal_term = ((1.0 - pt) / reduced_threshold).pow(gamma)
          focal_term = torch.masked_fill(
              focal_term, pt < reduced_threshold, 1
          )
  
      loss = focal_term * ce_loss
  
      if alpha is not None:
          loss *= alpha * target + (1 - alpha) * (1 - target)
  
      if ignore_index is not None:
          ignore_mask = target.eq(ignore_index)
          loss = torch.masked_fill(loss, ignore_mask, 0)
          if normalized:
              focal_term = torch.masked_fill(focal_term, ignore_mask, 0)
  
      if normalized:
          norm_factor = focal_term.sum(dtype=torch.float32).clamp_min(
              eps
          )
          loss /= norm_factor
  
      if reduction == "mean":
          loss = loss.mean()
      if reduction == "sum":
          loss = loss.sum()
      if reduction == "batchwise_mean":
          loss = loss.sum(dim=0)
  
      return loss
  
file_path: pytorch-toolbelt/pytorch_toolbelt/losses/functional.py
imports: import math
  from typing import Optional
  import torch
  import torch.nn.functional
  from ..utils import pytorch_toolbelt_deprecated
label_desc: Use image node_id 1 for calling a global function with example usage: focal_loss_with_logits(output, target, gamma, alpha, reduction, normalized, reduced_threshold, eps, ignore_index, activation, softmax_dim) and returns: loss