Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
b3f9e5e0
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
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看板
提交
b3f9e5e0
编写于
5月 31, 2018
作者:
W
weixing02
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
make test_hsigmoid_op.py right
上级
3e46ec41
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
23 addition
and
28 deletion
+23
-28
python/paddle/fluid/tests/unittests/test_hsigmoid_op.py
python/paddle/fluid/tests/unittests/test_hsigmoid_op.py
+23
-28
未找到文件。
python/paddle/fluid/tests/unittests/test_hsigmoid_op.py
浏览文件 @
b3f9e5e0
...
...
@@ -14,8 +14,8 @@
import
unittest
import
numpy
as
np
from
op_test
import
OpTest
import
math
from
op_test
import
OpTest
def
find_latest_set
(
num
):
...
...
@@ -37,40 +37,36 @@ class CodeTable(object):
def
hsigmoid
(
x
,
w
,
ids
,
bias
,
num_classes
):
# code length =
# initialize pre out with dims={batch_size, code_length}
global
pre_output
batch_size
=
x
.
shape
[
0
]
code_length
=
find_latest_set
(
num_classes
-
1
)
code_table
=
[
0
for
_
in
range
(
code_length
)]
pre_output
=
np
.
zeros
((
batch_size
,
code_length
))
pre_sum
=
np
.
zeros
((
batch_size
,
1
))
out
=
np
.
zeros
((
batch_size
,
1
)).
astype
(
"float32"
)
# pre_out += code(bias)
for
i
in
xrange
(
batch_size
):
for
i
in
range
(
batch_size
):
code_table
=
CodeTable
(
num_classes
,
ids
[
i
])
length
=
code_table
.
get_length
()
for
j
in
x
range
(
length
):
for
j
in
range
(
length
):
idx
=
code_table
.
cal_index
(
j
)
pre_output
[
i
][
j
]
+=
bias
[
0
][
idx
]
# pre_out += code(w) * x
for
i
in
xrange
(
batch_size
):
for
j
in
xrange
(
batch_size
):
for
j
in
range
(
batch_size
):
code_table
=
CodeTable
(
num_classes
,
ids
[
j
])
length
=
code_table
.
get_length
()
for
k
in
x
range
(
length
):
for
k
in
range
(
length
):
idx
=
code_table
.
cal_index
(
k
)
sum
=
0.0
for
l
in
x
range
(
x
.
shape
[
1
]):
sum
+=
w
[
i
]
[
idx
][
l
]
*
x
[
j
][
l
]
for
l
in
range
(
x
.
shape
[
1
]):
sum
+=
w
[
idx
][
l
]
*
x
[
j
][
l
]
pre_output
[
j
][
k
]
+=
sum
# clip[-40.0, 40.0]
np
.
clip
(
pre_output
,
-
40.0
,
40.0
)
# out(i, 0) = \sum_j bit(i, j) * preout(i, j)
for
i
in
x
range
(
batch_size
):
for
i
in
range
(
batch_size
):
code_table
=
CodeTable
(
num_classes
,
ids
[
i
])
length
=
code_table
.
get_length
()
sum
=
0.0
for
j
in
x
range
(
length
):
for
j
in
range
(
length
):
if
code_table
.
cal_bit
(
j
):
sum
+=
pre_output
[
i
][
j
]
out
[
i
]
=
-
1.0
*
sum
...
...
@@ -85,24 +81,23 @@ def hsigmoid(x, w, ids, bias, num_classes):
class
TestHSigmoidOp
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"hierarchical_sigmoid"
num_classes
=
6
embded_size
=
1
0
batch_size
=
5
num_classes
=
4
embded_size
=
1
batch_size
=
1
x
=
np
.
random
.
random
((
batch_size
,
embded_size
)).
astype
(
"float32"
)
w
=
np
.
random
.
random
(
(
batch_size
,
num_classes
-
1
,
embded_size
)).
astype
(
"float32"
)
w
=
np
.
random
.
random
((
num_classes
-
1
,
embded_size
)).
astype
(
"float32"
)
ids
=
np
.
random
.
randint
(
0
,
num_classes
,
batch_size
)
bias
=
np
.
random
.
random
((
1
,
num_classes
-
1
)).
astype
(
"float32"
)
self
.
inputs
=
{
'X'
:
x
,
'W'
:
w
,
'Ids'
:
ids
,
'Bias'
:
bias
}
self
.
attrs
=
{
'num_classes'
:
num_classes
}
self
.
inputs
=
{
'X'
:
x
,
'W'
:
w
,
'Ids'
:
ids
,
'Bias'
:
bias
}
out
=
hsigmoid
(
x
,
w
,
ids
,
bias
,
num_classes
)
self
.
outputs
=
{
'Out'
:
out
}
self
.
outputs
=
{
'
PreOut'
:
pre_output
,
'
Out'
:
out
}
def
test_check_output
(
self
):
self
.
check_output
()
def
test_check_grad
(
self
):
self
.
check_grad
([
'
X'
,
'W'
,
'Bias
'
],
'Out'
,
no_grad_set
=
set
(
'Ids'
))
self
.
check_grad
([
'
Bias'
,
'X'
,
'W
'
],
'Out'
,
no_grad_set
=
set
(
'Ids'
))
if
__name__
==
'__main__'
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录