未验证 提交 488f3577 编写于 作者: C ceci3 提交者: GitHub

fix bceloss weight (#23973)

* update docs, test=develop

* polish eng docs, test=develop
上级 735e9ccc
...@@ -66,18 +66,20 @@ class TestBCELoss(unittest.TestCase): ...@@ -66,18 +66,20 @@ class TestBCELoss(unittest.TestCase):
self.assertTrue(np.allclose(dy_result, expected)) self.assertTrue(np.allclose(dy_result, expected))
def test_BCELoss_weight(self): def test_BCELoss_weight(self):
input_np = np.random.random(size=(20, 30)).astype(np.float64) input_np = np.random.random(size=(2, 3, 4, 10)).astype(np.float64)
label_np = np.random.random(size=(20, 30)).astype(np.float64) label_np = np.random.random(size=(2, 3, 4, 10)).astype(np.float64)
weight_np = np.random.random(size=(20, 30)).astype(np.float64) weight_np = np.random.random(size=(3, 4, 10)).astype(np.float64)
prog = fluid.Program() prog = fluid.Program()
startup_prog = fluid.Program() startup_prog = fluid.Program()
place = fluid.CUDAPlace(0) if fluid.core.is_compiled_with_cuda( place = fluid.CUDAPlace(0) if fluid.core.is_compiled_with_cuda(
) else fluid.CPUPlace() ) else fluid.CPUPlace()
with fluid.program_guard(prog, startup_prog): with fluid.program_guard(prog, startup_prog):
input = fluid.data(name='input', shape=[None, 30], dtype='float64') input = fluid.data(
label = fluid.data(name='label', shape=[None, 30], dtype='float64') name='input', shape=[None, 3, 4, 10], dtype='float64')
label = fluid.data(
name='label', shape=[None, 3, 4, 10], dtype='float64')
weight = fluid.data( weight = fluid.data(
name='weight', shape=[None, 30], dtype='float64') name='weight', shape=[3, 4, 10], dtype='float64')
bce_loss = paddle.nn.loss.BCELoss(weight=weight) bce_loss = paddle.nn.loss.BCELoss(weight=weight)
res = bce_loss(input, label) res = bce_loss(input, label)
......
...@@ -315,40 +315,56 @@ class L1Loss(fluid.dygraph.Layer): ...@@ -315,40 +315,56 @@ class L1Loss(fluid.dygraph.Layer):
class BCELoss(fluid.dygraph.Layer): class BCELoss(fluid.dygraph.Layer):
""" """
This op accepts input predictions and target label and returns binary This interface is used to construct a callable object of the ``BCELoss`` class.
cross entropy error. The BCELoss layer measures the binary_cross_entropy loss between input predictions
For predictions label, and target label, the loss is calculated as follows. and target labels. The binary_cross_entropy loss can be described as:
If :attr:`weight` is set, the loss is: If :attr:`weight` is set, the loss is:
.. math::
Out = -1 * weight * (label * log(input) + (1 - label) * log(1 - input)) Out = -1 * weight * (label * log(input) + (1 - label) * log(1 - input))
If :attr:`weight` is None, the loss is: If :attr:`weight` is None, the loss is:
.. math::
Out = -1 * (label * log(input) + (1 - label) * log(1 - input)) 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 unreduced loss is:
.. math:: .. math::
Out = Out Out = Out
If :attr:`reduction` set to ``'mean'``, the reduced mean loss is: If :attr:`reduction` set to ``'mean'``, the reduced mean loss is:
.. math:: .. math::
Out = MEAN(Out) Out = MEAN(Out)
If :attr:`reduction` set to ``'sum'``, the reduced sum loss is: If :attr:`reduction` set to ``'sum'``, the reduced sum loss is:
.. math:: .. math::
Out = SUM(Out) 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.
Parameters: Parameters:
input (Variable): Input tensor, the data type is float32, weight (Variable, optional): A manual rescaling weight given to the loss of each
float64. Input must in (0, 1). batch element. If given, has to be a Variable of size nbatch and the data type
label (Variable): Label tensor, has the same shape with input, is float32, float64. Default is ``'None'``.
the data type is float32, float64.
weight (Variable, optional): Weight tensor, a manual rescaling weight given
to each class. It has the same dimensions as class number and the data type
is float32, float64, int32, int64. Default is ``'None'``.
reduction (str, optional): Indicate how to average the loss by batch_size, reduction (str, optional): Indicate how to average the loss by batch_size,
the candicates are ``'none'`` | ``'mean'`` | ``'sum'``. the candicates are ``'none'`` | ``'mean'`` | ``'sum'``.
If :attr:`reduction` is ``'none'``, the unreduced loss is returned;
If :attr:`reduction` is ``'mean'``, the reduced mean loss is returned; If :attr:`reduction` is ``'mean'``, the reduced mean loss is returned;
If :attr:`reduction` is ``'sum'``, the summed loss is returned.
Default is ``'mean'``. Default is ``'mean'``.
Returns:
The tensor variable storing the bce_loss of input and label. Returns:
Return type: Variable. A callable object of BCELoss.
Examples: Examples:
.. code-block:: python .. code-block:: python
# declarative mode # declarative mode
import paddle.fluid as fluid import paddle.fluid as fluid
import numpy as np import numpy as np
...@@ -409,7 +425,7 @@ class BCELoss(fluid.dygraph.Layer): ...@@ -409,7 +425,7 @@ class BCELoss(fluid.dygraph.Layer):
if self.weight is not None: if self.weight is not None:
if isinstance(self.weight, fluid.framework.Variable): if isinstance(self.weight, fluid.framework.Variable):
w = self.weight w = self.weight
out = fluid.layers.elementwise_mul(out, w, axis=0) out = fluid.layers.elementwise_mul(out, w, axis=-1)
else: else:
raise ValueError( raise ValueError(
"The weight is not a Variable, please convert to Variable.") "The weight is not a Variable, please convert to Variable.")
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册