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:
(labels=None,
logits=None,
pos_weight=None,
name=None,
targets=None)
| 324 | @tf_export(v1=["nn.weighted_cross_entropy_with_logits"]) |
| 325 | @deprecated_args(None, "targets is deprecated, use labels instead", "targets") |
| 326 | def weighted_cross_entropy_with_logits(labels=None, |
| 327 | logits=None, |
| 328 | pos_weight=None, |
| 329 | name=None, |
| 330 | targets=None): |
| 331 | """Computes a weighted cross entropy. |
| 332 | |
| 333 | This is like `sigmoid_cross_entropy_with_logits()` except that `pos_weight`, |
| 334 | allows one to trade off recall and precision by up- or down-weighting the |
| 335 | cost of a positive error relative to a negative error. |
| 336 | |
| 337 | The usual cross-entropy cost is defined as: |
| 338 | |
| 339 | labels * -log(sigmoid(logits)) + |
| 340 | (1 - labels) * -log(1 - sigmoid(logits)) |
| 341 | |
| 342 | A value `pos_weight > 1` decreases the false negative count, hence increasing |
| 343 | the recall. |
| 344 | Conversely setting `pos_weight < 1` decreases the false positive count and |
| 345 | increases the precision. |
| 346 | This can be seen from the fact that `pos_weight` is introduced as a |
| 347 | multiplicative coefficient for the positive labels term |
| 348 | in the loss expression: |
| 349 | |
| 350 | labels * -log(sigmoid(logits)) * pos_weight + |
| 351 | (1 - labels) * -log(1 - sigmoid(logits)) |
| 352 | |
| 353 | For brevity, let `x = logits`, `z = labels`, `q = pos_weight`. |
| 354 | The loss is: |
| 355 | |
| 356 | qz * -log(sigmoid(x)) + (1 - z) * -log(1 - sigmoid(x)) |
| 357 | = qz * -log(1 / (1 + exp(-x))) + (1 - z) * -log(exp(-x) / (1 + exp(-x))) |
| 358 | = qz * log(1 + exp(-x)) + (1 - z) * (-log(exp(-x)) + log(1 + exp(-x))) |
| 359 | = qz * log(1 + exp(-x)) + (1 - z) * (x + log(1 + exp(-x)) |
| 360 | = (1 - z) * x + (qz + 1 - z) * log(1 + exp(-x)) |
| 361 | = (1 - z) * x + (1 + (q - 1) * z) * log(1 + exp(-x)) |
| 362 | |
| 363 | Setting `l = (1 + (q - 1) * z)`, to ensure stability and avoid overflow, |
| 364 | the implementation uses |
| 365 | |
| 366 | (1 - z) * x + l * (log(1 + exp(-abs(x))) + max(-x, 0)) |
| 367 | |
| 368 | `logits` and `labels` must have the same type and shape. |
| 369 | |
| 370 | Args: |
| 371 | labels: A `Tensor` of the same type and shape as `logits`. |
| 372 | logits: A `Tensor` of type `float32` or `float64`. |
| 373 | pos_weight: A coefficient to use on the positive examples. |
| 374 | name: A name for the operation (optional). |
| 375 | targets: Deprecated alias for labels. |
| 376 | |
| 377 | Returns: |
| 378 | A `Tensor` of the same shape as `logits` with the componentwise |
| 379 | weighted logistic losses. |
| 380 | |
| 381 | Raises: |
| 382 | ValueError: If `logits` and `labels` do not have the same shape. |
| 383 | """ |
nothing calls this directly
no test coverage detected