L1Loss

class paddle.fluid.dygraph.L1Loss(reduction='mean')[source]

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

If reduction set to 'none', the unreduced loss is: .. math:

System Message: ERROR/3 (/usr/local/lib/python2.7/dist-packages/paddle/fluid/dygraph/nn.py:docstring of paddle.fluid.dygraph.L1Loss, line 7)

Unexpected indentation.

Out = |input - label|

System Message: WARNING/2 (/usr/local/lib/python2.7/dist-packages/paddle/fluid/dygraph/nn.py:docstring of paddle.fluid.dygraph.L1Loss, line 8)

Literal block ends without a blank line; unexpected unindent.

If reduction set to 'mean', the reduced mean loss is: .. math:

System Message: ERROR/3 (/usr/local/lib/python2.7/dist-packages/paddle/fluid/dygraph/nn.py:docstring of paddle.fluid.dygraph.L1Loss, line 10)

Unexpected indentation.

Out = MEAN(|input - label|)

System Message: WARNING/2 (/usr/local/lib/python2.7/dist-packages/paddle/fluid/dygraph/nn.py:docstring of paddle.fluid.dygraph.L1Loss, line 11)

Literal block ends without a blank line; unexpected unindent.

If reduction set to 'sum', the reduced sum loss is: .. math:

System Message: ERROR/3 (/usr/local/lib/python2.7/dist-packages/paddle/fluid/dygraph/nn.py:docstring of paddle.fluid.dygraph.L1Loss, line 13)

Unexpected indentation.

Out = SUM(|input - label|)

The shape of input predictions and target labels are [N, *], where N is batch_size and * means any number of additional dimensions. 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], which means the output is a scalar.

System Message: WARNING/2 (/usr/local/lib/python2.7/dist-packages/paddle/fluid/dygraph/nn.py:docstring of paddle.fluid.dygraph.L1Loss, line 15); backlink

Inline emphasis start-string without end-string.

System Message: WARNING/2 (/usr/local/lib/python2.7/dist-packages/paddle/fluid/dygraph/nn.py:docstring of paddle.fluid.dygraph.L1Loss, line 15); backlink

Inline emphasis start-string without end-string.

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'.

Returns

A callable object of L1Loss.

Examples

System Message: ERROR/3 (/usr/local/lib/python2.7/dist-packages/paddle/fluid/dygraph/nn.py:docstring of paddle.fluid.dygraph.L1Loss, line 32)

Error in “code-block” directive: maximum 1 argument(s) allowed, 33 supplied.

.. code-block:: python
    # declarative mode
    import paddle.fluid as fluid
    import numpy as np
    input = fluid.data(name="input", shape=[1])
    label = fluid.data(name="label", shape=[1])
    l1_loss = fluid.dygraph.L1Loss(reduction='mean')
    output = l1_loss(input,label)
    place = fluid.CPUPlace()
    exe = fluid.Executor(place)
    exe.run(fluid.default_startup_program())

    input_data = np.array([1.5]).astype("float32")
    label_data = np.array([1.7]).astype("float32")
    output_data = exe.run(fluid.default_main_program(),
            feed={"input":input_data, "label":label_data},
            fetch_list=[output],
            return_numpy=True)

    print(output_data)  # [array([0.2], dtype=float32)]

    # imperative mode
    import paddle.fluid.dygraph as dg
    with dg.guard(place) as g:
        input = dg.to_variable(input_data)
        label = dg.to_variable(label_data)
        l1_loss = fluid.dygraph.L1Loss(reduction='mean')
        output = l1_loss(input,label)
        print(output.numpy())  # [0.2]

forward(input, label)

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

Parameters
  • *inputs (tuple) – unpacked tuple arguments

  • **kwargs (dict) – unpacked dict arguments