code: def loss(labels, predictions, weights=None):
      """Calculate a per-batch sparse categorical crossentropy loss.
  
      This loss function assumes that the predictions are post-softmax.
      Args:
        labels: The labels to evaluate against. Should be a set of integer indices
          ranging from 0 to (vocab_size-1).
        predictions: The network predictions. Should have softmax already applied.
        weights: An optional weight array of the same shape as the 'labels' array.
          If None, all examples will be used.
  
      Returns:
        A loss scalar.
  
      Raises:
        RuntimeError if the passed tensors do not have the same rank.
      """
      # When using these functions with the Keras core API, we will need to squeeze
      # the labels tensor - Keras adds a spurious inner dimension.
      labels, predictions = _adjust_labels(labels, predictions)
      _validate_rank(labels, predictions, weights)
  
      per_example_loss_data = per_example_loss(
          labels, predictions, weights
      )
  
      if weights is None:
          return tf.keras.backend.mean(per_example_loss_data)
      else:
          numerator = tf.keras.backend.sum(per_example_loss_data)
          weights = tf.keras.backend.cast(weights, predictions.dtype)
          denominator = tf.keras.backend.sum(weights) + 1e-5
          return numerator / denominator
  
file_path: autodist/examples/benchmark/utils/modeling/losses/weighted_sparse_categorical_crossentropy.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import tensorflow
label_desc: Use image node_id 4 for calling a global function with example usage: loss(labels, predictions, weights) and returns: tf, unknown