Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
0f9858a1
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
大约 1 年 前同步成功
通知
695
Star
11112
Fork
2696
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
184
列表
看板
标记
里程碑
合并请求
40
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
184
Issue
184
列表
看板
标记
里程碑
合并请求
40
合并请求
40
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
0f9858a1
编写于
10月 30, 2017
作者:
C
chengduo
提交者:
GitHub
10月 30, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #5130 from chengduoZH/fix_sequence_conv_op
fix_sequence_conv_op
上级
03136f63
b08ae0b1
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
228 addition
and
151 deletion
+228
-151
paddle/operators/math/context_project.h
paddle/operators/math/context_project.h
+161
-80
paddle/operators/sequence_conv_op.cc
paddle/operators/sequence_conv_op.cc
+36
-30
paddle/operators/sequence_conv_op.h
paddle/operators/sequence_conv_op.h
+27
-37
python/paddle/v2/framework/tests/test_seq_conv.py
python/paddle/v2/framework/tests/test_seq_conv.py
+4
-4
未找到文件。
paddle/operators/math/context_project.h
浏览文件 @
0f9858a1
...
...
@@ -16,36 +16,36 @@ limitations under the License. */
#include "paddle/framework/eigen.h"
#include "paddle/framework/lod_tensor.h"
#include "paddle/framework/tensor.h"
#include "paddle/operators/math/im2col.h"
namespace
paddle
{
namespace
operators
{
namespace
math
{
using
Tensor
=
framework
::
Tensor
;
using
LoDTensor
=
framework
::
LoDTensor
;
template
<
typename
T
,
int
MajorType
=
Eigen
::
RowMajor
,
typename
IndexType
=
Eigen
::
DenseIndex
>
using
EigenMatrix
=
framework
::
EigenMatrix
<
T
,
MajorType
,
IndexType
>
;
/*
* \brief Context projection concatenate
features in adjacent time
steps in
* \brief Context projection concatenate
s features in adjacent time-
steps in
* a sequence. The i-th row of the output is the concatenation of
* context_length rows of the input. The context_length rows are the
* consecutive rows from the i+shift_start row.
* ContextProjectGradFunctor is the inverse process of ContextProjectFunctor.
*
* \param in Input data.
* \param Shape The shape of Input data,
* [minibatch, number_of_input_features].
* \param type A float LoDTensor.
* \param Shape The shape of Input data:
* [mini-batch, input_hidden_size].
*
* \param padding_data Padding data.
* \param Shape The shape of Padding data,
* [up_pad + down_pad, number_of_input_features].
* \param type A float Tensor.
* \param Shape The shape of Padding data:
* [up_pad + down_pad, input_hidden_size].
*
* \param col Col data.
* \param Shape The shape of Col data,
* [minibatch, context_length * number_of_input_features].
* \param type A float Tensor.
* \param Shape The shape of Col data:
* [mini-batch, context_length * input_hidden_size].
*
* For a mini-batch of 2 variable lengths sentences, containing 3, and 1
* time-steps:
...
...
@@ -63,72 +63,170 @@ using EigenMatrix = framework::EigenMatrix<T, MajorType, IndexType>;
* representation is 2.
*
* - Case1:
* If context_start is -1 and padding_trainable is false, we use zero to pad
* instead of learned weight to pad,
*
and the context_len
th is 3, the output (Out) is:
*
If context_start is -1 and padding_trainable is false, we use zero to pad
*
instead of learned weight to pad,
*
and the context_leng
th is 3, the output (Out) is:
*
* Out =[[0, 0, a1, a2, b1, b2;
* a1, a2, b1, b2, c1, c2;
* b1, b2, c1, c2, 0, 0 ]
*
[0, 0,
d1, d2, 0, 0 ]]
*
Out =[[0, 0, a1, a2, b1, b2;
*
a1, a2, b1, b2, c1, c2;
*
b1, b2, c1, c2, 0, 0 ]
*
[0, 0,
d1, d2, 0, 0 ]]
*
* - Case2:
* If context_start is -1 and padding_trainable is true, we use learned weight
* to pad,
*
and the context_len
th is 3, the output (Out) is:
*
If context_start is -1 and padding_trainable is true, we use learned weight
*
to pad,
*
and the context_leng
th is 3, the output (Out) is:
*
* Out = [[w1, w2, a1, a2, b1, b2;
* a1, a2, b1, b2, c1, c2;
* b1, b2, c1, c2, w3, w4]
* [w1, w2, d1, d2, w3, w4]]
*
Out = [[w1, w2, a1, a2, b1, b2;
*
a1, a2, b1, b2, c1, c2;
*
b1, b2, c1, c2, w3, w4]
*
[w1, w2, d1, d2, w3, w4]]
*
*/
template
<
typename
Place
,
typename
T
>
class
ContextProjectFunctor
{
public:
void
operator
()(
const
platform
::
DeviceContext
&
context
,
framework
::
LoDTensor
&
in
,
framework
::
Tensor
&
padding_data
,
framework
::
Tensor
&
col
,
bool
padding_trainable
,
void
operator
()(
const
platform
::
DeviceContext
&
context
,
const
LoDTensor
&
in
,
const
Tensor
&
padding_data
,
Tensor
&
col
,
bool
padding_trainable
,
int
context_start
,
int
context_length
,
int
context_stride
,
int
up_pad
,
int
down_pad
)
{
auto
lod_level_0
=
in
.
lod
()[
0
];
math
::
Im2ColFunctor
<
math
::
ColFormat
::
kOCF
,
Place
,
float
>
im2col_ocf
;
int
input_row_begin
,
input_row_end
;
int
sequence_height
,
sequence_width
;
sequence_width
=
in
.
dims
()[
1
];
for
(
int
i
=
0
;
i
<
static_cast
<
int
>
(
lod_level_0
.
size
())
-
1
;
++
i
)
{
input_row_begin
=
(
context_start
>
0
)
?
static_cast
<
int
>
(
lod_level_0
[
i
])
+
context_start
:
static_cast
<
int
>
(
lod_level_0
[
i
]);
input_row_end
=
static_cast
<
int
>
(
lod_level_0
[
i
+
1
]);
Tensor
out_t
=
col
.
Slice
(
static_cast
<
int
>
(
lod_level_0
[
i
]),
static_cast
<
int
>
(
lod_level_0
[
i
+
1
]));
sequence_height
=
static_cast
<
int
>
(
out_t
.
dims
()[
0
]);
if
(
input_row_begin
<
input_row_end
)
{
Tensor
in_t
=
in
.
Slice
(
input_row_begin
,
input_row_end
);
std
::
vector
<
int64_t
>
output_shape
(
{
sequence_height
,
1
,
1
,
context_length
,
sequence_width
});
// output_height, output_width,
// input_channels, filter_height, filter_width
out_t
.
Resize
(
framework
::
make_ddim
(
output_shape
));
std
::
vector
<
int64_t
>
input_shape
(
{
1
,
input_row_end
-
input_row_begin
,
sequence_width
});
// input_channels, input_height, input_width
in_t
.
Resize
(
framework
::
make_ddim
(
input_shape
));
im2col_ocf
(
context
,
in_t
,
out_t
,
/*stride_height*/
context_stride
,
/*stride_width*/
1
,
up_pad
,
down_pad
,
0
,
0
);
out_t
.
Resize
({
sequence_height
,
context_length
*
sequence_width
});
}
}
if
(
padding_trainable
)
{
for
(
int
i
=
0
;
i
<
static_cast
<
int
>
(
lod_level_0
.
size
())
-
1
;
++
i
)
{
Tensor
out_t
=
col
.
Slice
(
static_cast
<
int
>
(
lod_level_0
[
i
]),
static_cast
<
int
>
(
lod_level_0
[
i
+
1
]));
sequence_height
=
static_cast
<
int
>
(
out_t
.
dims
()[
0
]);
// add up trainable data
out_t
.
Resize
({
sequence_height
*
context_length
,
sequence_width
});
if
(
up_pad
>
0
)
{
// add up pad
int
padding_rows
=
std
::
min
(
up_pad
,
static_cast
<
int
>
(
lod_level_0
[
i
+
1
]
-
lod_level_0
[
i
]));
for
(
int
k
=
0
;
k
<
padding_rows
;
++
k
)
{
int
padding_size
=
k
+
context_length
<
up_pad
?
context_length
:
up_pad
-
k
;
Tensor
out_t_sub
=
out_t
.
Slice
(
k
*
context_length
,
k
*
context_length
+
padding_size
);
Tensor
w_sub
=
padding_data
.
Slice
(
k
,
k
+
padding_size
);
auto
out_t_sub_e
=
EigenMatrix
<
T
>::
From
(
out_t_sub
);
auto
w_sub_e
=
EigenMatrix
<
T
>::
From
(
w_sub
);
out_t_sub_e
.
device
(
*
context
.
GetEigenDevice
<
Place
>
())
=
w_sub_e
;
}
}
if
(
down_pad
>
0
)
{
// add down pad
int
down_pad_begin_row
=
std
::
max
(
0
,
(
sequence_height
-
context_start
-
context_length
)
+
1
)
+
1
;
int
padding_begin
=
std
::
max
(
0
,
context_start
-
sequence_height
);
int
padding_size
=
sequence_height
-
context_start
>=
context_length
?
1
:
context_length
-
(
sequence_height
-
context_start
);
if
(
context_start
>=
sequence_height
)
padding_size
=
context_length
;
int
padding_idx
=
padding_begin
;
for
(
int
t
=
0
;
t
+
down_pad_begin_row
<=
sequence_height
;
++
t
,
++
padding_size
)
{
if
(
context_start
>=
sequence_height
)
padding_size
=
context_length
;
if
(
padding_size
>
context_length
)
{
padding_size
=
context_length
;
padding_idx
++
;
}
if
(
padding_begin
>
0
||
sequence_height
==
context_start
)
padding_idx
=
padding_begin
+
t
;
Tensor
out_t_sub
=
out_t
.
Slice
(
(
down_pad_begin_row
+
t
)
*
context_length
-
padding_size
,
(
down_pad_begin_row
+
t
)
*
context_length
);
Tensor
w_sub
=
padding_data
.
Slice
(
up_pad
+
padding_idx
,
up_pad
+
padding_idx
+
padding_size
);
auto
out_t_sub_e
=
EigenMatrix
<
T
>::
From
(
out_t_sub
);
auto
w_sub_e
=
EigenMatrix
<
T
>::
From
(
w_sub
);
out_t_sub_e
.
device
(
*
context
.
GetEigenDevice
<
Place
>
())
=
w_sub_e
;
}
}
out_t
.
Resize
({
sequence_height
,
context_length
*
sequence_width
});
}
}
}
};
template
<
typename
Place
,
typename
T
>
class
ContextProjectGradFunctor
{
public:
void
operator
()(
const
platform
::
DeviceContext
&
context
,
LoDTensor
&
in
,
Tensor
&
padding_data
,
Tensor
&
col
,
bool
padding_trainable
,
int
context_start
,
int
context_length
,
int
context_stride
,
int
up_pad
,
int
down_pad
,
bool
gradient
,
bool
input_grad
,
bool
pad_grad
)
{
int
up_pad
,
int
down_pad
,
bool
input_grad
,
bool
pad_grad
)
{
auto
lod_level_0
=
in
.
lod
()[
0
];
paddle
::
operators
::
math
::
Im2ColFunctor
<
paddle
::
operators
::
math
::
ColFormat
::
kOCF
,
Place
,
float
>
im2col_ocf
;
paddle
::
operators
::
math
::
Col2ImFunctor
<
paddle
::
operators
::
math
::
ColFormat
::
kOCF
,
Place
,
float
>
col2im_ocf
;
math
::
Col2ImFunctor
<
math
::
ColFormat
::
kOCF
,
Place
,
float
>
col2im_ocf
;
int
input_row_begin
,
input_row_end
;
int
sequence_height
,
sequence_width
;
sequence_width
=
in
.
dims
()[
1
];
input_grad
=
gradient
&&
input_grad
;
pad_grad
=
gradient
&&
pad_grad
;
if
(
!
gradient
||
input_grad
)
{
if
(
input_grad
)
{
for
(
int
i
=
0
;
i
<
static_cast
<
int
>
(
lod_level_0
.
size
())
-
1
;
++
i
)
{
input_row_begin
=
(
context_start
>
0
)
?
static_cast
<
int
>
(
lod_level_0
[
i
])
+
context_start
:
static_cast
<
int
>
(
lod_level_0
[
i
]);
input_row_end
=
static_cast
<
int
>
(
lod_level_0
[
i
+
1
]);
framework
::
Tensor
out_t
=
col
.
Slice
(
static_cast
<
int
>
(
lod_level_0
[
i
]),
static_cast
<
int
>
(
lod_level_0
[
i
+
1
]));
Tensor
out_t
=
col
.
Slice
(
static_cast
<
int
>
(
lod_level_0
[
i
]),
static_cast
<
int
>
(
lod_level_0
[
i
+
1
]));
sequence_height
=
static_cast
<
int
>
(
out_t
.
dims
()[
0
]);
if
(
input_row_begin
<
input_row_end
)
{
framework
::
Tensor
in_t
=
in
.
Slice
(
input_row_begin
,
input_row_end
);
Tensor
in_t
=
in
.
Slice
(
input_row_begin
,
input_row_end
);
std
::
vector
<
int64_t
>
output_shape
(
{
sequence_height
,
1
,
1
,
context_length
,
sequence_width
});
// output_height, output_width,
// input_channels, filter_height, filter_width
out_t
.
Resize
(
framework
::
make_ddim
(
output_shape
));
std
::
vector
<
int64_t
>
input_shape
(
...
...
@@ -136,53 +234,39 @@ class ContextProjectFunctor {
sequence_width
});
// input_channels, input_height, input_width
in_t
.
Resize
(
framework
::
make_ddim
(
input_shape
));
if
(
gradient
)
{
col2im_ocf
(
context
,
in_t
,
out_t
,
/*stride_height*/
context_stride
,
/*stride_width*/
1
,
up_pad
,
down_pad
,
0
,
0
);
}
else
{
im2col_ocf
(
context
,
in_t
,
out_t
,
/*stride_height*/
context_stride
,
/*stride_width*/
1
,
up_pad
,
down_pad
,
0
,
0
);
}
col2im_ocf
(
context
,
in_t
,
out_t
,
/*stride_height*/
context_stride
,
/*stride_width*/
1
,
up_pad
,
down_pad
,
0
,
0
);
out_t
.
Resize
({
sequence_height
,
context_length
*
sequence_width
});
}
}
}
if
(
!
gradient
||
pad_grad
)
{
if
(
pad_grad
)
{
if
(
padding_trainable
)
{
for
(
int
i
=
0
;
i
<
static_cast
<
int
>
(
lod_level_0
.
size
())
-
1
;
++
i
)
{
framework
::
Tensor
out_t
=
col
.
Slice
(
static_cast
<
int
>
(
lod_level_0
[
i
]),
static_cast
<
int
>
(
lod_level_0
[
i
+
1
]));
Tensor
out_t
=
col
.
Slice
(
static_cast
<
int
>
(
lod_level_0
[
i
]),
static_cast
<
int
>
(
lod_level_0
[
i
+
1
]));
sequence_height
=
static_cast
<
int
>
(
out_t
.
dims
()[
0
]);
// add up trainable data
out_t
.
Resize
({
sequence_height
*
context_length
,
sequence_width
});
if
(
up_pad
>
0
)
{
// add up pad
if
(
up_pad
>
0
)
{
int
padding_rows
=
std
::
min
(
up_pad
,
static_cast
<
int
>
(
lod_level_0
[
i
+
1
]
-
lod_level_0
[
i
]));
for
(
int
k
=
0
;
k
<
padding_rows
;
++
k
)
{
int
padding_size
=
k
+
context_length
<
up_pad
?
context_length
:
up_pad
-
k
;
framework
::
Tensor
out_t_sub
=
out_t
.
Slice
(
k
*
context_length
,
k
*
context_length
+
padding_size
);
framework
::
Tensor
w_sub
=
padding_data
.
Slice
(
k
,
k
+
padding_size
);
// in this block, using EigenVector<T>::Flatten is ok too.
Tensor
out_t_sub
=
out_t
.
Slice
(
k
*
context_length
,
k
*
context_length
+
padding_size
);
Tensor
w_sub
=
padding_data
.
Slice
(
k
,
k
+
padding_size
);
auto
out_t_sub_e
=
EigenMatrix
<
T
>::
From
(
out_t_sub
);
auto
w_sub_e
=
EigenMatrix
<
T
>::
From
(
w_sub
);
if
(
gradient
)
{
w_sub_e
.
device
(
*
context
.
GetEigenDevice
<
Place
>
())
=
w_sub_e
+
out_t_sub_e
;
}
else
{
out_t_sub_e
.
device
(
*
context
.
GetEigenDevice
<
Place
>
())
=
w_sub_e
;
}
w_sub_e
.
device
(
*
context
.
GetEigenDevice
<
Place
>
())
=
w_sub_e
+
out_t_sub_e
;
}
}
if
(
down_pad
>
0
)
{
// add down pad
if
(
down_pad
>
0
)
{
int
down_pad_begin_row
=
std
::
max
(
0
,
(
sequence_height
-
context_start
-
context_length
)
+
1
)
+
...
...
@@ -204,19 +288,16 @@ class ContextProjectFunctor {
}
if
(
padding_begin
>
0
||
sequence_height
==
context_start
)
padding_idx
=
padding_begin
+
t
;
framework
::
Tensor
out_t_sub
=
out_t
.
Slice
(
Tensor
out_t_sub
=
out_t
.
Slice
(
(
down_pad_begin_row
+
t
)
*
context_length
-
padding_size
,
(
down_pad_begin_row
+
t
)
*
context_length
);
framework
::
Tensor
w_sub
=
padding_data
.
Slice
(
Tensor
w_sub
=
padding_data
.
Slice
(
up_pad
+
padding_idx
,
up_pad
+
padding_idx
+
padding_size
);
auto
out_t_sub_e
=
EigenMatrix
<
T
>::
From
(
out_t_sub
);
auto
w_sub_e
=
EigenMatrix
<
T
>::
From
(
w_sub
);
if
(
gradient
)
{
w_sub_e
.
device
(
*
context
.
GetEigenDevice
<
Place
>
())
=
w_sub_e
+
out_t_sub_e
;
}
else
{
out_t_sub_e
.
device
(
*
context
.
GetEigenDevice
<
Place
>
())
=
w_sub_e
;
}
w_sub_e
.
device
(
*
context
.
GetEigenDevice
<
Place
>
())
=
w_sub_e
+
out_t_sub_e
;
}
}
out_t
.
Resize
({
sequence_height
,
context_length
*
sequence_width
});
...
...
paddle/operators/sequence_conv_op.cc
浏览文件 @
0f9858a1
...
...
@@ -30,19 +30,20 @@ class SequenceConvOp : public framework::OperatorWithKernel {
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"Out"
),
"Output(Out) of SequenceConvOp should not be null."
);
int
context_length
=
ctx
->
Attrs
().
Get
<
int
>
(
"context_length"
);
bool
padding_trainable
=
ctx
->
Attrs
().
Get
<
bool
>
(
"padding_trainable"
);
int
context_start
=
ctx
->
Attrs
().
Get
<
int
>
(
"context_start"
);
int
context_length
=
ctx
->
Attrs
().
Get
<
int
>
(
"contextLength"
);
int
context_start
=
ctx
->
Attrs
().
Get
<
int
>
(
"contextStart"
);
auto
in_dims
=
ctx
->
GetInputDim
(
"X"
);
auto
filter_dims
=
ctx
->
GetInputDim
(
"Filter"
);
PADDLE_ENFORCE
(
ctx
->
Attrs
().
Get
<
int
>
(
"contextStride"
)
==
1
,
"Currently, SequenceConvOp only supports contextStride=1."
);
PADDLE_ENFORCE
(
in_dims
.
size
()
==
2
&&
filter_dims
.
size
()
==
2
,
"Input(X, Filter) should be 2-D tensor."
);
PADDLE_ENFORCE
(
filter_dims
[
0
]
==
context_length
*
in_dims
[
1
],
"Filter's height should be context_length * "
"
number_of_input_features
."
);
"
input_hidden_size
."
);
if
(
padding_trainable
)
{
if
(
ctx
->
Attrs
().
Get
<
bool
>
(
"paddingTrainable"
)
)
{
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"PaddingData"
),
"Input(PaddingData) of SequenceConvOp should not be null."
);
...
...
@@ -54,7 +55,7 @@ class SequenceConvOp : public framework::OperatorWithKernel {
if
(
context_start
==
0
&&
context_length
==
1
)
{
PADDLE_THROW
(
"If context_start is 0 and context_length is 1, padding
_t
rainable "
"If context_start is 0 and context_length is 1, padding
T
rainable "
"should be false."
);
}
PADDLE_ENFORCE
(
padding_dim
.
size
()
==
2
,
...
...
@@ -81,13 +82,14 @@ class SequenceConvGradOp : public framework::OperatorWithKernel {
"Gradient of output(Out) should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"X"
),
"The input(X) should not be null."
);
if
(
ctx
->
Attrs
().
Get
<
bool
>
(
"padding
_t
rainable"
)
&&
if
(
ctx
->
Attrs
().
Get
<
bool
>
(
"padding
T
rainable"
)
&&
ctx
->
HasOutput
(
framework
::
GradVarName
(
"PaddingData"
)))
{
ctx
->
SetOutputDim
(
framework
::
GradVarName
(
"PaddingData"
),
ctx
->
GetInputDim
(
"PaddingData"
));
}
if
(
ctx
->
HasOutput
(
framework
::
GradVarName
(
"X"
)))
{
ctx
->
SetOutputDim
(
framework
::
GradVarName
(
"X"
),
ctx
->
GetInputDim
(
"X"
));
ctx
->
ShareLoD
(
framework
::
GradVarName
(
"X"
),
"X"
);
}
if
(
ctx
->
HasOutput
(
framework
::
GradVarName
(
"Filter"
)))
{
ctx
->
SetOutputDim
(
framework
::
GradVarName
(
"Filter"
),
...
...
@@ -105,54 +107,58 @@ class SequenceConvOpMaker : public framework::OpProtoAndCheckerMaker {
"X"
,
"(LoDTensor) the input(X) is a LodTensor, which support "
"variable-time length input sequence. The underlying tensor in "
"this LoDTensor is a matrix with shape (T,
D
), where, T is the "
"total time steps in this mini-batch,
D is the input feature
size."
);
"this LoDTensor is a matrix with shape (T,
N
), where, T is the "
"total time steps in this mini-batch,
N is the input_hidden_
size."
);
AddInput
(
"PaddingData"
,
"(Tensor, optional) the input(PaddingData) is an optional "
"parameter, and it is learnable. "
"This is a tensor with shape (
N, D), where N
is the "
"top_pad + bottom_pad,
D is the input feature
size. In order to "
"This is a tensor with shape (
P, N), where P
is the "
"top_pad + bottom_pad,
N is the input_hidden_
size. In order to "
"ensure the equal length of sequence before and after "
"convolution, it is necessary to fill the top and bottom of each "
"sequence according to context_length, context_stride and "
"context_start"
)
.
AsDispensable
();
AddInput
(
"Filter"
,
"(Tensor) the input(Filter) is an learnable parameter."
"This is a tensor with shape (N, D), where N is the "
"context_length, D is the output feature size."
);
AddInput
(
"Filter"
,
"(Tensor) the input(Filter) is an learnable parameter."
"This is a tensor with shape (K, M), where K is the "
"context_length * input_hidden_size, M is the output feature size."
);
AddOutput
(
"Out"
,
"(LoDTensor) the output(Out) is a LodTensor, which support "
"variable-time length output sequence. The underlying tensor in "
"this LoDTensor is a matrix with shape (T,
D
), where, T is the "
"total time steps in this mini-batch,
D
is the output feature size."
);
"this LoDTensor is a matrix with shape (T,
M
), where, T is the "
"total time steps in this mini-batch,
M
is the output feature size."
);
AddAttr
<
bool
>
(
"padding
_t
rainable"
,
"(bool, default
false) the padding data of SequenceConvOp "
AddAttr
<
bool
>
(
"padding
T
rainable"
,
"(bool, default
:
false) the padding data of SequenceConvOp "
"is trainable or not."
)
.
SetDefault
(
false
);
AddAttr
<
int
>
(
"context
_l
ength"
,
"(int
, default 3) the context_l
ength of SequenceConvOp is the "
AddAttr
<
int
>
(
"context
L
ength"
,
"(int
) the contextL
ength of SequenceConvOp is the "
"height of the convolution kernel."
)
.
SetDefault
(
3
)
.
GreaterThan
(
0
);
AddAttr
<
int
>
(
"context
_s
tart"
,
"(int, default
0) the context_s
tart of SequenceConvOp "
AddAttr
<
int
>
(
"context
S
tart"
,
"(int, default
:0) the contextS
tart of SequenceConvOp "
"represents the beginning of the convolution of the number of "
"rows of sequence, which can be negative."
)
"rows of sequence, which can be negative. The negative number "
"means to pad contextStart time-steps of zeros or learnable "
"parameters at the beginning of each instance. The positive "
"number means to skip contextStart time-steps of each "
"instance."
)
.
SetDefault
(
0
);
AddAttr
<
int
>
(
"context
_s
tride"
,
"(int, default
1) the context_s
tride of SequenceConvOp "
"represents the st
ep length of convolution
. "
AddAttr
<
int
>
(
"context
S
tride"
,
"(int, default
:1) the contextS
tride of SequenceConvOp "
"represents the st
ride length of convolution kernel
. "
"Currently, SequenceConvOp only supports"
"context
_s
tride=1."
)
"context
S
tride=1."
)
.
SetDefault
(
1
)
.
GreaterThan
(
0
);
AddComment
(
R"DOC(
SequenceConvOp performs convolution operation on features of
context
_l
ength time-steps of each instance.
context
L
ength time-steps of each instance.
The convolution operation calculates the output based on the input, filter
and strides, paddings parameters. The size of each dimension of the
parameters is checked in the infer-shape. In order to ensure the equal
...
...
paddle/operators/sequence_conv_op.h
浏览文件 @
0f9858a1
...
...
@@ -35,12 +35,11 @@ class SequenceConvKernel : public framework::OpKernel<T> {
out
->
mutable_data
<
T
>
(
context
.
GetPlace
());
context
.
ShareLoD
(
"X"
,
"Out"
);
int
context_start
=
context
.
Attr
<
int
>
(
"context
_s
tart"
);
int
context_length
=
context
.
Attr
<
int
>
(
"context
_l
ength"
);
int
context_stride
=
context
.
Attr
<
int
>
(
"context
_s
tride"
);
bool
padding_trainable
=
context
.
Attr
<
bool
>
(
"padding
_t
rainable"
);
int
context_start
=
context
.
Attr
<
int
>
(
"context
S
tart"
);
int
context_length
=
context
.
Attr
<
int
>
(
"context
L
ength"
);
int
context_stride
=
context
.
Attr
<
int
>
(
"context
S
tride"
);
bool
padding_trainable
=
context
.
Attr
<
bool
>
(
"padding
T
rainable"
);
// InferShape by in_lod
PADDLE_ENFORCE_EQ
(
in
->
lod
().
size
(),
1UL
,
"Only support one level sequence now."
);
...
...
@@ -51,26 +50,21 @@ class SequenceConvKernel : public framework::OpKernel<T> {
int
up_pad
=
std
::
max
(
0
,
-
context_start
);
int
down_pad
=
std
::
max
(
0
,
context_start
+
context_length
-
1
);
int
sequence_width
;
sequence_width
=
static_cast
<
int
>
(
in
->
dims
()[
1
]);
int
sequence_width
=
static_cast
<
int
>
(
in
->
dims
()[
1
]);
// Use col_shape in the im2col calculation.
framework
::
DDim
col_shape
=
{
in
->
dims
()[
0
],
sequence_width
*
context_leng
th
};
context_length
*
sequence_wid
th
};
Tensor
col
;
col
.
mutable_data
<
T
>
(
col_shape
,
context
.
GetPlace
());
math
::
SetConstant
<
Place
,
T
>
set_zero
;
// Because if padding_trainable is false, padding data should be zeros.
math
::
SetConstant
<
Place
,
T
>
set_zero
;
set_zero
(
context
.
device_context
(),
&
col
,
static_cast
<
T
>
(
0
));
paddle
::
operators
::
math
::
ContextProjectFunctor
<
Place
,
T
>
seq_project_functor
;
LoDTensor
*
input
=
const_cast
<
LoDTensor
*>
(
in
);
Tensor
*
pad_data
=
const_cast
<
Tensor
*>
(
padding_data
);
math
::
ContextProjectFunctor
<
Place
,
T
>
seq_project_functor
;
seq_project_functor
(
context
.
device_context
(),
*
in
put
,
*
pad
_data
,
col
,
seq_project_functor
(
context
.
device_context
(),
*
in
,
*
padding
_data
,
col
,
padding_trainable
,
context_start
,
context_length
,
context_stride
,
up_pad
,
down_pad
,
false
,
false
,
false
);
context_stride
,
up_pad
,
down_pad
);
math
::
matmul
<
Place
,
T
>
(
context
.
device_context
(),
col
,
false
,
filter
,
false
,
static_cast
<
T
>
(
1.0
),
out
,
static_cast
<
T
>
(
0.0
));
...
...
@@ -81,18 +75,18 @@ template <typename Place, typename T>
class
SequenceConvGradKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
*
out_g
=
context
.
Input
<
LoDTensor
>
(
framework
::
GradVarName
(
"Out"
));
auto
*
in_g
=
context
.
Output
<
LoDTensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
out_g
=
context
.
Input
<
LoDTensor
>
(
framework
::
GradVarName
(
"Out"
));
auto
*
filter_g
=
context
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"Filter"
));
auto
*
padding_data_g
=
context
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"PaddingData"
));
auto
*
in
=
context
.
Input
<
LoDTensor
>
(
"X"
);
auto
*
filter
=
context
.
Input
<
Tensor
>
(
"Filter"
);
int
context_start
=
context
.
Attr
<
int
>
(
"context
_s
tart"
);
int
context_length
=
context
.
Attr
<
int
>
(
"context
_l
ength"
);
int
context_stride
=
context
.
Attr
<
int
>
(
"context
_s
tride"
);
bool
padding_trainable
=
context
.
Attr
<
bool
>
(
"padding
_t
rainable"
);
int
context_start
=
context
.
Attr
<
int
>
(
"context
S
tart"
);
int
context_length
=
context
.
Attr
<
int
>
(
"context
L
ength"
);
int
context_stride
=
context
.
Attr
<
int
>
(
"context
S
tride"
);
bool
padding_trainable
=
context
.
Attr
<
bool
>
(
"padding
T
rainable"
);
PADDLE_ENFORCE_EQ
(
in
->
lod
().
size
(),
1UL
,
"Only support one level sequence now."
);
...
...
@@ -115,17 +109,18 @@ class SequenceConvGradKernel : public framework::OpKernel<T> {
math
::
matmul
<
Place
,
T
>
(
context
.
device_context
(),
*
out_g
,
false
,
*
filter
,
true
,
T
(
1.0
),
&
col
,
T
(
1.0
));
}
paddle
::
operators
::
math
::
ContextProjectFunctor
<
Place
,
T
>
seq_project
_functor
;
math
::
ContextProjectFunctor
<
Place
,
T
>
seq_project_functor
;
math
::
ContextProjectGradFunctor
<
Place
,
T
>
seq_project_grad
_functor
;
if
(
in_g
)
{
in_g
->
mutable_data
<
T
>
(
context
.
GetPlace
());
in_g
->
set_lod
(
in
->
lod
());
set_zero
(
context
.
device_context
(),
in_g
,
static_cast
<
T
>
(
0
));
seq_project_functor
(
context
.
device_context
(),
*
in_g
,
*
padding_data_g
,
col
,
padding_trainable
,
context_start
,
context_length
,
context_stride
,
up_pad
,
down_pad
,
true
,
true
,
false
);
seq_project_grad_functor
(
context
.
device_context
(),
*
in_g
,
*
padding_data_g
,
col
,
padding_trainable
,
context_start
,
context_length
,
context_stride
,
up_pad
,
down_pad
,
true
,
false
);
}
if
(
padding_trainable
&&
padding_data_g
)
{
...
...
@@ -133,9 +128,10 @@ class SequenceConvGradKernel : public framework::OpKernel<T> {
set_zero
(
context
.
device_context
(),
padding_data_g
,
static_cast
<
T
>
(
0
));
LoDTensor
*
input
=
const_cast
<
LoDTensor
*>
(
in
);
seq_project_functor
(
context
.
device_context
(),
*
input
,
*
padding_data_g
,
col
,
padding_trainable
,
context_start
,
context_length
,
context_stride
,
up_pad
,
down_pad
,
true
,
false
,
true
);
seq_project_grad_functor
(
context
.
device_context
(),
*
input
,
*
padding_data_g
,
col
,
padding_trainable
,
context_start
,
context_length
,
context_stride
,
up_pad
,
down_pad
,
false
,
true
);
}
if
(
filter_g
)
{
...
...
@@ -150,15 +146,9 @@ class SequenceConvGradKernel : public framework::OpKernel<T> {
padding_data
=
context
.
Input
<
Tensor
>
(
"PaddingData"
);
}
sequence_width
=
static_cast
<
int
>
(
in
->
dims
()[
1
]);
LoDTensor
*
input
=
const_cast
<
LoDTensor
*>
(
in
);
Tensor
*
pad_data
=
const_cast
<
Tensor
*>
(
padding_data
);
seq_project_functor
(
context
.
device_context
(),
*
input
,
*
pad_data
,
col
,
seq_project_functor
(
context
.
device_context
(),
*
in
,
*
padding_data
,
col
,
padding_trainable
,
context_start
,
context_length
,
context_stride
,
up_pad
,
down_pad
,
false
,
false
,
false
);
context_stride
,
up_pad
,
down_pad
);
math
::
matmul
<
Place
,
T
>
(
context
.
device_context
(),
col
,
true
,
out_grad
,
false
,
T
(
1.0
),
&
filter_grad
,
T
(
1.0
));
...
...
python/paddle/v2/framework/tests/test_seq_conv.py
浏览文件 @
0f9858a1
...
...
@@ -45,10 +45,10 @@ class TestSeqProject(OpTest):
self
.
inputs_val_no_f
=
[
'PaddingData'
,
'X'
]
self
.
attrs
=
{
'context
_s
tart'
:
self
.
context_start
,
'context
_l
ength'
:
self
.
context_length
,
'padding
_t
rainable'
:
self
.
padding_trainable
,
'context
_s
tride'
:
self
.
context_stride
'context
S
tart'
:
self
.
context_start
,
'context
L
ength'
:
self
.
context_length
,
'padding
T
rainable'
:
self
.
padding_trainable
,
'context
S
tride'
:
self
.
context_stride
}
out
=
np
.
zeros
(
(
self
.
input_size
[
0
],
self
.
output_represention
)).
astype
(
'float32'
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录