class_docstr: Hook to compute precise batch normalization statistics.
  
  Batch norm stats are calculated and updated during training, when the weights are
  also changing, which makes the calculations imprecise. This hook recomputes the
  batch norm stats at the end of a train phase to make them more precise. See
  `fvcore's documentation <https://github.com/facebookresearch/fvcore/blob/master/
  fvcore/nn/precise_bn.py>`_ for more information.
class_docstr_tok: ["Hook","to","compute","precise","batch","normalization","statistics",".","Batch","norm","stats","are","calculated","and","updated","during","training",",","when","the","weights","are","also","changing",",","which","makes","the","calculations","imprecise",".","This","hook","recomputes","the","batch","norm","stats","at","the","end","of","a","train","phase","to","make","them","more","precise",".","See","`","fvcore","'s","documentation","<","https",":","\/\/github.com\/facebookresearch\/fvcore\/blob\/master\/","fvcore\/nn\/precise_bn.py",">","`","_","for","more","information","."]
docstr: The constructor method of PreciseBatchNormHook.
  
  Caches the required number of samples on the CPU during train phases
  
  Args:
      num_samples: Number of samples to calculate the batch norm stats per replica
      cache_samples: If True, we cache samples at training stage. This avoids re-creating
          data loaders, but consumes more memory. If False, we re-create data loader at the
          end of phase, which might be slow for large dataset, but saves memory.
docstr_tok: ["The","constructor","method","of","PreciseBatchNormHook",".","Caches","the","required","number","of","samples","on","the","CPU","during","train","phases","Args",":","num_samples",":","Number","of","samples","to","calculate","the","batch","norm","stats","per","replica","cache_samples",":","If","True",",","we","cache","samples","at","training","stage",".","This","avoids","re-creating","data","loaders",",","but","consumes","more","memory",".","If","False",",","we","re-create","data","loader","at","the","end","of","phase",",","which","might","be","slow","for","large","dataset",",","but","saves","memory","."]
code: def __init__(
      self, num_samples: int, cache_samples: bool = False
  ) -> None:
      """The constructor method of PreciseBatchNormHook.
  
      Caches the required number of samples on the CPU during train phases
  
      Args:
          num_samples: Number of samples to calculate the batch norm stats per replica
          cache_samples: If True, we cache samples at training stage. This avoids re-creating
              data loaders, but consumes more memory. If False, we re-create data loader at the
              end of phase, which might be slow for large dataset, but saves memory.
      """
      super().__init__()
      if num_samples <= 0:
          raise ValueError("num_samples has to be a positive integer")
      self.num_samples = num_samples
      self.cache_samples = cache_samples
      if cache_samples:
          self.cache = []
          self.current_samples = 0
      else:
          self.batch_size = None
  
file_path: ClassyVision/classy_vision/hooks/precise_batch_norm_hook.py
imports: import logging
  import math
  import torch
  from classy_vision.generic.util import get_batchsize_per_replica, recursive_copy_to_device, recursive_copy_to_gpu
  from classy_vision.hooks import ClassyHook, register_hook
  from fvcore.nn.precise_bn import update_bn_stats
label_desc: Use image node_id 1 to create a new PreciseBatchNormHook object from inherited base classes: ClassyHook with example: obj = PreciseBatchNormHook(num_samples, cache_samples)