def sigmoid_cross_entropy_with_logits(logits, targets, name=None):
    'Computes sigmoid cross entropy given `logits`.\n\n  Measures the probability error in discrete classification tasks in which each\n  class is independent and not mutually exclusive.  For instance, one could\n  perform multilabel classification where a picture can contain both an elephant\n  and a dog at the same time.\n\n  For brevity, let `x = logits`, `z = targets`.  The logistic loss is\n\n        z * -log(sigmoid(x)) + (1 - z) * -log(1 - sigmoid(x))\n      = z * -log(1 / (1 + exp(-x))) + (1 - z) * -log(exp(-x) / (1 + exp(-x)))\n      = z * log(1 + exp(-x)) + (1 - z) * (-log(exp(-x)) + log(1 + exp(-x)))\n      = z * log(1 + exp(-x)) + (1 - z) * (x + log(1 + exp(-x))\n      = (1 - z) * x + log(1 + exp(-x))\n      = x - x * z + log(1 + exp(-x))\n\n  For x < 0, to avoid overflow in exp(-x), we reformulate the above\n\n        x - x * z + log(1 + exp(-x))\n      = log(exp(x)) - x * z + log(1 + exp(-x))\n      = - x * z + log(1 + exp(x))\n\n  Hence, to ensure stability and avoid overflow, the implementation uses this\n  equivalent formulation\n\n      max(x, 0) - x * z + log(1 + exp(-abs(x)))\n\n  `logits` and `targets` must have the same type and shape.\n\n  Args:\n    logits: A `Tensor` of type `float32` or `float64`.\n    targets: A `Tensor` of the same type and shape as `logits`.\n    name: A name for the operation (optional).\n\n  Returns:\n    A `Tensor` of the same shape as `logits` with the componentwise\n    logistic losses.\n\n  Raises:\n    ValueError: If `logits` and `targets` do not have the same shape.\n  '
    with ops.op_scope([logits, targets], name, 'logistic_loss') as name:
        logits = ops.convert_to_tensor(logits, name='logits')
        targets = ops.convert_to_tensor(targets, name='targets')
        try:
            targets.get_shape().merge_with(logits.get_shape())
        except ValueError:
            raise ValueError(('logits and targets must have the same shape (%s vs %s)' % (logits.get_shape(), targets.get_shape())))
        zeros = array_ops.zeros_like(logits, dtype=logits.dtype)
        cond = (logits >= zeros)
        relu_logits = math_ops.select(cond, logits, zeros)
        neg_abs_logits = math_ops.select(cond, (- logits), logits)
        return math_ops.add((relu_logits - (logits * targets)), math_ops.log((1 + math_ops.exp(neg_abs_logits))), name=name)
Computes sigmoid cross entropy given `logits`.

Measures the probability error in discrete classification tasks in which each
class is independent and not mutually exclusive.  For instance, one could
perform multilabel classification where a picture can contain both an elephant
and a dog at the same time.

For brevity, let `x = logits`, `z = targets`.  The logistic loss is

      z * -log(sigmoid(x)) + (1 - z) * -log(1 - sigmoid(x))
    = z * -log(1 / (1 + exp(-x))) + (1 - z) * -log(exp(-x) / (1 + exp(-x)))
    = z * log(1 + exp(-x)) + (1 - z) * (-log(exp(-x)) + log(1 + exp(-x)))
    = z * log(1 + exp(-x)) + (1 - z) * (x + log(1 + exp(-x))
    = (1 - z) * x + log(1 + exp(-x))
    = x - x * z + log(1 + exp(-x))

For x < 0, to avoid overflow in exp(-x), we reformulate the above

      x - x * z + log(1 + exp(-x))
    = log(exp(x)) - x * z + log(1 + exp(-x))
    = - x * z + log(1 + exp(x))

Hence, to ensure stability and avoid overflow, the implementation uses this
equivalent formulation

    max(x, 0) - x * z + log(1 + exp(-abs(x)))

`logits` and `targets` must have the same type and shape.

Args:
  logits: A `Tensor` of type `float32` or `float64`.
  targets: A `Tensor` of the same type and shape as `logits`.
  name: A name for the operation (optional).

Returns:
  A `Tensor` of the same shape as `logits` with the componentwise
  logistic losses.

Raises:
  ValueError: If `logits` and `targets` do not have the same shape.
def sigmoid_cross_entropy_with_logits(logits, targets, name=None):
    'Computes sigmoid cross entropy given `logits`.\n\n  Measures the probability error in discrete classification tasks in which each\n  class is independent and not mutually exclusive.  For instance, one could\n  perform multilabel classification where a picture can contain both an elephant\n  and a dog at the same time.\n\n  For brevity, let `x = logits`, `z = targets`.  The logistic loss is\n\n        z * -log(sigmoid(x)) + (1 - z) * -log(1 - sigmoid(x))\n      = z * -log(1 / (1 + exp(-x))) + (1 - z) * -log(exp(-x) / (1 + exp(-x)))\n      = z * log(1 + exp(-x)) + (1 - z) * (-log(exp(-x)) + log(1 + exp(-x)))\n      = z * log(1 + exp(-x)) + (1 - z) * (x + log(1 + exp(-x))\n      = (1 - z) * x + log(1 + exp(-x))\n      = x - x * z + log(1 + exp(-x))\n\n  For x < 0, to avoid overflow in exp(-x), we reformulate the above\n\n        x - x * z + log(1 + exp(-x))\n      = log(exp(x)) - x * z + log(1 + exp(-x))\n      = - x * z + log(1 + exp(x))\n\n  Hence, to ensure stability and avoid overflow, the implementation uses this\n  equivalent formulation\n\n      max(x, 0) - x * z + log(1 + exp(-abs(x)))\n\n  `logits` and `targets` must have the same type and shape.\n\n  Args:\n    logits: A `Tensor` of type `float32` or `float64`.\n    targets: A `Tensor` of the same type and shape as `logits`.\n    name: A name for the operation (optional).\n\n  Returns:\n    A `Tensor` of the same shape as `logits` with the componentwise\n    logistic losses.\n\n  Raises:\n    ValueError: If `logits` and `targets` do not have the same shape.\n  '
    with ops.op_scope([logits, targets], name, 'logistic_loss') as name:
        logits = ops.convert_to_tensor(logits, name='logits')
        targets = ops.convert_to_tensor(targets, name='targets')
        try:
            targets.get_shape().merge_with(logits.get_shape())
        except ValueError:
            raise ValueError(('logits and targets must have the same shape (%s vs %s)' % (logits.get_shape(), targets.get_shape())))
        zeros = array_ops.zeros_like(logits, dtype=logits.dtype)
        cond = (logits >= zeros)
        relu_logits = math_ops.select(cond, logits, zeros)
        neg_abs_logits = math_ops.select(cond, (- logits), logits)
        return math_ops.add((relu_logits - (logits * targets)), math_ops.log((1 + math_ops.exp(neg_abs_logits))), name=name)