kldiv_loss_cn.rst 3.2 KB
Newer Older
H
Hao Wang 已提交
1 2 3 4 5 6 7
.. _cn_api_fluid_layers_kldiv_loss:

kldiv_loss
-------------------------------

.. py:function:: paddle.fluid.layers.kldiv_loss(x, target, reduction='mean', name=None)

K
Kaipeng Deng 已提交
8
该OP计算输入(X)和输入(Target)之间的Kullback-Leibler散度损失。注意其中输入(X)应为对数概率值,输入(Target)应为概率值。
H
Hao Wang 已提交
9 10 11 12 13 14 15

kL发散损失计算如下:

..  math::

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

K
Kaipeng Deng 已提交
16
:math:`x` 为输入(X),:math:`y` 输入(Target)。
H
Hao Wang 已提交
17

K
Kaipeng Deng 已提交
18
当 ``reduction``  为 ``none`` 时,输出损失与输入(x)形状相同,各点的损失单独计算,不会对结果做reduction 。
H
Hao Wang 已提交
19

K
Kaipeng Deng 已提交
20
当 ``reduction``  为 ``mean`` 时,输出损失为[1]的形状,输出为所有损失的平均值。
H
Hao Wang 已提交
21

K
Kaipeng Deng 已提交
22
当 ``reduction``  为 ``sum`` 时,输出损失为[1]的形状,输出为所有损失的总和。
H
Hao Wang 已提交
23

K
Kaipeng Deng 已提交
24
当 ``reduction``  为 ``batchmean`` 时,输出损失为[N]的形状,N为批大小,输出为所有损失的总和除以批量大小。
H
Hao Wang 已提交
25 26

参数:
K
Kaipeng Deng 已提交
27 28
    - **x** (Variable) - KL散度损失算子的输入张量。维度为[N, \*]的多维Tensor,其中N是批大小,\*表示任何数量的附加维度,数据类型为float32或float64。
    - **target** (Variable) - KL散度损失算子的张量。与输入 ``x`` 的维度和数据类型一致的多维Tensor。
H
Hao Wang 已提交
29
    - **reduction** (Variable)-要应用于输出的reduction类型,可用类型为‘none’ | ‘batchmean’ | ‘mean’ | ‘sum’,‘none’表示无reduction,‘batchmean’ 表示输出的总和除以批大小,‘mean’ 表示所有输出的平均值,‘sum’表示输出的总和。
30
    - **name** (str,可选) – 具体用法请参见 :ref:`api_guide_Name` ,一般无需设置。默认值:None。
K
Kaipeng Deng 已提交
31
    - **name** (None|str) – 该参数供开发人员打印调试信息时使用,具体用法请参见 :ref:`api_guide_Name` ,默认值为None。
H
Hao Wang 已提交
32

K
Kaipeng Deng 已提交
33
返回:Variable(Tensor) KL散度损失。
H
Hao Wang 已提交
34

K
Kaipeng Deng 已提交
35
返回类型:变量(Variable),数据类型与输入 ``x`` 一致。
H
Hao Wang 已提交
36 37 38 39 40

**代码示例**:

.. code-block:: python

K
Kaipeng Deng 已提交
41
    # 'batchmean' reduction, loss shape 为[N]
42
    x = fluid.data(name='x', shape=[None,4,2,2], dtype='float32') # shape=[-1, 4, 2, 2]
H
Hao Wang 已提交
43
    target = fluid.layers.data(name='target', shape=[4,2,2], dtype='float32')
K
Kaipeng Deng 已提交
44 45 46
    loss = fluid.layers.kldiv_loss(x=x, target=target, reduction='batchmean') # shape=[-1]

    # 'mean' reduction, loss shape 为[1]
47
    x = fluid.data(name='x', shape=[None,4,2,2], dtype='float32') # shape=[-1, 4, 2, 2]
K
Kaipeng Deng 已提交
48 49 50 51
    target = fluid.layers.data(name='target', shape=[4,2,2], dtype='float32')
    loss = fluid.layers.kldiv_loss(x=x, target=target, reduction='mean') # shape=[1]

    # 'sum' reduction, loss shape 为[1]
52
    x = fluid.data(name='x', shape=[None,4,2,2], dtype='float32') # shape=[-1, 4, 2, 2]
K
Kaipeng Deng 已提交
53 54 55 56
    target = fluid.layers.data(name='target', shape=[4,2,2], dtype='float32')
    loss = fluid.layers.kldiv_loss(x=x, target=target, reduction='sum') # shape=[1]

    # 'none' reduction, loss shape 与X相同
57
    x = fluid.data(name='x', shape=[None,4,2,2], dtype='float32') # shape=[-1, 4, 2, 2]
K
Kaipeng Deng 已提交
58 59
    target = fluid.layers.data(name='target', shape=[4,2,2], dtype='float32')
    loss = fluid.layers.kldiv_loss(x=x, target=target, reduction='none') # shape=[-1, 4, 2, 2]
H
Hao Wang 已提交
60 61 62 63 64 65 66