class_docstr: Calculates an adversarial loss on a Patch Discriminator or a Multi-scale Patch Discriminator.
  Warning: due to the possibility of using different criterions, the output of the discrimination
  mustn't be passed to a final activation layer. That is taken care of internally within the loss.
  
  Args:
      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.
  
      criterion: which criterion (hinge, least_squares or bce) you want to use on the discriminators outputs.
          Depending on the criterion, a different activation layer will be used. Make sure you don't run the outputs
          through an activation layer prior to calling the loss.
      no_activation_leastsq: if True, the activation layer in the case of least-squares is removed.
docstr: Args:
      input: output of Multi-Scale Patch Discriminator or Patch Discriminator; being a list of tensors
          or a tensor; they shouldn't have gone through an activation layer.
      target_is_real: whereas the input corresponds to discriminator output for real or fake images
      for_discriminator: whereas this is being calculated for discriminator or generator loss. In the last
          case, target_is_real is set to True, as the generator wants the input to be dimmed as real.
  Returns: if reduction is None, returns a list with the loss tensors of each discriminator if multi-scale
      discriminator is active, or the loss tensor if there is just one discriminator. Otherwise, it returns the
      summed or mean loss over the tensor and discriminator/s.
docstr_tok: ["Args",":","input",":","output","of","Multi-Scale","Patch","Discriminator","or","Patch","Discriminator",";","being","a","list","of","tensors","or","a","tensor",";","they","should","n't","have","gone","through","an","activation","layer",".","target_is_real",":","whereas","the","input","corresponds","to","discriminator","output","for","real","or","fake","images","for_discriminator",":","whereas","this","is","being","calculated","for","discriminator","or","generator","loss",".","In","the","last","case",",","target_is_real","is","set","to","True",",","as","the","generator","wants","the","input","to","be","dimmed","as","real",".","Returns",":","if","reduction","is","None",",","returns","a","list","with","the","loss","tensors","of","each","discriminator","if","multi-scale","discriminator","is","active",",","or","the","loss","tensor","if","there","is","just","one","discriminator",".","Otherwise",",","it","returns","the","summed","or","mean","loss","over","the","tensor","and","discriminator\/s","."]
code: def forward(
      self,
      input: torch.Tensor | list,
      target_is_real: bool,
      for_discriminator: bool,
  ) -> torch.Tensor | list[torch.Tensor]:
      """
  
      Args:
          input: output of Multi-Scale Patch Discriminator or Patch Discriminator; being a list of tensors
              or a tensor; they shouldn't have gone through an activation layer.
          target_is_real: whereas the input corresponds to discriminator output for real or fake images
          for_discriminator: whereas this is being calculated for discriminator or generator loss. In the last
              case, target_is_real is set to True, as the generator wants the input to be dimmed as real.
      Returns: if reduction is None, returns a list with the loss tensors of each discriminator if multi-scale
          discriminator is active, or the loss tensor if there is just one discriminator. Otherwise, it returns the
          summed or mean loss over the tensor and discriminator/s.
  
      """
  
      if not for_discriminator and not target_is_real:
          target_is_real = (
              True  # With generator, we always want this to be true!
          )
          warnings.warn(
              "Variable target_is_real has been set to False, but for_discriminator is set"
              "to False. To optimise a generator, target_is_real must be set to True."
          )
  
      if not isinstance(input, list):
          input = [input]
      target_ = []
      for _, disc_out in enumerate(input):
          if self.criterion != AdversarialCriterions.HINGE:
              target_.append(
                  self.get_target_tensor(disc_out, target_is_real)
              )
          else:
              target_.append(self.get_zero_tensor(disc_out))
  
      # Loss calculation
      loss_list = []
      for disc_ind, disc_out in enumerate(input):
          if self.activation is not None:
              disc_out = self.activation(disc_out)
          if (
              self.criterion == AdversarialCriterions.HINGE
              and not target_is_real
          ):
              loss_ = self._forward_single(-disc_out, target_[disc_ind])
          else:
              loss_ = self._forward_single(disc_out, target_[disc_ind])
          loss_list.append(loss_)
  
      loss: torch.Tensor | list[torch.Tensor]
      if loss_list is not None:
          if self.reduction == LossReduction.MEAN:
              loss = torch.mean(torch.stack(loss_list))
          elif self.reduction == LossReduction.SUM:
              loss = torch.sum(torch.stack(loss_list))
          else:
              loss = loss_list
      return loss
  
file_path: MONAI/monai/losses/adversarial_loss.py
imports: from __future__ import annotations
  import warnings
  import torch
  from torch.nn.modules.loss import _Loss
  from monai.networks.layers.utils import get_act_layer
  from monai.utils import LossReduction
  from monai.utils.enums import StrEnum
label_desc: Use image node_id 4 for calling the PatchAdversarialLoss obj's underlying member method code with example usage: obj.forward(input, target_is_real, for_discriminator) and returns: loss