mindspore/docs/api/api_python/ops/mindspore.ops.BCEWithLogits...

64 lines
3.7 KiB
ReStructuredText
Raw Permalink 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.ops.BCEWithLogitsLoss
===============================
.. py:class:: mindspore.ops.BCEWithLogitsLoss(reduction='mean')
输入经过sigmoid激活函数计算后作为预测值`BCEWithLogitsLoss` 计算预测值和目标值之间的二值交叉熵损失。
将输入 `input` 设置为 :math:`X` ,输入 `target` 设置为 :math:`Y` ,输入 `weight` 设置为 :math:`W` ,输出设置为 :math:`L` 。则,
.. math::
\begin{array}{ll} \\
p_{ij} = sigmoid(X_{ij}) = \frac{1}{1 + e^{-X_{ij}}} \\
L_{ij} = -[Y_{ij}log(p_{ij}) + (1 - Y_{ij})log(1 - p_{ij})]
\end{array}
:math:`i` 表示 :math:`i^{th}` 样例, :math:`j` 表示类别。则,
.. 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}
:math:`\ell` 表示计算损失的方法。有三种方法:第一种方法是直接提供损失值,第二种方法是计算所有损失的平均值,第三种方法是计算所有损失的总和。
该算子会将输出乘以相应的权重。
`weight` 表示一个batch中的每条数据分配不同的权重
`pos_weight` 为每个类别的正例添加相应的权重。
此外,它可以通过向正例添加权重来权衡召回率和精度。
在多标签分类的情况下,损失可以描述为:
.. math::
\begin{array}{ll} \\
p_{ij,c} = sigmoid(X_{ij,c}) = \frac{1}{1 + e^{-X_{ij,c}}} \\
L_{ij,c} = -[P_{c}Y_{ij,c} * log(p_{ij,c}) + (1 - Y_{ij,c})log(1 - p_{ij,c})]
\end{array}
其中 c 是类别数目c>1 表示多标签二元分类c=1 表示单标签二元分类n 是批次中样本的数量,:math:`P_c` 是 第c类正例的权重。
:math:`P_c>1` 增大召回率, :math:`P_c<1` 增大精度。
参数:
- **reduction** (str可选) - 指定应用于输出结果的归约计算方式,可选 ``'none'````'mean'````'sum'`` ,默认值: ``'mean'``
- ``'none'``:不应用归约方法。
- ``'mean'``:计算输出元素的加权平均值。
- ``'sum'``:计算输出元素的总和。
输入:
- **input** (Tensor) - 输入预测值其shape为 :math:`(N, *)` ,其中 :math:`*` 为任意数量的额外维度。数据类型必须为float16、float32或bfloat16仅Atlas A2训练系列产品支持
- **target** (Tensor) - 输入目标值shape与 `input` 相同。数据类型为float16、float32或bfloat16仅Atlas A2训练系列产品支持
- **weight** (Tensor) - 指定每个批次二值交叉熵的权重。支持广播使其shape与 `input` 的shape保持一致。数据类型必须为float16、float32或bfloat16仅Atlas A2训练系列产品支持
- **pos_weight** (Tensor) - 指定正类的权重。是一个长度等于分类数的向量。支持广播使其shape与 `input` 的shape保持一致。数据类型必须为float16、float32或bfloat16仅Atlas A2训练系列产品支持
输出:
Tensor或Scalar如果 `reduction```'none'`` 则为shape和数据类型与输入 `input` 相同的Tensor。否则输出为Scalar。
异常:
- **TypeError** - 所有的输入都不是Tensor。
- **TypeError** - 所有输入的数据类型不是float16、float32或bfloat16。
- **TypeError** - `reduction` 的数据类型不是string。
- **ValueError** - `weight``pos_weight` 不能广播到shape为 `input` 的Tensor。
- **ValueError** - `reduction` 不为 ``'none'````'mean'````'sum'``