tf.nn.sigmoid_cross_entropy_with_logits
Stay organized with collections
Save and categorize content based on your preferences.
Computes sigmoid cross entropy given logits
.
tf.nn.sigmoid_cross_entropy_with_logits(
labels=None, logits=None, name=None
)
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 = labels
. 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 labels
must have the same type and shape.
Args |
labels
|
A Tensor of the same type and shape as logits .
|
logits
|
A Tensor of type float32 or float64 .
|
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 labels do not have the same shape.
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2020-10-01 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2020-10-01 UTC."],[],[],null,["# tf.nn.sigmoid_cross_entropy_with_logits\n\n\u003cbr /\u003e\n\n|-------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------|\n| [TensorFlow 1 version](/versions/r1.15/api_docs/python/tf/nn/sigmoid_cross_entropy_with_logits) | [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.0.0/tensorflow/python/ops/nn_impl.py#L193-L240) |\n\nComputes sigmoid cross entropy given `logits`. \n\n tf.nn.sigmoid_cross_entropy_with_logits(\n labels=None, logits=None, name=None\n )\n\nMeasures the probability error in discrete classification tasks in which each\nclass is independent and not mutually exclusive. For instance, one could\nperform multilabel classification where a picture can contain both an elephant\nand a dog at the same time.\n\nFor brevity, let `x = logits`, `z = labels`. 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\nFor x \\\u003c 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\nHence, to ensure stability and avoid overflow, the implementation uses this\nequivalent formulation \n\n max(x, 0) - x * z + log(1 + exp(-abs(x)))\n\n`logits` and `labels` must have the same type and shape.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|----------|----------------------------------------------------|\n| `labels` | A `Tensor` of the same type and shape as `logits`. |\n| `logits` | A `Tensor` of type `float32` or `float64`. |\n| `name` | A name for the operation (optional). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A `Tensor` of the same shape as `logits` with the componentwise logistic losses. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|------------------------------------------------------|\n| `ValueError` | If `logits` and `labels` do not have the same shape. |\n\n\u003cbr /\u003e"]]