multi_label_soft_margin_loss

paddle.nn.functional. multi_label_soft_margin_loss ( input, label, weight=None, reduction='mean', name=None ) [源代码]

计算输入 inputlabel 间的 margin-based loss 损失。

损失函数按照下列公式计算

\[\text{loss}(x, y) = \sum_{ij}\frac{\max(0, 1 - (x[y[j]] - x[i]))}{\text{x.shape}(0)}\]

如果添加权重则再乘以对应的权重值

最后,添加 reduce 操作到前面的输出 Out 上。当 reductionnone 时,直接返回最原始的 Out 结果。当 reductionmean 时,返回输出的均值 \(Out = MEAN(Out)\) 。当 reductionsum 时,返回输出的求和 \(Out = SUM(Out)\)

参数

  • input (Tensor) - \([N, *]\) , 其中 N 是 batch_size, * 是任意其他维度。数据类型是 float32、float64。

  • label (Tensor) - \([N, *]\) , 标签 label 的维度、数据类型与输入 input 相同。

  • weight (Tensor,可选) - 手动设定权重,默认为 None

  • reduction (str,可选) - 指定应用于输出结果的计算方式,可选值有: 'none''mean''sum' 。默认为 'mean',计算 Loss 的均值;设置为 'sum' 时,计算 Loss 的总和;设置为 'none' 时,则返回原始 Loss。

  • name (str,可选) - 具体用法请参见 Name,一般无需设置,默认值为 None。

形状

  • input (Tensor) - \([N, *]\) , 其中 N 是 batch_size, * 是任意其他维度。数据类型是 float32、float64。

  • label (Tensor) - \([N, *]\) ,标签 label 的维度、数据类型与输入 input 相同。

  • output (Tensor) - 输出的 Tensor。如果 reduction'none', 则输出的维度为 \([N, *]\) , 与输入 input 的形状相同。如果 reduction'mean''sum', 则输出的维度为 \([]\)

返回

输出的 Tensor。如果 reduction'none', 则输出的维度为 \([N, *]\) , 与输入 input 的形状相同。如果 reduction'mean''sum'`, 则输出的维度为 \([]\)

代码示例

>>> import paddle
>>> import paddle.nn.functional as F
>>> input = paddle.to_tensor([[1, -2, 3], [0, -1, 2], [1, 0, 1]], dtype=paddle.float32)
>>> # label elements in {1., -1.}
>>> label = paddle.to_tensor([[-1, 1, -1], [1, 1, 1], [1, -1, 1]], dtype=paddle.float32)
>>> loss = F.multi_label_soft_margin_loss(input, label, reduction='none')
>>> print(loss)
Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
       [3.49625897, 0.71111226, 0.43989015])
>>> loss = F.multi_label_soft_margin_loss(input, label, reduction='mean')
>>> print(loss)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
       1.54908717)