def binary_cross_entropy_with_logits(input_,
                                     target,
                                     name=PROVIDED,
                                     loss_weight=None,
                                     per_example_weights=None,
                                     per_output_weights=None):
  """Calculates the binary cross entropy of the input_ vs inputs.

  Expects unscaled logits. Do not pass in results of sigmoid operation.

  Args:
    input_: A rank 2 Tensor or a Pretty Tensor holding the logits.
    target: A rank 2 tf.float32 or tf.float64 tensor containing class label
      probabilities. Note that binary cross entropy is equivalent to logistic
      loss.
    name: The optional name.
    loss_weight: A scalar multiplier for the loss.
    per_example_weights: A `Tensor` with a weight per example.
    per_output_weights: A weight `Tensor` that is the same shape as the
      input_ that can be used to scale individual prediction losses.  See
      `tf.tile` to turn a per-column weight vector into a `per_output_weights`
      `Tensor`.
  Returns:
    Binary cross entropy loss after sigmoid operation.
  Raises:
    ValueError: if target is None or the type is not float or double.
  """
  if target is None:
    raise ValueError('target must be set')
  target = _convert_and_assert_tensors_compatible(input_, target)

  with tf.name_scope('stats'):
    selected, sum_retrieved, sum_relevant = _compute_precision_recall(
        input_, target, 0, per_example_weights)
    precision = selected / sum_retrieved
    recall = selected / sum_relevant
    if precision.get_shape().is_fully_defined():
      input_.bookkeeper.add_average_summary(
          precision, 'average_precision_%s' % name)
    if recall.get_shape().is_fully_defined():
      input_.bookkeeper.add_average_summary(
          recall, 'average_recall_%s' % name)
    input_.bookkeeper.add_scalar_summary(
        tf.reduce_sum(tf.to_float(tf.greater(input_, 0))), 'activations')
    if per_output_weights is not None:
      per_output_weights = tf.convert_to_tensor(
          per_output_weights,
          name='per_output_weights',
          dtype=input_.dtype.base_dtype)
      input_.get_shape().assert_is_compatible_with(
          per_output_weights.get_shape())

  def _batch_sum_bce(x, target, name='binary_cross_entropy'):
    logits = functions.binary_cross_entropy_loss_with_logits(x,
                                                             target,
                                                             name=name)
    if per_output_weights is not None:
      logits *= per_output_weights
    return functions.reduce_batch_sum(logits)

  return apply_regression(
      input_,
      _batch_sum_bce,
      target,
      [],
      name='%s_bce_loss' % name,
      loss_weight=loss_weight,
      per_example_weights=per_example_weights)
def binary_cross_entropy_with_logits ( input_ , target , name = PROVIDED , loss_weight = None , per_example_weights = None , per_output_weights = None ) : if target is None : raise ValueError ( 'target must be set' ) target = _convert_and_assert_tensors_compatible ( input_ , target ) with tf . name_scope ( 'stats' ) : selected , sum_retrieved , sum_relevant = _compute_precision_recall ( input_ , target , 0 , per_example_weights ) precision = selected / sum_retrieved recall = selected / sum_relevant if precision . get_shape ( ) . is_fully_defined ( ) : input_ . bookkeeper . add_average_summary ( precision , 'average_precision_%s' % name ) if recall . get_shape ( ) . is_fully_defined ( ) : input_ . bookkeeper . add_average_summary ( recall , 'average_recall_%s' % name ) input_ . bookkeeper . add_scalar_summary ( tf . reduce_sum ( tf . to_float ( tf . greater ( input_ , 0 ) ) ) , 'activations' ) if per_output_weights is not None : per_output_weights = tf . convert_to_tensor ( per_output_weights , name = 'per_output_weights' , dtype = input_ . dtype . base_dtype ) input_ . get_shape ( ) . assert_is_compatible_with ( per_output_weights . get_shape ( ) ) def _batch_sum_bce ( x , target , name = 'binary_cross_entropy' ) : logits = functions . binary_cross_entropy_loss_with_logits ( x , target , name = name ) if per_output_weights is not None : logits *= per_output_weights return functions . reduce_batch_sum ( logits ) return apply_regression ( input_ , _batch_sum_bce , target , [ ] , name = '%s_bce_loss' % name , loss_weight = loss_weight , per_example_weights = per_example_weights )
Calculates the binary cross entropy of the input_ vs inputs.

  Expects unscaled logits. Do not pass in results of sigmoid operation.

  Args:
    input_: A rank 2 Tensor or a Pretty Tensor holding the logits.
    target: A rank 2 tf.float32 or tf.float64 tensor containing class label
      probabilities. Note that binary cross entropy is equivalent to logistic
      loss.
    name: The optional name.
    loss_weight: A scalar multiplier for the loss.
    per_example_weights: A `Tensor` with a weight per example.
    per_output_weights: A weight `Tensor` that is the same shape as the
      input_ that can be used to scale individual prediction losses.  See
      `tf.tile` to turn a per-column weight vector into a `per_output_weights`
      `Tensor`.
  Returns:
    Binary cross entropy loss after sigmoid operation.
  Raises:
    ValueError: if target is None or the type is not float or double.