class_docstr: Use this to upsample rare classes and downsample common classes in your unbalanced classification dataset.
docstr: Initialize the dataset used for sampling.
docstr_tok: ["Initialize","the","dataset","used","for","sampling","."]
code: def set_dataset(self, data_source):
      """Initialize the dataset used for sampling."""
      self.data_source = data_source
      self.num_samples = len(self.data_source)
      self.indices = list(range(len(data_source)))
  
      # first determine the distribution of classes in the dataset
      label_count: Dict[str, int] = defaultdict(int)
      for sentence in data_source:
          for label in sentence.labels:
              label_count[label.value] += 1
  
      # weight for each sample
      offset = 0
      weights = [
          1.0 / (offset + label_count[data_source[idx].labels[0].value])
          for idx in self.indices
      ]
  
      self.weights = torch.DoubleTensor(weights)
  
imports: import logging
  import random
  from collections import defaultdict
  from typing import Dict
  import torch
  from torch.utils.data.sampler import Sampler
label_desc: Use image node_id 2 for calling the ImbalancedClassificationDatasetSampler obj's underlying member method code with example usage: obj.set_dataset(data_source) without return types