L1Loss

class paddle.nn. L1Loss ( reduction='mean', name=None ) [源代码]

创建一个 L1Loss 的可调用类,L1Loss 计算输入 input 和标签 label 间的 L1 loss 损失。

该损失函数的数学计算公式如下:

reduction 设置为 'none' 时:

\[Out = \lvert input - label\rvert\]

reduction 设置为 'mean' 时:

\[Out = MEAN(\lvert input - label\rvert)\]

reduction 设置为 'sum' 时:

\[Out = SUM(\lvert input - label\rvert)\]

参数

  • reduction (str,可选) - 指定应用于输出结果的计算方式,可选值有:'none', 'mean', 'sum'。默认为 'mean',计算 L1Loss 的均值;设置为 'sum' 时,计算 L1Loss 的总和;设置为 'none' 时,则返回 L1Loss

  • name (str,可选) - 具体用法请参见 Name,一般无需设置,默认值为 None。

形状

  • input (Tensor):输入的 Tensor,维度是[N, *],其中 N 是 batch size, * 是任意数量的额外维度。数据类型为:float32、float64、int32、int64。

  • label (Tensor):标签,维度是[N, *],与 input 相同。数据类型为:float32、float64、int32、int64。

  • output (Tensor):输入 input 和标签 label 间的 L1 loss 损失。如果 reduction'none',则输出 Loss 的维度为 [N, *],与输入 input 相同。如果 reduction'mean''sum',则输出 Loss 的维度为 []。

代码示例

>>> import paddle

>>> input = paddle.to_tensor([[1.5, 0.8], [0.2, 1.3]])
>>> label = paddle.to_tensor([[1.7, 1], [0.4, 0.5]])

>>> l1_loss = paddle.nn.L1Loss()
>>> output = l1_loss(input, label)
>>> print(output)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
0.34999999)

>>> l1_loss = paddle.nn.L1Loss(reduction='sum')
>>> output = l1_loss(input, label)
>>> print(output)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
1.39999998)

>>> l1_loss = paddle.nn.L1Loss(reduction='none')
>>> output = l1_loss(input, label)
>>> print(output)
Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
[[0.20000005, 0.19999999],
 [0.20000000, 0.79999995]])