mindspore2022/docs/api/api_python/nn/mindspore.nn.MAELoss.rst

60 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.MAELoss
=============================
.. py:class:: mindspore.nn.MAELoss(reduction='mean')
MAELoss用于测量 :math:`x`:math:`y` 元素之间的平均绝对误差,其中 :math:`x` 是输入Tensor :math:`y` 是标签Tensor。
假设 :math:`x`:math:`y` 为一维Tensor长度 :math:`N` ,则计算 :math:`x`:math:`y` 的unreduced loss即reduction参数设置为"none")的公式如下:
.. math::
\ell(x, y) = L = \{l_1,\dots,l_N\}^\top, \quad \text{with } l_n = \left| x_n - y_n \right|,
其中, :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"。
**输入:**
- **logits** (Tensor) - shape为 :math:`(M, *)` 的tensor其中 :math:`*` 表示任意的附加维度。
- **labels** (Tensor) - shape为 :math:`(N, *)` 的tensor在通常情况下与 `logits` 的shape相同。但是如果 `logits``labels` 的shape不同需要保证他们之间可以互相广播。
**输出:**
Tensor为加权loss float tensor如果 `reduction` 为"mean"或"sum"则shape为零如果 `reduction` 为"none"则输出的shape为输入Tensor广播后的shape。
**异常:**
**ValueError** - `reduction` 不为"mean""sum",或"none"。
**支持平台:**
``Ascend`` ``GPU`` ``CPU``
**样例:**
>>> #用例1logits.shape = labels.shape = (3,)
>>> loss = nn.MAELoss()
>>> 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.33333334
>>> #用例2logits.shape = (3,), labels.shape = (2, 3)
>>> loss = nn.MAELoss(reduction='none')
>>> logits = Tensor(np.array([1, 2, 3]), mindspore.float32)
>>> labels = Tensor(np.array([[1, 1, 1], [1, 2, 2]]), mindspore.float32)
>>> output = loss(logits, labels)
>>> print(output)
[[0. 1. 2.]
[0. 0. 1.]]