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:
      include_background: If False channel index 0 (background category) is excluded from the calculation.
      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``.
      w_type: {``"square"``, ``"simple"``, ``"uniform"``}
          Type of function to transform ground truth volume to a weight factor. Defaults to ``"square"``.
      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, intersection over union is computed from each item in the batch.
          If True, the class-weighted intersection and union areas are first summed across the batches.
  
  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",".","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","``",".","w_type",":","{","``","``","square","''","``",",","``","``","simple","''","``",",","``","``","uniform","''","``","}","Type","of","function","to","transform","ground","truth","volume","to","a","weight","factor",".","Defaults","to","``","``","square","''","``",".","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",",","intersection","over","union","is","computed","from","each","item","in","the","batch",".","If","True",",","the","class-weighted","intersection","and","union","areas","are","first","summed","across","the","batches",".","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,
      w_type: Weight | str = Weight.SQUARE,
      reduction: LossReduction | str = LossReduction.MEAN,
      smooth_nr: float = 1e-5,
      smooth_dr: float = 1e-5,
      batch: bool = False,
  ) -> None:
      """
      Args:
          include_background: If False channel index 0 (background category) is excluded from the calculation.
          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``.
          w_type: {``"square"``, ``"simple"``, ``"uniform"``}
              Type of function to transform ground truth volume to a weight factor. Defaults to ``"square"``.
          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, intersection over union is computed from each item in the batch.
              If True, the class-weighted intersection and union areas are first summed across the batches.
  
      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.w_type = look_up_option(w_type, Weight)
  
      self.smooth_nr = float(smooth_nr)
      self.smooth_dr = float(smooth_dr)
      self.batch = batch
  
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 GeneralizedDiceLoss object from inherited base classes: _Loss with example: obj = GeneralizedDiceLoss(include_background, to_onehot_y, sigmoid, softmax, other_act, w_type, reduction, smooth_nr, smooth_dr, batch)