Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
a815d6ab
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看板
提交
a815d6ab
编写于
9月 29, 2017
作者:
Z
zhouxiao-coder
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
elu: Optimize gradient calculation;Add more comments
上级
a2657fea
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
89 addition
and
0 deletion
+89
-0
paddle/operators/activation_op.cc
paddle/operators/activation_op.cc
+25
-0
paddle/operators/activation_op.cu
paddle/operators/activation_op.cu
+4
-0
paddle/operators/activation_op.h
paddle/operators/activation_op.h
+40
-0
python/paddle/v2/framework/tests/test_activation_op.py
python/paddle/v2/framework/tests/test_activation_op.py
+20
-0
未找到文件。
paddle/operators/activation_op.cc
浏览文件 @
a815d6ab
...
@@ -174,6 +174,25 @@ class SoftReluOpMaker : public framework::OpProtoAndCheckerMaker {
...
@@ -174,6 +174,25 @@ class SoftReluOpMaker : public framework::OpProtoAndCheckerMaker {
}
}
};
};
template
<
typename
AttrType
>
class
ELUOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
ELUOpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
"Input of ELU operator, it shouldn't be empty. Input is flattened "
"and treated as a 1D array."
);
AddOutput
(
"Y"
,
"Output of ELU operator, has same shape as the input."
);
AddComment
(
"ELU activation operator. It applies this element-wise computation on "
"the input: f(x) = max(0, x) + min(0, alpha * (exp(x) - 1))."
"Check .. _Link: https://arxiv.org/abs/1511.07289 for more details"
);
AddAttr
<
AttrType
>
(
"alpha"
,
"alpha value in the elu formulation, default to 1."
)
.
SetDefault
(
static_cast
<
AttrType
>
(
1.
));
}
};
template
<
typename
AttrType
>
template
<
typename
AttrType
>
class
PowOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
class
PowOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
public:
...
@@ -311,6 +330,12 @@ REGISTER_OP_CPU_KERNEL(soft_relu,
...
@@ -311,6 +330,12 @@ REGISTER_OP_CPU_KERNEL(soft_relu,
REGISTER_OP_CPU_KERNEL
(
REGISTER_OP_CPU_KERNEL
(
soft_relu_grad
,
ops
::
SoftReluGradKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
soft_relu_grad
,
ops
::
SoftReluGradKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
REGISTER_OP
(
elu
,
ops
::
ActivationOp
,
ops
::
ELUOpMaker
<
float
>
,
elu_grad
,
ops
::
ActivationOpGrad
);
REGISTER_OP_CPU_KERNEL
(
elu
,
ops
::
ELUKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
REGISTER_OP_CPU_KERNEL
(
elu_grad
,
ops
::
ELUGradKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
REGISTER_OP
(
pow
,
ops
::
ActivationOp
,
ops
::
PowOpMaker
<
float
>
,
pow_grad
,
REGISTER_OP
(
pow
,
ops
::
ActivationOp
,
ops
::
PowOpMaker
<
float
>
,
pow_grad
,
ops
::
ActivationOpGrad
);
ops
::
ActivationOpGrad
);
REGISTER_OP_CPU_KERNEL
(
pow
,
ops
::
PowKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
REGISTER_OP_CPU_KERNEL
(
pow
,
ops
::
PowKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
...
...
paddle/operators/activation_op.cu
浏览文件 @
a815d6ab
...
@@ -97,6 +97,10 @@ REGISTER_OP_GPU_KERNEL(soft_relu,
...
@@ -97,6 +97,10 @@ REGISTER_OP_GPU_KERNEL(soft_relu,
REGISTER_OP_GPU_KERNEL
(
REGISTER_OP_GPU_KERNEL
(
soft_relu_grad
,
ops
::
SoftReluGradKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
);
soft_relu_grad
,
ops
::
SoftReluGradKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
);
REGISTER_OP_GPU_KERNEL
(
elu
,
ops
::
ELUKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
);
REGISTER_OP_GPU_KERNEL
(
elu_grad
,
ops
::
ELUGradKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
);
REGISTER_OP_GPU_KERNEL
(
pow
,
ops
::
PowKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
);
REGISTER_OP_GPU_KERNEL
(
pow
,
ops
::
PowKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
);
REGISTER_OP_GPU_KERNEL
(
pow_grad
,
REGISTER_OP_GPU_KERNEL
(
pow_grad
,
ops
::
PowGradKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
);
ops
::
PowGradKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
);
...
...
paddle/operators/activation_op.h
浏览文件 @
a815d6ab
...
@@ -296,6 +296,46 @@ class SoftReluGradKernel : public framework::OpKernel<T> {
...
@@ -296,6 +296,46 @@ class SoftReluGradKernel : public framework::OpKernel<T> {
}
}
};
};
template
<
typename
Place
,
typename
T
,
typename
AttrType
=
T
>
class
ELUKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
*
X
=
context
.
Input
<
framework
::
Tensor
>
(
"X"
);
auto
*
Y
=
context
.
Output
<
framework
::
Tensor
>
(
"Y"
);
auto
alpha
=
static_cast
<
T
>
(
context
.
Attr
<
AttrType
>
(
"alpha"
));
Y
->
mutable_data
<
T
>
(
context
.
GetPlace
());
auto
x
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
X
);
auto
y
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
Y
);
auto
place
=
context
.
GetEigenDevice
<
Place
>
();
y
.
device
(
place
)
=
x
.
cwiseMax
(
static_cast
<
T
>
(
0
))
+
(
alpha
*
(
x
.
exp
()
-
static_cast
<
T
>
(
1
))).
cwiseMin
(
static_cast
<
T
>
(
0
));
}
};
template
<
typename
Place
,
typename
T
,
typename
AttrType
=
T
>
class
ELUGradKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
*
X
=
context
.
Input
<
framework
::
Tensor
>
(
"X"
);
auto
*
Y
=
context
.
Input
<
framework
::
Tensor
>
(
"Y"
);
auto
*
dY
=
context
.
Input
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"Y"
));
auto
*
dX
=
context
.
Output
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"X"
));
auto
alpha
=
static_cast
<
T
>
(
context
.
Attr
<
AttrType
>
(
"alpha"
));
dX
->
mutable_data
<
T
>
(
context
.
GetPlace
());
auto
x
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
X
);
auto
y
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
Y
);
auto
dy
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
dY
);
auto
dx
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
dX
);
auto
place
=
context
.
GetEigenDevice
<
Place
>
();
dx
.
device
(
place
)
=
dy
*
(
x
>
static_cast
<
T
>
(
0
)).
template
cast
<
T
>()
+
dy
*
(
y
+
alpha
)
*
(
x
<
static_cast
<
T
>
(
0
)).
template
cast
<
T
>();
}
};
template
<
typename
Place
,
typename
T
,
typename
AttrType
=
T
>
template
<
typename
Place
,
typename
T
,
typename
AttrType
=
T
>
class
PowKernel
:
public
framework
::
OpKernel
<
T
>
{
class
PowKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
public:
...
...
python/paddle/v2/framework/tests/test_activation_op.py
浏览文件 @
a815d6ab
...
@@ -144,6 +144,26 @@ class TestSoftRelu(OpTest):
...
@@ -144,6 +144,26 @@ class TestSoftRelu(OpTest):
self
.
check_grad
([
'X'
],
'Y'
,
max_relative_error
=
0.02
)
self
.
check_grad
([
'X'
],
'Y'
,
max_relative_error
=
0.02
)
class
TestELU
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"elu"
x
=
np
.
random
.
uniform
(
-
3
,
3
,
[
4
,
4
]).
astype
(
"float32"
)
alpha
=
1.
# Note: unlike other Relu extensions, point 0 on standard ELU function (i.e. alpha = 1)
# is differentiable, so we can skip modifications like x[np.abs(x) < 0.005] = 0.02 here
self
.
inputs
=
{
'X'
:
x
}
self
.
attrs
=
{
'alpha'
:
alpha
}
self
.
outputs
=
{
'Y'
:
np
.
maximum
(
0
,
x
)
+
np
.
minimum
(
0
,
alpha
*
(
np
.
exp
(
x
)
-
1
))
}
def
test_check_output
(
self
):
self
.
check_output
()
def
test_check_grad
(
self
):
self
.
check_grad
([
'X'
],
'Y'
,
max_relative_error
=
0.02
)
class
TestReciprocal
(
OpTest
):
class
TestReciprocal
(
OpTest
):
def
setUp
(
self
):
def
setUp
(
self
):
self
.
op_type
=
"reciprocal"
self
.
op_type
=
"reciprocal"
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录