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:
      include_background: if False, channel index 0 (background category) is excluded from the calculation.
          if the non-background segmentations are small compared to the total image size they can get overwhelmed
          by the signal from the background so excluding it in such cases helps convergence.
      to_onehot_y: whether to convert the ``target`` into the one-hot format,
          using the number of classes inferred from `input` (``input.shape[1]``). Defaults to False.
      sigmoid: if True, apply a sigmoid function to the prediction.
      softmax: if True, apply a softmax function to the prediction.
      other_act: callable function to execute other activation layers, Defaults to ``None``. for example:
          ``other_act = torch.tanh``.
      squared_pred: use squared versions of targets and predictions in the denominator or not.
      jaccard: compute Jaccard Index (soft IoU) instead of dice or not.
      reduction: {``"none"``, ``"mean"``, ``"sum"``}
          Specifies the reduction to apply to the output. Defaults to ``"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.
  
      smooth_nr: a small constant added to the numerator to avoid zero.
      smooth_dr: a small constant added to the denominator to avoid nan.
      batch: whether to sum the intersection and union areas over the batch dimension before the dividing.
          Defaults to False, a Dice loss value is computed independently from each item in the batch
          before any `reduction`.
      weight: weights to apply to the voxels of each class. If None no weights are applied.
          The input can be a single value (same weight for all classes), a sequence of values (the length
          of the sequence should be the same as the number of classes. If not ``include_background``,
          the number of classes should not include the background category class 0).
          The value/values should be no less than 0. Defaults to None.
  
  Raises:
      TypeError: When ``other_act`` is not an ``Optional[Callable]``.
      ValueError: When more than 1 of [``sigmoid=True``, ``softmax=True``, ``other_act is not None``].
          Incompatible values.
docstr_tok: ["Args",":","include_background",":","if","False",",","channel","index","0","(","background","category",")","is","excluded","from","the","calculation",".","if","the","non-background","segmentations","are","small","compared","to","the","total","image","size","they","can","get","overwhelmed","by","the","signal","from","the","background","so","excluding","it","in","such","cases","helps","convergence",".","to_onehot_y",":","whether","to","convert","the","``","target","``","into","the","one-hot","format",",","using","the","number","of","classes","inferred","from","`","input","`","(","``","input.shape","[","1","]","``",")",".","Defaults","to","False",".","sigmoid",":","if","True",",","apply","a","sigmoid","function","to","the","prediction",".","softmax",":","if","True",",","apply","a","softmax","function","to","the","prediction",".","other_act",":","callable","function","to","execute","other","activation","layers",",","Defaults","to","``","None","``",".","for","example",":","``","other_act","=","torch.tanh","``",".","squared_pred",":","use","squared","versions","of","targets","and","predictions","in","the","denominator","or","not",".","jaccard",":","compute","Jaccard","Index","(","soft","IoU",")","instead","of","dice","or","not",".","reduction",":","{","``","``","none","''","``",",","``","``","mean","''","``",",","``","``","sum","''","``","}","Specifies","the","reduction","to","apply","to","the","output",".","Defaults","to","``","``","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",".","smooth_nr",":","a","small","constant","added","to","the","numerator","to","avoid","zero",".","smooth_dr",":","a","small","constant","added","to","the","denominator","to","avoid","nan",".","batch",":","whether","to","sum","the","intersection","and","union","areas","over","the","batch","dimension","before","the","dividing",".","Defaults","to","False",",","a","Dice","loss","value","is","computed","independently","from","each","item","in","the","batch","before","any","`","reduction","`",".","weight",":","weights","to","apply","to","the","voxels","of","each","class",".","If","None","no","weights","are","applied",".","The","input","can","be","a","single","value","(","same","weight","for","all","classes",")",",","a","sequence","of","values","(","the","length","of","the","sequence","should","be","the","same","as","the","number","of","classes",".","If","not","``","include_background","``",",","the","number","of","classes","should","not","include","the","background","category","class","0",")",".","The","value\/values","should","be","no","less","than","0",".","Defaults","to","None",".","Raises",":","TypeError",":","When","``","other_act","``","is","not","an","``","Optional","[","Callable","]","``",".","ValueError",":","When","more","than","1","of","[","``","sigmoid=True","``",",","``","softmax=True","``",",","``","other_act","is","not","None","``","]",".","Incompatible","values","."]
code: def __init__(
      self,
      include_background: bool = True,
      to_onehot_y: bool = False,
      sigmoid: bool = False,
      softmax: bool = False,
      other_act: Callable | None = None,
      squared_pred: bool = False,
      jaccard: bool = False,
      reduction: LossReduction | str = LossReduction.MEAN,
      smooth_nr: float = 1e-5,
      smooth_dr: float = 1e-5,
      batch: bool = False,
      weight: Sequence[float]
      | float
      | int
      | torch.Tensor
      | None = None,
  ) -> None:
      """
      Args:
          include_background: if False, channel index 0 (background category) is excluded from the calculation.
              if the non-background segmentations are small compared to the total image size they can get overwhelmed
              by the signal from the background so excluding it in such cases helps convergence.
          to_onehot_y: whether to convert the ``target`` into the one-hot format,
              using the number of classes inferred from `input` (``input.shape[1]``). Defaults to False.
          sigmoid: if True, apply a sigmoid function to the prediction.
          softmax: if True, apply a softmax function to the prediction.
          other_act: callable function to execute other activation layers, Defaults to ``None``. for example:
              ``other_act = torch.tanh``.
          squared_pred: use squared versions of targets and predictions in the denominator or not.
          jaccard: compute Jaccard Index (soft IoU) instead of dice or not.
          reduction: {``"none"``, ``"mean"``, ``"sum"``}
              Specifies the reduction to apply to the output. Defaults to ``"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.
  
          smooth_nr: a small constant added to the numerator to avoid zero.
          smooth_dr: a small constant added to the denominator to avoid nan.
          batch: whether to sum the intersection and union areas over the batch dimension before the dividing.
              Defaults to False, a Dice loss value is computed independently from each item in the batch
              before any `reduction`.
          weight: weights to apply to the voxels of each class. If None no weights are applied.
              The input can be a single value (same weight for all classes), a sequence of values (the length
              of the sequence should be the same as the number of classes. If not ``include_background``,
              the number of classes should not include the background category class 0).
              The value/values should be no less than 0. Defaults to None.
  
      Raises:
          TypeError: When ``other_act`` is not an ``Optional[Callable]``.
          ValueError: When more than 1 of [``sigmoid=True``, ``softmax=True``, ``other_act is not None``].
              Incompatible values.
  
      """
      super().__init__(reduction=LossReduction(reduction).value)
      if other_act is not None and not callable(other_act):
          raise TypeError(
              f"other_act must be None or callable but is {type(other_act).__name__}."
          )
      if int(sigmoid) + int(softmax) + int(other_act is not None) > 1:
          raise ValueError(
              "Incompatible values: more than 1 of [sigmoid=True, softmax=True, other_act is not None]."
          )
      self.include_background = include_background
      self.to_onehot_y = to_onehot_y
      self.sigmoid = sigmoid
      self.softmax = softmax
      self.other_act = other_act
      self.squared_pred = squared_pred
      self.jaccard = jaccard
      self.smooth_nr = float(smooth_nr)
      self.smooth_dr = float(smooth_dr)
      self.batch = batch
      weight = torch.as_tensor(weight) if weight is not None else None
      self.register_buffer("class_weight", weight)
      self.class_weight: None | torch.Tensor
  
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 1 to create a new DiceLoss object from inherited base classes: _Loss with example: obj = DiceLoss(include_background, to_onehot_y, sigmoid, softmax, other_act, squared_pred, jaccard, reduction, smooth_nr, smooth_dr, batch, weight)