Adds a Log Loss term to the training procedure. `weights` acts as a coefficient for the loss. If a scalar is provided, then the loss is simply scaled by the given value. If `weights` is a tensor of size `[batch_size]`, then the total loss for each sample of the batch is rescaled by the corr
(labels, predictions, weights=1.0, epsilon=1e-7, scope=None,
loss_collection=ops.GraphKeys.LOSSES,
reduction=Reduction.SUM_BY_NONZERO_WEIGHTS)
| 442 | |
| 443 | @tf_export(v1=["losses.log_loss"]) |
| 444 | def log_loss(labels, predictions, weights=1.0, epsilon=1e-7, scope=None, |
| 445 | loss_collection=ops.GraphKeys.LOSSES, |
| 446 | reduction=Reduction.SUM_BY_NONZERO_WEIGHTS): |
| 447 | """Adds a Log Loss term to the training procedure. |
| 448 | |
| 449 | `weights` acts as a coefficient for the loss. If a scalar is provided, then |
| 450 | the loss is simply scaled by the given value. If `weights` is a tensor of size |
| 451 | `[batch_size]`, then the total loss for each sample of the batch is rescaled |
| 452 | by the corresponding element in the `weights` vector. If the shape of |
| 453 | `weights` matches the shape of `predictions`, then the loss of each |
| 454 | measurable element of `predictions` is scaled by the corresponding value of |
| 455 | `weights`. |
| 456 | |
| 457 | Args: |
| 458 | labels: The ground truth output tensor, same dimensions as 'predictions'. |
| 459 | predictions: The predicted outputs. |
| 460 | weights: Optional `Tensor` whose rank is either 0, or the same rank as |
| 461 | `labels`, and must be broadcastable to `labels` (i.e., all dimensions must |
| 462 | be either `1`, or the same as the corresponding `losses` dimension). |
| 463 | epsilon: A small increment to add to avoid taking a log of zero. |
| 464 | scope: The scope for the operations performed in computing the loss. |
| 465 | loss_collection: collection to which the loss will be added. |
| 466 | reduction: Type of reduction to apply to loss. |
| 467 | |
| 468 | Returns: |
| 469 | Weighted loss float `Tensor`. If `reduction` is `NONE`, this has the same |
| 470 | shape as `labels`; otherwise, it is scalar. |
| 471 | |
| 472 | Raises: |
| 473 | ValueError: If the shape of `predictions` doesn't match that of `labels` or |
| 474 | if the shape of `weights` is invalid. Also if `labels` or `predictions` |
| 475 | is None. |
| 476 | |
| 477 | @compatibility(eager) |
| 478 | The `loss_collection` argument is ignored when executing eagerly. Consider |
| 479 | holding on to the return value or collecting losses via a `tf.keras.Model`. |
| 480 | @end_compatibility |
| 481 | """ |
| 482 | if labels is None: |
| 483 | raise ValueError("labels must not be None.") |
| 484 | if predictions is None: |
| 485 | raise ValueError("predictions must not be None.") |
| 486 | with ops.name_scope(scope, "log_loss", |
| 487 | (predictions, labels, weights)) as scope: |
| 488 | predictions = math_ops.cast(predictions, dtype=dtypes.float32) |
| 489 | labels = math_ops.cast(labels, dtype=dtypes.float32) |
| 490 | predictions.get_shape().assert_is_compatible_with(labels.get_shape()) |
| 491 | losses = -math_ops.multiply( |
| 492 | labels, |
| 493 | math_ops.log(predictions + epsilon)) - math_ops.multiply( |
| 494 | (1 - labels), math_ops.log(1 - predictions + epsilon)) |
| 495 | return compute_weighted_loss( |
| 496 | losses, weights, scope, loss_collection, reduction=reduction) |
| 497 | |
| 498 | |
| 499 | # TODO(b/37208492): Add reduction arg. |
nothing calls this directly
no test coverage detected