class_docstr: HardNegativeSampler is used to suppress false positive rate in classification tasks.
  During training, it selects negative samples with high prediction scores.
  
  The training workflow is described as the follows:
  1) forward network and get prediction scores (classification prob/logits) for all the samples;
  2) use hard negative sampler to choose negative samples with high prediction scores and some positive samples;
  3) compute classification loss for the selected samples;
  4) do back propagation.
  
  Args:
      batch_size_per_image: number of training samples to be randomly selected per image
      positive_fraction: percentage of positive elements in the selected samples
      min_neg: minimum number of negative samples to select if possible.
      pool_size: when we need ``num_neg`` hard negative samples, they will be randomly selected from
          ``num_neg * pool_size`` negative samples with the highest prediction scores.
          Larger ``pool_size`` gives more randomness, yet selects negative samples that are less 'hard',
          i.e., negative samples with lower prediction scores.
class_docstr_tok: ["HardNegativeSampler","is","used","to","suppress","false","positive","rate","in","classification","tasks",".","During","training",",","it","selects","negative","samples","with","high","prediction","scores",".","The","training","workflow","is","described","as","the","follows",":","1",")","forward","network","and","get","prediction","scores","(","classification","prob\/logits",")","for","all","the","samples",";","2",")","use","hard","negative","sampler","to","choose","negative","samples","with","high","prediction","scores","and","some","positive","samples",";","3",")","compute","classification","loss","for","the","selected","samples",";","4",")","do","back","propagation",".","Args",":","batch_size_per_image",":","number","of","training","samples","to","be","randomly","selected","per","image","positive_fraction",":","percentage","of","positive","elements","in","the","selected","samples","min_neg",":","minimum","number","of","negative","samples","to","select","if","possible",".","pool_size",":","when","we","need","``","num_neg","``","hard","negative","samples",",","they","will","be","randomly","selected","from","``","num_neg","*","pool_size","``","negative","samples","with","the","highest","prediction","scores",".","Larger","``","pool_size","``","gives","more","randomness",",","yet","selects","negative","samples","that","are","less","'hard","'",",","i.e.",",","negative","samples","with","lower","prediction","scores","."]
code: def __init__(
      self,
      batch_size_per_image: int,
      positive_fraction: float,
      min_neg: int = 1,
      pool_size: float = 10,
  ) -> None:
      super().__init__(pool_size=pool_size)
      self.min_neg = min_neg
      self.batch_size_per_image = batch_size_per_image
      self.positive_fraction = positive_fraction
      logging.info("Sampling hard negatives on a per batch basis")
  
file_path: MONAI/monai/apps/detection/utils/hard_negative_sampler.py
imports: from __future__ import annotations
  import logging
  import torch
  from torch import Tensor
label_desc: Use image node_id 1 to create a new HardNegativeSampler object from inherited base classes: HardNegativeSamplerBase with example: obj = HardNegativeSampler(batch_size_per_image, positive_fraction, min_neg, pool_size)