mindspore.nn.SmoothL1Loss

class mindspore.nn.SmoothL1Loss(beta=1.0)[source]

SmoothL1 loss function, if the absolute error element-wise between the predicted value and the target value is less than the set threshold beta, the square term is used, otherwise the absolute error term is used.

Given two input \(x,\ y\), the SmoothL1Loss can be described as follows:

\[\begin{split}L_{i} = \begin{cases} \frac{0.5 (x_i - y_i)^{2}}{\beta}, & \text{if } |x_i - y_i| < {\beta} \\ |x_i - y_i| - 0.5 {\beta}, & \text{otherwise.} \end{cases}\end{split}\]

Where \({\beta}\) represents the threshold beta.

Note

SmoothL1Loss can be regarded as modified version of L1Loss or a combination of L1Loss and L2Loss. L1Loss computes the element-wise absolute difference between two input tensors while L2Loss computes the squared difference between two input tensors. L2Loss often leads to faster convergence but it is less robust to outliers, and the loss function has better robustness.

Parameters

beta (float) – The loss function calculates the threshold of the transformation between L1Loss and L2Loss. Default: 1.0.

Inputs:
  • logits (Tensor) - Predictive value. Tensor of any dimension. Data type must be float16 or float32.

  • labels (Tensor) - Ground truth data, same shape and dtype as the logits.

Outputs:

Tensor, loss float tensor, same shape and dtype as the logits.

Raises
  • TypeError – If beta is not a float.

  • TypeError – If logits or labels are not Tensor.

  • TypeError – If dtype of logits or labels is neither float16 not float32.

  • TypeError – If dtype of logits is not the same as labels.

  • ValueError – If beta is less than or equal to 0.

  • ValueError – If shape of logits is not the same as labels.

Supported Platforms:

Ascend GPU CPU

Examples

>>> loss = nn.SmoothL1Loss()
>>> logits = Tensor(np.array([1, 2, 3]), mindspore.float32)
>>> labels = Tensor(np.array([1, 2, 2]), mindspore.float32)
>>> output = loss(logits, labels)
>>> print(output)
[0.  0.  0.5]