L1Loss

class paddle.nn.loss.L1Loss ( reduction\=’mean’, name\=None )

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

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

当 reduction 设置为 'none' 时,

L1Loss - 图1

当 reduction 设置为 'mean' 时,

L1Loss - 图2

当 reduction 设置为 'sum' 时,

Out\=SUM(|input−label|)Out\=SUM(|input−label|)

参数

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

  • name (str,可选): - 操作的名称(可选,默认值为None)。更多信息请参见 Name

形状

  • 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的维度为 [1]。

代码示例

  1. import paddle
  2. input = paddle.to_tensor([[1.5, 0.8], [0.2, 1.3]])
  3. label = paddle.to_tensor([[1.7, 1.0], [0.4, 0.5]])
  4. l1_loss = paddle.nn.loss.L1Loss()
  5. output = l1_loss(input, label)
  6. print(output)
  7. # [0.35]
  8. l1_loss = paddle.nn.loss.L1Loss(reduction='sum')
  9. output = l1_loss(input, label)
  10. print(output)
  11. # [1.4]
  12. l1_loss = paddle.nn.loss.L1Loss(reduction='none')
  13. output = l1_loss(input, label)
  14. print(output)
  15. # [[0.20000005 0.19999999]
  16. # [0.2 0.79999995]]