Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
08bf1b49
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看板
未验证
提交
08bf1b49
编写于
1月 11, 2023
作者:
L
Linjie Chen
提交者:
GitHub
1月 11, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add input check for NLLLoss (#49547)
* fix nll_loss * fix nll_loss * update * update * update * fix
上级
203f9594
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
50 addition
and
0 deletion
+50
-0
python/paddle/fluid/tests/unittests/test_nll_loss.py
python/paddle/fluid/tests/unittests/test_nll_loss.py
+31
-0
python/paddle/nn/functional/loss.py
python/paddle/nn/functional/loss.py
+19
-0
未找到文件。
python/paddle/fluid/tests/unittests/test_nll_loss.py
浏览文件 @
08bf1b49
...
...
@@ -1125,6 +1125,37 @@ class TestNLLLossInvalidArgs(unittest.TestCase):
self
.
assertRaises
(
ValueError
,
test_x_dim_imperative_lt_2
)
def
test_x_shape_lt_1
():
prog
=
paddle
.
static
.
Program
()
startup_prog
=
paddle
.
static
.
Program
()
place
=
paddle
.
CPUPlace
()
with
paddle
.
static
.
program_guard
(
prog
,
startup_prog
):
array
=
np
.
array
([],
dtype
=
np
.
float32
)
x
=
paddle
.
to_tensor
(
np
.
reshape
(
array
,
[
1
,
0
]),
dtype
=
'float32'
)
label
=
paddle
.
to_tensor
(
np
.
reshape
(
array
,
[
1
,
0
]),
dtype
=
'int64'
)
nll_loss
=
paddle
.
nn
.
loss
.
NLLLoss
()
res
=
nll_loss
(
x
,
label
)
self
.
assertRaises
(
ValueError
,
test_x_shape_lt_1
)
def
test_x_dim_and_label_dim
():
prog
=
paddle
.
static
.
Program
()
startup_prog
=
paddle
.
static
.
Program
()
place
=
paddle
.
CPUPlace
()
with
paddle
.
static
.
program_guard
(
prog
,
startup_prog
):
x_np
=
np
.
random
.
random
(
size
=
(
5
,)).
astype
(
np
.
float64
)
label_np
=
np
.
random
.
randint
(
0
,
10
,
size
=
(
5
,
1
)).
astype
(
np
.
int64
)
x
=
paddle
.
to_tensor
(
x_np
)
label
=
paddle
.
to_tensor
(
label_np
)
nll_loss
=
paddle
.
nn
.
loss
.
NLLLoss
()
res
=
nll_loss
(
x
,
label
)
self
.
assertRaises
(
ValueError
,
test_x_dim_and_label_dim
)
def
test_reduction_value_error
(
self
):
def
test_NLLLoss_reduction_not_sum_mean_none
():
prog
=
paddle
.
static
.
Program
()
...
...
python/paddle/nn/functional/loss.py
浏览文件 @
08bf1b49
...
...
@@ -1372,10 +1372,29 @@ def nll_loss(
input_shape
=
list
(
input
.
shape
)
input_dims
=
len
(
input_shape
)
label_shape
=
list
(
label
.
shape
)
label_dims
=
len
(
label_shape
)
if
input_dims
-
1
!=
label_dims
and
input_dims
!=
label_dims
:
raise
ValueError
(
"Expected input_dims - 1 = label_dims or input_dims == label_dims
\
(got input_dims{}, label_dims{})"
.
format
(
input_dims
,
label_dims
)
)
if
input_dims
<
2
:
raise
ValueError
(
'Expected 2 or more dimensions (got {})'
.
format
(
input_dims
)
)
if
input_shape
[
1
]
<
1
:
raise
ValueError
(
"Expected 1 or more classess (got num classes{})"
.
format
(
input_shape
[
1
]
)
)
n
=
input_shape
[
0
]
c
=
input_shape
[
1
]
if
in_dygraph_mode
():
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录