Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
s920243400
PaddleDetection
提交
00615ebc
P
PaddleDetection
项目概览
s920243400
/
PaddleDetection
与 Fork 源项目一致
Fork自
PaddlePaddle / PaddleDetection
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
00615ebc
编写于
7月 26, 2017
作者:
Y
Yu Yang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Refine OpRegistry::AddInput/AddOutput
Remove bool argument, use a class to handle that.
上级
b1b13f8f
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
89 addition
and
36 deletion
+89
-36
paddle/framework/CMakeLists.txt
paddle/framework/CMakeLists.txt
+1
-0
paddle/framework/backward_test.cc
paddle/framework/backward_test.cc
+50
-0
paddle/framework/op_registry.h
paddle/framework/op_registry.h
+32
-29
paddle/framework/op_registry_test.cc
paddle/framework/op_registry_test.cc
+2
-3
paddle/framework/operator_test.cc
paddle/framework/operator_test.cc
+2
-2
paddle/operators/fc_op.cc
paddle/operators/fc_op.cc
+2
-2
未找到文件。
paddle/framework/CMakeLists.txt
浏览文件 @
00615ebc
...
...
@@ -33,3 +33,4 @@ cc_library(net SRCS net.cc DEPS op_registry)
cc_test
(
net_op_test SRCS net_op_test.cc DEPS net add_op mul_op sigmoid_op softmax_op fc_op
)
cc_library
(
backward SRCS backward.cc DEPS net
)
cc_test
(
backward_test SRCS backward_test.cc DEPS net
)
paddle/framework/backward_test.cc
0 → 100644
浏览文件 @
00615ebc
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include <gtest/gtest.h>
#include "paddle/framework/op_registry.h"
namespace
paddle
{
namespace
framework
{
class
EmptyOp
:
public
OperatorBase
{
public:
void
InferShape
(
const
std
::
shared_ptr
<
Scope
>
&
scope
)
const
override
{}
void
Run
(
const
std
::
shared_ptr
<
Scope
>
&
scope
,
const
platform
::
DeviceContext
&
dev_ctx
)
const
override
{}
};
class
RowwiseAddOp
:
public
EmptyOp
{};
class
RowwiseAddOpMaker
:
public
OpProtoAndCheckerMaker
{
public:
RowwiseAddOpMaker
(
OpProto
*
proto
,
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
"Input X of Add"
).
IgnoreGradient
();
AddInput
(
"b"
,
"Bias of Add"
).
IgnoreGradient
();
AddOutput
(
"Out"
,
"Out of Add"
).
IgnoreGradient
();
AddComment
(
"Add Op"
);
}
};
class
RowwiseAddGradOp
:
public
EmptyOp
{};
}
// namespace framework
}
// namespace paddle
namespace
f
=
paddle
::
framework
;
REGISTER_OP
(
rowwise_add
,
f
::
RowwiseAddOp
,
f
::
RowwiseAddOpMaker
);
REGISTER_GRADIENT_OP
(
rowwise_add
,
rowwise_add_grad
,
f
::
RowwiseAddGradOp
);
TEST
(
Backward
,
simple_grad
)
{
auto
fwd
=
f
::
OpRegistry
::
CreateOp
(
"rowwise_add"
,
{
"X"
,
"b"
},
{
"Out"
},
{});
ASSERT_NE
(
fwd
,
nullptr
);
}
\ No newline at end of file
paddle/framework/op_registry.h
浏览文件 @
00615ebc
...
...
@@ -86,43 +86,46 @@ class OpProtoAndCheckerMaker {
}
protected:
void
AddInput
(
const
std
::
string
&
name
,
const
std
::
string
&
comment
,
bool
multiple
=
false
,
bool
ignore_gradient
=
false
)
{
struct
VariableBuilder
{
VarProto
*
var_
;
std
::
function
<
void
()
>
on_multiple_
;
std
::
function
<
void
()
>
on_temporary_
;
VariableBuilder
&
SetMultiple
()
{
var_
->
set_multiple
(
true
);
on_multiple_
();
return
*
this
;
}
VariableBuilder
&
SetTemporary
()
{
PADDLE_ENFORCE
(
bool
(
on_temporary_
),
"Cannot set temporary"
);
var_
->
set_temporary
(
true
);
on_temporary_
();
return
*
this
;
}
VariableBuilder
&
IgnoreGradient
()
{
var_
->
set_ignore_gradient
(
true
);
return
*
this
;
}
};
VariableBuilder
AddInput
(
const
std
::
string
&
name
,
const
std
::
string
&
comment
)
{
auto
input
=
proto_
->
mutable_inputs
()
->
Add
();
*
input
->
mutable_name
()
=
name
;
*
input
->
mutable_comment
()
=
comment
;
input
->
set_ignore_gradient
(
ignore_gradient
);
input
->
set_multiple
(
multiple
);
if
(
multiple
)
{
SetHasMultipleInput
();
}
}
void
AddInputs
(
const
std
::
string
&
name
,
const
std
::
string
&
comment
,
bool
ignore_gradient
=
false
)
{
AddInput
(
name
,
comment
,
true
,
ignore_gradient
);
return
VariableBuilder
{
input
,
[
=
]
{
this
->
SetHasMultipleInput
();
},
nullptr
};
}
void
AddOutput
(
const
std
::
string
&
name
,
const
std
::
string
&
comment
,
bool
temporary
=
false
,
bool
multiple
=
false
,
bool
ignore_gradient
=
false
)
{
VariableBuilder
AddOutput
(
const
std
::
string
&
name
,
const
std
::
string
&
comment
)
{
auto
output
=
proto_
->
mutable_outputs
()
->
Add
();
*
output
->
mutable_name
()
=
name
;
*
output
->
mutable_comment
()
=
comment
;
output
->
set_ignore_gradient
(
ignore_gradient
);
output
->
set_multiple
(
multiple
);
if
(
multiple
)
{
SetHasMultipleOutput
();
}
output
->
set_temporary
(
temporary
);
if
(
temporary
)
{
SetHasTemporaryOutput
();
}
}
void
AddOutputs
(
const
std
::
string
&
name
,
const
std
::
string
&
comment
,
bool
temporary
=
false
,
bool
ignore_gradient
=
false
)
{
AddOutput
(
name
,
comment
,
temporary
,
true
,
ignore_gradient
);
return
VariableBuilder
{
output
,
[
=
]
{
this
->
SetHasMultipleOutput
();
},
[
=
]
{
this
->
SetHasTemporaryOutput
();
}};
}
template
<
typename
T
>
...
...
paddle/framework/op_registry_test.cc
浏览文件 @
00615ebc
...
...
@@ -36,9 +36,8 @@ class MyTestOpProtoAndCheckerMaker : public OpProtoAndCheckerMaker {
public:
MyTestOpProtoAndCheckerMaker
(
OpProto
*
proto
,
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInputs
(
"input"
,
"input of cosine op"
);
AddOutput
(
"output"
,
"output of cosine op"
,
/*temporary*/
true
);
AddInput
(
"input"
,
"input of cosine op"
).
SetMultiple
();
AddOutput
(
"output"
,
"output of cosine op"
).
SetTemporary
();
auto
my_checker
=
[](
int
i
)
{
PADDLE_ENFORCE
(
i
%
2
==
0
,
"'test_attr' must be even!"
);
};
...
...
paddle/framework/operator_test.cc
浏览文件 @
00615ebc
...
...
@@ -137,9 +137,9 @@ class OpKernelTestMultiInputsProtoAndCheckerMaker
OpKernelTestMultiInputsProtoAndCheckerMaker
(
OpProto
*
proto
,
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
s
(
"xs"
,
"inputs of test op"
);
AddInput
(
"xs"
,
"inputs of test op"
).
SetMultiple
(
);
AddInput
(
"k"
,
"input of test op"
);
AddOutput
s
(
"ys"
,
"outputs of test op"
);
AddOutput
(
"ys"
,
"outputs of test op"
).
SetMultiple
(
);
AddAttr
<
float
>
(
"scale"
,
"scale of cosine op"
)
.
SetDefault
(
1.0
)
.
LargerThan
(
0.0
);
...
...
paddle/operators/fc_op.cc
浏览文件 @
00615ebc
...
...
@@ -50,8 +50,8 @@ public:
AddInput
(
"b"
,
"the bias of fc operator"
);
AddOutput
(
"Y"
,
"the output of fc operator"
);
AddOutput
(
"before_act"
,
"the before activation output of fc operator"
,
true
);
AddOutput
(
"before_act"
,
"the before activation output of fc operator"
)
.
SetTemporary
(
);
AddAttr
<
std
::
string
>
(
"activation"
,
"The activation key for fc layer"
)
.
SetDefault
(
"sigmoid"
)
.
InEnum
({
"sigmoid"
,
"softmax"
});
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录