Adds a Sum-of-Squares loss 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 th
(
labels, predictions, weights=1.0, scope=None,
loss_collection=ops.GraphKeys.LOSSES,
reduction=Reduction.SUM_BY_NONZERO_WEIGHTS)
| 595 | |
| 596 | @tf_export(v1=["losses.mean_squared_error"]) |
| 597 | def mean_squared_error( |
| 598 | labels, predictions, weights=1.0, scope=None, |
| 599 | loss_collection=ops.GraphKeys.LOSSES, |
| 600 | reduction=Reduction.SUM_BY_NONZERO_WEIGHTS): |
| 601 | """Adds a Sum-of-Squares loss to the training procedure. |
| 602 | |
| 603 | `weights` acts as a coefficient for the loss. If a scalar is provided, then |
| 604 | the loss is simply scaled by the given value. If `weights` is a tensor of size |
| 605 | `[batch_size]`, then the total loss for each sample of the batch is rescaled |
| 606 | by the corresponding element in the `weights` vector. If the shape of |
| 607 | `weights` matches the shape of `predictions`, then the loss of each |
| 608 | measurable element of `predictions` is scaled by the corresponding value of |
| 609 | `weights`. |
| 610 | |
| 611 | Args: |
| 612 | labels: The ground truth output tensor, same dimensions as 'predictions'. |
| 613 | predictions: The predicted outputs. |
| 614 | weights: Optional `Tensor` whose rank is either 0, or the same rank as |
| 615 | `labels`, and must be broadcastable to `labels` (i.e., all dimensions must |
| 616 | be either `1`, or the same as the corresponding `losses` dimension). |
| 617 | scope: The scope for the operations performed in computing the loss. |
| 618 | loss_collection: collection to which the loss will be added. |
| 619 | reduction: Type of reduction to apply to loss. |
| 620 | |
| 621 | Returns: |
| 622 | Weighted loss float `Tensor`. If `reduction` is `NONE`, this has the same |
| 623 | shape as `labels`; otherwise, it is scalar. |
| 624 | |
| 625 | Raises: |
| 626 | ValueError: If the shape of `predictions` doesn't match that of `labels` or |
| 627 | if the shape of `weights` is invalid. Also if `labels` or `predictions` |
| 628 | is None. |
| 629 | |
| 630 | @compatibility(eager) |
| 631 | The `loss_collection` argument is ignored when executing eagerly. Consider |
| 632 | holding on to the return value or collecting losses via a `tf.keras.Model`. |
| 633 | @end_compatibility |
| 634 | """ |
| 635 | if labels is None: |
| 636 | raise ValueError("labels must not be None.") |
| 637 | if predictions is None: |
| 638 | raise ValueError("predictions must not be None.") |
| 639 | with ops.name_scope(scope, "mean_squared_error", |
| 640 | (predictions, labels, weights)) as scope: |
| 641 | predictions = math_ops.cast(predictions, dtype=dtypes.float32) |
| 642 | labels = math_ops.cast(labels, dtype=dtypes.float32) |
| 643 | predictions.get_shape().assert_is_compatible_with(labels.get_shape()) |
| 644 | losses = math_ops.squared_difference(predictions, labels) |
| 645 | return compute_weighted_loss( |
| 646 | losses, weights, scope, loss_collection, reduction=reduction) |
| 647 | |
| 648 | |
| 649 | @tf_export(v1=["losses.sigmoid_cross_entropy"]) |
nothing calls this directly
no test coverage detected