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: Random select some elements from the gallery.
  
  It seems that Pytorch's implementation is slower than numpy so we use
  numpy to randperm the indices.
  
  Args:
      gallery (list | np.ndarray | torch.Tensor): The gallery from
          which to sample.
      num (int): The number of elements to sample.
docstr_tok: ["Random","select","some","elements","from","the","gallery",".","It","seems","that","Pytorch","'s","implementation","is","slower","than","numpy","so","we","use","numpy","to","randperm","the","indices",".","Args",":","gallery","(","list","|","np.ndarray","|","torch.Tensor",")",":","The","gallery","from","which","to","sample",".","num","(","int",")",":","The","number","of","elements","to","sample","."]
code: def random_choice(
      gallery: Union[list, np.ndarray, Tensor], num: int
  ) -> np.ndarray:
      """Random select some elements from the gallery.
  
      It seems that Pytorch's implementation is slower than numpy so we use
      numpy to randperm the indices.
  
      Args:
          gallery (list | np.ndarray | torch.Tensor): The gallery from
              which to sample.
          num (int): The number of elements to sample.
      """
      assert len(gallery) >= num
      if isinstance(gallery, list):
          gallery = np.array(gallery)
      cands = np.arange(len(gallery))
      np.random.shuffle(cands)
      rand_inds = cands[:num]
      if not isinstance(gallery, np.ndarray):
          rand_inds = (
              torch.from_numpy(rand_inds).long().to(gallery.device)
          )
      return gallery[rand_inds]
  
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 4 for calling the MarginL2Loss obj's underlying member method code with example usage: obj.random_choice(gallery, num) and returns: gallery