|
|
|
@ -25,7 +25,6 @@ from ...fluid.layers import center_loss #DEFINE_ALIAS
|
|
|
|
|
from ...fluid.layers import cross_entropy #DEFINE_ALIAS
|
|
|
|
|
from ...fluid.layers import dice_loss #DEFINE_ALIAS
|
|
|
|
|
from ...fluid.layers import iou_similarity #DEFINE_ALIAS
|
|
|
|
|
from ...fluid.layers import kldiv_loss #DEFINE_ALIAS
|
|
|
|
|
from ...fluid.layers import log_loss #DEFINE_ALIAS
|
|
|
|
|
from ...fluid.layers import npair_loss #DEFINE_ALIAS
|
|
|
|
|
from ...fluid.layers import rank_loss #DEFINE_ALIAS
|
|
|
|
@ -52,7 +51,7 @@ __all__ = [
|
|
|
|
|
'edit_distance',
|
|
|
|
|
'huber_loss',
|
|
|
|
|
'iou_similarity',
|
|
|
|
|
'kldiv_loss',
|
|
|
|
|
'kl_div',
|
|
|
|
|
'l1_loss',
|
|
|
|
|
'log_loss',
|
|
|
|
|
'mse_loss',
|
|
|
|
@ -374,6 +373,105 @@ def nll_loss(input,
|
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def kl_div(input, label, reduction='mean', name=None):
|
|
|
|
|
"""
|
|
|
|
|
This operator 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)$$
|
|
|
|
|
|
|
|
|
|
While :math:`x` is input and :math:`y` is label.
|
|
|
|
|
|
|
|
|
|
While :attr:`reduction` is :attr:`none`, output loss is in
|
|
|
|
|
the same shape as input, loss in each point is calculated
|
|
|
|
|
seperately and no reduction is applied.
|
|
|
|
|
|
|
|
|
|
While :attr:`reduction` is :attr:`mean`, output loss is in
|
|
|
|
|
shape of [1] and loss value is the mean value of all losses.
|
|
|
|
|
|
|
|
|
|
While :attr:`reduction` is :attr:`sum`, output loss is in
|
|
|
|
|
shape of [1] and loss value is the sum value of all losses.
|
|
|
|
|
|
|
|
|
|
While :attr:`reduction` is :attr:`batchmean`, output loss is
|
|
|
|
|
in shape of [1] and loss value is the sum value of all losses
|
|
|
|
|
divided by batch size.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
input (Tensor): The input tensor. The shapes is [N, *], where N is batch size and `*` means
|
|
|
|
|
any number of additional dimensions. It's data type should be float32, float64.
|
|
|
|
|
label (Tensor): label. The shapes is [N, *], same shape as ``input`` . It's data type should be float32, float64.
|
|
|
|
|
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'``.
|
|
|
|
|
name(str, optional): Name for the operation (optional, default is None). For more information,
|
|
|
|
|
please refer to :ref:`api_guide_Name`.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
Tensor: The KL divergence loss. The data type is same as input tensor
|
|
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
|
|
import paddle
|
|
|
|
|
import numpy as np
|
|
|
|
|
import paddle.nn.functional as F
|
|
|
|
|
|
|
|
|
|
paddle.enable_imperative()
|
|
|
|
|
|
|
|
|
|
shape = (5, 20)
|
|
|
|
|
input = np.random.uniform(-10, 10, shape).astype('float32')
|
|
|
|
|
target = np.random.uniform(-10, 10, shape).astype('float32')
|
|
|
|
|
|
|
|
|
|
# 'batchmean' reduction, loss shape will be [N]
|
|
|
|
|
pred_loss = F.kl_div(paddle.to_variable(input),
|
|
|
|
|
paddle.to_variable(target), reduction='batchmean')
|
|
|
|
|
# shape=[5]
|
|
|
|
|
|
|
|
|
|
# 'mean' reduction, loss shape will be [1]
|
|
|
|
|
pred_loss = F.kl_div(paddle.to_variable(input),
|
|
|
|
|
paddle.to_variable(target), reduction='mean')
|
|
|
|
|
# shape=[1]
|
|
|
|
|
|
|
|
|
|
# 'sum' reduction, loss shape will be [1]
|
|
|
|
|
pred_loss = F.kl_div(paddle.to_variable(input),
|
|
|
|
|
paddle.to_variable(target), reduction='sum')
|
|
|
|
|
# shape=[1]
|
|
|
|
|
|
|
|
|
|
# 'none' reduction, loss shape is same with input shape
|
|
|
|
|
pred_loss = F.kl_div(paddle.to_variable(input),
|
|
|
|
|
paddle.to_variable(target), reduction='none')
|
|
|
|
|
# shape=[5, 20]
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
if paddle.in_dynamic_mode():
|
|
|
|
|
out = core.ops.kldiv_loss(input, label, 'reduction', reduction)
|
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
helper = LayerHelper('kl_div', **locals())
|
|
|
|
|
|
|
|
|
|
fluid.data_feeder.check_variable_and_dtype(input, 'input',
|
|
|
|
|
['float32', 'float64'], 'kl_div')
|
|
|
|
|
fluid.data_feeder.check_variable_and_dtype(label, 'label',
|
|
|
|
|
['float32', 'float64'], 'kl_div')
|
|
|
|
|
fluid.data_feeder.check_type(reduction, 'reduction', str, 'kl_div')
|
|
|
|
|
|
|
|
|
|
loss = helper.create_variable_for_type_inference(dtype=input.dtype)
|
|
|
|
|
helper.append_op(
|
|
|
|
|
type='kldiv_loss',
|
|
|
|
|
inputs={'X': input,
|
|
|
|
|
'Target': label},
|
|
|
|
|
outputs={'Loss': loss},
|
|
|
|
|
attrs={'reduction': reduction})
|
|
|
|
|
return loss
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def mse_loss(input, label, reduction='mean', name=None):
|
|
|
|
|
"""
|
|
|
|
|
This op accepts input predications and label and returns the mean square error.
|
|
|
|
|