mindspore/docs/api/api_python/nn/mindspore.nn.HuberLoss.rst

48 lines
2.3 KiB
ReStructuredText
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

mindspore.nn.HuberLoss
=============================
.. py:class:: mindspore.nn.HuberLoss(reduction="mean", delta=1.0)
HuberLoss计算预测值和目标值之间的误差。它兼有L1Loss和MSELoss的优点。
假设 :math:`x`:math:`y` 为一维Tensor长度 :math:`N` ,则计算 :math:`x`:math:`y` 的loss而不进行降维操作即reduction参数设置为"none")。公式如下:
.. math::
\ell(x, y) = L = \{l_1,\dots,l_N\}^\top
以及
.. math::
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}
其中, :math:`N` 为batch size。如果 `reduction` 不是"none",则:
.. math::
\ell(x, y) =
\begin{cases}
\operatorname{mean}(L), & \text{if reduction} = \text{"mean";}\\
\operatorname{sum}(L), & \text{if reduction} = \text{"sum".}
\end{cases}
参数:
- **reduction** (str) - 应用于loss的reduction类型。取值为"mean""sum",或"none"。默认值:"mean"。如果 `reduction` 为"mean"或"sum"则输出一个标量Tensor如果 `reduction` 为"none"则输出Tensor的shape为广播后的shape。
- **delta** (Union[int, float]) - 两种损失之间变化的阈值。该值必须为正。默认值1.0。
输入:
- **logits** (Tensor) - 输入预测值任意维度的Tensor。其数据类型为float16或float32。
- **labels** (Tensor) - 目标值,通常情况下与 `logits` 的shape和dtype相同。但是如果 `logits``labels` 的shape不同需要保证他们之间可以互相广播。
输出:
Tensor或Scalar如果 `reduction` 为"none",返回与 `logits` 具有相同shape和dtype的Tensor。否则将返回一个Scalar。
异常:
- **TypeError** - `logits``labels` 的数据类型既不是float16也不是float32。
- **TypeError** - `logits``labels` 的数据类型不同。
- **TypeError** - `delta` 不是float或int。
- **ValueError** - `delta` 的值小于或等于0。
- **ValueError** - `reduction` 不为"mean"、"sum"或"none"。
- **ValueError** - `logits``labels` 有不同的shape且不能互相广播。