code: def sparse_balanced_crossentropy(logits, labels):
      """
      Calculates a class frequency balanced crossentropy loss from sparse labels.
  
      Args:
          logits (tf.Tensor): logits prediction for which to calculate
              crossentropy error
          labels (tf.Tensor): sparse labels used for crossentropy error
              calculation
  
      Returns:
          tf.Tensor: Tensor scalar representing the mean loss
      """
  
      epsilon = tf.constant(np.finfo(np.float32).tiny)
  
      num_classes = tf.cast(tf.shape(logits)[-1], tf.int32)
  
      probs = tf.nn.softmax(logits)
      probs += tf.cast(tf.less(probs, epsilon), tf.float32) * epsilon
      log = -1.0 * tf.log(probs)
  
      onehot_labels = tf.one_hot(labels, num_classes)
  
      class_frequencies = tf.stop_gradient(
          tf.bincount(labels, minlength=num_classes, dtype=tf.float32)
      )
  
      weights = 1.0 / (class_frequencies + tf.constant(1e-8))
      weights *= tf.cast(
          tf.reduce_prod(tf.shape(labels)), tf.float32
      ) / tf.cast(num_classes, tf.float32)
  
      new_shape = (
          [
              1,
          ]
          * len(labels.get_shape().as_list())
      ) + [logits.get_shape().as_list()[-1]]
  
      weights = tf.reshape(weights, new_shape)
  
      loss = tf.reduce_mean(
          tf.reduce_sum(onehot_labels * log * weights, axis=-1)
      )
  
      return loss
  
imports: from __future__ import unicode_literals
  from __future__ import print_function
  from __future__ import division
  from __future__ import absolute_import
  import tensorflow
  import numpy
label_desc: Use image node_id 1 for calling a global function with example usage: sparse_balanced_crossentropy(logits, labels) and returns: loss