Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
d211b51b
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看板
提交
d211b51b
编写于
10月 10, 2017
作者:
Y
Yancey1989
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update comment
上级
0028459b
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
67 addition
and
50 deletion
+67
-50
paddle/operators/sequence_concat_op.cc
paddle/operators/sequence_concat_op.cc
+29
-20
paddle/operators/sequence_concat_op.h
paddle/operators/sequence_concat_op.h
+8
-20
python/paddle/v2/framework/tests/test_seq_concat_op.py
python/paddle/v2/framework/tests/test_seq_concat_op.py
+30
-10
未找到文件。
paddle/operators/sequence_concat_op.cc
浏览文件 @
d211b51b
...
...
@@ -48,11 +48,11 @@ class SequenceConcatOpMaker : public framework::OpProtoAndCheckerMaker {
framework
::
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
"
The input Multip LoDTensors, which are variable-length
"
"sequence or nested sequence."
)
"
(A vector of LoDTensor), the input is a vector of LoDTensor,
"
"
each of which is a variable-length
sequence or nested sequence."
)
.
AsDuplicable
();
AddOutput
(
"Out"
,
"
A LoDTensor
, the variable-length output of "
"
(A LoDTensor)
, the variable-length output of "
"sequence_concat Op."
);
AddAttr
<
int
>
(
"axis"
,
"(int, default 0)"
...
...
@@ -61,27 +61,36 @@ class SequenceConcatOpMaker : public framework::OpProtoAndCheckerMaker {
.
SetDefault
(
0
);
AddAttr
<
int
>
(
"level"
,
"(int, default 0)"
"The level which the inputs will be joined with."
"If level is 0, the inputs will be joined with "
"nested sequences."
"If level is 1, the inputs will be joined with sequences."
)
"The level at which the inputs will be joined."
"If the level is 0, the inputs will be joined at the nested "
"sequence level."
"If the level is 1, the inputs will be joined at the "
"sequence level."
)
.
SetDefault
(
0
);
AddComment
(
R"DOC(
The sequence_concat operator concatenates multiple LoDTensors.
It only supports sequence
s ( LoD Tensor with level=
1)
or
nested sequences (LoD tensor with level=0) as its inputs
.
It only supports sequence
(LoD Tensor with level number is
1)
or
a nested sequence (LoD tensor with level number is 2) as its input
.
- Case1:
If the axis is 1, level is 1, the LoD of Inputs are the same,
LoD(x0) = {{0,2,4},{0,1,2,3,4}}; Dims(x0) = (2,3,4)
LoD(x1) = {{0,2,4},{0,1,2,3,4}}; Dims(x1) = (2,4,4)
LoD(Out) = {{0,2,4},{0,1,2,3,4}}; Dims(Out) = (2,7,4)
If the axis is other than 0(here, axis is 1 and level is 1),
each input should have the same LoD information and the LoD
information of the output keeps the same as the input.
LoD(x0) = {{0,2,4}, {0,1,2,3,4}}; Dims(x0) = (4,3,4)
LoD(x1) = {{0,2,4}, {0,1,2,3,4}}; Dims(x1) = (4,4,4)
LoD(Out) = {{0,2,4}, {0,1,2,3,4}}; Dims(Out) = (4,7,4)
- Case2:
If the axis is 0, level is 1, the LoD of inputs are different,
LoD(x0) = {{0,2,4}, {0,1,2,3,4}}; Dims(x0) = (2,3,4)
LoD(x1) = {{0,3,5}, {0,1,3,4,5}}; Dims(x1) = (3,3,4)
LoD(Out) = {{0,5,9}, {0,1,2,4,5,6,7,8,9}}; Dims(Out) = (5,3,4)
If the axis is 0(here, leve is 0), the inputs are concatenated along
time steps, the LoD information of the output need to re-compute.
LoD(x0) = {{0,2,4}, {0,1,2,3,4}}; Dims(x0) = (4,3,4)
LoD(x1) = {{0,3,5}, {0,1,2,3,5}}; Dims(x1) = (5,3,4)
LoD(Out) = {{0,5,9}, {0,1,2,3,4,5,6,7,9}}; Dims(Out) = (9,3,4)
- Case3:
If the axis is 0(here, level is 1).
LoD(x0) = {{0,2,4}, {0,1,2,3,4}}; Dims(x0) = (4,3,4)
LoD(x1) = {{0,3,5}, {0,1,3,4,5}}; Dims(x1) = (5,3,4)
LoD(Out) = {{0,5,9}, {0,2,5,7,9}}; Dims(Out) = (9,3,4)
NOTE: The level of all the inputs should be the same.
NOTE: The level
s
of all the inputs should be the same.
)DOC"
);
}
};
...
...
@@ -95,7 +104,7 @@ class SequenceConcatGradOp : public framework::OperatorWithKernel {
PADDLE_ENFORCE
(
ctx
->
HasInput
(
framework
::
GradVarName
(
"Out"
)),
"The gradient of Out should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasOutputs
(
framework
::
GradVarName
(
"X"
)),
"The gradient of X should not be
empty
."
);
"The gradient of X should not be
null
."
);
ctx
->
SetOutputsDim
(
framework
::
GradVarName
(
"X"
),
ctx
->
GetInputsDim
(
"X"
));
}
};
...
...
paddle/operators/sequence_concat_op.h
浏览文件 @
d211b51b
...
...
@@ -23,35 +23,22 @@ using Tensor = framework::Tensor;
using
LoDTensor
=
framework
::
LoDTensor
;
using
LoD
=
framework
::
LoD
;
// Concat LoD, the initialized LoD of Output is lod(x0),
// if axis is not 0, the LoD(Out) will be the same as Inputs, if axis is 0:
// Case1:
// There is one level, the Output LoD will be modified:
// LoD(x0) = {{0,2,4}}
// LoD(x1) = {{0,1,5}}
// LoD(Out) = {{0,3,9}}
// Case2:
// There is two level, and concat level is 1,
// the Output LoD will be modified as followed:
// LoD(x0) = {{0,2,4}, {0,1,2,3,4}}
// LoD(x1) = {{0,3,5}, {0,1,3,4,5}}
// LoD(Out) = {{0,5,9}, {0,1,2,4,5,6,7,8,9}}
template
<
typename
T
>
LoD
concatLoD
(
const
std
::
vector
<
const
T
*>
ins
,
const
size_t
axis
,
const
size_t
level
)
{
auto
out_lod
=
ins
[
0
]
->
lod
();
const
size_t
n
=
ins
.
size
();
if
(
axis
==
0UL
)
{
if
(
level
==
0
)
{
if
(
level
==
0
UL
)
{
for
(
size_t
i
=
1
;
i
<
n
;
++
i
)
{
for
(
size_t
j
=
0
;
j
<
ins
[
i
]
->
lod
()[
0
].
size
();
++
j
)
{
out_lod
[
0
][
j
]
+=
ins
[
i
]
->
lod
()[
0
][
j
];
}
}
}
else
if
(
level
==
1
)
{
}
else
if
(
level
==
1
UL
)
{
PADDLE_ENFORCE_EQ
(
ins
[
0
]
->
NumLevels
(),
2UL
,
"If the level is 1, all of the inputs "
"should be the
the
nested sequence."
);
"should be the nested sequence."
);
for
(
size_t
i
=
1
;
i
<
n
;
++
i
)
{
for
(
size_t
j
=
0
;
j
<
ins
[
i
]
->
lod
()[
0
].
size
();
++
j
)
{
out_lod
[
0
].
push_back
(
ins
[
i
]
->
lod
()[
0
][
j
]);
...
...
@@ -80,16 +67,17 @@ class SequenceConcatOpKernel : public framework::OpKernel<T> {
"The level number of all the input LoDTensors "
"should be the same."
);
PADDLE_ENFORCE_EQ
(
ins
[
0
]
->
dims
().
size
(),
ins
[
i
]
->
dims
().
size
(),
"The dimension
s
size of all the input LoDTensors "
"The dimension size of all the input LoDTensors "
"should be the same."
);
const
size_t
dims_size
=
ins
[
i
]
->
dims
().
size
();
for
(
size_t
j
=
0
;
j
<
dims_size
;
++
j
)
{
if
(
j
==
axis
)
continue
;
PADDLE_ENFORCE_EQ
(
ins
[
0
]
->
dims
()[
j
],
ins
[
i
]
->
dims
()[
j
],
"The dimensions of all the input LoDTensors "
"except for the specify axis should be "
"matched exactly."
);
"Except for the dimension of the specified "
"axis along which all the inputs are concatenated, "
"dimensions of all the other axises of the input "
"LoDTensors should be the same."
);
}
}
...
...
python/paddle/v2/framework/tests/test_seq_concat_op.py
浏览文件 @
d211b51b
...
...
@@ -6,16 +6,16 @@ from op_test import OpTest
class
TestConcatOp
(
OpTest
):
def
set_data
(
self
):
# two level, batch size is 3
x0
=
np
.
random
.
random
((
11
,
6
,
3
)).
astype
(
'float32'
)
lod0
=
[[
0
,
2
,
5
,
11
],
[
0
,
1
,
2
,
5
,
7
,
11
]]
x1
=
np
.
random
.
random
((
11
,
8
,
3
)).
astype
(
'float32'
)
lod1
=
[[
0
,
2
,
5
,
11
],
[
0
,
1
,
2
,
5
,
7
,
11
]]
x0
=
np
.
random
.
random
((
4
,
6
,
3
)).
astype
(
'float32'
)
lod0
=
[[
0
,
2
,
4
],
[
0
,
1
,
2
,
3
,
4
]]
x1
=
np
.
random
.
random
((
4
,
8
,
3
)).
astype
(
'float32'
)
lod1
=
[[
0
,
2
,
4
],
[
0
,
1
,
2
,
3
,
4
]]
axis
=
1
level
=
1
self
.
inputs
=
{
'X'
:
[(
'x0'
,
(
x0
,
lod0
)),
(
'x1'
,
(
x1
,
lod1
))]}
self
.
attrs
=
{
'axis'
:
axis
,
'level'
:
level
}
outs
=
[]
for
i
in
range
(
5
):
for
i
in
range
(
4
):
sub_x0
=
x0
[
lod0
[
level
][
i
]:
lod0
[
level
][
i
+
1
],
:]
sub_x1
=
x1
[
lod1
[
level
][
i
]:
lod1
[
level
][
i
+
1
],
:]
outs
.
append
(
np
.
concatenate
((
sub_x0
,
sub_x1
),
axis
=
axis
))
...
...
@@ -36,16 +36,36 @@ class TestConcatOp(OpTest):
class
TestConcatOpDiffLod
(
TestConcatOp
):
def
set_data
(
self
):
# two level, batch size is 3
x0
=
np
.
random
.
random
((
12
,
6
,
3
)).
astype
(
'float32'
)
lod0
=
[[
0
,
3
,
9
,
12
],
[
0
,
2
,
3
,
5
,
9
,
12
]]
x1
=
np
.
random
.
random
((
11
,
6
,
3
)).
astype
(
'float32'
)
lod1
=
[[
0
,
2
,
5
,
11
],
[
0
,
1
,
2
,
5
,
7
,
11
]]
x0
=
np
.
random
.
random
((
4
,
6
,
3
)).
astype
(
'float32'
)
lod0
=
[[
0
,
2
,
4
],
[
0
,
1
,
2
,
3
,
4
]]
x1
=
np
.
random
.
random
((
5
,
6
,
3
)).
astype
(
'float32'
)
lod1
=
[[
0
,
3
,
5
],
[
0
,
1
,
2
,
3
,
5
]]
axis
=
0
level
=
1
self
.
inputs
=
{
'X'
:
[(
'x0'
,
(
x0
,
lod0
)),
(
'x1'
,
(
x1
,
lod1
))]}
self
.
attrs
=
{
'axis'
:
axis
,
'level'
:
level
}
outs
=
[]
for
i
in
range
(
5
):
for
i
in
range
(
4
):
sub_x0
=
x0
[
lod0
[
level
][
i
]:
lod0
[
level
][
i
+
1
],
:]
sub_x1
=
x1
[
lod1
[
level
][
i
]:
lod1
[
level
][
i
+
1
],
:]
outs
.
append
(
np
.
concatenate
((
sub_x0
,
sub_x1
),
axis
=
axis
))
self
.
outputs
=
{
'Out'
:
np
.
concatenate
(
outs
,
axis
=
0
)}
class
TestConcatOpLevelZero
(
TestConcatOp
):
def
set_data
(
self
):
# two level, batch size is 3
x0
=
np
.
random
.
random
((
4
,
3
,
4
)).
astype
(
'float32'
)
lod0
=
[[
0
,
2
,
4
],
[
0
,
1
,
2
,
3
,
4
]]
x1
=
np
.
random
.
random
((
5
,
3
,
4
)).
astype
(
'float32'
)
lod1
=
[[
0
,
3
,
5
],
[
0
,
1
,
3
,
4
,
5
]]
axis
=
0
level
=
0
self
.
inputs
=
{
'X'
:
[(
'x0'
,
(
x0
,
lod0
)),
(
'x1'
,
(
x1
,
lod1
))]}
self
.
attrs
=
{
'axis'
:
axis
,
'level'
:
level
}
outs
=
[]
for
i
in
range
(
2
):
sub_x0
=
x0
[
lod0
[
level
][
i
]:
lod0
[
level
][
i
+
1
],
:]
sub_x1
=
x1
[
lod1
[
level
][
i
]:
lod1
[
level
][
i
+
1
],
:]
outs
.
append
(
np
.
concatenate
((
sub_x0
,
sub_x1
),
axis
=
axis
))
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录