import tensorflow as tf

    probs: matrix of probabilities for the minibatch
    eps: value to clip the probabilities at
    class_weights: vector of relative weights to be assigned to each class
    sumd: dimensions along which to sum the x-ent matrix

  Returns:
    cross entropy loss for each example in the minibatch
  """
  adjusted_probs = tf.clip_by_value(probs, eps, 1.0 - eps)
  xent_mat = -y * tf.log(adjusted_probs)
  if class_weights is not None:
    xent_mat *= class_weights

  return tf.reduce_sum(xent_mat, sumd)


def _SafeNegEntropy(probs, batch_size, eps=0.0001):
  """Computes negative entropy in a way that will not overflow."""
  adjusted_probs = tf.clip_by_value(probs, eps, 1.0 - eps)
  entropy = tf.mul(probs, tf.log(adjusted_probs))
  return tf.reduce_sum(entropy) / batch_size
