mindspore.nn.SmoothL1Loss
- class mindspore.nn.SmoothL1Loss(beta=1.0, reduction='none')[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}}{\text{beta}}, & \text{if } |x_i - y_i| < \text{beta} \\ |x_i - y_i| - 0.5 * {\text{beta}}, & \text{otherwise.} \end{cases}\end{split}\]Where \({\text{beta}}\) represents the threshold beta.
If reduction is not none, then:
\[\begin{split}L = \begin{cases} \operatorname{mean}(L_{i}), & \text{if reduction} = \text{'mean';}\\ \operatorname{sum}(L_{i}), & \text{if reduction} = \text{'sum'.} \end{cases}\end{split}\]Note
SmoothL1Loss can be regarded as modified version of
mindspore.nn.L1Loss
or a combination ofmindspore.nn.L1Loss
andmindspore.ops.L2Loss
.mindspore.nn.L1Loss
computes the element-wise absolute difference between two input tensor whilemindspore.ops.L2Loss
computes thesquared difference between two input tensors.
mindspore.ops.L2Loss
often leads to faster convergence but it is less robust to outliers, and the loss function has better robustness.
- Parameters
beta (number, optional) –
The loss function calculates the threshold of the transformation between L1Loss and L2Loss. Default:
1.0
.Ascend: The value should be equal to or greater than zero.
CPU/GPU: The value should be greater than zero.
reduction (str, optional) –
Apply specific reduction method to the output:
'none'
,'mean'
,'sum'
. Default:'none'
.'none'
: no reduction will be applied.'mean'
: compute and return the mean of elements in the output.'sum'
: the output elements will be summed.
- Inputs:
logits (Tensor) - Predictive value. Tensor of any dimension. Supported dtypes:
Ascend: float16, float32, bfloat16.
CPU/GPU: float16, float32, float64.
labels (Tensor) - Ground truth data.
CPU/Ascend: has the same shape as the logits, logits and labels comply with the implicit type conversion rules to make the data types consistent.
GPU: has the same shape and dtype as the logits.
- Outputs:
Tensor, if reduction is
'none'
, then output is a tensor with the same shape as logits. Otherwise the shape of output tensor is \(()\).
- Raises
TypeError – If input logits or labels are not Tensor.
RuntimeError – If dtype of logits or labels is not one of float16, float32, float64, bfloat16.
ValueError – If shape of logits is not the same as labels.
ValueError – If reduction is not one of
'none'
,'mean'
,'sum'
.TypeError – If beta is not a float, int or bool.
RuntimeError – If beta is less than or equal to 0.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore >>> from mindspore import Tensor, nn >>> import numpy as np >>> 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]