Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
狗剩和翠花
Paddle
提交
5e20a8ef
P
Paddle
项目概览
狗剩和翠花
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
2
Star
0
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
5e20a8ef
编写于
6月 08, 2018
作者:
G
guosheng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Make python unit test of beam_search_op and beam_searc_decode_op run correctly
上级
a281e101
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
63 addition
and
35 deletion
+63
-35
paddle/fluid/operators/beam_search_decode_op_test.cc
paddle/fluid/operators/beam_search_decode_op_test.cc
+1
-1
paddle/fluid/operators/beam_search_op_test.cc
paddle/fluid/operators/beam_search_op_test.cc
+1
-1
python/paddle/fluid/tests/unittests/test_beam_search_decode_op.py
...addle/fluid/tests/unittests/test_beam_search_decode_op.py
+42
-28
python/paddle/fluid/tests/unittests/test_beam_search_op.py
python/paddle/fluid/tests/unittests/test_beam_search_op.py
+19
-5
未找到文件。
paddle/fluid/operators/beam_search_decode_op_test.cc
浏览文件 @
5e20a8ef
...
...
@@ -76,7 +76,7 @@ void GenerateExample(const std::vector<size_t>& level_0,
TEST
(
BeamSearchDecodeOp
,
Backtrace
)
{
CPUPlace
place
;
//
we will constuct a sample data with 4
steps and 2 source sentences
//
Construct sample data with 5
steps and 2 source sentences
// beam_size = 2, start_id = 0, end_id = 1
LoDTensorArray
ids
;
LoDTensorArray
scores
;
...
...
paddle/fluid/operators/beam_search_op_test.cc
浏览文件 @
5e20a8ef
...
...
@@ -67,7 +67,7 @@ TEST(beam_search_op, run) {
LoDTensor
pre_scores
;
pre_scores
.
Resize
(
framework
::
make_ddim
(
vector
<
int64_t
>
(
4
,
1
)));
for
(
int
i
=
0
;
i
<
4
;
i
++
)
{
pre_scores
.
mutable_data
<
float
>
(
place
)[
i
]
=
0.1
;
pre_scores
.
mutable_data
<
float
>
(
place
)[
i
]
=
0.1
*
(
i
+
1
)
;
}
BeamSearch
beamsearch
(
ids
,
scores
,
(
size_t
)
0
,
(
size_t
)
2
,
0
);
...
...
python/paddle/fluid/tests/unittests/test_beam_search_decode_op.py
浏览文件 @
5e20a8ef
...
...
@@ -32,32 +32,44 @@ class TestBeamSearchDecodeOp(unittest.TestCase):
def
test_get_set
(
self
):
ids
=
self
.
scope
.
var
(
"ids"
).
get_lod_tensor_array
()
self
.
append_lod_tensor
(
ids
,
[[
0
,
3
,
6
],
[
0
,
1
,
2
,
3
,
4
,
5
,
6
]],
np
.
array
(
[
1
,
2
,
3
,
4
,
5
,
6
],
dtype
=
"int64"
))
self
.
append_lod_tensor
(
ids
,
[[
0
,
3
,
6
],
[
0
,
1
,
1
,
3
,
5
,
5
,
6
]],
np
.
array
(
[
0
,
1
,
2
,
3
,
4
,
5
],
dtype
=
"int64"
))
self
.
append_lod_tensor
(
ids
,
[[
0
,
3
,
6
],
[
0
,
0
,
1
,
2
,
3
,
4
,
5
]],
np
.
array
(
[
0
,
1
,
2
,
3
,
4
],
dtype
=
"int64"
))
scores
=
self
.
scope
.
var
(
"scores"
).
get_lod_tensor_array
()
self
.
append_lod_tensor
(
scores
,
[[
0
,
3
,
6
],
[
0
,
1
,
2
,
3
,
4
,
5
,
6
]],
np
.
array
(
[
1
,
2
,
3
,
4
,
5
,
6
],
dtype
=
"float64"
))
self
.
append_lod_tensor
(
scores
,
[[
0
,
3
,
6
],
[
0
,
1
,
1
,
3
,
5
,
5
,
6
]],
np
.
array
(
[
0
,
1
,
2
,
3
,
4
,
5
],
dtype
=
"float64"
))
self
.
append_lod_tensor
(
scores
,
[[
0
,
3
,
6
],
[
0
,
0
,
1
,
2
,
3
,
4
,
5
]],
np
.
array
(
[
0
,
1
,
2
,
3
,
4
],
dtype
=
"float64"
))
# Construct sample data with 5 steps and 2 source sentences
# beam_size = 2, end_id = 1
# start with start_id
[
self
.
append_lod_tensor
(
array
,
[[
0
,
1
,
2
],
[
0
,
1
,
2
]],
np
.
array
(
[
0
,
0
],
dtype
=
dtype
))
for
array
,
dtype
in
((
ids
,
"int64"
),
(
scores
,
"float32"
))
]
[
self
.
append_lod_tensor
(
array
,
[[
0
,
1
,
2
],
[
0
,
2
,
4
]],
np
.
array
(
[
2
,
3
,
4
,
5
],
dtype
=
dtype
))
for
array
,
dtype
in
((
ids
,
"int64"
),
(
scores
,
"float32"
))
]
[
self
.
append_lod_tensor
(
array
,
[[
0
,
2
,
4
],
[
0
,
2
,
2
,
4
,
4
]],
np
.
array
(
[
3
,
1
,
5
,
4
],
dtype
=
dtype
))
for
array
,
dtype
in
((
ids
,
"int64"
),
(
scores
,
"float32"
))
]
[
self
.
append_lod_tensor
(
array
,
[[
0
,
2
,
4
],
[
0
,
1
,
2
,
3
,
4
]],
np
.
array
(
[
1
,
1
,
3
,
5
],
dtype
=
dtype
))
for
array
,
dtype
in
((
ids
,
"int64"
),
(
scores
,
"float32"
))
]
[
self
.
append_lod_tensor
(
array
,
[[
0
,
2
,
4
],
[
0
,
0
,
0
,
2
,
2
]],
np
.
array
(
[
5
,
1
],
dtype
=
dtype
))
for
array
,
dtype
in
((
ids
,
"int64"
),
(
scores
,
"float32"
))
]
sentence_ids
=
self
.
scope
.
var
(
"sentence_ids"
).
get_tensor
()
sentence_scores
=
self
.
scope
.
var
(
"sentence_scores"
).
get_tensor
()
...
...
@@ -69,16 +81,18 @@ class TestBeamSearchDecodeOp(unittest.TestCase):
Scores
=
"scores"
,
# outputs
SentenceIds
=
"sentence_ids"
,
SentenceScores
=
"sentence_scores"
)
SentenceScores
=
"sentence_scores"
,
beam_size
=
2
,
end_id
=
1
,
)
beam_search_decode_op
.
run
(
self
.
scope
,
self
.
place
)
expected_lod
=
[[
0
,
4
,
8
],
[
0
,
1
,
3
,
6
,
9
,
10
,
13
,
16
,
19
]]
expected_lod
=
[[
0
,
2
,
4
],
[
0
,
4
,
7
,
12
,
17
]]
self
.
assertEqual
(
sentence_ids
.
lod
(),
expected_lod
)
self
.
assertEqual
(
sentence_scores
.
lod
(),
expected_lod
)
expected_data
=
np
.
array
(
[
2
,
1
,
0
,
3
,
1
,
0
,
3
,
2
,
1
,
5
,
4
,
3
,
2
,
4
,
4
,
3
,
6
,
5
,
4
],
"int64"
)
[
0
,
2
,
3
,
1
,
0
,
2
,
1
,
0
,
4
,
5
,
3
,
5
,
0
,
4
,
5
,
3
,
1
],
"int64"
)
self
.
assertTrue
(
np
.
array_equal
(
np
.
array
(
sentence_ids
),
expected_data
))
self
.
assertTrue
(
np
.
array_equal
(
np
.
array
(
sentence_scores
),
expected_data
))
...
...
python/paddle/fluid/tests/unittests/test_beam_search_op.py
浏览文件 @
5e20a8ef
...
...
@@ -29,6 +29,7 @@ class BeamSearchOpTester(unittest.TestCase):
def
setUp
(
self
):
self
.
scope
=
core
.
Scope
()
self
.
_create_ids
()
self
.
_create_pre_scores
()
self
.
_create_scores
()
self
.
_create_pre_ids
()
self
.
scope
.
var
(
'selected_ids'
)
...
...
@@ -37,7 +38,8 @@ class BeamSearchOpTester(unittest.TestCase):
def
test_run
(
self
):
op
=
Operator
(
'beam_search'
,
pre_ids
=
"pre_ids"
,
pre_ids
=
'pre_ids'
,
pre_scores
=
'pre_scores'
,
ids
=
'ids'
,
scores
=
'scores'
,
selected_ids
=
'selected_ids'
,
...
...
@@ -47,15 +49,27 @@ class BeamSearchOpTester(unittest.TestCase):
end_id
=
0
,
)
op
.
run
(
self
.
scope
,
core
.
CPUPlace
())
selected_ids
=
self
.
scope
.
find_var
(
"selected_ids"
).
get_tensor
()
print
'selected_ids'
,
np
.
array
(
selected_ids
)
print
'lod'
,
selected_ids
.
lod
()
selected_scores
=
self
.
scope
.
find_var
(
"selected_scores"
).
get_tensor
()
self
.
assertTrue
(
np
.
allclose
(
np
.
array
(
selected_ids
),
np
.
array
([
4
,
2
,
3
,
8
])[:,
np
.
newaxis
]))
self
.
assertTrue
(
np
.
allclose
(
np
.
array
(
selected_scores
),
np
.
array
([
0.5
,
0.6
,
0.9
,
0.7
])[:,
np
.
newaxis
]))
self
.
assertEqual
(
selected_ids
.
lod
(),
[[
0L
,
2L
,
4L
],
[
0L
,
1L
,
2L
,
3L
,
4L
]])
def
_create_pre_ids
(
self
):
np_data
=
np
.
array
([[
1
,
2
,
3
,
4
]],
dtype
=
'int64'
)
tensor
=
create_tensor
(
self
.
scope
,
"pre_ids"
,
np_data
)
tensor
=
create_tensor
(
self
.
scope
,
'pre_ids'
,
np_data
)
def
_create_pre_scores
(
self
):
np_data
=
np
.
array
([[
0.1
,
0.2
,
0.3
,
0.4
]],
dtype
=
'float32'
)
tensor
=
create_tensor
(
self
.
scope
,
'pre_scores'
,
np_data
)
def
_create_ids
(
self
):
self
.
lod
=
[[
0
,
1
,
4
],
[
0
,
1
,
2
,
3
,
4
]]
self
.
lod
=
[[
0
,
2
,
4
],
[
0
,
1
,
2
,
3
,
4
]]
np_data
=
np
.
array
(
[[
4
,
2
,
5
],
[
2
,
1
,
3
],
[
3
,
5
,
2
],
[
8
,
2
,
1
]],
dtype
=
'int64'
)
tensor
=
create_tensor
(
self
.
scope
,
"ids"
,
np_data
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录