Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
ab270c38
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看板
提交
ab270c38
编写于
8月 18, 2017
作者:
Q
Qiao Longfei
提交者:
GitHub
8月 18, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #3183 from dzhwinter/add_op_gradient
"add rowwise add backward op"
上级
8c9a7f1c
82b820e9
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
62 addition
and
9 deletion
+62
-9
paddle/operators/rowwise_add_op.cc
paddle/operators/rowwise_add_op.cc
+28
-6
paddle/operators/rowwise_add_op.cu
paddle/operators/rowwise_add_op.cu
+1
-1
paddle/operators/rowwise_add_op.h
paddle/operators/rowwise_add_op.h
+21
-1
python/paddle/v2/framework/tests/test_rowwise_add_op.py
python/paddle/v2/framework/tests/test_rowwise_add_op.py
+12
-1
未找到文件。
paddle/operators/rowwise_add_op.cc
浏览文件 @
ab270c38
...
@@ -17,7 +17,9 @@
...
@@ -17,7 +17,9 @@
namespace
paddle
{
namespace
paddle
{
namespace
operators
{
namespace
operators
{
class
RowWiseAddOp
:
public
framework
::
OperatorWithKernel
{
using
framework
::
Tensor
;
class
RowwiseAddOp
:
public
framework
::
OperatorWithKernel
{
public:
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
...
@@ -34,9 +36,9 @@ class RowWiseAddOp : public framework::OperatorWithKernel {
...
@@ -34,9 +36,9 @@ class RowWiseAddOp : public framework::OperatorWithKernel {
}
}
};
};
class
Row
W
iseAddOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
class
Row
w
iseAddOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
public:
Row
W
iseAddOpMaker
(
framework
::
OpProto
*
proto
,
Row
w
iseAddOpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
framework
::
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
"The left input of row-wise add op, must be matrix"
);
AddInput
(
"X"
,
"The left input of row-wise add op, must be matrix"
);
...
@@ -49,12 +51,32 @@ for i in xrange(X.shape[0]):
...
@@ -49,12 +51,32 @@ for i in xrange(X.shape[0]):
)DOC"
);
)DOC"
);
}
}
};
};
class
RowwiseAddGradOp
:
public
framework
::
OperatorWithKernel
{
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
protected:
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{
PADDLE_ENFORCE_NOT_NULL
(
ctx
.
InputVar
(
"X"
),
"X should not be null"
);
PADDLE_ENFORCE_NOT_NULL
(
ctx
.
InputVar
(
"b"
),
"b should not be null"
);
PADDLE_ENFORCE_NOT_NULL
(
ctx
.
InputVar
(
framework
::
GradVarName
(
"Out"
)),
"Input(Out@GRAD) should not be null"
);
auto
dims0
=
ctx
.
Input
<
Tensor
>
(
"X"
)
->
dims
();
auto
dims1
=
ctx
.
Input
<
Tensor
>
(
"b"
)
->
dims
();
PADDLE_ENFORCE_EQ
(
1
,
dims1
.
size
(),
"b dims should be 1"
)
ctx
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"X"
))
->
Resize
(
dims0
);
ctx
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"b"
))
->
Resize
(
dims1
);
}
};
}
// namespace operators
}
// namespace operators
}
// namespace paddle
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_WITHOUT_GRADIENT
(
rowwise_add
,
ops
::
RowWiseAddOp
,
REGISTER_OP
(
rowwise_add
,
ops
::
RowwiseAddOp
,
ops
::
RowwiseAddOpMaker
,
ops
::
RowWiseAddOpMaker
);
rowwise_add_grad
,
ops
::
RowwiseAddGradOp
);
REGISTER_OP_CPU_KERNEL
(
rowwise_add
,
ops
::
RowwiseAddKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
REGISTER_OP_CPU_KERNEL
(
REGISTER_OP_CPU_KERNEL
(
rowwise_add
,
ops
::
RowWiseAddKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
rowwise_add_grad
,
ops
::
RowwiseAddGradKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
paddle/operators/rowwise_add_op.cu
浏览文件 @
ab270c38
...
@@ -17,4 +17,4 @@
...
@@ -17,4 +17,4 @@
namespace
ops
=
paddle
::
operators
;
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_GPU_KERNEL
(
REGISTER_OP_GPU_KERNEL
(
rowwise_add
,
ops
::
Row
W
iseAddKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
);
rowwise_add
,
ops
::
Row
w
iseAddKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
);
paddle/operators/rowwise_add_op.h
浏览文件 @
ab270c38
...
@@ -28,7 +28,7 @@ template <typename T, int MajorType = Eigen::RowMajor,
...
@@ -28,7 +28,7 @@ template <typename T, int MajorType = Eigen::RowMajor,
using
EigenMatrix
=
framework
::
EigenMatrix
<
T
,
MajorType
,
IndexType
>
;
using
EigenMatrix
=
framework
::
EigenMatrix
<
T
,
MajorType
,
IndexType
>
;
template
<
typename
Place
,
typename
T
>
template
<
typename
Place
,
typename
T
>
class
Row
W
iseAddKernel
:
public
framework
::
OpKernel
{
class
Row
w
iseAddKernel
:
public
framework
::
OpKernel
{
public:
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
out
=
context
.
Output
<
Tensor
>
(
"Out"
);
auto
out
=
context
.
Output
<
Tensor
>
(
"Out"
);
...
@@ -47,5 +47,25 @@ class RowWiseAddKernel : public framework::OpKernel {
...
@@ -47,5 +47,25 @@ class RowWiseAddKernel : public framework::OpKernel {
}
}
};
};
template
<
typename
Place
,
typename
T
>
class
RowwiseAddGradKernel
:
public
framework
::
OpKernel
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
*
dOut
=
context
.
Input
<
Tensor
>
(
framework
::
GradVarName
(
"Out"
));
auto
*
dX
=
context
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
db
=
context
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"b"
));
dX
->
mutable_data
<
T
>
(
context
.
GetPlace
());
db
->
mutable_data
<
T
>
(
context
.
GetPlace
());
auto
OutGrad
=
EigenMatrix
<
T
>::
From
(
*
dOut
);
auto
place
=
context
.
GetEigenDevice
<
Place
>
();
EigenMatrix
<
T
>::
From
(
*
dX
).
device
(
place
)
=
OutGrad
;
// https://eigen.tuxfamily.org/dox/unsupported/TensorBase_8h_source.html
// colwise add
Eigen
::
array
<
int
,
1
>
dims
{{
1
}};
/* dimension to reduce */
EigenVector
<
T
>::
Flatten
(
*
db
).
device
(
place
)
=
OutGrad
.
sum
(
dims
);
}
};
}
// namespace operators
}
// namespace operators
}
// namespace paddle
}
// namespace paddle
python/paddle/v2/framework/tests/test_rowwise_add_op.py
浏览文件 @
ab270c38
import
unittest
import
unittest
from
op_test_util
import
OpTestMeta
import
numpy
as
np
import
numpy
as
np
from
op_test_util
import
OpTestMeta
from
gradient_checker
import
GradientChecker
,
create_op
class
TestRowwiseAddOp
(
unittest
.
TestCase
):
class
TestRowwiseAddOp
(
unittest
.
TestCase
):
...
@@ -15,5 +16,15 @@ class TestRowwiseAddOp(unittest.TestCase):
...
@@ -15,5 +16,15 @@ class TestRowwiseAddOp(unittest.TestCase):
self
.
outputs
=
{
'Out'
:
np
.
add
(
self
.
inputs
[
'X'
],
self
.
inputs
[
'b'
])}
self
.
outputs
=
{
'Out'
:
np
.
add
(
self
.
inputs
[
'X'
],
self
.
inputs
[
'b'
])}
class
RowwiseAddGradOpTest
(
GradientChecker
):
def
test_rowwise_add
(
self
):
op
=
create_op
(
"rowwise_add"
)
inputs
=
{
"X"
:
np
.
random
.
uniform
(
0.1
,
1
,
[
10
,
10
]).
astype
(
"float32"
),
"b"
:
np
.
random
.
uniform
(
0.1
,
1
,
[
10
]).
astype
(
"float32"
)
}
self
.
check_grad
(
op
,
inputs
,
set
([
"X"
,
"b"
]),
"Out"
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
unittest
.
main
()
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录