code: def get_class_weights(y, num_classes, eps=1e-8):
      """Computes class weights inversely proportional to their observed occurence. The most frequent
      class receives a weight of `1.0` and the other classes receive a weight greater than that.
  
      Parameters
      ----------
      y : np.ndarray[int] or csr_amtrix
          A sequence of labels.
      num_classes : int
          The number of classes.
      eps : float
          A stability term which prevents division by zero.
  
      Returns
      -------
      class_weights : torch.FloatTensor
          A tensor of class weights.
  
      Note
      ----
      This functionality is intended for single-label classification and is likely to be a
      suboptimal solution for a multi-label scenario.
      """
      # deprecated: the eps kwarg will be removed in the next version
      _unused = eps
  
      label_counter = _get_class_histogram(
          y, num_classes, normalize=False
      )
      class_weights = torch.ones(num_classes, dtype=torch.float)
      num_samples = label_counter.sum()
      for c in range(num_classes):
          class_weights[c] = (num_samples - label_counter[c]) / max(
              label_counter[c], 1
          )
  
      if num_classes == 2:
          class_weights[class_weights.argmin()] = 1.0
      else:
          class_weights /= max(class_weights.min(), 1)
  
      return class_weights
  
imports: import numpy
  from small_text.data.sampling import _get_class_histogram
  from small_text.integrations.pytorch.exceptions import PytorchNotFoundError
label_desc: Use image node_id 2 for calling a global function with example usage: get_class_weights(y, num_classes, eps) and returns: class_weights