code: def softmax_focal_loss(
      input: torch.Tensor,
      target: torch.Tensor,
      gamma: float = 2.0,
      alpha: Optional[float] = None,
  ) -> torch.Tensor:
      """
      FL(pt) = -alpha * (1 - pt)**gamma * log(pt)
  
      where p_i = exp(s_i) / sum_j exp(s_j), t is the target (ground truth) class, and
      s_j is the unnormalized score for class j.
      """
      input_ls = input.log_softmax(1)
      loss: torch.Tensor = (
          -(1 - input_ls.exp()).pow(gamma) * input_ls * target
      )
  
      if alpha is not None:
          # (1-alpha) for the background class and alpha for the other classes
          alpha_fac = torch.tensor(
              [1 - alpha] + [alpha] * (target.shape[1] - 1)
          ).to(loss)
          broadcast_dims = [-1] + [1] * len(target.shape[2:])
          alpha_fac = alpha_fac.view(broadcast_dims)
          loss = alpha_fac * loss
  
      return loss
  
file_path: MONAI/monai/losses/focal_loss.py
imports: from __future__ import annotations
  import warnings
  from collections.abc import Sequence
  from typing import Optional
  import torch
  import torch.nn.functional
  from torch.nn.modules.loss import _Loss
  from monai.networks import one_hot
  from monai.utils import LossReduction
label_desc: Use image node_id 1 for calling a global function with example usage: softmax_focal_loss(input, target, gamma, alpha) and returns: loss