Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
672def6c
P
Paddle
项目概览
机器未来
/
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看板
未验证
提交
672def6c
编写于
2月 16, 2022
作者:
W
Weilong Wu
提交者:
GitHub
2月 16, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Support nce in eager mode (#39589)
上级
5b5656d0
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
119 addition
and
2 deletion
+119
-2
paddle/fluid/pybind/op_function_generator.h
paddle/fluid/pybind/op_function_generator.h
+3
-0
python/paddle/fluid/dygraph/nn.py
python/paddle/fluid/dygraph/nn.py
+13
-0
python/paddle/fluid/tests/unittests/test_layers.py
python/paddle/fluid/tests/unittests/test_layers.py
+103
-2
未找到文件。
paddle/fluid/pybind/op_function_generator.h
浏览文件 @
672def6c
...
...
@@ -84,6 +84,9 @@ std::map<std::string, std::set<std::string>> op_ins_map = {
{
"Q"
,
"K"
,
"V"
,
"Offset"
,
"Columns"
,
"KeyPaddingMask"
,
"AttnMask"
}},
{
"sgd"
,
{
"Param"
,
"LearningRate"
,
"Grad"
,
"MasterParam"
}},
{
"graph_khop_sampler"
,
{
"Row"
,
"Eids"
,
"Col_Ptr"
,
"X"
}},
{
"nce"
,
{
"Input"
,
"Label"
,
"Weight"
,
"Bias"
,
"SampleWeight"
,
"CustomDistProbs"
,
"CustomDistAlias"
,
"CustomDistAliasProbs"
}},
};
// NOTE(zhiqiu): Like op_ins_map.
...
...
python/paddle/fluid/dygraph/nn.py
浏览文件 @
672def6c
...
...
@@ -2233,6 +2233,19 @@ class NCE(layers.Layer):
self
.
_inputs
[
'Weight'
]
=
self
.
weight
def
forward
(
self
,
input
,
label
,
sample_weight
=
None
):
if
in_dygraph_mode
():
attrs
=
(
'num_total_classes'
,
self
.
_attrs
[
'num_total_classes'
],
'num_neg_samples'
,
self
.
_attrs
[
'num_neg_samples'
],
'seed'
,
self
.
_attrs
[
'seed'
],
'sampler'
,
self
.
_attrs
[
'sampler'
],
'is_sparse'
,
self
.
_attrs
[
'is_sparse'
],
'remote_prefetch'
,
self
.
_attrs
[
'remote_prefetch'
])
cost
,
_
,
_
=
_C_ops
.
nce
(
input
,
label
,
self
.
weight
,
self
.
bias
,
self
.
_inputs
[
'SampleWeight'
],
self
.
_inputs
[
'CustomDistProbs'
],
self
.
_inputs
[
'CustomDistAlias'
],
self
.
_inputs
[
'CustomDistAliasProbs'
],
*
attrs
)
return
cost
/
(
self
.
_num_neg_samples
+
1
)
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
)),
...
...
python/paddle/fluid/tests/unittests/test_layers.py
浏览文件 @
672def6c
...
...
@@ -1361,6 +1361,7 @@ class TestLayer(LayerTest):
feed_dict
[
'word_{0}'
.
format
(
i
)]
=
inp_word
[
i
]
static_rlt
=
self
.
get_static_graph_result
(
feed
=
feed_dict
,
fetch_list
=
[
nce_loss
])[
0
]
with
self
.
static_graph
():
words
=
[]
for
i
in
range
(
window_size
):
...
...
@@ -1401,7 +1402,41 @@ class TestLayer(LayerTest):
feed
=
feed_dict
,
fetch_list
=
[
nce_loss2
])[
0
]
with
self
.
dynamic_graph
():
# TODO(wuweilong): Add with _test_eager_guard():
with
_test_eager_guard
():
words
=
[]
for
i
in
range
(
window_size
):
words
.
append
(
base
.
to_variable
(
inp_word
[
i
]))
sample_weights
=
layers
.
fill_constant
(
shape
=
[
5
,
1
],
dtype
=
'float32'
,
value
=
1
)
emb
=
nn
.
Embedding
(
size
=
[
dict_size
,
32
],
param_attr
=
'eager_emb.w'
,
is_sparse
=
False
)
embs3
=
[]
for
i
in
range
(
window_size
):
if
i
==
label_word
:
continue
emb_rlt
=
emb
(
words
[
i
])
embs3
.
append
(
emb_rlt
)
embs3
=
layers
.
concat
(
input
=
embs3
,
axis
=
fluid
.
dygraph
.
to_variable
(
np
.
array
([
1
])))
nce
=
nn
.
NCE
(
num_total_classes
=
dict_size
,
dim
=
embs3
.
shape
[
1
],
num_neg_samples
=
2
,
sampler
=
"custom_dist"
,
custom_dist
=
nid_freq_arr
.
tolist
(),
seed
=
seed
,
param_attr
=
'eager_nce.w'
,
bias_attr
=
'eager_nce.b'
,
sample_weight
=
sample_weights
)
wl
=
fluid
.
layers
.
unsqueeze
(
words
[
label_word
],
axes
=
[
0
])
dy_eager_rlt
=
nce
(
embs3
,
wl
)
dy_eager_rlt_value
=
dy_eager_rlt
.
numpy
()
words
=
[]
for
i
in
range
(
window_size
):
words
.
append
(
base
.
to_variable
(
inp_word
[
i
]))
...
...
@@ -1436,9 +1471,75 @@ class TestLayer(LayerTest):
self
.
assertTrue
(
np
.
allclose
(
static_rlt2
,
static_rlt
))
self
.
assertTrue
(
np
.
allclose
(
dy_rlt_value
,
static_rlt
))
self
.
assertTrue
(
np
.
allclose
(
dy_eager_rlt_value
,
static_rlt
))
with
self
.
dynamic_graph
():
# TODO(wuweilong): Add with _test_eager_guard():
with
_test_eager_guard
():
custom_weight
=
np
.
random
.
randn
(
dict_size
,
128
).
astype
(
"float32"
)
weight_attr
=
fluid
.
ParamAttr
(
initializer
=
fluid
.
initializer
.
NumpyArrayInitializer
(
custom_weight
))
words
=
[]
for
i
in
range
(
window_size
):
words
.
append
(
base
.
to_variable
(
inp_word
[
i
]))
sample_weights
=
layers
.
fill_constant
(
shape
=
fluid
.
dygraph
.
to_variable
(
np
.
array
([
5
,
1
])),
dtype
=
'float32'
,
value
=
1
)
emb
=
nn
.
Embedding
(
size
=
[
dict_size
,
32
],
param_attr
=
'eager_emb.w'
,
is_sparse
=
False
)
embs3
=
[]
for
i
in
range
(
window_size
):
if
i
==
label_word
:
continue
emb_rlt
=
emb
(
words
[
i
])
embs3
.
append
(
emb_rlt
)
embs3
=
layers
.
concat
(
input
=
embs3
,
axis
=
1
)
nce1
=
nn
.
NCE
(
num_total_classes
=
dict_size
,
dim
=
embs3
.
shape
[
1
],
num_neg_samples
=
2
,
sampler
=
"custom_dist"
,
custom_dist
=
nid_freq_arr
.
tolist
(),
seed
=
seed
,
param_attr
=
'eager_nce1.w'
,
bias_attr
=
'eager_nce1.b'
,
sample_weight
=
sample_weights
)
nce2
=
nn
.
NCE
(
num_total_classes
=
dict_size
,
dim
=
embs3
.
shape
[
1
],
num_neg_samples
=
2
,
sampler
=
"custom_dist"
,
custom_dist
=
nid_freq_arr
.
tolist
(),
seed
=
seed
,
param_attr
=
weight_attr
,
bias_attr
=
'eager_nce2.b'
,
sample_weight
=
sample_weights
)
wl
=
fluid
.
layers
.
unsqueeze
(
words
[
label_word
],
axes
=
[
0
])
nce1_loss
=
nce1
(
embs3
,
wl
)
nce2_loss
=
nce2
(
embs3
,
wl
)
self
.
assertFalse
(
np
.
array_equal
(
nce1_loss
.
numpy
(),
nce2_loss
.
numpy
()))
nce2
.
weight
.
set_value
(
nce1
.
weight
.
numpy
())
nce2
.
bias
.
set_value
(
nce1
.
bias
)
nce1_loss
=
nce1
(
embs3
,
wl
)
nce2_loss
=
nce2
(
embs3
,
wl
)
self
.
assertTrue
(
np
.
array_equal
(
nce1_loss
.
numpy
(),
nce2_loss
.
numpy
()))
nce2
.
weight
=
nce1
.
weight
nce2
.
bias
=
nce1
.
bias
self
.
assertTrue
(
np
.
array_equal
(
nce1
.
weight
.
numpy
(),
nce2
.
weight
.
numpy
()))
self
.
assertTrue
(
np
.
array_equal
(
nce1
.
bias
.
numpy
(),
nce2
.
bias
.
numpy
()))
custom_weight
=
np
.
random
.
randn
(
dict_size
,
128
).
astype
(
"float32"
)
weight_attr
=
fluid
.
ParamAttr
(
initializer
=
fluid
.
initializer
.
NumpyArrayInitializer
(
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录