def weighted_cross_entropy_with_logits(logits, targets, pos_weight, name=None):
    'Computes a weighted cross entropy.\n\n  This is like `sigmoid_cross_entropy_with_logits()` except that `pos_weight`,\n  allows one to trade off recall and precision by up- or down-weighting the\n  cost of a positive error relative to a negative error.\n\n  The usual cross-entropy cost is defined as:\n\n    targets * -log(sigmoid(logits)) + (1 - targets) * -log(1 - sigmoid(logits))\n\n  The argument `pos_weight` is used as a multiplier for the positive targets:\n\n    targets * -log(sigmoid(logits)) * pos_weight +\n        (1 - targets) * -log(1 - sigmoid(logits))\n\n  For brevity, let `x = logits`, `z = targets`, `q = pos_weight`.\n  The loss is:\n\n        qz * -log(sigmoid(x)) + (1 - z) * -log(1 - sigmoid(x))\n      = qz * -log(1 / (1 + exp(-x))) + (1 - z) * -log(exp(-x) / (1 + exp(-x)))\n      = qz * log(1 + exp(-x)) + (1 - z) * (-log(exp(-x)) + log(1 + exp(-x)))\n      = qz * log(1 + exp(-x)) + (1 - z) * (x + log(1 + exp(-x))\n      = (1 - z) * x + (qz +  1 - z) * log(1 + exp(-x))\n      = (1 - z) * x + (1 + (q - 1) * z) * log(1 + exp(-x))\n\n  Setting `l = (1 + (q - 1) * z)`, to ensure stability and avoid overflow,\n  the implementation uses\n\n      (1 - z) * x + l * (log(1 + exp(-abs(x))) + max(-x, 0))\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    pos_weight: A coefficient to use on the positive examples.\n    name: A name for the operation (optional).\n\n  Returns:\n    A `Tensor` of the same shape as `logits` with the componentwise\n    weightedlogistic 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())))
        log_weight = (1 + ((pos_weight - 1) * targets))
        return math_ops.add(((1 - targets) * logits), (log_weight * (math_ops.log((1 + math_ops.exp((- math_ops.abs(logits))))) + nn_ops.relu((- logits)))), name=name)
Computes a weighted cross entropy.

This is like `sigmoid_cross_entropy_with_logits()` except that `pos_weight`,
allows one to trade off recall and precision by up- or down-weighting the
cost of a positive error relative to a negative error.

The usual cross-entropy cost is defined as:

  targets * -log(sigmoid(logits)) + (1 - targets) * -log(1 - sigmoid(logits))

The argument `pos_weight` is used as a multiplier for the positive targets:

  targets * -log(sigmoid(logits)) * pos_weight +
      (1 - targets) * -log(1 - sigmoid(logits))

For brevity, let `x = logits`, `z = targets`, `q = pos_weight`.
The loss is:

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

Setting `l = (1 + (q - 1) * z)`, to ensure stability and avoid overflow,
the implementation uses

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

`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`.
  pos_weight: A coefficient to use on the positive examples.
  name: A name for the operation (optional).

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

Raises:
  ValueError: If `logits` and `targets` do not have the same shape.
def weighted_cross_entropy_with_logits(logits, targets, pos_weight, name=None):
    'Computes a weighted cross entropy.\n\n  This is like `sigmoid_cross_entropy_with_logits()` except that `pos_weight`,\n  allows one to trade off recall and precision by up- or down-weighting the\n  cost of a positive error relative to a negative error.\n\n  The usual cross-entropy cost is defined as:\n\n    targets * -log(sigmoid(logits)) + (1 - targets) * -log(1 - sigmoid(logits))\n\n  The argument `pos_weight` is used as a multiplier for the positive targets:\n\n    targets * -log(sigmoid(logits)) * pos_weight +\n        (1 - targets) * -log(1 - sigmoid(logits))\n\n  For brevity, let `x = logits`, `z = targets`, `q = pos_weight`.\n  The loss is:\n\n        qz * -log(sigmoid(x)) + (1 - z) * -log(1 - sigmoid(x))\n      = qz * -log(1 / (1 + exp(-x))) + (1 - z) * -log(exp(-x) / (1 + exp(-x)))\n      = qz * log(1 + exp(-x)) + (1 - z) * (-log(exp(-x)) + log(1 + exp(-x)))\n      = qz * log(1 + exp(-x)) + (1 - z) * (x + log(1 + exp(-x))\n      = (1 - z) * x + (qz +  1 - z) * log(1 + exp(-x))\n      = (1 - z) * x + (1 + (q - 1) * z) * log(1 + exp(-x))\n\n  Setting `l = (1 + (q - 1) * z)`, to ensure stability and avoid overflow,\n  the implementation uses\n\n      (1 - z) * x + l * (log(1 + exp(-abs(x))) + max(-x, 0))\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    pos_weight: A coefficient to use on the positive examples.\n    name: A name for the operation (optional).\n\n  Returns:\n    A `Tensor` of the same shape as `logits` with the componentwise\n    weightedlogistic 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())))
        log_weight = (1 + ((pos_weight - 1) * targets))
        return math_ops.add(((1 - targets) * logits), (log_weight * (math_ops.log((1 + math_ops.exp((- math_ops.abs(logits))))) + nn_ops.relu((- logits)))), name=name)