Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
490ca5f1
P
Paddle
项目概览
机器未来
/
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
490ca5f1
编写于
9月 14, 2017
作者:
Z
zchen0211
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
prelu_op
上级
e615c84a
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
20 addition
and
39 deletion
+20
-39
paddle/operators/prelu_op.cc
paddle/operators/prelu_op.cc
+9
-7
paddle/operators/prelu_op.cu
paddle/operators/prelu_op.cu
+0
-21
paddle/operators/prelu_op.h
paddle/operators/prelu_op.h
+8
-9
python/paddle/v2/framework/tests/test_prelu_op.py
python/paddle/v2/framework/tests/test_prelu_op.py
+3
-2
未找到文件。
paddle/operators/prelu_op.cc
浏览文件 @
490ca5f1
...
...
@@ -33,20 +33,20 @@ class PreluOp : public framework::OperatorWithKernel {
}
};
template
<
typename
AttrType
>
//
template <typename AttrType>
class
PreluOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
PreluOpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
"The input tensor of prelu operator."
)
.
NotInGradient
()
;
AddOutput
(
"Out"
,
"The output tensor of prelu operator."
)
.
NotInGradient
()
;
AddInput
(
"X"
,
"The input tensor of prelu operator."
);
AddOutput
(
"Out"
,
"The output tensor of prelu operator."
);
AddComment
(
R"DOC(Prelu operator
The equation is:
f(x) = alpha * x , for x < 0
f(x) = x , for x >= 0
)DOC"
);
AddAttr
<
AttrType
>
(
"alpha"
,
"The scaling factor alpha of prelu."
)
AddAttr
<
float
>
(
"alpha"
,
"The scaling factor alpha of prelu."
)
.
SetDefault
(
0.0
);
}
};
...
...
@@ -58,8 +58,10 @@ class PreluGradOp : public framework::OperatorWithKernel {
protected:
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{
auto
X_grad
=
ctx
.
Output
<
framework
::
LoDTensor
>
(
framework
::
GradVarName
(
"X"
));
auto
X
=
ctx
.
Input
<
Tensor
>
(
"X"
);
PADDLE_ENFORCE_NOT_NULL
(
ctx
.
InputVar
(
"X"
),
"Input(X) must not be null."
);
auto
*
X_grad
=
ctx
.
Output
<
framework
::
LoDTensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
X
=
ctx
.
Input
<
framework
::
Tensor
>
(
"X"
);
X_grad
->
Resize
(
X
->
dims
());
}
...
...
@@ -70,7 +72,7 @@ class PreluGradOp : public framework::OperatorWithKernel {
namespace
ops
=
paddle
::
operators
;
REGISTER_OP
(
prelu
,
ops
::
PreluOp
,
ops
::
PreluOpMaker
<
float
>
,
prelu_grad
,
REGISTER_OP
(
prelu
,
ops
::
PreluOp
,
ops
::
PreluOpMaker
,
prelu_grad
,
ops
::
PreluGradOp
);
REGISTER_OP_CPU_KERNEL
(
prelu
,
ops
::
PreluKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
...
...
paddle/operators/prelu_op.cu
已删除
100644 → 0
浏览文件 @
e615c84a
/* 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 "paddle/operators/prelu_op.h"
REGISTER_OP_GPU_KERNEL
(
prelu
,
paddle
::
operators
::
PreluKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
);
REGISTER_OP_GPU_KERNEL
(
prelu_grad
,
paddle
::
operators
::
PreluGradKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
);
paddle/operators/prelu_op.h
浏览文件 @
490ca5f1
...
...
@@ -24,7 +24,7 @@ template <typename T, int MajorType = Eigen::RowMajor,
typename
IndexType
=
Eigen
::
DenseIndex
>
using
EigenVector
=
framework
::
EigenVector
<
T
,
MajorType
,
IndexType
>
;
template
<
typename
Place
,
typename
T
,
typename
AttrType
=
T
>
template
<
typename
Place
,
typename
T
>
class
PreluKernel
:
public
framework
::
OpKernel
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
...
...
@@ -33,30 +33,29 @@ class PreluKernel : public framework::OpKernel {
Out
->
mutable_data
<
T
>
(
context
.
GetPlace
());
auto
alpha
=
static_cast
<
T
>
(
context
.
Attr
<
AttrType
>
(
"alpha"
));
auto
alpha
=
static_cast
<
T
>
(
context
.
Attr
<
float
>
(
"alpha"
));
auto
X_vec
=
EigenVector
<
T
>::
Flatten
(
*
X
);
auto
Out_vec
=
EigenVector
<
T
>::
Flatten
(
*
Out
);
auto
place
=
context
.
GetEigenDevice
<
Place
>
();
Out_vec
.
device
(
place
)
=
X_vec
.
cwiseMax
(
0.
f
)
+
X_vec
.
cwiseMin
(
0.
f
)
*
alpha
;
//
auto place = context.GetEigenDevice<Place>();
// Out_vec.device(place)
Out_vec
=
X_vec
.
cwiseMax
(
0.
f
)
+
X_vec
.
cwiseMin
(
0.
f
)
*
alpha
;
}
};
template
<
typename
Place
,
typename
T
,
typename
AttrType
=
T
>
template
<
typename
Place
,
typename
T
>
class
PreluGradKernel
:
public
framework
::
OpKernel
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
*
dX
=
context
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
dO
=
context
.
Input
<
Tensor
>
(
framework
::
GradVarName
(
"Out"
));
auto
*
Out
=
context
.
Out
put
<
Tensor
>
(
"Out"
);
auto
*
Out
=
context
.
In
put
<
Tensor
>
(
"Out"
);
auto
alpha
=
static_cast
<
T
>
(
context
.
Attr
<
AttrType
>
(
"alpha"
));
auto
alpha
=
static_cast
<
T
>
(
context
.
Attr
<
float
>
(
"alpha"
));
dX
->
mutable_data
<
T
>
(
context
.
GetPlace
());
for
(
int
i
=
0
;
i
<
dX
->
numel
();
++
i
)
{
if
(
Out
->
data
<
T
>
()[
i
]
>
0
)
{
dX
->
data
<
T
>
()[
i
]
=
dO
->
data
<
T
>
()[
i
];
...
...
python/paddle/v2/framework/tests/test_prelu_op.py
浏览文件 @
490ca5f1
...
...
@@ -6,11 +6,12 @@ from op_test import OpTest
class
PreluTest
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"prelu"
self
.
inputs
=
{
'X'
:
np
.
random
.
random
((
10
,
10
)).
astype
(
"float32"
)}
self
.
inputs
=
{
'X'
:
np
.
random
.
normal
(
size
=
(
3
,
5
)).
astype
(
"float32"
)}
self
.
attrs
=
{
'alpha'
:
0.1
}
out_np
=
np
.
maximum
(
self
.
inputs
[
'X'
],
0.
)
out_np
=
out_np
+
np
.
minimum
(
self
.
inputs
[
'X'
],
0.
)
*
self
.
attrs
[
'alpha'
]
self
.
outputs
=
{
'Out'
:
self
.
inputs
[
'X'
]
*
self
.
attrs
[
'scale'
]}
assert
out_np
is
not
self
.
inputs
[
'X'
]
self
.
outputs
=
{
'Out'
:
out_np
}
def
test_check_output
(
self
):
self
.
check_output
()
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录