# KLDivLoss¶

class paddle.nn. KLDivLoss ( reduction='mean' ) [source]

This interface calculates the Kullback-Leibler divergence loss between Input(X) and Input(Target). Notes that Input(X) is the log-probability and Input(Target) is the probability.

KL divergence loss is calculated as follows:

\$\$l(x, y) = y * (log(y) - x)\$\$

Parameters

reduction (Tensor) – Indicate how to average the loss, the candicates are `'none'` | `'batchmean'` | `'mean'` | `'sum'`. If reduction is `'mean'`, the reduced mean loss is returned; If reduction is `'batchmean'`, the sum loss divided by batch size is returned; if reduction is `'sum'`, the reduced sum loss is returned; if reduction is `'none'`, no reduction will be apllied. Default is `'mean'`.

Shape:

• input (Tensor): (N, *), where * means, any number of additional dimensions.

Inline emphasis start-string without end-string.

• label (Tensor): (N, *), same shape as input.

Inline emphasis start-string without end-string.

• output (Tensor): tensor with shape:  by default.

Examples

```import paddle
import numpy as np

shape = (5, 20)
x = np.random.uniform(-10, 10, shape).astype('float32')
target = np.random.uniform(-10, 10, shape).astype('float32')

# 'batchmean' reduction, loss shape will be 
kldiv_criterion = nn.KLDivLoss(reduction='batchmean')
# shape=

# 'mean' reduction, loss shape will be 
kldiv_criterion = nn.KLDivLoss(reduction='mean')
# shape=

# 'sum' reduction, loss shape will be 
kldiv_criterion = nn.KLDivLoss(reduction='sum')
# shape=

# 'none' reduction, loss shape is same with X shape
kldiv_criterion = nn.KLDivLoss(reduction='none')