Adds a Huber Loss term to the training procedure. For each value x in `error=labels-predictions`, the following is calculated: ``` 0.5 * x^2 if |x| <= d 0.5 * d^2 + d * (|x| - d) if |x| > d ``` where d is `delta`. See: https://en.wikipedia.org/wiki/Huber_loss
(labels, predictions, weights=1.0, delta=1.0, scope=None,
loss_collection=ops.GraphKeys.LOSSES,
reduction=Reduction.SUM_BY_NONZERO_WEIGHTS)
| 364 | |
| 365 | @tf_export(v1=["losses.huber_loss"]) |
| 366 | def huber_loss(labels, predictions, weights=1.0, delta=1.0, scope=None, |
| 367 | loss_collection=ops.GraphKeys.LOSSES, |
| 368 | reduction=Reduction.SUM_BY_NONZERO_WEIGHTS): |
| 369 | """Adds a Huber Loss term to the training procedure. |
| 370 | |
| 371 | For each value x in `error=labels-predictions`, the following is calculated: |
| 372 | |
| 373 | ``` |
| 374 | 0.5 * x^2 if |x| <= d |
| 375 | 0.5 * d^2 + d * (|x| - d) if |x| > d |
| 376 | ``` |
| 377 | |
| 378 | where d is `delta`. |
| 379 | |
| 380 | See: https://en.wikipedia.org/wiki/Huber_loss |
| 381 | |
| 382 | `weights` acts as a coefficient for the loss. If a scalar is provided, then |
| 383 | the loss is simply scaled by the given value. If `weights` is a tensor of size |
| 384 | `[batch_size]`, then the total loss for each sample of the batch is rescaled |
| 385 | by the corresponding element in the `weights` vector. If the shape of |
| 386 | `weights` matches the shape of `predictions`, then the loss of each |
| 387 | measurable element of `predictions` is scaled by the corresponding value of |
| 388 | `weights`. |
| 389 | |
| 390 | Args: |
| 391 | labels: The ground truth output tensor, same dimensions as 'predictions'. |
| 392 | predictions: The predicted outputs. |
| 393 | weights: Optional `Tensor` whose rank is either 0, or the same rank as |
| 394 | `labels`, and must be broadcastable to `labels` (i.e., all dimensions must |
| 395 | be either `1`, or the same as the corresponding `losses` dimension). |
| 396 | delta: `float`, the point where the huber loss function |
| 397 | changes from a quadratic to linear. |
| 398 | scope: The scope for the operations performed in computing the loss. |
| 399 | loss_collection: collection to which the loss will be added. |
| 400 | reduction: Type of reduction to apply to loss. |
| 401 | |
| 402 | Returns: |
| 403 | Weighted loss float `Tensor`. If `reduction` is `NONE`, this has the same |
| 404 | shape as `labels`; otherwise, it is scalar. |
| 405 | |
| 406 | Raises: |
| 407 | ValueError: If the shape of `predictions` doesn't match that of `labels` or |
| 408 | if the shape of `weights` is invalid. Also if `labels` or |
| 409 | `predictions` is None. |
| 410 | |
| 411 | @compatibility(eager) |
| 412 | The `loss_collection` argument is ignored when executing eagerly. Consider |
| 413 | holding on to the return value or collecting losses via a `tf.keras.Model`. |
| 414 | @end_compatibility |
| 415 | """ |
| 416 | if labels is None: |
| 417 | raise ValueError("labels must not be None.") |
| 418 | if predictions is None: |
| 419 | raise ValueError("predictions must not be None.") |
| 420 | with ops.name_scope(scope, "huber_loss", |
| 421 | (predictions, labels, weights)) as scope: |
| 422 | predictions = math_ops.cast(predictions, dtype=dtypes.float32) |
| 423 | labels = math_ops.cast(labels, dtype=dtypes.float32) |
nothing calls this directly
no test coverage detected