class_docstr: ClassPowerlawSampler
  
  Batch-sampler that samples 'batch-size' images using a power law distribution
  Intended to be used on balanced dataset such as Imagenet
  
  :param world_size: total number of workers in network
  :param rank: local rank in network
  :param batch_size: number of images to load from each class
  :param powerlaw: strength of the power law (coefficient)
  :param seed: common seed across workers for subsampling classes
class_docstr_tok: ["ClassPowerlawSampler","Batch-sampler","that","samples","'batch-size","'","images","using","a","power","law","distribution","Intended","to","be","used","on","balanced","dataset","such","as","Imagenet",":","param","world_size",":","total","number","of","workers","in","network",":","param","rank",":","local","rank","in","network",":","param","batch_size",":","number","of","images","to","load","from","each","class",":","param","powerlaw",":","strength","of","the","power","law","(","coefficient",")",":","param","seed",":","common","seed","across","workers","for","subsampling","classes"]
code: def inverse_square_root_resampling(labels):
      import math
      from collections import Counter
  
      class_index = {}
      for i, c in enumerate(labels):
          class_index.setdefault(c, []).append(i)
  
      total_len = len(labels)
      class_prob = {
          c: len(v) / total_len for c, v in class_index.items()
      }
      inv_sqrt_prob = {
          c: 1 / math.sqrt(p) for c, p in class_prob.items()
      }
  
      total_prob = 0.0
      final_prob = Counter()
      for idx in range(total_len):
          c = labels[idx]
          final_prob[c] += inv_sqrt_prob[c]
          total_prob += inv_sqrt_prob[c]
  
      final_prob = {k: v / total_prob for k, v in final_prob.items()}
      return final_prob
  
imports: import contextlib
  import logging
  import queue
  from typing import Dict, List
  import numpy
  import torch
  from PIL import Image
  from torch.utils.data import Dataset
  from torch.utils.data.distributed import DistributedSampler
label_desc: Use image node_id 2 for calling the ClassPowerlawSampler obj's underlying member method code with example usage: obj.inverse_square_root_resampling(labels) and returns: final_prob