code: def softmax_focal_loss_with_logits(
      output: torch.Tensor,
      target: torch.Tensor,
      gamma: float = 2.0,
      reduction: str = "mean",
      normalized: bool = False,
      reduced_threshold: Optional[float] = None,
      eps: float = 1e-6,
      ignore_index: int = -100,
  ) -> torch.Tensor:
      """
      Softmax version of focal loss between target and output logits.
      See :class:`~pytorch_toolbelt.losses.FocalLoss` for details.
  
      Args:
          output: Tensor of shape [B, C, *] (Similar to nn.CrossEntropyLoss)
          target: Tensor of shape [B, *] (Similar to nn.CrossEntropyLoss)
          gamma: Focal loss power factor
          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).
      """
      log_softmax = F.log_softmax(output, dim=1)
  
      loss = F.nll_loss(
          log_softmax,
          target,
          reduction="none",
          ignore_index=ignore_index,
      )
      pt = torch.exp(-loss)
  
      # 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[pt < reduced_threshold] = 1
  
      loss = focal_term * loss
  
      if normalized:
          norm_factor = focal_term.sum().clamp_min(eps)
          loss = loss / norm_factor
  
      if reduction == "mean":
          loss = loss.mean()
      if reduction == "sum":
          loss = loss.sum()
      if reduction == "batchwise_mean":
          loss = loss.sum(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 2 for calling a global function with example usage: softmax_focal_loss_with_logits(output, target, gamma, reduction, normalized, reduced_threshold, eps, ignore_index) and returns: loss