BCELoss

class paddle.fluid.dygraph.BCELoss(weight=None, reduction='mean')[source]

This interface is used to construct a callable object of the BCELoss class. The BCELoss layer measures the binary_cross_entropy loss between input predictions and target labels. The binary_cross_entropy loss can be described as:

If weight is set, the loss is:

\[Out = -1 * weight * (label * log(input) + (1 - label) * log(1 - input))\]

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

Explicit markup ends without a blank line; unexpected unindent.

If weight is None, the loss is:

\[Out = -1 * (label * log(input) + (1 - label) * log(1 - input))\]

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

\[Out = Out\]

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

Explicit markup ends without a blank line; unexpected unindent.

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

\[Out = MEAN(Out)\]

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

Explicit markup ends without a blank line; unexpected unindent.

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

\[Out = SUM(Out)\]

Note that the input predictions always be the output of sigmoid, and the target labels should be numbers between 0 and 1.

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 is scalar, else the shape of output is same as input.

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

Inline emphasis start-string without end-string.

Parameters
  • weight (Variable, optional) – A manual rescaling weight given to the loss of each batch element. If given, has to be a Variable of size nbatch and the data type is float32, float64. Default is 'None'.

  • reduction (str, optional) – Indicate how to average the loss by batch_size, 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 summed loss is returned. Default is 'mean'.

Returns

A callable object of BCELoss.

Examples

# declarative mode
import paddle.fluid as fluid
import numpy as np
input = fluid.data(name="input", shape=[3, 1], dtype='float32')
label = fluid.data(name="label", shape=[3, 1], dtype='float32')
bce_loss = fluid.dygraph.BCELoss()
output = bce_loss(input, label)
place = fluid.CPUPlace()
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())

input_data = np.array([0.5, 0.6, 0.7]).astype("float32")
label_data = np.array([1.0, 0.0, 1.0]).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.65537095], 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)
    output = bce_loss(input, label)
    print(output.numpy())  # [0.65537095]
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