L1Loss

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

This interface is used to construct a callable object of the L1Loss class. The L1Loss layer calculates the L1 Loss of input and label as follows.

If reduction set to 'none', the loss is:

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

If reduction set to 'mean', the loss is:

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

If reduction set to 'sum', the loss is:

\[Out = SUM(\lvert input - label\rvert)\]
Parameters
  • reduction (str, optional) – Indicate the reduction to apply to the loss, the candicates are 'none' | 'mean' | 'sum'. If reduction is 'none', the unreduced loss is returned; If reduction is 'mean', the reduced mean loss is returned. If reduction is 'sum', the reduced sum loss is returned. Default is 'mean'.

  • name (str, optional) – Name for the operation (optional, default is None). For more information, please refer to Name.

Shape:

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, int32, int64. label (Tensor): label. The shapes is [N, *], same shape as input . It’s data type should be float32, float64, int32, int64. output (Tensor): The L1 Loss of input and label.

System Message: WARNING/2 (/usr/local/lib/python3.8/site-packages/paddle/nn/layer/loss.py:docstring of paddle.nn.layer.loss.L1Loss, line 31); backlink

Inline emphasis start-string without end-string.

System Message: WARNING/2 (/usr/local/lib/python3.8/site-packages/paddle/nn/layer/loss.py:docstring of paddle.nn.layer.loss.L1Loss, line 31); backlink

Inline emphasis start-string without end-string.

System Message: ERROR/3 (/usr/local/lib/python3.8/site-packages/paddle/nn/layer/loss.py:docstring of paddle.nn.layer.loss.L1Loss, line 34)

Unexpected indentation.

If reduction is 'none', the shape of output loss is [N, *], the same as input . If reduction is 'mean' or 'sum', the shape of output loss is [1].

System Message: WARNING/2 (/usr/local/lib/python3.8/site-packages/paddle/nn/layer/loss.py:docstring of paddle.nn.layer.loss.L1Loss, line 34); backlink

Inline emphasis start-string without end-string.

Examples

import paddle
import numpy as np

input_data = np.array([[1.5, 0.8], [0.2, 1.3]]).astype("float32")
label_data = np.array([[1.7, 1], [0.4, 0.5]]).astype("float32")
input = paddle.to_tensor(input_data)
label = paddle.to_tensor(label_data)

l1_loss = paddle.nn.L1Loss()
output = l1_loss(input, label)
print(output.numpy())
# [0.35]

l1_loss = paddle.nn.L1Loss(reduction='sum')
output = l1_loss(input, label)
print(output.numpy())
# [1.4]

l1_loss = paddle.nn.L1Loss(reduction='none')
output = l1_loss(input, label)
print(output)
# [[0.20000005 0.19999999]
# [0.2        0.79999995]]
forward ( input, label )

forward

Defines the computation performed at every call. Should be overridden by all subclasses.

Parameters
  • *inputs (tuple) – unpacked tuple arguments

  • **kwargs (dict) – unpacked dict arguments