mindspore.ops.KLDivLoss

class mindspore.ops.KLDivLoss(reduction='mean')[源代码]

计算输入 logitslabels 的KL散度。

对于相同形状的张量 \(x\)\(target\) ,KLDivLoss的计算公式如下:

\[L(x, target) = target \cdot (\log target - x)\]

输出

\[\begin{split}\ell(x, target) = \begin{cases} L(x, target), & \text{if reduction} = \text{'none';}\\ \operatorname{mean}(L(x, target)), & \text{if reduction} = \text{'mean';}\\ \operatorname{sum}(L(x, target)) / x.\operatorname{shape}[0], & \text{if reduction} = \text{'batchmean';}\\ \operatorname{sum}(L(x, target)), & \text{if reduction} = \text{'sum'.} \end{cases}\end{split}\]

其中 \(x\) 代表 logits\(target\) 代表 labels\(\ell(x, target)\)output

说明

  • 目前Ascend平台不支持数据类型float64。

  • 仅当 reduction 设置为”batchmean”时输出才与Kullback-Leibler散度的数学定义一致。

参数:
  • reduction (str) - 指定输出结果的计算方式。默认值: “mean”。

    • 在Ascend平台上, reduction 的可选值为”batchmean”、”none”或”sum”。

    • 在GPU平台上, reduction 的可选值为”mean”、”none”或”sum”。

    • 在CPU平台上, reduction 的可选值为”mean”、”batchmean”、”none”或”sum”。

输入:
  • logits (Tensor) - 数据类型支持float16、float32或float64。

  • labels (Tensor) - 标签Tensor,与 logits 的shape和数据类型相同。

输出:

Tensor或标量。如果 reduction 为”none” ,则输出为Tensor且与 logits 的shape相同。否则为标量。

异常:
  • TypeError - reduction 不是str。

  • TypeError - logitslabels 不是Tensor。

  • TypeError - logitslabels 的数据类型不是支持的类型。

  • ValueError - logitslabels 的shape不一致。

  • RuntimeError - logitslabels 是标量并且 reduction 是”batchmean”。

支持平台:

Ascend GPU CPU

样例:

>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.kldiv_loss = ops.KLDivLoss(reduction='sum')
...     def construct(self, logits, labels):
...         result = self.kldiv_loss(logits, labels)
...         return result
...
>>> net = Net()
>>> logits = Tensor(np.array([0.2, 0.7, 0.1]), mindspore.float32)
>>> labels = Tensor(np.array([0., 1., 0.]), mindspore.float32)
>>> output = net(logits, labels)
>>> print(output)
-0.7