class_docstr: L2 loss with margin.
  
  Args:
      neg_pos_ub (int, optional): The upper bound of negative to positive
          samples in hard mining. Defaults to -1.
      pos_margin (float, optional): The similarity margin for positive
          samples in hard mining. Defaults to -1.
      neg_margin (float, optional): The similarity margin for negative
          samples in hard mining. Defaults to -1.
      hard_mining (bool, optional): Whether to use hard mining. Defaults to
          False.
      reduction (str, optional): The method to reduce the loss.
          Options are "none", "mean" and "sum". Defaults to "mean".
      loss_weight (float, optional): The weight of loss. Defaults to 1.0.
class_docstr_tok: ["L2","loss","with","margin",".","Args",":","neg_pos_ub","(","int",",","optional",")",":","The","upper","bound","of","negative","to","positive","samples","in","hard","mining",".","Defaults","to","-1.","pos_margin","(","float",",","optional",")",":","The","similarity","margin","for","positive","samples","in","hard","mining",".","Defaults","to","-1.","neg_margin","(","float",",","optional",")",":","The","similarity","margin","for","negative","samples","in","hard","mining",".","Defaults","to","-1.","hard_mining","(","bool",",","optional",")",":","Whether","to","use","hard","mining",".","Defaults","to","False",".","reduction","(","str",",","optional",")",":","The","method","to","reduce","the","loss",".","Options","are","``","none","''",",","``","mean","''","and","``","sum","''",".","Defaults","to","``","mean","''",".","loss_weight","(","float",",","optional",")",":","The","weight","of","loss",".","Defaults","to","1.0","."]
docstr: Update the weight according to targets.
  
  Args:
      pred (torch.Tensor): The prediction.
      target (torch.Tensor): The learning target of the prediction.
      weight (torch.Tensor): The weight of loss for each prediction.
      avg_factor (float): Average factor that is used to average the
          loss.
  
  Returns:
      tuple[torch.Tensor]: The updated prediction, weight and average
      factor.
docstr_tok: ["Update","the","weight","according","to","targets",".","Args",":","pred","(","torch.Tensor",")",":","The","prediction",".","target","(","torch.Tensor",")",":","The","learning","target","of","the","prediction",".","weight","(","torch.Tensor",")",":","The","weight","of","loss","for","each","prediction",".","avg_factor","(","float",")",":","Average","factor","that","is","used","to","average","the","loss",".","Returns",":","tuple","[","torch.Tensor","]",":","The","updated","prediction",",","weight","and","average","factor","."]
code: def update_weight(
      self,
      pred: Tensor,
      target: Tensor,
      weight: Tensor,
      avg_factor: float,
  ) -> Tuple[Tensor, Tensor, float]:
      """Update the weight according to targets.
  
      Args:
          pred (torch.Tensor): The prediction.
          target (torch.Tensor): The learning target of the prediction.
          weight (torch.Tensor): The weight of loss for each prediction.
          avg_factor (float): Average factor that is used to average the
              loss.
  
      Returns:
          tuple[torch.Tensor]: The updated prediction, weight and average
          factor.
      """
      if weight is None:
          weight = target.new_ones(target.size())
  
      invalid_inds = weight <= 0
      target[invalid_inds] = -1
      pos_inds = target == 1
      neg_inds = target == 0
  
      if self.pos_margin > 0:
          pred[pos_inds] -= self.pos_margin
      if self.neg_margin > 0:
          pred[neg_inds] -= self.neg_margin
      pred = torch.clamp(pred, min=0, max=1)
  
      num_pos = int((target == 1).sum())
      num_neg = int((target == 0).sum())
      if (
          self.neg_pos_ub > 0
          and num_neg / (num_pos + 1e-6) > self.neg_pos_ub
      ):
          num_neg = num_pos * self.neg_pos_ub
          neg_idx = torch.nonzero(target == 0, as_tuple=False)
  
          if self.hard_mining:
              costs = mse_loss(pred, target.float(), reduction="none")[
                  neg_idx[:, 0], neg_idx[:, 1]
              ].detach()
              neg_idx = neg_idx[costs.topk(num_neg)[1], :]
          else:
              neg_idx = self.random_choice(neg_idx, num_neg)
  
          new_neg_inds = neg_inds.new_zeros(neg_inds.size()).bool()
          new_neg_inds[neg_idx[:, 0], neg_idx[:, 1]] = True
  
          invalid_neg_inds = torch.logical_xor(neg_inds, new_neg_inds)
          weight[invalid_neg_inds] = 0
  
      avg_factor = (weight > 0).sum()
      return pred, weight, avg_factor
  
file_path: mmdetection/mmdet/models/losses/margin_loss.py
imports: from typing import Optional, Tuple, Union
  import numpy
  import torch
  from mmengine.model import BaseModule
  from torch import Tensor
  from mmdet.registry import MODELS
  from .mse_loss import mse_loss
label_desc: Use image node_id 3 for calling the MarginL2Loss obj's underlying member method code with example usage: obj.update_weight(pred, target, weight, avg_factor) and returns: pred,  weight,  avg_factor