class_docstr: Compute average Dice loss between two tensors. It can support both multi-classes and multi-labels tasks.
  The data `input` (BNHW[D] where N is number of classes) is compared with ground truth `target` (BNHW[D]).
  
  Note that axis N of `input` is expected to be logits or probabilities for each class, if passing logits as input,
  must set `sigmoid=True` or `softmax=True`, or specifying `other_act`. And the same axis of `target`
  can be 1 or N (one-hot format).
  
  The `smooth_nr` and `smooth_dr` parameters are values added to the intersection and union components of
  the inter-over-union calculation to smooth results respectively, these values should be small.
  
  The original paper: Milletari, F. et. al. (2016) V-Net: Fully Convolutional Neural Networks forVolumetric
  Medical Image Segmentation, 3DV, 2016.
class_docstr_tok: ["Compute","average","Dice","loss","between","two","tensors",".","It","can","support","both","multi-classes","and","multi-labels","tasks",".","The","data","`","input","`","(","BNHW","[","D","]","where","N","is","number","of","classes",")","is","compared","with","ground","truth","`","target","`","(","BNHW","[","D","]",")",".","Note","that","axis","N","of","`","input","`","is","expected","to","be","logits","or","probabilities","for","each","class",",","if","passing","logits","as","input",",","must","set","`","sigmoid=True","`","or","`","softmax=True","`",",","or","specifying","`","other_act","`",".","And","the","same","axis","of","`","target","`","can","be","1","or","N","(","one-hot","format",")",".","The","`","smooth_nr","`","and","`","smooth_dr","`","parameters","are","values","added","to","the","intersection","and","union","components","of","the","inter-over-union","calculation","to","smooth","results","respectively",",","these","values","should","be","small",".","The","original","paper",":","Milletari",",","F.","et",".","al",".","(","2016",")","V-Net",":","Fully","Convolutional","Neural","Networks","forVolumetric","Medical","Image","Segmentation",",","3DV",",","2016","."]
docstr: Args:
      input: the shape should be BNH[WD], where N is the number of classes.
      target: the shape should be BNH[WD] or B1H[WD], where N is the number of classes.
  
  Raises:
      AssertionError: When input and target (after one hot transform if set)
          have different shapes.
      ValueError: When ``self.reduction`` is not one of ["mean", "sum", "none"].
  
  Example:
      >>> from monai.losses.dice import *  # NOQA
      >>> import torch
      >>> from monai.losses.dice import DiceLoss
      >>> B, C, H, W = 7, 5, 3, 2
      >>> input = torch.rand(B, C, H, W)
      >>> target_idx = torch.randint(low=0, high=C - 1, size=(B, H, W)).long()
      >>> target = one_hot(target_idx[:, None, ...], num_classes=C)
      >>> self = DiceLoss(reduction='none')
      >>> loss = self(input, target)
      >>> assert np.broadcast_shapes(loss.shape, input.shape) == input.shape
docstr_tok: ["Args",":","input",":","the","shape","should","be","BNH","[","WD","]",",","where","N","is","the","number","of","classes",".","target",":","the","shape","should","be","BNH","[","WD","]","or","B1H","[","WD","]",",","where","N","is","the","number","of","classes",".","Raises",":","AssertionError",":","When","input","and","target","(","after","one","hot","transform","if","set",")","have","different","shapes",".","ValueError",":","When","``","self.reduction","``","is","not","one","of","[","``","mean","''",",","``","sum","''",",","``","none","''","]",".","Example",":",">",">",">","from","monai.losses.dice","import","*","#","NOQA",">",">",">","import","torch",">",">",">","from","monai.losses.dice","import","DiceLoss",">",">",">","B",",","C",",","H",",","W","=","7",",","5",",","3",",","2",">",">",">","input","=","torch.rand","(","B",",","C",",","H",",","W",")",">",">",">","target_idx","=","torch.randint","(","low=0",",","high=C","-","1",",","size=","(","B",",","H",",","W",")",")",".long","(",")",">",">",">","target","=","one_hot","(","target_idx","[",":",",","None",",","...","]",",","num_classes=C",")",">",">",">","self","=","DiceLoss","(","reduction='none","'",")",">",">",">","loss","=","self","(","input",",","target",")",">",">",">","assert","np.broadcast_shapes","(","loss.shape",",","input.shape",")","==","input.shape"]
code: def forward(
      self, input: torch.Tensor, target: torch.Tensor
  ) -> torch.Tensor:
      """
      Args:
          input: the shape should be BNH[WD], where N is the number of classes.
          target: the shape should be BNH[WD] or B1H[WD], where N is the number of classes.
  
      Raises:
          AssertionError: When input and target (after one hot transform if set)
              have different shapes.
          ValueError: When ``self.reduction`` is not one of ["mean", "sum", "none"].
  
      Example:
          >>> from monai.losses.dice import *  # NOQA
          >>> import torch
          >>> from monai.losses.dice import DiceLoss
          >>> B, C, H, W = 7, 5, 3, 2
          >>> input = torch.rand(B, C, H, W)
          >>> target_idx = torch.randint(low=0, high=C - 1, size=(B, H, W)).long()
          >>> target = one_hot(target_idx[:, None, ...], num_classes=C)
          >>> self = DiceLoss(reduction='none')
          >>> loss = self(input, target)
          >>> assert np.broadcast_shapes(loss.shape, input.shape) == input.shape
      """
      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 different 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:
          # reducing spatial dimensions and batch
          reduce_axis = [0] + reduce_axis
  
      intersection = torch.sum(target * input, dim=reduce_axis)
  
      if self.squared_pred:
          ground_o = torch.sum(target**2, dim=reduce_axis)
          pred_o = torch.sum(input**2, dim=reduce_axis)
      else:
          ground_o = torch.sum(target, dim=reduce_axis)
          pred_o = torch.sum(input, dim=reduce_axis)
  
      denominator = ground_o + pred_o
  
      if self.jaccard:
          denominator = 2.0 * (denominator - intersection)
  
      f: torch.Tensor = 1.0 - (2.0 * intersection + self.smooth_nr) / (
          denominator + self.smooth_dr
      )
  
      num_of_classes = target.shape[1]
      if self.class_weight is not None and num_of_classes != 1:
          # make sure the lengths of weights are equal to the number of classes
          if self.class_weight.ndim == 0:
              self.class_weight = torch.as_tensor(
                  [self.class_weight] * num_of_classes
              )
          else:
              if self.class_weight.shape[0] != num_of_classes:
                  raise ValueError(
                      """the length of the `weight` sequence should be the same as the number of classes.
                          If `include_background=False`, the weight should not include
                          the background category class 0."""
                  )
          if self.class_weight.min() < 0:
              raise ValueError(
                  "the value/values of the `weight` should be no less than 0."
              )
          # apply class_weight to loss
          f = f * self.class_weight.to(f)
  
      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 2 for calling the DiceLoss obj's underlying member method code with example usage: obj.forward(input, target) and returns: f