Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wmsofts
Paddle
提交
f30a1f42
P
Paddle
项目概览
wmsofts
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
f30a1f42
编写于
10月 09, 2017
作者:
K
kavyasrinet
提交者:
GitHub
10月 09, 2017
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Adding relu6 activation function (#4607)
上级
d9585f9a
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
59 addition
and
7 deletion
+59
-7
paddle/operators/activation_op.cc
paddle/operators/activation_op.cc
+16
-0
paddle/operators/activation_op.h
paddle/operators/activation_op.h
+31
-0
python/paddle/v2/framework/tests/test_activation_op.py
python/paddle/v2/framework/tests/test_activation_op.py
+12
-7
未找到文件。
paddle/operators/activation_op.cc
浏览文件 @
f30a1f42
...
@@ -201,6 +201,19 @@ class SoftReluOpMaker : public framework::OpProtoAndCheckerMaker {
...
@@ -201,6 +201,19 @@ class SoftReluOpMaker : public framework::OpProtoAndCheckerMaker {
}
}
};
};
template
<
typename
AttrType
>
class
Relu6OpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
Relu6OpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
"Input of Relu6 operator"
);
AddOutput
(
"Y"
,
"Output of Relu6 operator"
);
AddComment
(
"Relu6 activation operator, relu6 = min(max(0, x), 6)"
);
AddAttr
<
AttrType
>
(
"threshold"
,
"The threshold value of Relu6"
)
.
SetDefault
(
static_cast
<
AttrType
>
(
6
));
}
};
template
<
typename
AttrType
>
template
<
typename
AttrType
>
class
PowOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
class
PowOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
public:
...
@@ -276,6 +289,9 @@ REGISTER_OP(leaky_relu, ops::ActivationOp, ops::LeakyReluOpMaker<float>,
...
@@ -276,6 +289,9 @@ REGISTER_OP(leaky_relu, ops::ActivationOp, ops::LeakyReluOpMaker<float>,
REGISTER_OP
(
soft_relu
,
ops
::
ActivationOp
,
ops
::
SoftReluOpMaker
<
float
>
,
REGISTER_OP
(
soft_relu
,
ops
::
ActivationOp
,
ops
::
SoftReluOpMaker
<
float
>
,
soft_relu_grad
,
ops
::
ActivationOpGrad
);
soft_relu_grad
,
ops
::
ActivationOpGrad
);
REGISTER_OP
(
relu6
,
ops
::
ActivationOp
,
ops
::
Relu6OpMaker
<
float
>
,
relu6_grad
,
ops
::
ActivationOpGrad
);
REGISTER_OP
(
pow
,
ops
::
ActivationOp
,
ops
::
PowOpMaker
<
float
>
,
pow_grad
,
REGISTER_OP
(
pow
,
ops
::
ActivationOp
,
ops
::
PowOpMaker
<
float
>
,
pow_grad
,
ops
::
ActivationOpGrad
);
ops
::
ActivationOpGrad
);
...
...
paddle/operators/activation_op.h
浏览文件 @
f30a1f42
...
@@ -280,6 +280,36 @@ struct BReluGradFunctor : public BaseActivationFunctor<T> {
...
@@ -280,6 +280,36 @@ struct BReluGradFunctor : public BaseActivationFunctor<T> {
}
}
};
};
// relu6(x) = min(max(0, x), 6)
template
<
typename
T
>
struct
Relu6Functor
:
public
BaseActivationFunctor
<
T
>
{
float
threshold
;
// NOTE: Explicit hides the `BaseActivationFunctor<T>::GetAttrs`
// not polymorphism for speed.
typename
BaseActivationFunctor
<
T
>::
AttrPair
GetAttrs
()
{
return
{{
"threshold"
,
&
threshold
}};
}
template
<
typename
Device
,
typename
X
,
typename
Y
>
void
operator
()(
Device
d
,
X
x
,
Y
y
)
const
{
y
.
device
(
d
)
=
x
.
cwiseMax
(
static_cast
<
T
>
(
0
)).
cwiseMin
(
threshold
);
}
};
template
<
typename
T
>
struct
Relu6GradFunctor
:
public
BaseActivationFunctor
<
T
>
{
float
threshold
;
typename
BaseActivationFunctor
<
T
>::
AttrPair
GetAttrs
()
{
return
{{
"threshold"
,
&
threshold
}};
}
template
<
typename
Device
,
typename
X
,
typename
Y
,
typename
dY
,
typename
dX
>
void
operator
()(
Device
d
,
X
x
,
Y
y
,
dY
dy
,
dX
dx
)
const
{
dx
.
device
(
d
)
=
dy
*
((
x
>
static_cast
<
T
>
(
0
))
*
(
x
<
threshold
)).
template
cast
<
T
>();
}
};
// softsign(x) = x / (1 + |x|)
// softsign(x) = x / (1 + |x|)
template
<
typename
T
>
template
<
typename
T
>
struct
SoftsignFunctor
:
public
BaseActivationFunctor
<
T
>
{
struct
SoftsignFunctor
:
public
BaseActivationFunctor
<
T
>
{
...
@@ -425,5 +455,6 @@ struct STanhGradFunctor : public BaseActivationFunctor<T> {
...
@@ -425,5 +455,6 @@ struct STanhGradFunctor : public BaseActivationFunctor<T> {
__macro(pow, PowFunctor, PowGradFunctor); \
__macro(pow, PowFunctor, PowGradFunctor); \
__macro(stanh, STanhFunctor, STanhGradFunctor); \
__macro(stanh, STanhFunctor, STanhGradFunctor); \
__macro(softsign, SoftsignFunctor, SoftsignGradFunctor); \
__macro(softsign, SoftsignFunctor, SoftsignGradFunctor); \
__macro(relu6, Relu6Functor, Relu6GradFunctor); \
__macro(leaky_relu, LeakyReluFunctor, LeakyReluGradFunctor); \
__macro(leaky_relu, LeakyReluFunctor, LeakyReluGradFunctor); \
__macro(tanh_shrink, TanhShrinkFunctor, TanhShrinkGradFunctor)
__macro(tanh_shrink, TanhShrinkFunctor, TanhShrinkGradFunctor)
python/paddle/v2/framework/tests/test_activation_op.py
浏览文件 @
f30a1f42
...
@@ -137,21 +137,26 @@ class TestBRelu(OpTest):
...
@@ -137,21 +137,26 @@ class TestBRelu(OpTest):
self
.
check_grad
([
'X'
],
'Y'
,
max_relative_error
=
0.02
)
self
.
check_grad
([
'X'
],
'Y'
,
max_relative_error
=
0.02
)
class
Test
LeakyRelu
(
OpTest
):
class
Test
Relu6
(
OpTest
):
def
setUp
(
self
):
def
setUp
(
self
):
self
.
op_type
=
"leaky_relu"
self
.
op_type
=
"relu6"
alpha
=
0.02
x
=
np
.
random
.
uniform
(
-
1
,
1
,
[
4
,
10
]).
astype
(
"float32"
)
self
.
attrs
=
{
'alpha'
:
alpha
}
threshold
=
6.0
self
.
inputs
=
{
'X'
:
np
.
random
.
uniform
(
-
3
,
3
,
[
4
,
4
]).
astype
(
"float32"
)}
# The same with TestAbs
x
[
np
.
abs
(
x
)
<
0.005
]
=
0.02
x
[
np
.
abs
(
x
-
threshold
)
<
0.005
]
=
threshold
+
0.02
self
.
inputs
=
{
'X'
:
x
}
self
.
attrs
=
{
'threshold'
:
threshold
}
self
.
outputs
=
{
self
.
outputs
=
{
'Y'
:
np
.
m
aximum
(
self
.
inputs
[
'X'
],
alpha
*
self
.
inputs
[
'X'
]
)
'Y'
:
np
.
m
inimum
(
np
.
maximum
(
self
.
inputs
[
'X'
],
0
),
threshold
)
}
}
def
test_check_output
(
self
):
def
test_check_output
(
self
):
self
.
check_output
()
self
.
check_output
()
def
test_check_grad
(
self
):
def
test_check_grad
(
self
):
self
.
check_grad
([
'X'
],
'Y'
,
max_relative_error
=
0.0
07
)
self
.
check_grad
([
'X'
],
'Y'
,
max_relative_error
=
0.0
2
)
class
TestSoftRelu
(
OpTest
):
class
TestSoftRelu
(
OpTest
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录