Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
31531ab5
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看板
提交
31531ab5
编写于
10月 19, 2017
作者:
W
wanghaoshuang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add backward kernel
上级
8de04be7
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
39 addition
and
38 deletion
+39
-38
paddle/framework/lod_tensor.cc
paddle/framework/lod_tensor.cc
+1
-1
paddle/operators/seq_expand_op.cc
paddle/operators/seq_expand_op.cc
+7
-23
paddle/operators/seq_expand_op.h
paddle/operators/seq_expand_op.h
+24
-3
paddle/operators/sequence_concat_op.cc
paddle/operators/sequence_concat_op.cc
+5
-5
python/paddle/v2/framework/tests/op_test.py
python/paddle/v2/framework/tests/op_test.py
+0
-3
python/paddle/v2/framework/tests/test_seq_expand.py
python/paddle/v2/framework/tests/test_seq_expand.py
+2
-3
未找到文件。
paddle/framework/lod_tensor.cc
浏览文件 @
31531ab5
...
...
@@ -110,7 +110,7 @@ Vector<size_t> repeat_lod(Vector<size_t> data, Vector<size_t> starts,
size_t
p
=
0
,
start
=
0
,
end
=
0
;
if
(
is_first
==
true
)
{
for
(
size_t
i
=
0
;
i
<
times
.
size
();
++
i
)
{
result
.
push_back
(
data
.
back
()
+
times
[
i
]
*
(
data
[
i
+
1
]
-
data
[
i
]));
result
.
push_back
(
result
.
back
()
+
times
[
i
]
*
(
data
[
i
+
1
]
-
data
[
i
]));
}
}
else
{
for
(
size_t
i
=
0
;
i
<
times
.
size
();
++
i
)
{
...
...
paddle/operators/seq_expand_op.cc
浏览文件 @
31531ab5
...
...
@@ -60,7 +60,8 @@ As an example:
Given:
X = [1, 2 , 3]
X.data = [1, 2 , 3, 4]
X.lod = [[0, 3, 4], [0, 1, 3, 4]]
and
...
...
@@ -69,8 +70,8 @@ repeat = 2
then we get
Out.data = [1,
1, 2, 2, 3, 3
]
Out.lod = [[0,
2, 4, 6
]]
Out.data = [1,
2, 3, 1, 2, 3, 4, 4
]
Out.lod = [[0,
6, 8], [0, 3, 6, 7, 8], [0, 1, 3, 4, 6, 7, 8
]]
)DOC"
);
}
...
...
@@ -83,6 +84,7 @@ class SeqExpandOpGrad : public framework::OperatorWithKernel {
protected:
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"X"
),
"Input(X) should not be null"
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"Out"
),
"Input(Out) should not be null"
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
framework
::
GradVarName
(
"Out"
)),
"Input(Out@GRAD) should not be null"
);
auto
x_dims
=
ctx
->
GetInputDim
(
"X"
);
...
...
@@ -93,30 +95,12 @@ class SeqExpandOpGrad : public framework::OperatorWithKernel {
}
};
class
SeqExpandOpGradMaker
:
public
framework
::
SingleGradOpDescMaker
{
public:
using
framework
::
SingleGradOpDescMaker
::
SingleGradOpDescMaker
;
protected:
std
::
unique_ptr
<
framework
::
OpDescBind
>
Apply
()
const
override
{
auto
*
bind
=
new
framework
::
OpDescBind
();
bind
->
SetInput
(
"X"
,
Input
(
"X"
));
bind
->
SetInput
(
framework
::
GradVarName
(
"Out"
),
OutputGrad
(
"Out"
));
bind
->
SetOutput
(
framework
::
GradVarName
(
"X"
),
InputGrad
(
"X"
));
bind
->
SetAttrMap
(
Attrs
());
bind
->
SetType
(
"seq_expand_grad"
);
return
std
::
unique_ptr
<
framework
::
OpDescBind
>
(
bind
);
}
};
}
// namespace operators
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
REGISTER_OPERATOR
(
seq_expand
,
ops
::
SeqExpandOp
,
ops
::
SeqExpandOpMaker
,
ops
::
SeqExpandOpGradMaker
);
REGISTER_OPERATOR
(
seq_expand_grad
,
ops
::
SeqExpandOpGrad
);
REGISTER_OP
(
seq_expand
,
ops
::
SeqExpandOp
,
ops
::
SeqExpandOpMaker
,
seq_expand_grad
,
ops
::
SeqExpandOpGrad
);
REGISTER_OP_CPU_KERNEL
(
seq_expand
,
ops
::
SeqExpandKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
REGISTER_OP_CPU_KERNEL
(
...
...
paddle/operators/seq_expand_op.h
浏览文件 @
31531ab5
...
...
@@ -16,6 +16,7 @@
#include "paddle/framework/op_registry.h"
#include "paddle/memory/memcpy.h"
#include "unsupported/Eigen/CXX11/Tensor"
namespace
paddle
{
namespace
operators
{
...
...
@@ -93,9 +94,29 @@ template <typename Place, typename T>
class
SeqExpandGradKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
*
d_out
=
context
.
Input
<
Tensor
>
(
framework
::
GradVarName
(
"Out"
));
auto
*
d_x
=
context
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"X"
));
d_x
->
mutable_data
<
T
>
(
context
.
GetPlace
());
auto
*
d_out
=
context
.
Input
<
LoDTensor
>
(
framework
::
GradVarName
(
"Out"
));
auto
*
d_x
=
context
.
Output
<
LoDTensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
x
=
context
.
Input
<
LoDTensor
>
(
"X"
);
auto
*
out
=
context
.
Input
<
LoDTensor
>
(
"Out"
);
auto
out_lod
=
out
->
lod
();
d_x
->
set_lod
(
x
->
lod
());
const
T
*
d_out_data
=
d_out
->
data
<
T
>
();
auto
d_out_dims
=
d_out
->
dims
();
T
*
d_x_data
=
d_x
->
mutable_data
<
T
>
(
context
.
GetPlace
());
size_t
element_len
=
framework
::
product
(
d_out_dims
)
/
d_out_dims
[
0
];
for
(
size_t
i
=
0
;
i
<
out
->
NumElements
();
++
i
)
{
size_t
ele_count
=
out_lod
[
0
][
i
+
1
]
-
out_lod
[
0
][
i
];
size_t
repeat
=
out
->
NumElements
(
0
,
i
);
Eigen
::
TensorMap
<
Eigen
::
Tensor
<
const
T
,
2
>>
d_out_t
(
d_out_data
,
static_cast
<
int
>
(
repeat
),
static_cast
<
int
>
((
ele_count
*
element_len
)
/
repeat
));
Eigen
::
TensorMap
<
Eigen
::
Tensor
<
T
,
1
>>
d_x_t
(
d_x_data
,
static_cast
<
int
>
((
ele_count
*
element_len
)
/
repeat
));
auto
place
=
context
.
GetEigenDevice
<
Place
>
();
d_x_t
.
device
(
place
)
=
d_out_t
.
sum
(
Eigen
::
array
<
int
,
1
>
({
0
}));
d_out_data
+=
(
ele_count
*
element_len
);
d_x_data
+=
((
ele_count
*
element_len
)
/
repeat
);
}
}
};
...
...
paddle/operators/sequence_concat_op.cc
浏览文件 @
31531ab5
...
...
@@ -68,12 +68,12 @@ class SequenceConcatOpMaker : public framework::OpProtoAndCheckerMaker {
"The level should be less than the level number of inputs."
)
.
SetDefault
(
0
);
AddComment
(
R"DOC(
The sequence_concat operator concatenates multiple LoDTensors.
It only supports sequence (LoD Tensor with level number is 1)
The sequence_concat operator concatenates multiple LoDTensors.
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 other than 0(here, axis is 1 and level is 1),
each input should have the same LoD information and the LoD
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)
...
...
@@ -81,7 +81,7 @@ class SequenceConcatOpMaker : public framework::OpProtoAndCheckerMaker {
LoD(Out) = {{0,2,4}, {0,1,2,3,4}}; Dims(Out) = (4,7,4)
- Case2:
If the axis is 0(here, leve is 0), the inputs are concatenated along
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)
...
...
@@ -94,7 +94,7 @@ class SequenceConcatOpMaker : public framework::OpProtoAndCheckerMaker {
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 levels of all the inputs should be the same.
)DOC"
);
}
...
...
python/paddle/v2/framework/tests/op_test.py
浏览文件 @
31531ab5
...
...
@@ -246,9 +246,6 @@ class OpTest(unittest.TestCase):
else
:
actual
=
np
.
array
(
self
.
scope
.
find_var
(
out_name
).
get_tensor
())
expect
=
self
.
outputs
[
out_name
]
print
"out_name: %s"
%
out_name
print
"actual: %s"
%
actual
print
"expcept: %s"
%
expect
self
.
assertTrue
(
np
.
allclose
(
actual
,
expect
,
atol
=
atol
),
...
...
python/paddle/v2/framework/tests/test_seq_expand.py
浏览文件 @
31531ab5
...
...
@@ -62,9 +62,8 @@ class TestSeqExpand(OpTest):
def
test_check_output
(
self
):
self
.
check_output
()
# def test_check_grad(self):
# self.check_grad(["X"], "Out")
def
test_check_grad
(
self
):
self
.
check_grad
([
"X"
],
"Out"
)
class
TestSeqExpandCase1
(
TestSeqExpand
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录