def weighted_binary_cross_entropy_from_probs(target,
                                             output,
                                             positive_class_weight=None,
                                             negative_class_weight=None):
  """Compute the binary cross entropy weighted by the given weights.

  Args:
    target: Prediction target
    output: Model prediction
    positive_class_weight: Weight to give positive class
    negative_class_weight: Weight to give negative class

  Returns:
    Batch loss
  """
  epsilon = tf.constant(_EPSILON, dtype=output.dtype.base_dtype)
  output = tf.clip_by_value(output, epsilon, 1. - epsilon)
  if positive_class_weight is None:
    positive_class_weight = tf.constant(1.0, dtype=output.dtype.base_dtype)
  else:
    positive_class_weight = tf.constant(
        positive_class_weight, dtype=output.dtype.base_dtype)

  if negative_class_weight is None:
    negative_class_weight = tf.constant(1.0, dtype=output.dtype.base_dtype)
  else:
    negative_class_weight = tf.constant(
        negative_class_weight, dtype=output.dtype.base_dtype)

  # Compute cross entropy from probabilities.
  target = tf.cast(target, output.dtype.base_dtype)
  bce = positive_class_weight * target * tf.math.log(output + epsilon)
  bce += negative_class_weight * (1 - target) * tf.math.log(1 - output +
                                                            epsilon)
  return -bce
Compute the binary cross entropy weighted by the given weights.

  Args:
    target: Prediction target
    output: Model prediction
    positive_class_weight: Weight to give positive class
    negative_class_weight: Weight to give negative class

  Returns:
    Batch loss
  
language/serene/losses.py