Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
23d3e36a
P
Paddle
项目概览
Crayon鑫
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
23d3e36a
编写于
4月 27, 2021
作者:
G
Guanghua Yu
提交者:
GitHub
4月 27, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix cross_entropy calculation error (#32545)
* fix cross_entropy calculation error * add unittest and fix static
上级
97794eca
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
49 addition
and
10 deletion
+49
-10
python/paddle/fluid/tests/unittests/test_cross_entropy_loss.py
...n/paddle/fluid/tests/unittests/test_cross_entropy_loss.py
+43
-4
python/paddle/nn/functional/loss.py
python/paddle/nn/functional/loss.py
+6
-6
未找到文件。
python/paddle/fluid/tests/unittests/test_cross_entropy_loss.py
浏览文件 @
23d3e36a
...
...
@@ -59,8 +59,8 @@ def cross_entropy_loss_1d(input,
if
reduction
==
'sum'
:
return
np
.
sum
(
out
),
np
.
array
([
total_weight
]).
astype
(
'float64'
)
elif
reduction
==
'mean'
:
return
out
.
sum
()
/
total_weight
,
np
.
array
(
[
total_weight
]).
astype
(
'float64'
)
out
=
out
.
sum
()
/
total_weight
if
total_weight
!=
0
else
out
.
sum
()
return
out
,
np
.
array
(
[
total_weight
]).
astype
(
'float64'
)
elif
reduction
==
'none'
:
return
out
...
...
@@ -92,8 +92,8 @@ def cross_entropy_loss_2d(input,
if
reduction
==
'sum'
:
return
np
.
sum
(
out
),
np
.
array
([
total_weight
]).
astype
(
'float64'
)
elif
reduction
==
'mean'
:
return
out
.
sum
()
/
total_weight
,
np
.
array
(
[
total_weight
]).
astype
(
'float64'
)
out
=
out
.
sum
()
/
total_weight
if
total_weight
!=
0
else
out
.
sum
()
return
out
,
np
.
array
(
[
total_weight
]).
astype
(
'float64'
)
elif
reduction
==
'none'
:
return
out
...
...
@@ -759,6 +759,45 @@ class CrossEntropyLoss(unittest.TestCase):
self
.
assertTrue
(
np
.
allclose
(
static_ret
,
expected
))
self
.
assertTrue
(
np
.
allclose
(
dy_ret_value
,
expected
))
def
test_cross_entropy_loss_1d_with_mean_ignore_negative
(
self
):
N
=
100
C
=
200
input_np
=
np
.
random
.
random
([
N
,
C
]).
astype
(
self
.
dtype
)
label_np
=
-
np
.
ones
((
N
)).
astype
(
np
.
int64
)
paddle
.
enable_static
()
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
=
[
N
,
C
],
dtype
=
self
.
dtype
)
label
=
fluid
.
data
(
name
=
'label'
,
shape
=
[
N
],
dtype
=
'int64'
)
cross_entropy_loss
=
paddle
.
nn
.
loss
.
CrossEntropyLoss
(
ignore_index
=-
1
)
ret
=
cross_entropy_loss
(
input
,
label
)
exe
=
fluid
.
Executor
(
place
)
static_ret
=
exe
.
run
(
prog
,
feed
=
{
'input'
:
input_np
,
'label'
:
label_np
,
},
fetch_list
=
[
ret
])
self
.
assertIsNotNone
(
static_ret
)
with
fluid
.
dygraph
.
guard
():
cross_entropy_loss
=
paddle
.
nn
.
loss
.
CrossEntropyLoss
(
axis
=
1
,
ignore_index
=-
1
)
dy_ret
=
cross_entropy_loss
(
fluid
.
dygraph
.
to_variable
(
input_np
),
fluid
.
dygraph
.
to_variable
(
label_np
))
dy_ret_value
=
dy_ret
.
numpy
()
self
.
assertIsNotNone
(
dy_ret_value
)
expected
=
cross_entropy_loss_1d
(
input_np
,
label_np
,
ignore_index
=-
1
)[
0
]
self
.
assertTrue
(
np
.
allclose
(
static_ret
,
dy_ret_value
))
self
.
assertTrue
(
np
.
allclose
(
static_ret
,
expected
))
self
.
assertTrue
(
np
.
allclose
(
dy_ret_value
,
expected
))
def
test_cross_entropy_loss_1d_with_weight_mean_ignore
(
self
):
N
=
100
C
=
200
...
...
python/paddle/nn/functional/loss.py
浏览文件 @
23d3e36a
...
...
@@ -1454,20 +1454,20 @@ def cross_entropy(input,
if
weight
is
None
:
mask
=
paddle
.
cast
(
mask
,
dtype
=
out_sum
.
dtype
)
count
=
core
.
ops
.
reduce_sum
(
mask
,
'reduce_all'
,
True
)
ret
=
out_sum
/
count
ret
=
out_sum
/
(
count
+
(
count
==
0.0
))
else
:
mask
=
paddle
.
cast
(
mask
,
weight_gather_reshape
.
dtype
)
weight_ignored
=
core
.
ops
.
elementwise_mul
(
mask
,
weight_gather_reshape
)
weight_sum
=
core
.
ops
.
reduce_sum
(
weight_ignored
,
'reduce_all'
,
True
)
ret
=
out_sum
/
weight_sum
ret
=
out_sum
/
(
weight_sum
+
(
weight_sum
==
0.0
))
return
ret
elif
weight
is
not
None
:
out_sum
=
core
.
ops
.
reduce_sum
(
out
,
'reduce_all'
,
True
)
total_weight
=
core
.
ops
.
reduce_sum
(
weight_gather_reshape
,
'reduce_all'
,
True
)
return
out_sum
/
total_weight
return
out_sum
/
(
total_weight
+
(
total_weight
==
0.0
))
else
:
return
core
.
ops
.
mean
(
out
)
...
...
@@ -1537,17 +1537,17 @@ def cross_entropy(input,
if
(
weight
is
None
):
mask
=
paddle
.
cast
(
mask
,
dtype
=
out_sum
.
dtype
)
count
=
paddle
.
sum
(
mask
,
name
=
name
)
ret
=
out_sum
/
count
ret
=
out_sum
/
(
count
+
(
count
==
0.0
))
else
:
mask
=
paddle
.
cast
(
mask
,
weight_gather_reshape
.
dtype
)
weight_ignored
=
paddle
.
multiply
(
mask
,
weight_gather_reshape
)
weight_sum
=
paddle
.
sum
(
weight_ignored
,
name
=
name
)
ret
=
out_sum
/
weight_sum
ret
=
out_sum
/
(
weight_sum
+
(
weight_sum
==
0.0
))
return
ret
elif
weight
is
not
None
:
out_sum
=
paddle
.
sum
(
out
,
name
=
name
)
total_weight
=
paddle
.
sum
(
weight_gather_reshape
)
return
out_sum
/
total_weight
return
out_sum
/
(
total_weight
+
(
total_weight
==
0.0
))
else
:
return
paddle
.
mean
(
out
,
name
=
name
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录