Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
f4be1d99
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2302
Star
20931
Fork
5422
代码
文件
提交
分支
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看板
提交
f4be1d99
编写于
11月 13, 2018
作者:
J
JiabinYang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
polish code and test
上级
b8ff0972
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
63 addition
and
22 deletion
+63
-22
paddle/fluid/operators/hierarchical_sigmoid_op.cc
paddle/fluid/operators/hierarchical_sigmoid_op.cc
+1
-1
python/paddle/fluid/layers/nn.py
python/paddle/fluid/layers/nn.py
+45
-21
python/paddle/fluid/tests/unittests/test_layers.py
python/paddle/fluid/tests/unittests/test_layers.py
+17
-0
未找到文件。
paddle/fluid/operators/hierarchical_sigmoid_op.cc
浏览文件 @
f4be1d99
...
...
@@ -115,7 +115,7 @@ class HierarchicalSigmoidOpMaker : public framework::OpProtoAndCheckerMaker {
"[batch_size, code_length], where code_length represents the "
"maximum path length from root to leaf nodes."
)
.
AsIntermediate
();
AddAttr
<
AttrType
>
(
"num_classes"
,
"(int,
required
), The number of classes"
)
AddAttr
<
AttrType
>
(
"num_classes"
,
"(int,
optional
), The number of classes"
)
.
SetDefault
(
2
);
AddComment
(
R"DOC(
The hierarchical sigmoid operator organize the classes into a binary tree.
...
...
python/paddle/fluid/layers/nn.py
浏览文件 @
f4be1d99
...
...
@@ -4348,12 +4348,14 @@ def nce(input,
def
hsigmoid
(
input
,
label
,
num_classes
,
ptabl
=
None
,
num_classes
=
None
,
non_leaf_num
=
None
,
ptable
=
None
,
pcode
=
None
,
param_attr
=
None
,
bias_attr
=
None
,
name
=
None
):
name
=
None
,
is_costum
=
False
):
"""
The hierarchical sigmoid operator is used to accelerate the training
process of language model. This operator organizes the classes into a
...
...
@@ -4373,7 +4375,8 @@ def hsigmoid(input,
and :math:`D` is the feature size.
label (Variable): The tensor variable contains labels of training data.
It's a tensor with shape is :math:`[N
\\
times 1]`.
num_classes: (int), The number of classes, must not be less than 2.
num_classes: (int), The number of classes, must not be less than 2. with default tree this has to be set
non_leaf_num: this defines the number of non-leaf nodes in costumed tree
ptable: (Variable|None) this variable can store each batch of samples' path to root,
it should be in leaf -> root order
ptable should have the same shape with pcode, and for each sample i ptable[i] indicates a np.array like
...
...
@@ -4409,20 +4412,33 @@ def hsigmoid(input,
out
=
helper
.
create_variable_for_type_inference
(
dtype
)
pre_out
=
helper
.
create_variable_for_type_inference
(
dtype
)
dim
=
input
.
shape
[
1
]
if
num_classes
<
2
:
raise
ValueError
(
"num_classes must not be less than 2."
)
if
(
ptable
is
not
None
)
and
(
pcode
is
None
):
raise
ValueError
(
"pcode should not be None when ptable has been set"
)
elif
(
ptable
is
None
)
and
(
pcode
is
not
None
):
raise
ValueError
(
"ptable should not be None when pcode has been set"
)
if
((
num_classes
<
2
)
or
(
num_classes
is
None
))
and
(
not
is_costum
):
raise
ValueError
(
"num_classes must not be less than 2 with default tree"
)
if
(
is_costum
)
and
(
pcode
is
None
):
raise
ValueError
(
"pcode should not be None with costum tree"
)
elif
(
is_costum
)
and
(
ptable
is
None
):
raise
ValueError
(
"ptable should not be None with costum tree"
)
elif
(
is_costum
)
and
(
non_leaf_num
is
None
):
raise
ValueError
(
"non_leaf_num should not be None with costum tree"
)
else
:
pass
weights
=
None
if
not
is_costum
:
weights
=
helper
.
create_parameter
(
attr
=
helper
.
param_attr
,
shape
=
[
num_classes
-
1
,
dim
],
is_bias
=
False
,
dtype
=
input
.
dtype
)
else
:
weights
=
helper
.
create_parameter
(
attr
=
helper
.
param_attr
,
shape
=
[
non_leaf_num
,
dim
],
is_bias
=
False
,
dtype
=
input
.
dtype
)
inputs
=
{
"X"
:
input
,
"W"
:
weights
,
...
...
@@ -4431,12 +4447,20 @@ def hsigmoid(input,
"Label"
:
label
}
if
helper
.
bias_attr
:
if
not
is_costum
:
bias
=
helper
.
create_parameter
(
attr
=
helper
.
bias_attr
,
shape
=
[
1
,
num_classes
-
1
],
is_bias
=
True
,
dtype
=
input
.
dtype
)
inputs
[
'Bias'
]
=
bias
else
:
bias
=
helper
.
create_parameter
(
attr
=
helper
.
bias_attr
,
shape
=
[
1
,
non_leaf_num
],
is_bias
=
True
,
dtype
=
input
.
dtype
)
inputs
[
'Bias'
]
=
bias
helper
.
append_op
(
type
=
"hierarchical_sigmoid"
,
inputs
=
inputs
,
...
...
python/paddle/fluid/tests/unittests/test_layers.py
浏览文件 @
f4be1d99
...
...
@@ -185,6 +185,23 @@ class TestBook(unittest.TestCase):
input
=
x
,
label
=
y
,
num_classes
=
2
))
print
(
str
(
program
))
program2
=
Program
()
with
program_guard
(
program2
):
x2
=
layers
.
data
(
name
=
'x2'
,
shape
=
[
4
,
8
],
dtype
=
'float32'
)
y2
=
layers
.
data
(
name
=
'y2'
,
shape
=
[
4
],
dtype
=
'int64'
)
ptable
=
layers
.
data
(
name
=
'ptable'
,
shape
=
[
4
,
6
],
dtype
=
'int64'
)
pcode
=
layers
.
data
(
name
=
'pcode'
,
shape
=
[
4
,
6
],
dtype
=
'int64'
)
self
.
assertIsNotNone
(
layers
.
hsigmoid
(
input
=
x2
,
label
=
y2
,
non_leaf_num
=
6
,
ptable
=
ptable
,
pcode
=
pcode
,
is_costum
=
True
))
print
(
str
(
program2
))
def
test_sequence_expand
(
self
):
program
=
Program
()
with
program_guard
(
program
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录