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

40 lines
1.6 KiB
ReStructuredText
Raw Normal View History

2021-12-08 15:50:20 +08:00
mindspore.nn.MSELoss
=============================
.. py:class:: mindspore.nn.MSELoss(reduction='mean')
2022-01-08 17:47:30 +08:00
用于计算预测值与标签值之间的均方误差。
2021-12-08 15:50:20 +08:00
假设 :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} \quad l_n = (x_n - y_n)^2.
其中, :math:`N` 为batch size。如果 `reduction` 不是"none",则:
.. math::
\ell(x, y) =
\begin{cases}
2022-01-19 17:23:39 +08:00
\operatorname{mean}(L), & \text{if reduction} = \text{'mean';}\\
\operatorname{sum}(L), & \text{if reduction} = \text{'sum'.}
2021-12-08 15:50:20 +08:00
\end{cases}
**参数:**
**reduction** (str) - 应用于loss的reduction类型。取值为"mean""sum",或"none"。默认值:"mean"。
**输入:**
2022-01-08 17:47:30 +08:00
- **logits** (Tensor) - 输入预测值任意维度的Tensor。
- **labels** (Tensor) - 输入标签任意维度的Tensor在通常情况下与 `logits` 的shape相同。但是如果 `logits``labels` 的shape不同需要保证他们之间可以互相广播。
2021-12-08 15:50:20 +08:00
**输出:**
2022-01-08 17:47:30 +08:00
Tensor为float类型的loss如果 `reduction` 为"mean"或"sum"则shape为0
如果 `reduction` 为"none"则输出的shape为输入Tensor广播后的shape。
2021-12-08 15:50:20 +08:00
**异常:**
2022-01-22 14:32:07 +08:00
- **ValueError** - `reduction` 不为"mean""sum",或"none"。
- **ValueError** - `logits``labels` 的shape不同且不能广播。