Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
9995aed1
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
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看板
提交
9995aed1
编写于
10月 10, 2017
作者:
K
kexinzhao
提交者:
GitHub
10月 10, 2017
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Implementing Softplus operator (#4690)
* implementing softplus * small fix * small fix * small fix * small fix
上级
c3bf3326
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
57 addition
and
0 deletion
+57
-0
paddle/operators/activation_op.cc
paddle/operators/activation_op.cc
+14
-0
paddle/operators/activation_op.h
paddle/operators/activation_op.h
+28
-0
python/paddle/v2/framework/tests/test_activation_op.py
python/paddle/v2/framework/tests/test_activation_op.py
+15
-0
未找到文件。
paddle/operators/activation_op.cc
浏览文件 @
9995aed1
...
...
@@ -206,6 +206,17 @@ class SquareOpMaker : public framework::OpProtoAndCheckerMaker {
}
};
class
SoftplusOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
SoftplusOpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
"Input of Softplus operator"
);
AddOutput
(
"Y"
,
"Output of Softplus operator"
);
AddComment
(
"Softplus activation operator, softplus(x) = log(1 + exp(x))"
);
}
};
class
SoftsignOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
SoftsignOpMaker
(
framework
::
OpProto
*
proto
,
...
...
@@ -351,6 +362,9 @@ REGISTER_OP(log, ops::ActivationOp, ops::LogOpMaker, log_grad,
REGISTER_OP
(
square
,
ops
::
ActivationOp
,
ops
::
SquareOpMaker
,
square_grad
,
ops
::
ActivationOpGrad
);
REGISTER_OP
(
softplus
,
ops
::
ActivationOp
,
ops
::
SoftplusOpMaker
,
softplus_grad
,
ops
::
ActivationOpGrad
);
REGISTER_OP
(
softsign
,
ops
::
ActivationOp
,
ops
::
SoftsignOpMaker
,
softsign_grad
,
ops
::
ActivationOpGrad
);
...
...
paddle/operators/activation_op.h
浏览文件 @
9995aed1
...
...
@@ -407,6 +407,33 @@ struct Relu6GradFunctor : public BaseActivationFunctor<T> {
}
};
// softplus(x) = log(1 + exp(x))
// When x is a very large positive number, exp(x) may explode to inf,
// Using trick below for numerical stability
// https://hips.seas.harvard.edu/blog/2013/01/09/computing-log-sum-exp/
// Then: softplus(x) = max(x, 0) + log(exp(-max(x, 0)) + exp(x - max(x, 0)))
template
<
typename
T
>
struct
SoftplusFunctor
:
public
BaseActivationFunctor
<
T
>
{
template
<
typename
Device
,
typename
X
,
typename
Y
>
void
operator
()(
Device
d
,
X
x
,
Y
y
)
{
auto
temp
=
x
.
cwiseMax
(
static_cast
<
T
>
(
0
));
// temp = max(x, 0)
y
.
device
(
d
)
=
temp
+
(((
-
temp
).
exp
()
+
(
x
-
temp
).
exp
()).
log
());
}
};
// d(softplus(x))/dx = exp(x) / (1 + exp(x))
// For numerical stability:
// d(softplus(x))/dx = exp(x - max(x, 0)) / (exp(-max(x, 0)) +
// exp(x - max(x, 0)))
template
<
typename
T
>
struct
SoftplusGradFunctor
:
public
BaseActivationFunctor
<
T
>
{
template
<
typename
Device
,
typename
X
,
typename
Y
,
typename
dY
,
typename
dX
>
void
operator
()(
Device
d
,
X
x
,
Y
y
,
dY
dy
,
dX
dx
)
{
auto
temp
=
x
.
cwiseMax
(
static_cast
<
T
>
(
0
));
// temp = max(x, 0)
dx
.
device
(
d
)
=
dy
*
((
x
-
temp
).
exp
()
/
((
-
temp
).
exp
()
+
(
x
-
temp
).
exp
()));
}
};
// softsign(x) = x / (1 + |x|)
template
<
typename
T
>
struct
SoftsignFunctor
:
public
BaseActivationFunctor
<
T
>
{
...
...
@@ -582,6 +609,7 @@ struct STanhGradFunctor : public BaseActivationFunctor<T> {
__macro(soft_relu, SoftReluFunctor, SoftReluGradFunctor); \
__macro(pow, PowFunctor, PowGradFunctor); \
__macro(stanh, STanhFunctor, STanhGradFunctor); \
__macro(softplus, SoftplusFunctor, SoftplusGradFunctor); \
__macro(softsign, SoftsignFunctor, SoftsignGradFunctor); \
__macro(relu6, Relu6Functor, Relu6GradFunctor); \
__macro(leaky_relu, LeakyReluFunctor, LeakyReluGradFunctor); \
...
...
python/paddle/v2/framework/tests/test_activation_op.py
浏览文件 @
9995aed1
...
...
@@ -331,6 +331,21 @@ class TestSTanh(OpTest):
self
.
check_grad
([
'X'
],
'Y'
,
max_relative_error
=
0.007
)
class
TestSoftplus
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"softplus"
self
.
inputs
=
{
'X'
:
np
.
random
.
uniform
(
-
1
,
1
,
[
11
,
17
]).
astype
(
"float32"
)
}
self
.
outputs
=
{
'Y'
:
np
.
log
(
1
+
np
.
exp
(
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
TestSoftsign
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"softsign"
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录