Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
d0cfbba4
P
Paddle
项目概览
Crayon鑫
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
d0cfbba4
编写于
10月 21, 2017
作者:
G
Guo Sheng
提交者:
GitHub
10月 21, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #4496 from guoshengCS/add-NormOp
Add norm_op
上级
5fd4bee2
b12988f0
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
91 addition
and
0 deletion
+91
-0
paddle/operators/reduce_op.cc
paddle/operators/reduce_op.cc
+63
-0
python/paddle/v2/framework/tests/test_reduce_op.py
python/paddle/v2/framework/tests/test_reduce_op.py
+28
-0
未找到文件。
paddle/operators/reduce_op.cc
浏览文件 @
d0cfbba4
...
...
@@ -13,6 +13,7 @@
limitations under the License. */
#include "paddle/operators/reduce_op.h"
#include "paddle/operators/net_op.h"
namespace
paddle
{
namespace
operators
{
...
...
@@ -159,6 +160,66 @@ class ReduceMinOpMaker : public ReduceOpMaker {
}
};
class
NormOp
:
public
NetOp
{
public:
NormOp
(
const
std
::
string
&
type
,
const
framework
::
VariableNameMap
&
inputs
,
const
framework
::
VariableNameMap
&
outputs
,
const
framework
::
AttributeMap
&
attrs
)
:
NetOp
(
type
,
inputs
,
outputs
,
attrs
)
{
PADDLE_ENFORCE_NE
(
Input
(
"X"
),
framework
::
kEmptyVarName
,
"Input(X) of NormOp should not be null."
);
PADDLE_ENFORCE_NE
(
Output
(
"AbsOut"
),
framework
::
kEmptyVarName
,
"Output(AbsOut) of NormOp should not be null."
);
PADDLE_ENFORCE_NE
(
Output
(
"PowOut"
),
framework
::
kEmptyVarName
,
"Output(PowOut) of NormOp should not be null."
);
PADDLE_ENFORCE_NE
(
Output
(
"SumOut"
),
framework
::
kEmptyVarName
,
"Output(SumOut) of NormOp should not be null."
);
PADDLE_ENFORCE_NE
(
Output
(
"Out"
),
framework
::
kEmptyVarName
,
"Output(Out) of NormOp should not be null."
);
auto
dim
=
Attr
<
int
>
(
"dim"
);
auto
keep_dim
=
Attr
<
bool
>
(
"keep_dim"
);
auto
p
=
Attr
<
float
>
(
"p"
);
PADDLE_ENFORCE_GT
(
p
,
0
,
"Order of the norm should be positive."
);
AppendOp
(
framework
::
OpRegistry
::
CreateOp
(
"abs"
,
{{
"X"
,
{
Input
(
"X"
)}}},
{{
"Y"
,
{
Output
(
"AbsOut"
)}}},
{}));
AppendOp
(
framework
::
OpRegistry
::
CreateOp
(
"pow"
,
{{
"X"
,
{
Output
(
"AbsOut"
)}}},
{{
"Y"
,
{
Output
(
"PowOut"
)}}},
{{
"factor"
,
p
}}));
framework
::
AttributeMap
sum_attr
;
sum_attr
[
"dim"
]
=
dim
;
sum_attr
[
"keep_dim"
]
=
keep_dim
;
AppendOp
(
framework
::
OpRegistry
::
CreateOp
(
"reduce_sum"
,
{{
"X"
,
{
Output
(
"PowOut"
)}}},
{{
"Out"
,
{
Output
(
"SumOut"
)}}},
sum_attr
));
AppendOp
(
framework
::
OpRegistry
::
CreateOp
(
"pow"
,
{{
"X"
,
{
Output
(
"SumOut"
)}}},
{{
"Y"
,
{
Output
(
"Out"
)}}},
{{
"factor"
,
static_cast
<
float
>
(
1.
/
p
)}}));
CompleteAddOp
(
false
);
}
};
class
NormOpMaker
:
public
ReduceOpMaker
{
public:
NormOpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
:
ReduceOpMaker
(
proto
,
op_checker
)
{
AddOutput
(
"AbsOut"
,
"(Tensor) The intermediate output of Norm operator, "
"saving the absolute value of the input tensor X."
)
.
AsIntermediate
();
AddOutput
(
"PowOut"
,
"(Tensor) The intermediate output of Norm operator, "
"saving the p-th power of the output tensor AbsOut."
)
.
AsIntermediate
();
AddOutput
(
"SumOut"
,
"(Tensor) the intermediate output of Norm operator, "
"saving the sum of PowOut reduced on the given dimension."
)
.
AsIntermediate
();
AddAttr
<
float
>
(
"p"
,
"(float, default 2) The order of Norm."
).
SetDefault
(
2
);
SetComment
(
"Norm"
,
"vector p-norm"
);
AddComment
(
comment_
);
}
};
}
// namespace operators
}
// namespace paddle
...
...
@@ -176,6 +237,8 @@ REGISTER_OP(reduce_max, ops::ReduceOp, ops::ReduceMaxOpMaker, reduce_max_grad,
REGISTER_OP
(
reduce_min
,
ops
::
ReduceOp
,
ops
::
ReduceMinOpMaker
,
reduce_min_grad
,
ops
::
ReduceGradOp
);
REGISTER_OP_WITHOUT_GRADIENT
(
norm
,
ops
::
NormOp
,
ops
::
NormOpMaker
);
#define REGISTER_REDUCE_CPU_KERNEL(reduce_type, functor, grad_functor) \
REGISTER_OP_CPU_KERNEL( \
reduce_type, \
...
...
python/paddle/v2/framework/tests/test_reduce_op.py
浏览文件 @
d0cfbba4
...
...
@@ -85,5 +85,33 @@ class Test1DReduce(OpTest):
self
.
check_grad
([
'X'
],
'Out'
)
class
TestNorm
(
OpTest
):
def
setUp
(
self
):
# use x away from 0 to avoid errors of numerical gradient when gradient near 0
x
=
np
.
random
.
random
((
5
,
6
,
10
)).
astype
(
"float32"
)
+
0.2
p
=
2
dim
=
1
keep_dim
=
False
abs_out
=
np
.
absolute
(
x
)
pow_out
=
np
.
power
(
x
,
p
)
sum_out
=
np
.
sum
(
pow_out
,
axis
=
dim
,
keepdims
=
keep_dim
)
out
=
np
.
power
(
sum_out
,
1.
/
p
)
self
.
op_type
=
"norm"
self
.
inputs
=
{
'X'
:
x
}
self
.
attrs
=
{
"p"
:
p
,
"dim"
:
dim
,
"keep_dim"
:
keep_dim
}
self
.
outputs
=
{
"AbsOut"
:
abs_out
,
"PowOut"
:
pow_out
,
"SumOut"
:
sum_out
,
"Out"
:
out
}
def
test_check_output
(
self
):
self
.
check_output
()
def
test_check_grad
(
self
):
self
.
check_grad
([
'X'
],
'Out'
,
max_relative_error
=
0.01
)
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录