class_docstr: Compute both Dice loss and Cross Entropy Loss, and return the weighted sum of these two losses.
  The details of Dice loss is shown in ``monai.losses.DiceLoss``.
  The details of Cross Entropy Loss is shown in ``torch.nn.CrossEntropyLoss`` and ``torch.nn.BCEWithLogitsLoss()``.
  In this implementation, two deprecated parameters ``size_average`` and ``reduce``, and the parameter ``ignore_index`` are
  not supported.
class_docstr_tok: ["Compute","both","Dice","loss","and","Cross","Entropy","Loss",",","and","return","the","weighted","sum","of","these","two","losses",".","The","details","of","Dice","loss","is","shown","in","``","monai.losses.DiceLoss","``",".","The","details","of","Cross","Entropy","Loss","is","shown","in","``","torch.nn.CrossEntropyLoss","``","and","``","torch.nn.BCEWithLogitsLoss","(",")","``",".","In","this","implementation",",","two","deprecated","parameters","``","size_average","``","and","``","reduce","``",",","and","the","parameter","``","ignore_index","``","are","not","supported","."]
docstr: Args:
      ``lambda_ce`` are only used for cross entropy loss.
      ``reduction`` and ``weight`` is used for both losses and other parameters are only used for dice loss.
  
      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, only used by the `DiceLoss`,
          don't need to specify activation function for `CrossEntropyLoss` and `BCEWithLogitsLoss`.
      softmax: if True, apply a softmax function to the prediction, only used by the `DiceLoss`,
          don't need to specify activation function for `CrossEntropyLoss` and `BCEWithLogitsLoss`.
      other_act: callable function to execute other activation layers, Defaults to ``None``. for example:
          ``other_act = torch.tanh``. only used by the `DiceLoss`, not for the `CrossEntropyLoss` and `BCEWithLogitsLoss`.
      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: {``"mean"``, ``"sum"``}
          Specifies the reduction to apply to the output. Defaults to ``"mean"``. The dice loss should
          as least reduce the spatial dimensions, which is different from cross entropy loss, thus here
          the ``none`` option cannot be used.
  
          - ``"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: a rescaling weight given to each class for cross entropy loss for `CrossEntropyLoss`.
          or a weight of positive examples to be broadcasted with target used as `pos_weight` for `BCEWithLogitsLoss`.
          See ``torch.nn.CrossEntropyLoss()`` or ``torch.nn.BCEWithLogitsLoss()`` for more information.
          The weight is also used in `DiceLoss`.
      lambda_dice: the trade-off weight value for dice loss. The value should be no less than 0.0.
          Defaults to 1.0.
      lambda_ce: the trade-off weight value for cross entropy loss. The value should be no less than 0.0.
          Defaults to 1.0.
docstr_tok: ["Args",":","``","lambda_ce","``","are","only","used","for","cross","entropy","loss",".","``","reduction","``","and","``","weight","``","is","used","for","both","losses","and","other","parameters","are","only","used","for","dice","loss",".","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",",","only","used","by","the","`","DiceLoss","`",",","do","n't","need","to","specify","activation","function","for","`","CrossEntropyLoss","`","and","`","BCEWithLogitsLoss","`",".","softmax",":","if","True",",","apply","a","softmax","function","to","the","prediction",",","only","used","by","the","`","DiceLoss","`",",","do","n't","need","to","specify","activation","function","for","`","CrossEntropyLoss","`","and","`","BCEWithLogitsLoss","`",".","other_act",":","callable","function","to","execute","other","activation","layers",",","Defaults","to","``","None","``",".","for","example",":","``","other_act","=","torch.tanh","``",".","only","used","by","the","`","DiceLoss","`",",","not","for","the","`","CrossEntropyLoss","`","and","`","BCEWithLogitsLoss","`",".","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",":","{","``","``","mean","''","``",",","``","``","sum","''","``","}","Specifies","the","reduction","to","apply","to","the","output",".","Defaults","to","``","``","mean","''","``",".","The","dice","loss","should","as","least","reduce","the","spatial","dimensions",",","which","is","different","from","cross","entropy","loss",",","thus","here","the","``","none","``","option","can","not","be","used",".","-","``","``","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",":","a","rescaling","weight","given","to","each","class","for","cross","entropy","loss","for","`","CrossEntropyLoss","`",".","or","a","weight","of","positive","examples","to","be","broadcasted","with","target","used","as","`","pos_weight","`","for","`","BCEWithLogitsLoss","`",".","See","``","torch.nn.CrossEntropyLoss","(",")","``","or","``","torch.nn.BCEWithLogitsLoss","(",")","``","for","more","information",".","The","weight","is","also","used","in","`","DiceLoss","`",".","lambda_dice",":","the","trade-off","weight","value","for","dice","loss",".","The","value","should","be","no","less","than","0.0",".","Defaults","to","1.0.","lambda_ce",":","the","trade-off","weight","value","for","cross","entropy","loss",".","The","value","should","be","no","less","than","0.0",".","Defaults","to","1.0","."]
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: str = "mean",
      smooth_nr: float = 1e-5,
      smooth_dr: float = 1e-5,
      batch: bool = False,
      ce_weight: torch.Tensor | None = None,
      weight: torch.Tensor | None = None,
      lambda_dice: float = 1.0,
      lambda_ce: float = 1.0,
  ) -> None:
      """
      Args:
          ``lambda_ce`` are only used for cross entropy loss.
          ``reduction`` and ``weight`` is used for both losses and other parameters are only used for dice loss.
  
          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, only used by the `DiceLoss`,
              don't need to specify activation function for `CrossEntropyLoss` and `BCEWithLogitsLoss`.
          softmax: if True, apply a softmax function to the prediction, only used by the `DiceLoss`,
              don't need to specify activation function for `CrossEntropyLoss` and `BCEWithLogitsLoss`.
          other_act: callable function to execute other activation layers, Defaults to ``None``. for example:
              ``other_act = torch.tanh``. only used by the `DiceLoss`, not for the `CrossEntropyLoss` and `BCEWithLogitsLoss`.
          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: {``"mean"``, ``"sum"``}
              Specifies the reduction to apply to the output. Defaults to ``"mean"``. The dice loss should
              as least reduce the spatial dimensions, which is different from cross entropy loss, thus here
              the ``none`` option cannot be used.
  
              - ``"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: a rescaling weight given to each class for cross entropy loss for `CrossEntropyLoss`.
              or a weight of positive examples to be broadcasted with target used as `pos_weight` for `BCEWithLogitsLoss`.
              See ``torch.nn.CrossEntropyLoss()`` or ``torch.nn.BCEWithLogitsLoss()`` for more information.
              The weight is also used in `DiceLoss`.
          lambda_dice: the trade-off weight value for dice loss. The value should be no less than 0.0.
              Defaults to 1.0.
          lambda_ce: the trade-off weight value for cross entropy loss. The value should be no less than 0.0.
              Defaults to 1.0.
  
      """
      super().__init__()
      reduction = look_up_option(reduction, DiceCEReduction).value
      weight = ce_weight if ce_weight is not None else weight
      dice_weight: torch.Tensor | None
      if weight is not None and not include_background:
          dice_weight = weight[1:]
      else:
          dice_weight = weight
      self.dice = DiceLoss(
          include_background=include_background,
          to_onehot_y=to_onehot_y,
          sigmoid=sigmoid,
          softmax=softmax,
          other_act=other_act,
          squared_pred=squared_pred,
          jaccard=jaccard,
          reduction=reduction,
          smooth_nr=smooth_nr,
          smooth_dr=smooth_dr,
          batch=batch,
          weight=dice_weight,
      )
      self.cross_entropy = nn.CrossEntropyLoss(
          weight=weight, reduction=reduction
      )
      self.binary_cross_entropy = nn.BCEWithLogitsLoss(
          pos_weight=weight, reduction=reduction
      )
      if lambda_dice < 0.0:
          raise ValueError("lambda_dice should be no less than 0.0.")
      if lambda_ce < 0.0:
          raise ValueError("lambda_ce should be no less than 0.0.")
      self.lambda_dice = lambda_dice
      self.lambda_ce = lambda_ce
      self.old_pt_ver = not pytorch_after(1, 10)
  
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 DiceCELoss object from inherited base classes: _Loss with example: obj = DiceCELoss(include_background, to_onehot_y, sigmoid, softmax, other_act, squared_pred, jaccard, reduction, smooth_nr, smooth_dr, batch, ce_weight, weight, lambda_dice, lambda_ce)