Document feedback

Question document fragment

When a question document fragment contains a formula, it is displayed as a space.

Submission type
issue

It's a little complicated...

I'd like to ask someone.

PR

Just a small problem.

I can fix it online!

Please select the submission type

Problem type
Specifications and Common Mistakes

- Specifications and Common Mistakes:

- Misspellings or punctuation mistakes,incorrect formulas, abnormal display.

- Incorrect links, empty cells, or wrong formats.

- Chinese characters in English context.

- Minor inconsistencies between the UI and descriptions.

- Low writing fluency that does not affect understanding.

- Incorrect version numbers, including software package names and version numbers on the UI.

Usability

- Usability:

- Incorrect or missing key steps.

- Missing main function descriptions, keyword explanation, necessary prerequisites, or precautions.

- Ambiguous descriptions, unclear reference, or contradictory context.

- Unclear logic, such as missing classifications, items, and steps.

Correctness

- Correctness:

- Technical principles, function descriptions, supported platforms, parameter types, or exceptions inconsistent with that of software implementation.

- Incorrect schematic or architecture diagrams.

- Incorrect commands or command parameters.

- Incorrect code.

- Commands inconsistent with the functions.

- Wrong screenshots.

- Sample code running error, or running results inconsistent with the expectation.

Risk Warnings

- Risk Warnings:

- Lack of risk warnings for operations that may damage the system or important data.

Content Compliance

- Content Compliance:

- Contents that may violate applicable laws and regulations or geo-cultural context-sensitive words and expressions.

- Copyright infringement.

Please select the type of question

Problem description

Describe the bug so that we can quickly locate the problem.

mindspore.nn.SmoothL1Loss

View Source On Gitee
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:

Li={0.5(xiyi)2β,if |xiyi|<β|xiyi|0.5β,otherwise.

Where β represents the threshold beta.

If reduction is not none, then:

L={mean(Li),if reduction='mean';sum(Li),if reduction='sum'.

Note

  • On the Ascend platform, float64 data type will result in low operator performance.

  • 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 .

  • 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. Data type must be one of float16, float32 and float64.

  • labels (Tensor) - Ground truth data, 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 beta is not a float.

  • ValueError – If reduction is not one of 'none', 'mean', 'sum'.

  • 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

>>> 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]