Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
7277df47
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看板
未验证
提交
7277df47
编写于
4月 10, 2020
作者:
S
silingtong123
提交者:
GitHub
4月 10, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
error message of NCE API enhancement (#23544)
* error message of NCE API enhancement
上级
f10100eb
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
47 addition
and
0 deletion
+47
-0
python/paddle/fluid/dygraph/nn.py
python/paddle/fluid/dygraph/nn.py
+5
-0
python/paddle/fluid/tests/unittests/test_nce.py
python/paddle/fluid/tests/unittests/test_nce.py
+42
-0
未找到文件。
python/paddle/fluid/dygraph/nn.py
浏览文件 @
7277df47
...
...
@@ -26,6 +26,7 @@ from ..param_attr import ParamAttr
from
..initializer
import
Normal
,
Constant
,
NumpyArrayInitializer
from
..
import
unique_name
from
.layer_object_helper
import
LayerObjectHelper
from
..data_feeder
import
check_variable_and_dtype
,
check_type
import
numpy
as
np
import
numbers
import
logging
...
...
@@ -2019,6 +2020,10 @@ class NCE(layers.Layer):
self
.
_inputs
[
'Weight'
]
=
self
.
weight
def
forward
(
self
,
input
,
label
,
sample_weight
=
None
):
check_variable_and_dtype
(
input
,
"input"
,
[
'float32'
,
'float64'
],
"NCE"
)
check_variable_and_dtype
(
label
,
"label"
,
[
'int64'
],
"NCE"
)
check_type
(
sample_weight
,
'sample_weight'
,
(
Variable
,
type
(
None
)),
'NCE'
)
assert
isinstance
(
input
,
Variable
)
assert
isinstance
(
label
,
Variable
)
...
...
python/paddle/fluid/tests/unittests/test_nce.py
浏览文件 @
7277df47
...
...
@@ -252,5 +252,47 @@ class TestNCE_OpError(unittest.TestCase):
self
.
assertRaises
(
TypeError
,
fluid
.
layers
.
nce
,
input4
,
label4
,
5
)
class
TestDygraphNCE_OpError
(
unittest
.
TestCase
):
def
test_NCE_errors
(
self
):
with
program_guard
(
Program
(),
Program
()):
nce
=
fluid
.
NCE
(
20
,
5
)
input1
=
fluid
.
create_lod_tensor
(
np
.
array
([
0.0
,
3.0
,
2.0
,
4.0
]),
[[
1
,
1
,
2
]],
fluid
.
CPUPlace
())
label1
=
fluid
.
layers
.
data
(
name
=
'label1'
,
shape
=
[
-
1
,
4
],
dtype
=
"int64"
)
# the input(input) of NCE layer must be Variable.
self
.
assertRaises
(
TypeError
,
nce
,
input1
,
label1
)
input2
=
fluid
.
layers
.
data
(
name
=
'input2'
,
shape
=
[
-
1
,
4
],
dtype
=
"float32"
)
label2
=
fluid
.
create_lod_tensor
(
np
.
array
([
0.0
,
3.0
,
2.0
,
4.0
]),
[[
1
,
1
,
2
]],
fluid
.
CPUPlace
())
# the input(label) of NCE layer must be Variable.
self
.
assertRaises
(
TypeError
,
nce
,
input2
,
label2
)
input3
=
fluid
.
layers
.
data
(
name
=
'input3'
,
shape
=
[
-
1
,
4
],
dtype
=
"float16"
)
label3
=
fluid
.
layers
.
data
(
name
=
'label3'
,
shape
=
[
-
1
,
1
],
dtype
=
"int64"
)
# the data type of input(input) must be float32 or float64.
self
.
assertRaises
(
TypeError
,
nce
,
input3
,
label3
)
input4
=
fluid
.
layers
.
data
(
name
=
'input4'
,
shape
=
[
-
1
,
4
],
dtype
=
"float32"
)
label4
=
fluid
.
layers
.
data
(
name
=
'label4'
,
shape
=
[
-
1
,
1
],
dtype
=
"int32"
)
# the data type of input(label) must be int64.
self
.
assertRaises
(
TypeError
,
nce
,
input4
,
label4
)
input5
=
fluid
.
layers
.
data
(
name
=
'input5'
,
shape
=
[
-
1
,
4
],
dtype
=
"float32"
)
label5
=
fluid
.
layers
.
data
(
name
=
'label5'
,
shape
=
[
-
1
,
1
],
dtype
=
"int64"
)
sample_weight
=
fluid
.
create_lod_tensor
(
np
.
array
([
0.0
,
3.0
,
2.0
,
4.0
]),
[[
1
,
1
,
2
]],
fluid
.
CPUPlace
())
# the sample_weight of nce must be Variable or None.
self
.
assertRaises
(
TypeError
,
nce
,
input5
,
label5
,
sample_weight
)
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录