Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
10779460
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
10779460
编写于
1月 10, 2018
作者:
Y
Yibing Liu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Simplify calc in test_sequence_erase_op
上级
7b9d5b32
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
30 addition
and
45 deletion
+30
-45
paddle/operators/sequence_erase_op.cc
paddle/operators/sequence_erase_op.cc
+1
-1
paddle/operators/sequence_erase_op.cu
paddle/operators/sequence_erase_op.cu
+2
-0
paddle/operators/sequence_erase_op.h
paddle/operators/sequence_erase_op.h
+15
-10
python/paddle/v2/fluid/tests/test_sequence_erase_op.py
python/paddle/v2/fluid/tests/test_sequence_erase_op.py
+12
-34
未找到文件。
paddle/operators/sequence_erase_op.cc
浏览文件 @
10779460
...
...
@@ -50,7 +50,7 @@ class SequenceEraseOpMaker : public framework::OpProtoAndCheckerMaker {
AddComment
(
R"DOC(
Sequence Erase Operator.
Sequence erase operator erases tokens specified by Attr(tokens)
in
the input
Sequence erase operator erases tokens specified by Attr(tokens)
from
the input
sequences Input(X), and outputs the remaining data and modifies the LoD
information at the same time. For example, given a 2-D LoDTensor
...
...
paddle/operators/sequence_erase_op.cu
浏览文件 @
10779460
...
...
@@ -70,6 +70,8 @@ class SequenceEraseOpCUDAKernel : public framework::OpKernel<T> {
auto
lod
=
in
->
lod
();
PADDLE_ENFORCE_EQ
(
lod
.
size
(),
1UL
,
"Only support one level sequence now."
);
PADDLE_ENFORCE_EQ
(
lod
[
0
].
back
(),
(
size_t
)
in
->
numel
(),
"The actual size mismatches with the LoD information."
);
auto
tokens
=
ctx
.
Attr
<
std
::
vector
<
T
>>
(
"tokens"
);
auto
tokens_len
=
tokens
.
size
();
auto
in_len
=
in
->
numel
();
...
...
paddle/operators/sequence_erase_op.h
浏览文件 @
10779460
...
...
@@ -28,22 +28,27 @@ class SequenceEraseKernel : public framework::OpKernel<T> {
auto
lod
=
in
->
lod
();
PADDLE_ENFORCE_EQ
(
lod
.
size
(),
1UL
,
"Only support one level sequence now."
);
PADDLE_ENFORCE_EQ
(
lod
[
0
].
back
(),
(
size_t
)
in
->
numel
(),
"The actual size mismatches with the LoD information."
);
auto
tokens
=
ctx
.
Attr
<
std
::
vector
<
int
>>
(
"tokens"
);
auto
in_len
=
in
->
numel
();
auto
in_dat
=
in
->
data
<
T
>
();
auto
lod0
=
lod
[
0
];
std
::
vector
<
size_t
>
num_erased
(
in_len
+
1
,
0
);
for
(
int64_t
i
=
1
;
i
<
in_len
+
1
;
++
i
)
{
num_erased
[
i
]
=
num_erased
[
i
-
1
];
if
(
std
::
find
(
tokens
.
begin
(),
tokens
.
end
(),
in_dat
[
i
-
1
])
!=
tokens
.
end
())
{
num_erased
[
i
]
+=
1
;
std
::
vector
<
size_t
>
out_lod0
(
1
,
0
);
for
(
size_t
i
=
0
;
i
<
lod0
.
size
()
-
1
;
++
i
)
{
size_t
num_out
=
0
;
for
(
auto
j
=
lod0
[
i
]
+
1
;
j
<=
lod0
[
i
+
1
];
++
j
)
{
num_erased
[
j
]
=
num_erased
[
j
-
1
];
if
(
std
::
find
(
tokens
.
begin
(),
tokens
.
end
(),
in_dat
[
j
-
1
])
!=
tokens
.
end
())
{
num_erased
[
j
]
+=
1
;
}
else
{
num_out
+=
1
;
}
}
}
std
::
vector
<
size_t
>
out_lod0
(
lod0
.
size
(),
0
);
for
(
size_t
i
=
1
;
i
<
lod0
.
size
();
++
i
)
{
out_lod0
[
i
]
=
lod0
[
i
]
-
num_erased
[
lod0
[
i
]];
out_lod0
.
push_back
(
out_lod0
.
back
()
+
num_out
);
}
auto
out_len
=
in_len
-
num_erased
[
in_len
];
...
...
python/paddle/v2/fluid/tests/test_sequence_erase_op.py
浏览文件 @
10779460
...
...
@@ -4,32 +4,23 @@ from op_test import OpTest
def
sequence_erase
(
in_seq
,
lod0
,
tokens
):
# num_erased[i]: the number of elments to be removed before #i elements
num_erased
=
[
0
]
*
(
len
(
in_seq
)
+
1
)
for
i
in
range
(
1
,
len
(
in_seq
)
+
1
):
num_erased
[
i
]
=
num_erased
[
i
-
1
]
if
in_seq
[
i
-
1
]
in
tokens
:
num_erased
[
i
]
+=
1
# recalculate lod information
new_lod0
=
[
0
]
*
len
(
lod0
)
for
i
in
range
(
1
,
len
(
lod0
)):
new_lod0
[
i
]
=
lod0
[
i
]
-
num_erased
[
lod0
[
i
]]
out_seq
=
np
.
zeros
(
(
len
(
in_seq
)
-
num_erased
[
len
(
in_seq
)],
1
)).
astype
(
"int32"
)
for
i
in
range
(
0
,
len
(
in_seq
)):
if
num_erased
[
i
]
==
num_erased
[
i
+
1
]:
out_seq
[
i
-
num_erased
[
i
]]
=
in_seq
[
i
]
# else in_seq[i] needs to be removed
return
out_seq
,
new_lod0
new_lod0
=
[
0
]
out_seq
=
[]
for
i
in
range
(
0
,
len
(
lod0
)
-
1
):
num_out
=
0
for
dat
in
in_seq
[
lod0
[
i
]:
lod0
[
i
+
1
]]:
if
dat
not
in
tokens
:
out_seq
.
append
(
dat
)
num_out
+=
1
new_lod0
.
append
(
new_lod0
[
-
1
]
+
num_out
)
return
np
.
array
(
out_seq
).
astype
(
"int32"
),
new_lod0
class
TestSequenceEraseOp
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"sequence_erase"
in_seq
=
np
.
random
.
randint
(
0
,
10
,
(
1
0
,
1
)).
astype
(
"int32"
)
lod
=
[[
0
,
3
,
6
,
1
0
]]
in_seq
=
np
.
random
.
randint
(
0
,
10
,
(
3
0
,
1
)).
astype
(
"int32"
)
lod
=
[[
0
,
9
,
13
,
24
,
3
0
]]
tokens
=
[
2
,
3
,
5
]
out_seq
,
new_lod0
=
sequence_erase
(
in_seq
,
lod
[
0
],
tokens
)
self
.
attrs
=
{
'tokens'
:
tokens
}
...
...
@@ -41,17 +32,4 @@ class TestSequenceEraseOp(OpTest):
if
__name__
==
'__main__'
:
"""
in_seq = np.random.randint(0, 10, (30, 1)).astype("int32")
lod0 = [0, 5, 15, 30]
tokens = [2, 5]
out_seq, new_lod = sequence_erase(in_seq, lod0, tokens)
print lod0, new_lod
print("compare")
for i in range(0, len(lod0)-1):
print(np.transpose(in_seq[lod0[i] : lod0[i+1]]))
print(np.transpose(out_seq[new_lod[i] : new_lod[i+1]]))
print("
\n
")
"""
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录