code: def _apply_class_weight(
      losses, targets, class_weights=None, norm_grads=True
  ):
      if class_weights is not None:
          # loss multiplier applied based on true class
          weights = tf.reduce_sum(
              input_tensor=class_weights
              * tf.cast(targets, dtype=tf.float32),
              axis=1,
          )
          if norm_grads:
              weights *= tf.math.divide_no_nan(
                  tf.cast(
                      tf.reduce_prod(
                          input_tensor=tf.shape(input=weights)
                      ),
                      dtype=tf.float32,
                  ),
                  tf.reduce_sum(input_tensor=weights),
              )
          losses *= tf.expand_dims(weights, 1)
      return losses
  
imports: import functools
  import tensorflow
  from tensorflow_addons.text.crf import crf_log_likelihood
  from finetune.base_models.gpt.featurizer import attn, dropout, norm
  from finetune.util.shapes import shape_list, merge_leading_dims
  from finetune.optimizers.recompute_grads import recompute_grad
  from finetune.errors import FinetuneError
  from finetune.nn.activations import act_fns
  from finetune.nn.nn_utils import norm
  from tensorflow.python.framework import function
label_desc: Use image node_id 4 for calling a global function with example usage: _apply_class_weight(losses, targets, class_weights, norm_grads) and returns: losses