Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
9fbf94b6
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看板
提交
9fbf94b6
编写于
9月 28, 2017
作者:
Y
Yu Yang
提交者:
GitHub
9月 28, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #4487 from abhinavarora/softsign_activation
Implementing the SoftSign activation operator
上级
704245ae
0c3eee09
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
64 addition
and
0 deletion
+64
-0
paddle/operators/activation_op.cc
paddle/operators/activation_op.cc
+20
-0
paddle/operators/activation_op.cu
paddle/operators/activation_op.cu
+7
-0
paddle/operators/activation_op.h
paddle/operators/activation_op.h
+20
-0
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
浏览文件 @
9fbf94b6
...
@@ -132,6 +132,17 @@ class SquareOpMaker : public framework::OpProtoAndCheckerMaker {
...
@@ -132,6 +132,17 @@ class SquareOpMaker : public framework::OpProtoAndCheckerMaker {
}
}
};
};
class
SoftsignOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
SoftsignOpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
"Input of Softsign operator"
);
AddOutput
(
"Y"
,
"Output of Softsign operator"
);
AddComment
(
"Softsign activation operator, softsign(x) = x / (1 + |x|)"
);
}
};
template
<
typename
AttrType
>
template
<
typename
AttrType
>
class
BReluOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
class
BReluOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
public:
...
@@ -277,6 +288,15 @@ REGISTER_OP_CPU_KERNEL(
...
@@ -277,6 +288,15 @@ REGISTER_OP_CPU_KERNEL(
square_grad
,
ops
::
ActivationGradKernel
<
paddle
::
platform
::
CPUPlace
,
float
,
square_grad
,
ops
::
ActivationGradKernel
<
paddle
::
platform
::
CPUPlace
,
float
,
ops
::
SquareGradFunctor
<
float
>>
);
ops
::
SquareGradFunctor
<
float
>>
);
REGISTER_OP
(
softsign
,
ops
::
ActivationOp
,
ops
::
SoftsignOpMaker
,
softsign_grad
,
ops
::
ActivationOpGrad
);
REGISTER_OP_CPU_KERNEL
(
softsign
,
ops
::
ActivationKernel
<
paddle
::
platform
::
CPUPlace
,
float
,
ops
::
SoftsignFunctor
<
float
>>
);
REGISTER_OP_CPU_KERNEL
(
softsign_grad
,
ops
::
ActivationGradKernel
<
paddle
::
platform
::
CPUPlace
,
float
,
ops
::
SoftsignGradFunctor
<
float
>>
);
REGISTER_OP
(
brelu
,
ops
::
ActivationOp
,
ops
::
BReluOpMaker
<
float
>
,
brelu_grad
,
REGISTER_OP
(
brelu
,
ops
::
ActivationOp
,
ops
::
BReluOpMaker
<
float
>
,
brelu_grad
,
ops
::
ActivationOpGrad
);
ops
::
ActivationOpGrad
);
REGISTER_OP_CPU_KERNEL
(
brelu
,
REGISTER_OP_CPU_KERNEL
(
brelu
,
...
...
paddle/operators/activation_op.cu
浏览文件 @
9fbf94b6
...
@@ -80,6 +80,13 @@ REGISTER_OP_GPU_KERNEL(
...
@@ -80,6 +80,13 @@ REGISTER_OP_GPU_KERNEL(
square_grad
,
ops
::
ActivationGradKernel
<
paddle
::
platform
::
GPUPlace
,
float
,
square_grad
,
ops
::
ActivationGradKernel
<
paddle
::
platform
::
GPUPlace
,
float
,
ops
::
SquareGradFunctor
<
float
>>
);
ops
::
SquareGradFunctor
<
float
>>
);
REGISTER_OP_GPU_KERNEL
(
softsign
,
ops
::
ActivationKernel
<
paddle
::
platform
::
GPUPlace
,
float
,
ops
::
SoftsignFunctor
<
float
>>
);
REGISTER_OP_GPU_KERNEL
(
softsign_grad
,
ops
::
ActivationGradKernel
<
paddle
::
platform
::
GPUPlace
,
float
,
ops
::
SoftsignGradFunctor
<
float
>>
);
REGISTER_OP_GPU_KERNEL
(
brelu
,
REGISTER_OP_GPU_KERNEL
(
brelu
,
ops
::
BReluKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
);
ops
::
BReluKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
);
REGISTER_OP_GPU_KERNEL
(
brelu_grad
,
REGISTER_OP_GPU_KERNEL
(
brelu_grad
,
...
...
paddle/operators/activation_op.h
浏览文件 @
9fbf94b6
...
@@ -201,6 +201,26 @@ struct SquareGradFunctor {
...
@@ -201,6 +201,26 @@ struct SquareGradFunctor {
}
}
};
};
// softsign(x) = x / (1 + |x|)
template
<
typename
T
>
struct
SoftsignFunctor
{
template
<
typename
Device
,
typename
X
,
typename
Y
>
void
operator
()(
Device
d
,
X
x
,
Y
y
)
{
y
.
device
(
d
)
=
x
/
(
static_cast
<
T
>
(
1
)
+
x
.
abs
());
}
};
// d(softsign(x))/dx = 1 / (1 + |x|)^2
// Taken from https://en.wikipedia.org/wiki/Activation_function
template
<
typename
T
>
struct
SoftsignGradFunctor
{
template
<
typename
Device
,
typename
X
,
typename
Y
,
typename
dY
,
typename
dX
>
void
operator
()(
Device
d
,
X
x
,
Y
y
,
dY
dy
,
dX
dx
)
{
dx
.
device
(
d
)
=
dy
*
(
static_cast
<
T
>
(
1
)
/
(
static_cast
<
T
>
(
1
)
+
x
.
abs
()).
square
());
}
};
template
<
typename
Place
,
typename
T
,
typename
AttrType
=
T
>
template
<
typename
Place
,
typename
T
,
typename
AttrType
=
T
>
class
BReluKernel
:
public
framework
::
OpKernel
<
T
>
{
class
BReluKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
public:
...
...
python/paddle/v2/framework/tests/test_activation_op.py
浏览文件 @
9fbf94b6
...
@@ -219,5 +219,22 @@ class TestSTanh(OpTest):
...
@@ -219,5 +219,22 @@ class TestSTanh(OpTest):
self
.
check_grad
([
'X'
],
'Y'
,
max_relative_error
=
0.007
)
self
.
check_grad
([
'X'
],
'Y'
,
max_relative_error
=
0.007
)
class
TestSoftsign
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"softsign"
self
.
inputs
=
{
'X'
:
np
.
random
.
uniform
(
-
1
,
1
,
[
11
,
17
]).
astype
(
"float32"
)
}
self
.
outputs
=
{
'Y'
:
np
.
divide
(
self
.
inputs
[
'X'
],
1
+
np
.
abs
(
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
)
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
unittest
.
main
()
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录