class_docstr: BatchSampler - from a MNIST-like dataset, samples n_samples for each of the n_classes.
  Returns batches of size n_classes * (batch_size // n_classes)
  adapted from https://github.com/adambielski/siamese-triplet/blob/master/datasets.py
class_docstr_tok: ["BatchSampler","-","from","a","MNIST-like","dataset",",","samples","n_samples","for","each","of","the","n_classes",".","Returns","batches","of","size","n_classes","*","(","batch_size","\/\/","n_classes",")","adapted","from","https",":","\/\/github.com\/adambielski\/siamese-triplet\/blob\/master\/datasets.py"]
code: def __init__(self, dataset, batch_size):
      labels = get_labels(dataset)
      classes = sorted(set(labels))
  
      n_classes = len(classes)
      self._n_samples = batch_size // n_classes
      if self._n_samples == 0:
          raise ValueError(
              f"batch_size should be bigger than the number of classes, got {batch_size}"
          )
  
      self._class_iters = [
          InfiniteSliceIterator(
              np.where(labels == class_)[0], class_=class_
          )
          for class_ in classes
      ]
  
      batch_size = self._n_samples * n_classes
      self._n_batches = len(labels) // batch_size
      if self._n_batches == 0:
          raise ValueError(
              f"Dataset is not big enough to generate batches with size {batch_size}"
          )
      logging.debug("K=", n_classes, "nk=", self._n_samples)
      logging.debug("Batch size = ", batch_size)
  
imports: import logging
  import numpy
  import torch.utils.data
  import torchvision
  from torch.utils.data.sampler import BatchSampler, RandomSampler
label_desc: Use image node_id 1 to create a new BalancedBatchSampler object from inherited base classes: torch.utils.data.sampler with example: obj = BalancedBatchSampler(dataset, batch_size)