BCELoss

class paddle.nn. BCELoss ( weight=None, reduction='mean', name=None ) [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 input and target labels label . 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))\]

If weight is None, the loss is:

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

If reduction set to 'none', the interface will return the original loss Out.

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

\[Out = MEAN(Out)\]

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

\[Out = SUM(Out)\]

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

Parameters
  • weight (Tensor, optional) – A manual rescaling weight given to the loss of each batch element. If given, has to be a Tensor 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'.

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

Shape:
input (Tensor): 2-D tensor with shape: [N, *], N is batch_size, * means

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

Inline emphasis start-string without end-string.

number of additional dimensions. The input input should always be the output of sigmod. Available dtype is float32, float64.

label (Tensor): 2-D tensor with the same shape as input. The target

labels which values should be numbers between 0 and 1. Available dtype is float32, float64.

output (Tensor): If reduction is 'none', the shape of output is

same as input , else the shape of output is scalar.

Returns

A callable object of BCELoss.

Examples

import numpy as np
import paddle
input_data = np.array([0.5, 0.6, 0.7]).astype("float32")
label_data = np.array([1.0, 0.0, 1.0]).astype("float32")

input = paddle.to_tensor(input_data)
label = paddle.to_tensor(label_data)
bce_loss = paddle.nn.BCELoss()
output = bce_loss(input, label)
print(output)  # [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