def compute_loss_sparse(logits, indices, weights=None):
  """Cross-entropy loss when only 1 label per example."""
  losses = tf.nn.sparse_softmax_cross_entropy_with_logits(
      labels=indices, logits=logits)
  if weights is not None:
    row_sums = tf.squeeze(batch_gather(weights, tf.expand_dims(indices, 1)), 1)
    return tf.reduce_sum(row_sums * losses) / tf.maximum(
        tf.reduce_sum(row_sums), 1e-12), row_sums
  else:
    return tf.reduce_mean(losses), None
Cross-entropy loss when only 1 label per example.
language/labs/drkit/model_fns.py