Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
488f3577
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
488f3577
编写于
4月 22, 2020
作者:
C
ceci3
提交者:
GitHub
4月 22, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix bceloss weight (#23973)
* update docs, test=develop * polish eng docs, test=develop
上级
735e9ccc
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
38 addition
and
20 deletion
+38
-20
python/paddle/fluid/tests/unittests/test_bce_loss.py
python/paddle/fluid/tests/unittests/test_bce_loss.py
+8
-6
python/paddle/nn/layer/loss.py
python/paddle/nn/layer/loss.py
+30
-14
未找到文件。
python/paddle/fluid/tests/unittests/test_bce_loss.py
浏览文件 @
488f3577
...
...
@@ -66,18 +66,20 @@ class TestBCELoss(unittest.TestCase):
self
.
assertTrue
(
np
.
allclose
(
dy_result
,
expected
))
def
test_BCELoss_weight
(
self
):
input_np
=
np
.
random
.
random
(
size
=
(
2
0
,
3
0
)).
astype
(
np
.
float64
)
label_np
=
np
.
random
.
random
(
size
=
(
2
0
,
3
0
)).
astype
(
np
.
float64
)
weight_np
=
np
.
random
.
random
(
size
=
(
20
,
3
0
)).
astype
(
np
.
float64
)
input_np
=
np
.
random
.
random
(
size
=
(
2
,
3
,
4
,
1
0
)).
astype
(
np
.
float64
)
label_np
=
np
.
random
.
random
(
size
=
(
2
,
3
,
4
,
1
0
)).
astype
(
np
.
float64
)
weight_np
=
np
.
random
.
random
(
size
=
(
3
,
4
,
1
0
)).
astype
(
np
.
float64
)
prog
=
fluid
.
Program
()
startup_prog
=
fluid
.
Program
()
place
=
fluid
.
CUDAPlace
(
0
)
if
fluid
.
core
.
is_compiled_with_cuda
(
)
else
fluid
.
CPUPlace
()
with
fluid
.
program_guard
(
prog
,
startup_prog
):
input
=
fluid
.
data
(
name
=
'input'
,
shape
=
[
None
,
30
],
dtype
=
'float64'
)
label
=
fluid
.
data
(
name
=
'label'
,
shape
=
[
None
,
30
],
dtype
=
'float64'
)
input
=
fluid
.
data
(
name
=
'input'
,
shape
=
[
None
,
3
,
4
,
10
],
dtype
=
'float64'
)
label
=
fluid
.
data
(
name
=
'label'
,
shape
=
[
None
,
3
,
4
,
10
],
dtype
=
'float64'
)
weight
=
fluid
.
data
(
name
=
'weight'
,
shape
=
[
None
,
3
0
],
dtype
=
'float64'
)
name
=
'weight'
,
shape
=
[
3
,
4
,
1
0
],
dtype
=
'float64'
)
bce_loss
=
paddle
.
nn
.
loss
.
BCELoss
(
weight
=
weight
)
res
=
bce_loss
(
input
,
label
)
...
...
python/paddle/nn/layer/loss.py
浏览文件 @
488f3577
...
...
@@ -315,40 +315,56 @@ class L1Loss(fluid.dygraph.Layer):
class
BCELoss
(
fluid
.
dygraph
.
Layer
):
"""
This op accepts input predictions and target label and returns binary
cross entropy error.
For predictions label, and target label, the loss is calculated as follows.
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 :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:
.. 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
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:
input (Variable): Input tensor, the data type is float32,
float64. Input must in (0, 1).
label (Variable): Label tensor, has the same shape with input,
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'``.
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 :attr:`reduction` is ``'none'``, the unreduced 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'``.
Returns:
The tensor variable storing the bce_loss of input and label.
Return type: Variable.
Returns:
A callable object of BCELoss.
Examples:
.. code-block:: python
# declarative mode
import paddle.fluid as fluid
import numpy as np
...
...
@@ -409,7 +425,7 @@ class BCELoss(fluid.dygraph.Layer):
if
self
.
weight
is
not
None
:
if
isinstance
(
self
.
weight
,
fluid
.
framework
.
Variable
):
w
=
self
.
weight
out
=
fluid
.
layers
.
elementwise_mul
(
out
,
w
,
axis
=
0
)
out
=
fluid
.
layers
.
elementwise_mul
(
out
,
w
,
axis
=
-
1
)
else
:
raise
ValueError
(
"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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录