class_docstr: Compute contrastive loss
  Args:
      margin (int): margin
      measure (bool): measure
      max_violation (int): image feature dimension
class_docstr_tok: ["Compute","contrastive","loss","Args",":","margin","(","int",")",":","margin","measure","(","bool",")",":","measure","max_violation","(","int",")",":","image","feature","dimension"]
code: def compute_contrastive_loss(self, scores):
      diagonal = scores.diag().view(scores.size(0), 1)
      d1 = diagonal.expand_as(scores)
      d2 = diagonal.t().expand_as(scores)
  
      # compare every diagonal score to scores in its column
      # caption retrieval
      cost_s = (self.margin + scores - d1).clamp(min=0)
      # compare every diagonal score to scores in its row
      # image retrieval
      cost_im = (self.margin + scores - d2).clamp(min=0)
  
      # clear diagonals
      mask = torch.eye(scores.size(0)) > 0.5
      i = mask
      if torch.cuda.is_available():
          i = i.cuda()
      cost_s = cost_s.masked_fill_(i, 0)
      cost_im = cost_im.masked_fill_(i, 0)
  
      # keep the maximum violating negative for each query
      if self.max_violation:
          cost_s = cost_s.max(1)[0]
          cost_im = cost_im.max(0)[0]
  
      return cost_s.sum() + cost_im.sum()
  
file_path: towhee/towhee/models/mcprop/loss.py
imports: import torch
  from torch import nn
label_desc: Use image node_id 2 for calling the Contrastive obj's underlying member method code with example usage: obj.compute_contrastive_loss(scores) and returns: unknown