class_docstr: Compute the Tversky loss defined in:
  
      Sadegh et al. (2017) Tversky loss function for image segmentation
      using 3D fully convolutional deep networks. (https://arxiv.org/abs/1706.05721)
  
  Adapted from:
      https://github.com/NifTK/NiftyNet/blob/v0.6.0/niftynet/layer/loss_segmentation.py#L631
class_docstr_tok: ["Compute","the","Tversky","loss","defined","in",":","Sadegh","et","al",".","(","2017",")","Tversky","loss","function","for","image","segmentation","using","3D","fully","convolutional","deep","networks",".","(","https",":","\/\/arxiv.org\/abs\/1706.05721",")","Adapted","from",":","https",":","\/\/github.com\/NifTK\/NiftyNet\/blob\/v0.6.0\/niftynet\/layer\/loss_segmentation.py","#","L631"]
docstr: Args:
      input: the shape should be BNH[WD].
      target: the shape should be BNH[WD].
  
  Raises:
      ValueError: When ``self.reduction`` is not one of ["mean", "sum", "none"].
docstr_tok: ["Args",":","input",":","the","shape","should","be","BNH","[","WD","]",".","target",":","the","shape","should","be","BNH","[","WD","]",".","Raises",":","ValueError",":","When","``","self.reduction","``","is","not","one","of","[","``","mean","''",",","``","sum","''",",","``","none","''","]","."]
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].
  
      Raises:
          ValueError: When ``self.reduction`` is not one of ["mean", "sum", "none"].
  
      """
      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 differing shape ({target.shape}) from input ({input.shape})"
          )
  
      p0 = input
      p1 = 1 - p0
      g0 = target
      g1 = 1 - g0
  
      # 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
  
      tp = torch.sum(p0 * g0, reduce_axis)
      fp = self.alpha * torch.sum(p0 * g1, reduce_axis)
      fn = self.beta * torch.sum(p1 * g0, reduce_axis)
      numerator = tp + self.smooth_nr
      denominator = tp + fp + fn + self.smooth_dr
  
      score: torch.Tensor = 1.0 - numerator / denominator
  
      if self.reduction == LossReduction.SUM.value:
          return torch.sum(score)  # sum over the batch and channel dims
      if self.reduction == LossReduction.NONE.value:
          return score  # returns [N, num_classes] losses
      if self.reduction == LossReduction.MEAN.value:
          return torch.mean(score)
      raise ValueError(
          f'Unsupported reduction: {self.reduction}, available options are ["mean", "sum", "none"].'
      )
  
file_path: MONAI/monai/losses/tversky.py
imports: from __future__ import annotations
  import warnings
  from collections.abc import Callable
  import torch
  from torch.nn.modules.loss import _Loss
  from monai.networks import one_hot
  from monai.utils import LossReduction
label_desc: Use image node_id 2 for calling the TverskyLoss obj's underlying member method code with example usage: obj.forward(input, target) and returns: torch, score, torch