class_docstr: Compute the generalised Dice loss defined in:
  
      Sudre, C. et. al. (2017) Generalised Dice overlap as a deep learning
      loss function for highly unbalanced segmentations. DLMIA 2017.
  
  Adapted from:
      https://github.com/NifTK/NiftyNet/blob/v0.6.0/niftynet/layer/loss_segmentation.py#L279
class_docstr_tok: ["Compute","the","generalised","Dice","loss","defined","in",":","Sudre",",","C.","et",".","al",".","(","2017",")","Generalised","Dice","overlap","as","a","deep","learning","loss","function","for","highly","unbalanced","segmentations",".","DLMIA","2017",".","Adapted","from",":","https",":","\/\/github.com\/NifTK\/NiftyNet\/blob\/v0.6.0\/niftynet\/layer\/loss_segmentation.py","#","L279"]
docstr: Args:
      input: the shape should be BNH[WD].
      target: the shape should be BNH[WD].
  
  Raises:
      ValueError: When ``self.reduction`` is not one of ["mean", "sum", "none"].
docstr_tok: ["Args",":","input",":","the","shape","should","be","BNH","[","WD","]",".","target",":","the","shape","should","be","BNH","[","WD","]",".","Raises",":","ValueError",":","When","``","self.reduction","``","is","not","one","of","[","``","mean","''",",","``","sum","''",",","``","none","''","]","."]
code: def forward(
      self, input: torch.Tensor, target: torch.Tensor
  ) -> torch.Tensor:
      """
      Args:
          input: the shape should be BNH[WD].
          target: the shape should be BNH[WD].
  
      Raises:
          ValueError: When ``self.reduction`` is not one of ["mean", "sum", "none"].
  
      """
      if self.sigmoid:
          input = torch.sigmoid(input)
      n_pred_ch = input.shape[1]
      if self.softmax:
          if n_pred_ch == 1:
              warnings.warn(
                  "single channel prediction, `softmax=True` ignored."
              )
          else:
              input = torch.softmax(input, 1)
  
      if self.other_act is not None:
          input = self.other_act(input)
  
      if self.to_onehot_y:
          if n_pred_ch == 1:
              warnings.warn(
                  "single channel prediction, `to_onehot_y=True` ignored."
              )
          else:
              target = one_hot(target, num_classes=n_pred_ch)
  
      if not self.include_background:
          if n_pred_ch == 1:
              warnings.warn(
                  "single channel prediction, `include_background=False` ignored."
              )
          else:
              # if skipping background, removing first channel
              target = target[:, 1:]
              input = input[:, 1:]
  
      if target.shape != input.shape:
          raise AssertionError(
              f"ground truth has differing shape ({target.shape}) from input ({input.shape})"
          )
  
      # reducing only spatial dimensions (not batch nor channels)
      reduce_axis: list[int] = torch.arange(
          2, len(input.shape)
      ).tolist()
      if self.batch:
          reduce_axis = [0] + reduce_axis
      intersection = torch.sum(target * input, reduce_axis)
  
      ground_o = torch.sum(target, reduce_axis)
      pred_o = torch.sum(input, reduce_axis)
  
      denominator = ground_o + pred_o
  
      w = self.w_func(ground_o.float())
      infs = torch.isinf(w)
      if self.batch:
          w[infs] = 0.0
          w = w + infs * torch.max(w)
      else:
          w[infs] = 0.0
          max_values = torch.max(w, dim=1)[0].unsqueeze(dim=1)
          w = w + infs * max_values
  
      final_reduce_dim = 0 if self.batch else 1
      numer = (
          2.0 * (intersection * w).sum(final_reduce_dim, keepdim=True)
          + self.smooth_nr
      )
      denom = (denominator * w).sum(
          final_reduce_dim, keepdim=True
      ) + self.smooth_dr
      f: torch.Tensor = 1.0 - (numer / denom)
  
      if self.reduction == LossReduction.MEAN.value:
          f = torch.mean(f)  # the batch and channel average
      elif self.reduction == LossReduction.SUM.value:
          f = torch.sum(f)  # sum over the batch and channel dims
      elif self.reduction == LossReduction.NONE.value:
          # If we are not computing voxelwise loss components at least
          # make sure a none reduction maintains a broadcastable shape
          broadcast_shape = list(f.shape[0:2]) + [1] * (
              len(input.shape) - 2
          )
          f = f.view(broadcast_shape)
      else:
          raise ValueError(
              f'Unsupported reduction: {self.reduction}, available options are ["mean", "sum", "none"].'
          )
  
      return f
  
imports: from __future__ import annotations
  import warnings
  from collections.abc import Callable, Sequence
  from typing import Any
  import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  from torch.nn.modules.loss import _Loss
  from monai.losses.focal_loss import FocalLoss
  from monai.losses.spatial_mask import MaskedLoss
  from monai.networks import one_hot
  from monai.utils import DiceCEReduction, LossReduction, Weight, deprecated_arg, look_up_option, pytorch_after
label_desc: Use image node_id 3 for calling the GeneralizedDiceLoss obj's underlying member method code with example usage: obj.forward(input, target) and returns: f