Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
马赛克医院
Paddle
提交
31531ab5
P
Paddle
项目概览
马赛克医院
/
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
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
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录