mindspore/docs/api/api_python/ops/mindspore.ops.KLDivLoss.rst

51 lines
2.1 KiB
ReStructuredText
Raw Normal View History

mindspore.ops.KLDivLoss
=======================
.. py:class:: mindspore.ops.KLDivLoss(reduction='mean')
计算输入 `logits``labels` 的KL散度。
2022-06-06 11:02:25 +08:00
对于相同形状的张量 :math:`x`:math:`target` KLDivLoss的计算公式如下
.. math::
2022-06-06 11:02:25 +08:00
L(x, target) = target \cdot (\log target - x)
输出
.. math::
\ell(x, target) = \begin{cases}
2022-06-06 11:02:25 +08:00
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}
其中 :math:`x` 代表 `logits`
:math:`target` 代表 `labels`
:math:`\ell(x, target)``output`
2022-06-06 14:46:51 +08:00
.. note::
2022-05-30 11:42:21 +08:00
- 目前Ascend平台不支持数据类型float64。
2022-10-26 21:21:18 +08:00
- 仅当 `reduction` 设置为"batchmean"时输出才与Kullback-Leibler散度的数学定义一致。
2022-05-29 11:14:01 +08:00
2022-07-26 10:27:56 +08:00
参数:
- **reduction** (str) - 指定输出结果的计算方式。默认值: "mean"。
2022-06-06 11:02:25 +08:00
2022-07-26 10:27:56 +08:00
- 在Ascend平台上 `reduction` 的可选值为"batchmean"、"none"或"sum"。
- 在GPU平台上 `reduction` 的可选值为"mean"、"none"或"sum"。
- 在CPU平台上 `reduction` 的可选值为"mean"、"batchmean"、"none"或"sum"。
2022-06-06 11:02:25 +08:00
2022-07-26 10:27:56 +08:00
输入:
- **logits** (Tensor) - 数据类型支持float16、float32或float64。
- **labels** (Tensor) - 标签Tensor`logits` 的shape和数据类型相同。
2022-07-26 10:27:56 +08:00
输出:
Tensor或标量。如果 `reduction` 为"none" 则输出为Tensor且与 `logits` 的shape相同。否则为标量。
2022-06-06 11:02:25 +08:00
2022-07-26 10:27:56 +08:00
异常:
- **TypeError** - `reduction` 不是str。
- **TypeError** - `logits``labels` 不是Tensor。
- **TypeError** - `logits``labels` 的数据类型不是支持的类型。
2022-08-30 17:46:45 +08:00
- **ValueError** - `logits``labels` 的shape不一致。
2022-07-26 10:27:56 +08:00
- **RuntimeError** - `logits``labels` 是标量并且 `reduction` 是"batchmean"。