code: def weighted_cross_entropy_loss(labels, log_dampened=False):
      """
      Either return a loss function which reweights all examples so the
      classes have the same effective weight, or dampened reweighting
      using log() so that the biggest class has some priority
      """
      if isinstance(labels, list):
          all_labels = np.array(labels)
      _, weights = np.unique(labels, return_counts=True)
      weights = weights / float(np.sum(weights))
      weights = np.sum(weights) / weights
      if log_dampened:
          weights = 1 + np.log(weights)
      logger.debug("Reweighting cross entropy by {}".format(weights))
      loss = nn.CrossEntropyLoss(
          weight=torch.from_numpy(weights).type("torch.FloatTensor")
      )
      return loss
  
file_path: stanza/stanza/models/common/loss.py
imports: import logging
  import numpy
  import torch
  import torch.nn
  import stanza.models.common.seq2seq_constant
label_desc: Use image node_id 2 for calling a global function with example usage: weighted_cross_entropy_loss(labels, log_dampened) and returns: loss