class_docstr: A fast way to sample from a multinomial distribution.
  Faster than torch.multinomial or np.multinomial.
  The setup (__init__) for this class is slow, however
  `draw' (actual sampling) is fast.
class_docstr_tok: ["A","fast","way","to","sample","from","a","multinomial","distribution",".","Faster","than","torch.multinomial","or","np.multinomial",".","The","setup","(","__init__",")","for","this","class","is","slow",",","however","`","draw","'","(","actual","sampling",")","is","fast","."]
code: def __init__(self, probs):
      super(AliasMethod, self).__init__()
      if probs.sum() > 1:
          probs.div_(probs.sum())
      K = len(probs)
      self.register_buffer("prob", torch.zeros(K))
      self.register_buffer("alias", torch.LongTensor([0] * K))
  
      # Sort the data into the outcomes with probabilities that are larger and
      # smaller than 1/K.
      smaller = []
      larger = []
      for kk, prob in enumerate(probs):
          self.prob[kk] = K * prob
          if self.prob[kk] < 1.0:
              smaller.append(kk)
          else:
              larger.append(kk)
  
      # Loop though and create little binary mixtures that appropriately allocate
      # the larger outcomes over the overall uniform mixture.
      while len(smaller) > 0 and len(larger) > 0:
          small = smaller.pop()
          large = larger.pop()
  
          self.alias[small] = large
          self.prob[large] = (self.prob[large] - 1.0) + self.prob[small]
  
          if self.prob[large] < 1.0:
              smaller.append(large)
          else:
              larger.append(large)
  
      for last_one in smaller + larger:
          self.prob[last_one] = 1
  
imports: import logging
  import math
  import pprint
  from typing import List, Union
  import numpy
  import torch
  from classy_vision.generic.distributed_util import all_reduce_mean, gather_from_all, get_rank
  from classy_vision.generic.util import is_pos_int
  from classy_vision.losses import ClassyLoss, register_loss
  from torch import nn
  from vissl.config import AttrDict
label_desc: Use image node_id 1 to create a new AliasMethod object from inherited base classes: nn with example: obj = AliasMethod(probs)