2.4K Star 8.2K Fork 4.4K

GVPMindSpore / mindspore

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
mindspore.nn.MultiLabelSoftMarginLoss.rst 1.99 KB
一键复制 编辑 原始数据 按行查看 历史
俞涵 提交于 2023-09-13 11:41 . modify format

mindspore.nn.MultiLabelSoftMarginLoss

.. py:class:: mindspore.nn.MultiLabelSoftMarginLoss(weight=None, reduction='mean')

    基于最大熵计算用于多标签优化的损失。

    多标签软间隔损失通常用于多标签分类任务中,输入样本可以属于多个目标类别。
    给定输入 :math:`x` 和二元标签 :math:`y` ,其shape为 :math:`(N,C)` , :math:`N` 表示样本数量, :math:`C` 为样本类别数,损失计算公式如下:

    .. math::
        \mathcal{loss\left( x , y \right)} = - \frac{1}{N}\frac{1}{C}\sum_{i = 1}^{N}
        \sum_{j = 1}^{C}\left(y_{ij}\log\frac{1}{1 + e^{- x_{ij}}} + \left( 1 - y_{ij}
        \right)\log\frac{e^{-x_{ij}}}{1 + e^{-x_{ij}}} \right)

    其中 :math:`x_{ij}` 表示样本 :math:`i` 在 :math:`j` 类别的概率得分。 :math:`y_{ij}` 表示样本 :math:`i` 是否属于类别 :math:`j` ,
    :math:`y_{ij}=1` 时属于,为0时不属于。对于多标签分类任务,每个样本可以属于多个类别,即标签中含有多个1。
    如果 `weight` 不为 ``None`` ,将会和每个分类的loss相乘。

    参数:
        - **weight** (Union[Tensor, int, float]) - 每个类别的缩放权重。默认值: ``None`` 。
        - **reduction** (str,可选) - 指定应用于输出结果的规约计算方式,可选 ``'none'`` 、 ``'mean'`` 、 ``'sum'`` ,默认值: ``'mean'`` 。

          - ``"none"``:不应用规约方法。
          - ``"mean"``:计算输出元素的加权平均值。
          - ``"sum"``:计算输出元素的总和。

    输入:
        - **x** (Tensor) - shape为 :math:`(N, C)` 的Tensor,N为batch size,C为类别个数。
        - **target** (Tensor) - 目标值,数据类型和shape与 `x` 的相同。

    输出:
        Tensor,数据类型和 `x` 相同。如果 `reduction` 为 ``"none"`` ,其shape为(N)。否则,其shape为0。

    异常:
        - **ValueError** - `x` 或 `target` 的维度不等于2。
Python
1
https://gitee.com/mindspore/mindspore.git
git@gitee.com:mindspore/mindspore.git
mindspore
mindspore
mindspore
r2.3

搜索帮助