class_docstr: Compute the generalized Wasserstein Dice Loss defined in:
  
      Fidon L. et al. (2017) Generalised Wasserstein Dice Score for Imbalanced Multi-class
      Segmentation using Holistic Convolutional Networks. BrainLes 2017.
  
  Or its variant (use the option weighting_mode="GDL") defined in the Appendix of:
  
      Tilborghs, S. et al. (2020) Comparative study of deep learning methods for the automatic
      segmentation of lung, lesion and lesion type in CT scans of COVID-19 patients.
      arXiv preprint arXiv:2007.15546
  
  Adapted from:
      https://github.com/LucasFidon/GeneralizedWassersteinDiceLoss
class_docstr_tok: ["Compute","the","generalized","Wasserstein","Dice","Loss","defined","in",":","Fidon","L.","et","al",".","(","2017",")","Generalised","Wasserstein","Dice","Score","for","Imbalanced","Multi-class","Segmentation","using","Holistic","Convolutional","Networks",".","BrainLes","2017",".","Or","its","variant","(","use","the","option","weighting_mode=","''","GDL","''",")","defined","in","the","Appendix","of",":","Tilborghs",",","S.","et","al",".","(","2020",")","Comparative","study","of","deep","learning","methods","for","the","automatic","segmentation","of","lung",",","lesion","and","lesion","type","in","CT","scans","of","COVID-19","patients",".","arXiv","preprint","arXiv:2007.15546","Adapted","from",":","https",":","\/\/github.com\/LucasFidon\/GeneralizedWassersteinDiceLoss"]
docstr: Args:
      input: the shape should be BNH[WD].
      target: the shape should be BNH[WD].
docstr_tok: ["Args",":","input",":","the","shape","should","be","BNH","[","WD","]",".","target",":","the","shape","should","be","BNH","[","WD","]","."]
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].
  
      """
      # Aggregate spatial dimensions
      flat_input = input.reshape(input.size(0), input.size(1), -1)
      flat_target = target.reshape(target.size(0), -1).long()
  
      # Apply the softmax to the input scores map
      probs = F.softmax(flat_input, dim=1)
  
      # Compute the Wasserstein distance map
      wass_dist_map = self.wasserstein_distance_map(probs, flat_target)
  
      # Compute the values of alpha to use
      alpha = self._compute_alpha_generalized_true_positives(
          flat_target
      )
  
      # Compute the numerator and denominator of the generalized Wasserstein Dice loss
      if self.alpha_mode == "GDL":
          # use GDL-style alpha weights (i.e. normalize by the volume of each class)
          # contrary to the original definition we also use alpha in the "generalized all error".
          true_pos = self._compute_generalized_true_positive(
              alpha, flat_target, wass_dist_map
          )
          denom = self._compute_denominator(
              alpha, flat_target, wass_dist_map
          )
      else:  # default: as in the original paper
          # (i.e. alpha=1 for all foreground classes and 0 for the background).
          # Compute the generalised number of true positives
          true_pos = self._compute_generalized_true_positive(
              alpha, flat_target, wass_dist_map
          )
          all_error = torch.sum(wass_dist_map, dim=1)
          denom = 2 * true_pos + all_error
  
      # Compute the final loss
      wass_dice: torch.Tensor = (2.0 * true_pos + self.smooth_nr) / (
          denom + self.smooth_dr
      )
      wass_dice_loss: torch.Tensor = 1.0 - wass_dice
  
      if self.reduction == LossReduction.MEAN.value:
          wass_dice_loss = torch.mean(
              wass_dice_loss
          )  # the batch and channel average
      elif self.reduction == LossReduction.SUM.value:
          wass_dice_loss = torch.sum(
              wass_dice_loss
          )  # 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 = input.shape[0:2] + (1,) * (
              len(input.shape) - 2
          )
          wass_dice_loss = wass_dice_loss.view(broadcast_shape)
      else:
          raise ValueError(
              f'Unsupported reduction: {self.reduction}, available options are ["mean", "sum", "none"].'
          )
  
      return wass_dice_loss
  
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 2 for calling the GeneralizedWassersteinDiceLoss obj's underlying member method code with example usage: obj.forward(input, target) and returns: wass_dice_loss