Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
3e2be065
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
大约 1 年 前同步成功
通知
694
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
3e2be065
编写于
10月 05, 2017
作者:
K
kavyasrinet
提交者:
GitHub
10月 05, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #4604 from kavyasrinet/activations
Added Leaky Relu activation
上级
828c5b3e
ba9a0bcc
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
65 addition
and
1 deletion
+65
-1
paddle/operators/activation_op.cc
paddle/operators/activation_op.cc
+19
-0
paddle/operators/activation_op.h
paddle/operators/activation_op.h
+29
-1
python/paddle/v2/framework/tests/test_activation_op.py
python/paddle/v2/framework/tests/test_activation_op.py
+17
-0
未找到文件。
paddle/operators/activation_op.cc
浏览文件 @
3e2be065
...
...
@@ -69,6 +69,22 @@ class ReluOpMaker : public framework::OpProtoAndCheckerMaker {
}
};
template
<
typename
AttrType
>
class
LeakyReluOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
LeakyReluOpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
"Input of LeakyRelu operator"
);
AddOutput
(
"Y"
,
"Output of LeakyRelu operator"
);
AddComment
(
"LeakyRelu activation operator, "
"leaky_relu = max(x, alpha * x)"
);
AddAttr
<
AttrType
>
(
"alpha"
,
"The small negative slope"
)
.
SetDefault
(
static_cast
<
AttrType
>
(
0.02
f
));
}
};
class
TanhOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
TanhOpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
...
...
@@ -240,6 +256,9 @@ REGISTER_OP(softsign, ops::ActivationOp, ops::SoftsignOpMaker, softsign_grad,
REGISTER_OP
(
brelu
,
ops
::
ActivationOp
,
ops
::
BReluOpMaker
<
float
>
,
brelu_grad
,
ops
::
ActivationOpGrad
);
REGISTER_OP
(
leaky_relu
,
ops
::
ActivationOp
,
ops
::
LeakyReluOpMaker
<
float
>
,
leaky_relu_grad
,
ops
::
ActivationOpGrad
);
REGISTER_OP
(
soft_relu
,
ops
::
ActivationOp
,
ops
::
SoftReluOpMaker
<
float
>
,
soft_relu_grad
,
ops
::
ActivationOpGrad
);
...
...
paddle/operators/activation_op.h
浏览文件 @
3e2be065
...
...
@@ -309,6 +309,33 @@ struct SoftReluGradFunctor : public BaseActivationFunctor<T> {
}
};
template
<
typename
T
>
struct
LeakyReluFunctor
:
public
BaseActivationFunctor
<
T
>
{
float
alpha
;
typename
BaseActivationFunctor
<
T
>::
AttrPair
GetAttrs
()
{
return
{{
"alpha"
,
&
alpha
}};
}
template
<
typename
Device
,
typename
X
,
typename
Y
>
void
operator
()(
Device
d
,
X
x
,
Y
y
)
const
{
y
.
device
(
d
)
=
x
.
cwiseMax
(
alpha
*
x
);
}
};
template
<
typename
T
>
struct
LeakyReluGradFunctor
:
public
BaseActivationFunctor
<
T
>
{
float
alpha
;
typename
BaseActivationFunctor
<
T
>::
AttrPair
GetAttrs
()
{
return
{{
"alpha"
,
&
alpha
}};
}
template
<
typename
Device
,
typename
X
,
typename
Y
,
typename
dY
,
typename
dX
>
void
operator
()(
Device
d
,
X
x
,
Y
y
,
dY
dy
,
dX
dx
)
const
{
auto
temp1
=
alpha
*
(
x
<
static_cast
<
T
>
(
0
)).
template
cast
<
T
>().
eval
();
auto
temp2
=
(
x
>=
static_cast
<
T
>
(
0
)).
template
cast
<
T
>().
eval
();
dx
.
device
(
d
)
=
dy
*
(
temp1
+
temp2
).
template
cast
<
T
>();
}
};
template
<
typename
T
>
struct
PowFunctor
:
public
BaseActivationFunctor
<
T
>
{
float
factor
;
...
...
@@ -379,4 +406,5 @@ struct STanhGradFunctor : public BaseActivationFunctor<T> {
__macro(soft_relu, SoftReluFunctor, SoftReluGradFunctor); \
__macro(pow, PowFunctor, PowGradFunctor); \
__macro(stanh, STanhFunctor, STanhGradFunctor); \
__macro(softsign, SoftsignFunctor, SoftsignGradFunctor)
__macro(softsign, SoftsignFunctor, SoftsignGradFunctor); \
__macro(leaky_relu, LeakyReluFunctor, LeakyReluGradFunctor)
python/paddle/v2/framework/tests/test_activation_op.py
浏览文件 @
3e2be065
...
...
@@ -122,6 +122,23 @@ class TestBRelu(OpTest):
self
.
check_grad
([
'X'
],
'Y'
,
max_relative_error
=
0.02
)
class
TestLeakyRelu
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"leaky_relu"
alpha
=
0.02
self
.
attrs
=
{
'alpha'
:
alpha
}
self
.
inputs
=
{
'X'
:
np
.
random
.
uniform
(
-
3
,
3
,
[
4
,
4
]).
astype
(
"float32"
)}
self
.
outputs
=
{
'Y'
:
np
.
maximum
(
self
.
inputs
[
'X'
],
alpha
*
self
.
inputs
[
'X'
])
}
def
test_check_output
(
self
):
self
.
check_output
()
def
test_check_grad
(
self
):
self
.
check_grad
([
'X'
],
'Y'
,
max_relative_error
=
0.007
)
class
TestSoftRelu
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"soft_relu"
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录