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

44 lines
2.0 KiB
ReStructuredText
Raw Normal View History

mindspore.nn.BCELoss
====================
.. py:class:: mindspore.nn.BCELoss(weight=None, reduction='none')
计算目标值和预测值之间的二值交叉熵损失值。
将预测值设置为 :math:`x` ,目标值设置为 :math:`y` ,输出损失设置为 :math:`\ell(x,y)`
则公式如下:
.. math::
L = \{l_1,\dots,l_N\}^\top, \quad
l_n = - w_n \left[ y_n \cdot \log x_n + (1 - y_n) \cdot \log (1 - x_n) \right]
2022-09-07 16:16:07 +08:00
其中N是批次大小。公式如下
.. math::
\ell(x, y) = \begin{cases}
L, & \text{if reduction} = \text{'none';}\\
\operatorname{mean}(L), & \text{if reduction} = \text{'mean';}\\
\operatorname{sum}(L), & \text{if reduction} = \text{'sum'.}
\end{cases}
.. note::
2022-08-15 17:41:47 +08:00
预测值一般是sigmoid函数的输出。因为是二分类所以目标值应是0或者1。如果输入是0或1则上述损失函数是无意义的。
2022-07-22 16:25:38 +08:00
参数:
- **weight** (Tensor, 可选) - 指定每个批次二值交叉熵的权重。与输入数据的shape和数据类型相同。默认值None。
- **reduction** (str) - 指定输出结果的计算方式。可选值有:'mean''sum',或'none'。默认值:'none'。
2022-07-22 16:25:38 +08:00
输入:
- **logits** (Tensor) - 输入预测值Tensorshape :math:`(N,*)` ,其中 `*` 代表任意数量的附加维度。数据类型必须为float16或float32。
- **labels** (Tensor) - 输入目标值Tensorshape :math:`(N,*)` ,其中 `*` 代表任意数量的附加维度。与 `logits` 的shape和数据类型相同。
2022-07-22 16:25:38 +08:00
输出:
Tensor数据类型与 `logits` 相同。如果 `reduction` 为'none'则shape与 `logits` 相同。否则输出为Scalar的Tensor。
2022-07-22 16:25:38 +08:00
异常:
- **TypeError** - `logits` 的数据类型,`labels``weight` 如果给定既不是float16也不是float32。
- **ValueError** - `reduction` 不为'none'、'mean'或'sum'。
- **ValueError** - `logits` 的shape与 `labels``weight` (如果给定)不同。