Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
113c026d
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看板
未验证
提交
113c026d
编写于
12月 07, 2017
作者:
A
Abhinav Arora
提交者:
GitHub
12月 07, 2017
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Swish activation operator (#6358)
上级
3a0a4586
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
65 addition
and
0 deletion
+65
-0
paddle/operators/activation_op.cc
paddle/operators/activation_op.cc
+19
-0
paddle/operators/activation_op.h
paddle/operators/activation_op.h
+30
-0
python/paddle/v2/fluid/tests/test_activation_op.py
python/paddle/v2/fluid/tests/test_activation_op.py
+16
-0
未找到文件。
paddle/operators/activation_op.cc
浏览文件 @
113c026d
...
...
@@ -506,6 +506,22 @@ It is recommended to use the defaults for this activation.
}
};
class
SwishOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
SwishOpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
"Input of Swish operator"
);
AddOutput
(
"Y"
,
"Output of Swish operator"
);
AddAttr
<
float
>
(
"beta"
,
"Constant beta of swish operator"
).
SetDefault
(
1.0
f
);
AddComment
(
R"DOC(
Swish Activation Operator.
$$y = \frac{x}{1 + e^{- \beta x}}$$
)DOC"
);
}
};
}
// namespace operators
}
// namespace paddle
...
...
@@ -592,6 +608,9 @@ REGISTER_OP(thresholded_relu, ops::ActivationOp, ops::ThresholdedReluOpMaker,
REGISTER_OP
(
hard_sigmoid
,
ops
::
ActivationOp
,
ops
::
HardSigmoidOpMaker
,
hard_sigmoid_grad
,
ops
::
ActivationOpGrad
);
REGISTER_OP
(
swish
,
ops
::
ActivationOp
,
ops
::
SwishOpMaker
,
swish_grad
,
ops
::
ActivationOpGrad
);
#define REGISTER_ACTIVATION_CPU_KERNEL(act_type, functor, grad_functor) \
REGISTER_OP_CPU_KERNEL( \
act_type, \
...
...
paddle/operators/activation_op.h
浏览文件 @
113c026d
...
...
@@ -700,6 +700,35 @@ struct HardSigmoidGradFunctor : public BaseActivationFunctor<T> {
}
};
template
<
typename
T
>
struct
SwishFunctor
:
public
BaseActivationFunctor
<
T
>
{
float
beta
;
typename
BaseActivationFunctor
<
T
>::
AttrPair
GetAttrs
()
{
return
{{
"beta"
,
&
beta
}};
}
template
<
typename
Device
,
typename
X
,
typename
Y
>
void
operator
()(
Device
d
,
X
x
,
Y
y
)
const
{
y
.
device
(
d
)
=
x
/
(
static_cast
<
T
>
(
1
)
+
(
static_cast
<
T
>
(
-
beta
)
*
x
).
exp
());
}
};
template
<
typename
T
>
struct
SwishGradFunctor
:
public
BaseActivationFunctor
<
T
>
{
float
beta
;
typename
BaseActivationFunctor
<
T
>::
AttrPair
GetAttrs
()
{
return
{{
"beta"
,
&
beta
}};
}
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
=
static_cast
<
T
>
(
1
)
/
(
static_cast
<
T
>
(
1
)
+
(
static_cast
<
T
>
(
-
beta
)
*
x
).
exp
());
auto
temp2
=
temp1
*
(
static_cast
<
T
>
(
1
)
-
(
beta
*
y
));
dx
.
device
(
d
)
=
dy
*
((
beta
*
y
)
+
temp2
);
}
};
}
// namespace operators
}
// namespace paddle
...
...
@@ -730,4 +759,5 @@ struct HardSigmoidGradFunctor : public BaseActivationFunctor<T> {
__macro(elu, ELUFunctor, ELUGradFunctor); \
__macro(hard_shrink, HardShrinkFunctor, HardShrinkGradFunctor); \
__macro(hard_sigmoid, HardSigmoidFunctor, HardSigmoidGradFunctor); \
__macro(swish, SwishFunctor, SwishGradFunctor); \
__macro(thresholded_relu, ThresholdedReluFunctor, ThresholdedReluGradFunctor);
python/paddle/v2/fluid/tests/test_activation_op.py
浏览文件 @
113c026d
import
unittest
import
numpy
as
np
from
op_test
import
OpTest
from
scipy.special
import
expit
class
TestExp
(
OpTest
):
...
...
@@ -455,5 +456,20 @@ class TestHardSigmoid(OpTest):
self
.
check_grad
([
'X'
],
'Y'
,
max_relative_error
=
0.002
)
class
TestSwish
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"swish"
X
=
np
.
random
.
uniform
(
0.1
,
1
,
[
11
,
17
]).
astype
(
"float32"
)
self
.
inputs
=
{
'X'
:
X
}
self
.
attrs
=
{
'beta'
:
2.3
}
self
.
outputs
=
{
'Y'
:
X
*
expit
(
self
.
attrs
[
'beta'
]
*
X
)}
def
test_check_output
(
self
):
self
.
check_output
()
def
test_check_grad
(
self
):
self
.
check_grad
([
'X'
],
'Y'
,
max_relative_error
=
0.008
)
if
__name__
==
"__main__"
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录