mindspore.ops.KLDivLoss

class mindspore.ops.KLDivLoss(*args, **kwargs)[source]

Computes the Kullback-Leibler divergence between the target and the output.

The updating formulas of KLDivLoss algorithm are as follows,

\[L = \{l_1,\dots,l_N\}^\top, \quad l_n = y_n \cdot (\log y_n - x_n)\]

Then,

\[\begin{split}\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}\end{split}\]

where \(x\) represents input. \(y\) represents label. \(\ell(x, y)\) represents output.

Parameters

reduction (str) – Specifies the reduction to be applied to the output. Its value must be one of ‘none’, ‘mean’, ‘sum’. Default: ‘mean’.

Inputs:
  • input_x (Tensor) - The input Tensor. The data type must be float32.

  • input_y (Tensor) - The label Tensor which has the same shape as input_x. The data type must be float32.

Outputs:

Tensor or Scalar, if reduction is ‘none’, then output is a tensor and has the same shape as input_x. Otherwise it is a scalar.

Raises
  • TypeError – If reduction is not a str.

  • TypeError – If neither input_x nor input_y is a Tensor.

  • TypeError – If dtype of input_x or input_y is not float32.

Supported Platforms:

GPU

Examples

>>> import mindspore
>>> import mindspore.nn as nn
>>> import numpy as np
>>> from mindspore import Tensor
>>> from mindspore.ops import operations as ops
>>> class Net(nn.Cell):
...     def __init__(self):
...         super(Net, self).__init__()
...         self.kldiv_loss = ops.KLDivLoss()
...     def construct(self, x, y):
...         result = self.kldiv_loss(x, y)
...         return result
...
>>> net = Net()
>>> input_x = Tensor(np.array([0.2, 0.7, 0.1]), mindspore.float32)
>>> input_y = Tensor(np.array([0., 1., 0.]), mindspore.float32)
>>> output = net(input_x, input_y)
>>> print(output)
-0.23333333