Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
16c36465
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2305
Star
20932
Fork
5423
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
16c36465
编写于
4月 13, 2023
作者:
L
lzydev
提交者:
GitHub
4月 13, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix bug in cross_entropy in static mode (#52771)
* fix bug in cross_entropy in static mode * fix ci-coverage
上级
84d34ddd
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
68 addition
and
9 deletion
+68
-9
paddle/phi/infermeta/binary.cc
paddle/phi/infermeta/binary.cc
+0
-7
python/paddle/fluid/tests/unittests/test_cross_entropy_op.py
python/paddle/fluid/tests/unittests/test_cross_entropy_op.py
+15
-0
python/paddle/fluid/tests/unittests/test_softmax_with_cross_entropy_op.py
...uid/tests/unittests/test_softmax_with_cross_entropy_op.py
+31
-2
python/paddle/nn/functional/loss.py
python/paddle/nn/functional/loss.py
+22
-0
未找到文件。
paddle/phi/infermeta/binary.cc
浏览文件 @
16c36465
...
...
@@ -886,7 +886,6 @@ void CrossEntropyWithSoftmaxInferMeta(const MetaTensor& logits,
auto
logits_dims
=
logits
.
dims
();
auto
labels_dims
=
label
.
dims
();
auto
logits_rank
=
logits_dims
.
size
();
auto
labels_rank
=
labels_dims
.
size
();
PADDLE_ENFORCE_GE
(
axis
,
-
logits_rank
,
phi
::
errors
::
InvalidArgument
(
...
...
@@ -919,12 +918,6 @@ void CrossEntropyWithSoftmaxInferMeta(const MetaTensor& logits,
"when not in numeric_stable_mode."
));
}
PADDLE_ENFORCE_EQ
(
(
logits_rank
-
1
!=
labels_rank
)
&&
(
logits_rank
!=
labels_rank
),
false
,
phi
::
errors
::
InvalidArgument
(
"Expected input_dims - 1 == label_dims "
"or input_dims == label_dims."
));
if
(
soft_label
)
{
if
(
config
.
is_runtime
||
(
logits_dims
[
axis
]
>
0
&&
labels_dims
[
axis
]
>
0
))
{
PADDLE_ENFORCE_EQ
(
logits_dims
[
axis
],
...
...
python/paddle/fluid/tests/unittests/test_cross_entropy_op.py
浏览文件 @
16c36465
...
...
@@ -441,6 +441,21 @@ class TestCrossEntropyOpError(unittest.TestCase):
self
.
assertRaises
(
TypeError
,
test_dtype
)
def
test_input_dims
():
with
paddle_static_guard
():
# "input_dims - 1 != label_dims and input_dims != label_dims" must be false.
x3
=
paddle
.
static
.
data
(
name
=
'x3'
,
shape
=
[
-
1
,
3
,
4
,
5
],
dtype
=
"int32"
)
lab3
=
paddle
.
static
.
data
(
name
=
'lab3'
,
shape
=
[
-
1
,
3
,
4
,
5
,
6
],
dtype
=
"int32"
)
paddle
.
nn
.
functional
.
cross_entropy
(
x3
,
lab3
,
reduction
=
'none'
,
use_softmax
=
False
)
self
.
assertRaises
(
ValueError
,
test_input_dims
)
if
__name__
==
"__main__"
:
unittest
.
main
()
python/paddle/fluid/tests/unittests/test_softmax_with_cross_entropy_op.py
浏览文件 @
16c36465
...
...
@@ -15,11 +15,11 @@
import
unittest
import
numpy
as
np
from
eager_op_test
import
OpTest
,
convert_float_to_uint16
from
eager_op_test
import
OpTest
,
convert_float_to_uint16
,
paddle_static_guard
from
test_softmax_op
import
stable_softmax
import
paddle
from
paddle.fluid
import
core
from
paddle.fluid
import
Program
,
core
,
program_guard
def
cross_entropy
(
softmax
,
label
,
soft_label
,
axis
,
ignore_index
=-
1
):
...
...
@@ -974,6 +974,35 @@ class TestSoftmaxWithCrossEntropyOpBF16(TestSoftmaxWithCrossEntropyOp):
)
class
TestSoftmaxWithCrossEntropyOpError
(
unittest
.
TestCase
):
def
test_errors
(
self
):
with
program_guard
(
Program
(),
Program
()):
def
test_input_dims1
():
with
paddle_static_guard
():
# the input dims of cross_entropy can't be 0,
x1
=
paddle
.
static
.
data
(
name
=
'x1'
,
shape
=
[],
dtype
=
"int32"
)
lab1
=
paddle
.
static
.
data
(
name
=
'lab1'
,
shape
=
[
-
1
,
3
,
4
,
5
,
6
],
dtype
=
"int32"
)
paddle
.
nn
.
functional
.
softmax_with_cross_entropy
(
x1
,
lab1
)
self
.
assertRaises
(
ValueError
,
test_input_dims1
)
def
test_input_dims2
():
with
paddle_static_guard
():
# "input_dims - 1 != label_dims and input_dims != label_dims" must be false.
x2
=
paddle
.
static
.
data
(
name
=
'x2'
,
shape
=
[
-
1
,
3
,
4
,
5
],
dtype
=
"int32"
)
lab2
=
paddle
.
static
.
data
(
name
=
'lab2'
,
shape
=
[
-
1
,
3
,
4
,
5
,
6
],
dtype
=
"int32"
)
paddle
.
nn
.
functional
.
softmax_with_cross_entropy
(
x2
,
lab2
)
self
.
assertRaises
(
ValueError
,
test_input_dims2
)
if
__name__
==
"__main__"
:
paddle
.
enable_static
()
unittest
.
main
()
python/paddle/nn/functional/loss.py
浏览文件 @
16c36465
...
...
@@ -253,6 +253,20 @@ def fluid_softmax_with_cross_entropy(
# Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
# [1.15328646])
"""
input_dims
=
len
(
list
(
logits
.
shape
))
if
input_dims
==
0
:
raise
ValueError
(
'The dimention of input should be larger than zero!'
)
label_dims
=
len
(
list
(
label
.
shape
))
if
input_dims
-
1
!=
label_dims
and
input_dims
!=
label_dims
:
raise
ValueError
(
'Expected nput_dims - 1 = label_dims or input_dims == label_dims
\
(got nput_dims{}, label_dims{})'
.
format
(
input_dims
,
label_dims
)
)
if
input_dims
-
1
==
label_dims
:
label
=
paddle
.
unsqueeze
(
label
,
axis
=
axis
)
if
in_dygraph_mode
():
if
core
.
is_compiled_with_custom_device
(
"npu"
):
if
not
soft_label
:
...
...
@@ -2700,6 +2714,14 @@ def cross_entropy(
if
input_dims
-
1
==
label_dims
:
label
=
paddle
.
unsqueeze
(
label
,
axis
=
axis
)
if
input_dims
-
1
!=
label_dims
and
input_dims
!=
label_dims
:
raise
ValueError
(
'Expected nput_dims - 1 = label_dims or input_dims == label_dims
\
(got nput_dims{}, label_dims{})'
.
format
(
input_dims
,
label_dims
)
)
if
in_dygraph_mode
():
if
not
soft_label
:
valid_label
=
(
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录