Table of Contents
Shortcuts

# HuberLoss

class torch.nn.HuberLoss(reduction='mean', delta=1.0)

  Creates a criterion that uses a squared term if the absolute element-wise error falls below delta and a delta-scaled L1 term otherwise. This loss combines advantages of both `L1Loss` and `MSELoss`; the delta-scaled L1 region makes the loss less sensitive to outliers than `MSELoss`, while the L2 region provides smoothness over `L1Loss` near 0. See Huber loss for more information.

  For a batch of size N N N, the unreduced loss can be described as:

  ℓ(x,y)=L={l 1,...,l N}T\ell(x, y) = L = \{l_1, ..., l_N\}^T ℓ(x,y)=L={l 1​,...,l N​}T

  with

  l n={0.5(x n−y n)2,if ∣x n−y n∣<d e l t a d e l t a∗(∣x n−y n∣−0.5∗d e l t a),otherwise l_n = \begin{cases} 0.5 (x_n - y_n)^2, & \text{if } |x_n - y_n| < delta \\ delta * (|x_n - y_n| - 0.5 * delta), & \text{otherwise } \end{cases} l n​={0.5(x n​−y n​)2,d e lt a∗(∣x n​−y n​∣−0.5∗d e lt a),​if ∣x n​−y n​∣<d e lt a otherwise ​

  If reduction is not none, then:

  ℓ(x,y)={mean⁡(L),if reduction=‘mean’;sum⁡(L),if reduction=‘sum’.\ell(x, y) = \begin{cases} \operatorname{mean}(L), & \text{if reduction} = \text{`mean';}\\ \operatorname{sum}(L), & \text{if reduction} = \text{`sum'.} \end{cases} ℓ(x,y)={mean(L),sum(L),​if reduction=‘mean’;if reduction=‘sum’.​

  Note

  When delta is set to 1, this loss is equivalent to `SmoothL1Loss`. In general, this loss differs from `SmoothL1Loss` by a factor of delta (AKA beta in Smooth L1). See `SmoothL1Loss` for additional discussion on the differences in behavior between the two losses.

  Parameters:

    - reduction (str, optional) – Specifies the reduction to apply to the output: `'none'` | `'mean'` | `'sum'`. `'none'`: no reduction will be applied, `'mean'`: the sum of the output will be divided by the number of elements in the output, `'sum'`: the output will be summed. Default: `'mean'`

    - delta (float, optional) – Specifies the threshold at which to change between delta-scaled L1 and L2 loss. The value must be positive. Default: 1.0

  Shape:

    - Input: (∗)(*)(∗) where ∗*∗ means any number of dimensions.

    - Target: (∗)(*)(∗), same shape as the input.

    - Output: scalar. If `reduction` is `'none'`, then (∗)(*)(∗), same shape as the input.

## Docs

Access comprehensive developer documentation for PyTorch

View Docs

## Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

## Resources

Find development resources and get your questions answered

View Resources

To analyze traffic and optimize your experience, we serve cookies on this site. By clicking or navigating, you agree to allow our usage of cookies. As the current maintainers of this site, Facebook’s Cookies Policy applies. Learn more, including about available controls: Cookies Policy.