|
|
|
@ -18,6 +18,7 @@ import paddle.fluid as fluid
|
|
|
|
|
import paddle.fluid.core as core
|
|
|
|
|
import paddle
|
|
|
|
|
from .. import functional as F
|
|
|
|
|
from paddle.fluid.framework import core, in_dygraph_mode, _varbase_creator
|
|
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
|
# 'NCELoss',
|
|
|
|
@ -335,45 +336,38 @@ class L1Loss(fluid.dygraph.Layer):
|
|
|
|
|
|
|
|
|
|
class BCELoss(fluid.dygraph.Layer):
|
|
|
|
|
"""
|
|
|
|
|
:alias_main: paddle.nn.BCELoss
|
|
|
|
|
:alias: paddle.nn.BCELoss,paddle.nn.layer.BCELoss,paddle.nn.layer.loss.BCELoss
|
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
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 :attr:`weight` is set, the loss is:
|
|
|
|
|
|
|
|
|
|
.. math::
|
|
|
|
|
Out = -1 * weight * (label * log(input) + (1 - label) * log(1 - input))
|
|
|
|
|
|
|
|
|
|
If :attr:`weight` is None, the loss is:
|
|
|
|
|
|
|
|
|
|
.. math::
|
|
|
|
|
Out = -1 * (label * log(input) + (1 - label) * log(1 - input))
|
|
|
|
|
|
|
|
|
|
If :attr:`reduction` set to ``'none'``, the unreduced loss is:
|
|
|
|
|
If :attr:`reduction` set to ``'none'``, the interface will return the original loss `Out`.
|
|
|
|
|
|
|
|
|
|
.. math::
|
|
|
|
|
Out = Out
|
|
|
|
|
If :attr:`reduction` set to ``'mean'``, the reduced mean loss is:
|
|
|
|
|
|
|
|
|
|
.. math::
|
|
|
|
|
Out = MEAN(Out)
|
|
|
|
|
|
|
|
|
|
If :attr:`reduction` set to ``'sum'``, the reduced sum loss is:
|
|
|
|
|
|
|
|
|
|
.. math::
|
|
|
|
|
Out = SUM(Out)
|
|
|
|
|
|
|
|
|
|
Note that the input predictions always be the output of sigmoid, and the target labels
|
|
|
|
|
Note that the input predictions ``input`` always be the output of sigmoid, and the target labels ``label``
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
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'``.
|
|
|
|
@ -381,6 +375,18 @@ class BCELoss(fluid.dygraph.Layer):
|
|
|
|
|
If :attr:`reduction` is ``'mean'``, the reduced mean loss is returned;
|
|
|
|
|
If :attr:`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 :ref:`api_guide_Name`.
|
|
|
|
|
|
|
|
|
|
Shape:
|
|
|
|
|
input (Tensor): 2-D tensor with shape: (N, *), N is batch_size, `*` means
|
|
|
|
|
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.
|
|
|
|
@ -388,37 +394,22 @@ class BCELoss(fluid.dygraph.Layer):
|
|
|
|
|
Examples:
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
|
|
# declarative mode
|
|
|
|
|
import paddle.fluid as fluid
|
|
|
|
|
import numpy as np
|
|
|
|
|
import paddle
|
|
|
|
|
input = fluid.data(name="input", shape=[3, 1], dtype='float32')
|
|
|
|
|
label = fluid.data(name="label", shape=[3, 1], dtype='float32')
|
|
|
|
|
bce_loss = paddle.nn.loss.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)
|
|
|
|
|
paddle.disable_static()
|
|
|
|
|
input = paddle.to_variable(input_data)
|
|
|
|
|
label = paddle.to_variable(label_data)
|
|
|
|
|
bce_loss = paddle.nn.loss.BCELoss()
|
|
|
|
|
output = bce_loss(input, label)
|
|
|
|
|
print(output.numpy()) # [0.65537095]
|
|
|
|
|
paddle.enable_static()
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def __init__(self, weight=None, reduction='mean'):
|
|
|
|
|
def __init__(self, weight=None, reduction='mean', name=None):
|
|
|
|
|
if reduction not in ['sum', 'mean', 'none']:
|
|
|
|
|
raise ValueError(
|
|
|
|
|
"The value of 'reduction' in bce_loss should be 'sum', 'mean' or 'none', but "
|
|
|
|
@ -427,37 +418,11 @@ class BCELoss(fluid.dygraph.Layer):
|
|
|
|
|
super(BCELoss, self).__init__()
|
|
|
|
|
self.weight = weight
|
|
|
|
|
self.reduction = reduction
|
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
|
|
def forward(self, input, label):
|
|
|
|
|
dtype = self._helper.input_dtype(input)
|
|
|
|
|
|
|
|
|
|
fluid.data_feeder.check_variable_and_dtype(
|
|
|
|
|
input, 'input', ['float32', 'float64'], 'bce_loss')
|
|
|
|
|
fluid.data_feeder.check_variable_and_dtype(
|
|
|
|
|
label, 'label', ['float32', 'float64'], 'bce_loss')
|
|
|
|
|
|
|
|
|
|
out = self._helper.create_variable_for_type_inference(dtype=input.dtype)
|
|
|
|
|
self._helper.append_op(
|
|
|
|
|
type='bce_loss',
|
|
|
|
|
inputs={
|
|
|
|
|
'X': [input],
|
|
|
|
|
'Label': [label],
|
|
|
|
|
},
|
|
|
|
|
outputs={'Out': [out]})
|
|
|
|
|
|
|
|
|
|
if self.weight is not None:
|
|
|
|
|
if isinstance(self.weight, fluid.framework.Variable):
|
|
|
|
|
w = self.weight
|
|
|
|
|
out = fluid.layers.elementwise_mul(out, w, axis=-1)
|
|
|
|
|
else:
|
|
|
|
|
raise ValueError(
|
|
|
|
|
"The weight is not a Variable, please convert to Variable.")
|
|
|
|
|
|
|
|
|
|
if self.reduction == 'sum':
|
|
|
|
|
return fluid.layers.reduce_sum(out)
|
|
|
|
|
elif self.reduction == 'mean':
|
|
|
|
|
return fluid.layers.reduce_mean(out)
|
|
|
|
|
else:
|
|
|
|
|
out = paddle.nn.functional.binary_cross_entropy(
|
|
|
|
|
input, label, self.weight, self.reduction, self.name)
|
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|