Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
e85c5133
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
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看板
提交
e85c5133
编写于
1月 07, 2018
作者:
Y
Yibing Liu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add sequencee erase operator
上级
907e6d04
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
199 addition
and
0 deletion
+199
-0
paddle/operators/sequence_erase_op.cc
paddle/operators/sequence_erase_op.cc
+61
-0
paddle/operators/sequence_erase_op.h
paddle/operators/sequence_erase_op.h
+80
-0
python/paddle/v2/fluid/tests/test_sequence_erase_op.py
python/paddle/v2/fluid/tests/test_sequence_erase_op.py
+58
-0
未找到文件。
paddle/operators/sequence_erase_op.cc
0 → 100644
浏览文件 @
e85c5133
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include "paddle/operators/sequence_erase_op.h"
namespace
paddle
{
namespace
operators
{
class
SequenceEraseOp
:
public
framework
::
OperatorWithKernel
{
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"X"
),
"Input(X) of SequenceEraseOp should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"Out"
),
"Output(Out) of SequenceEraseOp should not be null."
);
ctx
->
SetOutputDim
(
"Out"
,
ctx
->
GetInputDim
(
"X"
));
}
};
class
SequenceEraseOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
SequenceEraseOpMaker
(
OpProto
*
proto
,
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
"(LoDTensor) 2-D input LoDTensor with the 2-nd dimension "
"of length 1."
);
AddOutput
(
"Out"
,
"(LoDTensor) 2-D output LoDTensor with the 2-nd dimension "
"of length 1."
);
AddAttr
<
std
::
vector
<
int
>>
(
"tokens"
,
"(vector<int>) "
"Tokens to be removed from input."
);
AddComment
(
R"DOC(
Sequence Erase Operator.
)DOC"
);
}
};
}
// namespace operators
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_WITHOUT_GRADIENT
(
sequence_erase
,
ops
::
SequenceEraseOp
,
ops
::
SequenceEraseOpMaker
);
REGISTER_OP_CPU_KERNEL
(
sequence_erase
,
ops
::
SequenceEraseKernel
<
paddle
::
platform
::
CPUDeviceContext
,
int32_t
>
);
paddle/operators/sequence_erase_op.h
0 → 100644
浏览文件 @
e85c5133
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#pragma once
#include "paddle/framework/op_registry.h"
#include "paddle/operators/math/softmax.h"
namespace
paddle
{
namespace
operators
{
using
Tensor
=
framework
::
Tensor
;
using
LoDTensor
=
framework
::
LoDTensor
;
template
<
typename
DeviceContext
,
typename
T
>
class
SequenceEraseKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
auto
*
in
=
ctx
.
Input
<
LoDTensor
>
(
"X"
);
auto
*
out
=
ctx
.
Output
<
LoDTensor
>
(
"Out"
);
auto
lod
=
in
->
lod
();
PADDLE_ENFORCE_EQ
(
lod
.
size
(),
1UL
,
"Only support one level sequence now."
);
// auto dims = x->dims();
/*
const size_t level = lod.size() - 1;
PADDLE_ENFORCE_EQ(dims[0], static_cast<int64_t>(lod[level].back()),
"The first dimension of Input(X) should be equal to the "
"sum of all sequences' lengths.");
PADDLE_ENFORCE_EQ(dims[0], x->numel(),
"The width of each timestep in Input(X) of "
"SequenceEraseOp should be 1.");
out->mutable_data<T>(ctx.GetPlace());
*/
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
(
lod0
.
size
(),
0
);
for
(
size_t
i
=
1
;
i
<
lod0
.
size
();
++
i
)
{
out_lod0
[
i
]
=
lod0
[
i
]
-
num_erased
[
lod0
[
i
]];
}
auto
out_len
=
in_len
-
num_erased
[
in_len
];
out
->
Resize
({
static_cast
<
int64_t
>
(
out_len
),
1
});
auto
out_dat
=
out
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
for
(
size_t
i
=
0
;
i
<
in_len
;
++
i
)
{
if
(
num_erased
[
i
]
==
num_erased
[
i
+
1
])
{
out_dat
[
i
-
num_erased
[
i
]]
=
in_dat
[
i
];
}
}
framework
::
LoD
out_lod
;
out_lod
.
push_back
(
out_lod0
);
out
->
set_lod
(
out_lod
);
}
};
}
// namespace operators
}
// namespace paddle
python/paddle/v2/fluid/tests/test_sequence_erase_op.py
0 → 100644
浏览文件 @
e85c5133
import
unittest
import
numpy
as
np
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
class
TestSequenceEraseOp
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"sequence_erase"
in_seq
=
np
.
random
.
randint
(
0
,
10
,
(
30
,
1
)).
astype
(
"int32"
)
lod
=
[[
0
,
5
,
15
,
30
]]
tokens
=
[
2
,
5
]
out_seq
,
new_lod0
=
sequence_erase
(
in_seq
,
lod
[
0
],
tokens
)
self
.
attrs
=
{
'tokens'
:
tokens
}
self
.
inputs
=
{
'X'
:
(
in_seq
,
lod
)}
self
.
outputs
=
{
'Out'
:
(
out_seq
,
[
new_lod0
])}
def
test_check_output
(
self
):
self
.
check_output
()
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录