Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
41790f13
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看板
提交
41790f13
编写于
12月 18, 2018
作者:
T
tangwei12
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add ut about nce
上级
e196fa36
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
33 addition
and
119 deletion
+33
-119
python/paddle/fluid/tests/unittests/test_nce_remote_table_op.py
.../paddle/fluid/tests/unittests/test_nce_remote_table_op.py
+33
-119
未找到文件。
python/paddle/fluid/tests/unittests/test_nce_remote_table_op.py
浏览文件 @
41790f13
...
...
@@ -88,158 +88,73 @@ class TestListenAndServOp(unittest.TestCase):
port
=
int
(
f
.
read
().
strip
())
return
port
def
_run_nce_op_
one_pserver
(
self
,
place
,
port
):
def
_run_nce_op_
two_pserver
(
self
,
place
,
port0
,
port1
):
scope
=
fluid
.
core
.
Scope
()
program
=
Program
()
with
fluid
.
scope_guard
(
scope
):
with
program_guard
(
program
,
startup_program
=
Program
()):
x
=
scope
.
var
(
'
X
'
).
get_tensor
()
x
=
scope
.
var
(
'
Input
'
).
get_tensor
()
x_array
=
np
.
random
.
random
((
4
,
8
)).
astype
(
"float32"
)
*
2
x
.
set
(
x_array
,
place
)
# create and initialize Param Variable
param
=
scope
.
var
(
'W'
).
get_tensor
()
param
=
scope
.
var
(
'W
eight
'
).
get_tensor
()
param_array
=
np
.
zeros
((
5
,
8
)).
astype
(
"float32"
)
*
2
param
.
set
(
param_array
,
place
)
path_table
=
scope
.
var
(
'PathTable'
).
get_tensor
()
path_table_array
=
np
.
array
(
[(
0
,
2
,
-
1
,
-
1
,
-
1
),
(
0
,
1
,
2
,
-
1
,
-
1
),
(
0
,
1
,
4
,
-
1
,
-
1
),
(
0
,
2
,
-
1
,
-
1
,
-
1
)]).
astype
(
"int64"
)
#np.array to store 1,2,5,6s' non-leaf path(root -> leaf)
path_table
.
set
(
path_table_array
,
place
)
path_code
=
scope
.
var
(
'PathCode'
).
get_tensor
()
path_code_array
=
np
.
array
(
[(
0
,
0
,
-
1
,
-
1
,
-
1
),
(
1
,
1
,
1
,
-
1
,
-
1
),
(
1
,
0
,
0
,
-
1
,
-
1
),
(
0
,
1
,
-
1
,
-
1
,
-
1
)]).
astype
(
"int64"
)
#np.array to store
path_code
.
set
(
path_code_array
,
place
)
label
=
scope
.
var
(
'Label'
).
get_tensor
()
label_array
=
np
.
array
([
0
,
1
,
4
,
5
])
label
.
set
(
label_array
,
place
)
bias
=
scope
.
var
(
'Bias'
).
get_tensor
()
bias_array
=
np
.
random
.
random
((
5
,
1
)).
astype
(
"float32"
)
bias
.
set
(
bias_array
,
place
)
out
=
scope
.
var
(
'Out'
).
get_tensor
()
pre_out
=
scope
.
var
(
'PreOut'
).
get_tensor
w_out
=
scope
.
var
(
'W_Out'
).
get_tensor
()
w_out
.
set
(
param_array
,
place
)
emaps
=
[
'127.0.0.1:'
+
str
(
port
)]
table_names
=
[
'table'
]
height_sections
=
[
2
]
# create and run sgd operator
hsigmoid_op
=
Operator
(
"hierarchical_sigmoid"
,
X
=
'X'
,
W
=
'W'
,
PathTable
=
'PathTable'
,
PathCode
=
'PathCode'
,
Label
=
'Label'
,
Bias
=
'Bias'
,
Out
=
'Out'
,
PreOut
=
'PreOut'
,
W_Out
=
'W_Out'
,
remote_prefetch
=
True
,
epmap
=
emaps
,
table_names
=
table_names
,
height_sections
=
height_sections
)
hsigmoid_op
.
run
(
scope
,
place
)
# get and compare result
result_array
=
np
.
array
(
w_out
)
self
.
assertEqual
(
list
(
result_array
.
shape
),
[
5
,
8
])
correct
=
None
for
i
in
range
(
5
):
if
i
!=
3
:
correct
=
np
.
full
((
1
,
8
),
i
+
1
).
astype
(
"float32"
)
self
.
assertTrue
((
result_array
[
i
]
==
correct
).
all
())
else
:
correct
=
np
.
full
((
1
,
8
),
0
).
astype
(
"float32"
)
self
.
assertTrue
((
result_array
[
i
]
==
correct
).
all
())
def
_run_nce_op_two_pserver
(
self
,
place
,
port0
,
port1
):
scope
=
fluid
.
core
.
Scope
()
program
=
Program
()
with
fluid
.
scope_guard
(
scope
):
with
program_guard
(
program
,
startup_program
=
Program
()):
x
=
scope
.
var
(
'X'
).
get_tensor
()
x_array
=
np
.
random
.
random
((
4
,
8
)).
astype
(
"float32"
)
*
2
x
.
set
(
x_array
,
place
)
# create and initialize Param Variable
param
=
scope
.
var
(
'W'
).
get_tensor
()
param_array
=
np
.
zeros
((
5
,
8
)).
astype
(
"float32"
)
*
2
param
.
set
(
param_array
,
place
)
path_table
=
scope
.
var
(
'PathTable'
).
get_tensor
()
path_table_array
=
np
.
array
(
[(
0
,
2
,
-
1
,
-
1
,
-
1
),
(
0
,
1
,
3
,
-
1
,
-
1
),
(
0
,
1
,
4
,
-
1
,
-
1
),
(
0
,
2
,
-
1
,
-
1
,
-
1
)]).
astype
(
"int64"
)
#np.array to store 1,2,5,6s' non-leaf path(root -> leaf)
path_table
.
set
(
path_table_array
,
place
)
path_code
=
scope
.
var
(
'PathCode'
).
get_tensor
()
path_code_array
=
np
.
array
(
[(
0
,
0
,
-
1
,
-
1
,
-
1
),
(
1
,
1
,
1
,
-
1
,
-
1
),
(
1
,
0
,
0
,
-
1
,
-
1
),
(
0
,
1
,
-
1
,
-
1
,
-
1
)]).
astype
(
"int64"
)
#np.array to store
path_code
.
set
(
path_code_array
,
place
)
sample_w
=
scope
.
var
(
'SampleWeight'
).
get_tensor
()
sample_weight
=
np
.
random
.
random
((
4
,
1
)).
astype
(
"float32"
)
sample_w
.
set
(
sample_weight
,
place
)
label
=
scope
.
var
(
'Label'
).
get_tensor
()
label_array
=
np
.
array
([
0
,
1
,
4
,
5
])
label
.
set
(
label_array
,
place
)
bias
=
scope
.
var
(
'Bias
'
).
get_tensor
()
bias_array
=
np
.
random
.
random
((
5
,
1
)).
astype
(
"float32"
)
bias
.
set
(
bias_array
,
place
)
cost
=
scope
.
var
(
'Cost
'
).
get_tensor
()
cost_w
=
np
.
zeros
((
4
,
1
)).
astype
(
"float32"
)
cost
.
set
(
cost_w
,
place
)
out
=
scope
.
var
(
'Out'
).
get_tensor
()
sample_l
=
scope
.
var
(
'SampleLogits'
).
get_tensor
()
sample_l_w
=
np
.
zeros
((
4
,
3
)).
astype
(
"float32"
)
sample_l
.
set
(
sample_l_w
,
place
)
pre_out
=
scope
.
var
(
'PreOut'
).
get_tensor
w_out
=
scope
.
var
(
'W_Out'
).
get_tensor
()
w_out
.
set
(
param_array
,
place
)
sample_la
=
scope
.
var
(
'SampleLabels'
).
get_tensor
()
sample_la_w
=
np
.
zeros
((
4
,
3
)).
astype
(
"float32"
)
sample_la
.
set
(
sample_la_w
,
place
)
emaps
=
[
'127.0.0.1:'
+
str
(
port0
),
'127.0.0.1:'
+
str
(
port1
)]
table_names
=
[
'table'
,
'table'
]
height_sections
=
[
2
,
3
]
# create and run sgd operator
hsigmoid_op
=
Operator
(
"hierarchical_sigmoid"
,
X
=
'X'
,
W
=
'W'
,
PathTable
=
'PathTable'
,
PathCode
=
'PathCode'
,
# create and run nce operator
nce_op
=
Operator
(
"nce"
,
Input
=
'Input'
,
Weight
=
'Weight'
,
Label
=
'Label'
,
Bias
=
'Bias'
,
Out
=
'Out'
,
PreOut
=
'PreOut'
,
W_Out
=
'W_Out'
,
Cost
=
'Cost'
,
SampleLogits
=
'SampleLogits'
,
SampleLabels
=
'SampleLabels'
,
num_total_classes
=
5
,
num_neg_samples
=
2
,
sampler
=
0
,
seed
=
1
,
is_sparse
=
True
,
remote_prefetch
=
True
,
epmap
=
emaps
,
table_names
=
table_names
,
height_sections
=
height_sections
)
hsigmoid_op
.
run
(
scope
,
place
)
nce_op
.
run
(
scope
,
place
)
# get and compare result
result_array
=
np
.
array
(
w_out
)
self
.
assertEqual
(
list
(
result_array
.
shape
),
[
5
,
8
])
correct
=
None
for
i
in
range
(
5
):
if
i
<
2
:
correct
=
np
.
full
((
1
,
8
),
i
+
1
).
astype
(
"float32"
)
self
.
assertTrue
((
result_array
[
i
]
==
correct
).
all
())
else
:
correct
=
np
.
full
((
1
,
8
),
i
+
9
).
astype
(
"float32"
)
self
.
assertTrue
((
result_array
[
i
]
==
correct
).
all
())
o_cost
=
np
.
array
(
cost_w
)
o_logits
=
np
.
array
(
sample_l
)
o_labels
=
np
.
array
(
sample_la
)
def
test_nce_op_remote
(
self
):
os
.
environ
[
'PADDLE_ENABLE_REMOTE_PREFETCH'
]
=
"1"
...
...
@@ -257,7 +172,6 @@ class TestListenAndServOp(unittest.TestCase):
places
.
append
(
core
.
CUDAPlace
(
0
))
for
place
in
places
:
self
.
_run_nce_op_one_pserver
(
place
,
port0
)
self
.
_run_nce_op_two_pserver
(
place
,
port0
,
port1
)
# raise SIGTERM to pserver
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录