You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

mindspore.nn.BCELoss.rst 2.0 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. mindspore.nn.BCELoss
  2. ====================
  3. .. py:class:: mindspore.nn.BCELoss(weight=None, reduction='none')
  4. 计算目标值和预测值之间的二值交叉熵损失值。
  5. 将预测值设置为 :math:`x` ,目标值设置为 :math:`y` ,输出损失设置为 :math:`\ell(x,y)` 。
  6. 则公式如下:
  7. .. math::
  8. L = \{l_1,\dots,l_N\}^\top, \quad
  9. l_n = - w_n \left[ y_n \cdot \log x_n + (1 - y_n) \cdot \log (1 - x_n) \right]
  10. 其中N是批次大小,公式如下:
  11. .. math::
  12. \ell(x, y) = \begin{cases}
  13. L, & \text{if reduction} = \text{'none';}\\
  14. \operatorname{mean}(L), & \text{if reduction} = \text{'mean';}\\
  15. \operatorname{sum}(L), & \text{if reduction} = \text{'sum'.}
  16. \end{cases}
  17. .. note::
  18. 预测值一般是sigmoid函数的输出,因为是二分类,所以目标值应是0或者1。如果输入是0或1,则上述损失函数是无意义的。
  19. **参数:**
  20. - **weight** (Tensor, optional) - 指定每个批次二值交叉熵的权重。与输入数据的shape和数据类型相同。默认值:None。
  21. - **reduction** (str) - 指定应用于输出结果的计算方式。可选值有:'mean','sum',或'none'。默认值:'none'。
  22. **输入:**
  23. - **logits** (Tensor) - 输入预测值,任意维度的Tensor。数据类型必须为float16或float32。
  24. - **labels** (Tensor) - 输入目标值,任意维度的Tensor,一般与 `logits` 的shape相同。
  25. **输出:**
  26. Tensor或Scalar,如果 `reduction` 为'None',则输出是Tensor,其shape与 `logits` 相同。否则,输出为Scalar。
  27. **异常:**
  28. - **TypeError** - `logits` 的数据类型,`labels` 或 `weight` (如果给定)既不是float16,也不是float32。
  29. - **ValueError** - `reduction` 不为'none'、'mean'或'sum'。
  30. - **ValueError** - `logits` 的shape与 `labels` 或 `weight` (如果给定)不同。