class_docstr: Compute the Contrastive loss defined in:
  
      Chen, Ting, et al. "A simple framework for contrastive learning of visual representations." International
      conference on machine learning. PMLR, 2020. (http://proceedings.mlr.press/v119/chen20j.html)
  
  Adapted from:
      https://github.com/Sara-Ahmed/SiT/blob/1aacd6adcd39b71efc903d16b4e9095b97dda76f/losses.py#L5
docstr: Args:
      input: the shape should be B[F].
      target: the shape should be B[F].
docstr_tok: ["Args",":","input",":","the","shape","should","be","B","[","F","]",".","target",":","the","shape","should","be","B","[","F","]","."]
code: def forward(
      self, input: torch.Tensor, target: torch.Tensor
  ) -> torch.Tensor:
      """
      Args:
          input: the shape should be B[F].
          target: the shape should be B[F].
      """
      if len(target.shape) > 2 or len(input.shape) > 2:
          raise ValueError(
              f"Either target or input has dimensions greater than 2 where target "
              f"shape is ({target.shape}) and input shape is ({input.shape})"
          )
  
      if target.shape != input.shape:
          raise ValueError(
              f"ground truth has differing shape ({target.shape}) from input ({input.shape})"
          )
  
      temperature_tensor = torch.as_tensor(self.temperature).to(
          input.device
      )
      batch_size = input.shape[0]
  
      negatives_mask = ~torch.eye(
          batch_size * 2, batch_size * 2, dtype=torch.bool
      )
      negatives_mask = torch.clone(negatives_mask.type(torch.float)).to(
          input.device
      )
  
      repr = torch.cat([input, target], dim=0)
      sim_matrix = F.cosine_similarity(
          repr.unsqueeze(1), repr.unsqueeze(0), dim=2
      )
      sim_ij = torch.diag(sim_matrix, batch_size)
      sim_ji = torch.diag(sim_matrix, -batch_size)
  
      positives = torch.cat([sim_ij, sim_ji], dim=0)
      nominator = torch.exp(positives / temperature_tensor)
      denominator = negatives_mask * torch.exp(
          sim_matrix / temperature_tensor
      )
  
      loss_partial = -torch.log(
          nominator / torch.sum(denominator, dim=1)
      )
  
      return torch.sum(loss_partial) / (2 * batch_size)
  
file_path: MONAI/monai/losses/contrastive.py
imports: from __future__ import annotations
  from warnings import warn
  import torch
  from torch.nn import functional
  from torch.nn.modules.loss import _Loss
label_desc: Use image node_id 2 for calling the ContrastiveLoss obj's underlying member method code with example usage: obj.forward(input, target) and returns: unknown