Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
1397e17f
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看板
提交
1397e17f
编写于
10月 10, 2017
作者:
K
kavyasrinet
提交者:
GitHub
10月 10, 2017
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Implemented the hardShrink activation (#4653)
* Implemented the hardShrink activation * Fixing the unit test
上级
6604d7cd
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
76 addition
and
3 deletion
+76
-3
paddle/operators/activation_op.cc
paddle/operators/activation_op.cc
+21
-0
paddle/operators/activation_op.h
paddle/operators/activation_op.h
+35
-3
python/paddle/v2/framework/tests/test_activation_op.py
python/paddle/v2/framework/tests/test_activation_op.py
+20
-0
未找到文件。
paddle/operators/activation_op.cc
浏览文件 @
1397e17f
...
...
@@ -137,6 +137,24 @@ class TanhShrinkOpMaker : public framework::OpProtoAndCheckerMaker {
}
};
template
<
typename
AttrType
>
class
HardShrinkOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
HardShrinkOpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
"Input of HardShrink operator"
);
AddOutput
(
"Y"
,
"Output of HardShrink operator"
);
AddComment
(
"HardShrink activation operator, "
"hard_shrink(x) = x if x > lambda"
"hard_shrink(x) = x if x < -lambda"
"hard_shrink(x) = 0 otherwise"
);
AddAttr
<
AttrType
>
(
"threshold"
,
"The value of threshold for HardShrink"
)
.
SetDefault
(
static_cast
<
AttrType
>
(
0.5
));
}
};
class
SqrtOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
SqrtOpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
...
...
@@ -357,6 +375,9 @@ REGISTER_OP(pow, ops::ActivationOp, ops::PowOpMaker<float>, pow_grad,
REGISTER_OP
(
stanh
,
ops
::
ActivationOp
,
ops
::
STanhOpMaker
<
float
>
,
stanh_grad
,
ops
::
ActivationOpGrad
);
REGISTER_OP
(
hard_shrink
,
ops
::
ActivationOp
,
ops
::
HardShrinkOpMaker
<
float
>
,
hard_shrink_grad
,
ops
::
ActivationOpGrad
);
#define REGISTER_ACTIVATION_CPU_KERNEL(act_type, functor, grad_functor) \
REGISTER_OP_CPU_KERNEL( \
act_type, \
...
...
paddle/operators/activation_op.h
浏览文件 @
1397e17f
...
...
@@ -199,6 +199,39 @@ struct TanhShrinkGradFunctor : public BaseActivationFunctor<T> {
}
};
// tanhshrink(x) = x - tanh(x)
// where tanh(x) = (exp(x) - exp(-x)) / (exp(x) + exp(-x))
template
<
typename
T
>
struct
HardShrinkFunctor
:
public
BaseActivationFunctor
<
T
>
{
float
threshold
;
typename
BaseActivationFunctor
<
T
>::
AttrPair
GetAttrs
()
{
return
{{
"threshold"
,
&
threshold
}};
}
template
<
typename
Device
,
typename
X
,
typename
Y
>
void
operator
()(
Device
d
,
X
x
,
Y
y
)
const
{
auto
temp1
=
(
x
<
(
threshold
*
-
1
)).
template
cast
<
T
>().
eval
();
auto
temp2
=
(
x
>
threshold
).
template
cast
<
T
>().
eval
();
y
.
device
(
d
)
=
x
*
(
temp1
+
temp2
);
}
};
template
<
typename
T
>
struct
HardShrinkGradFunctor
:
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
{
auto
temp1
=
(
x
<
(
threshold
*
-
1
)).
template
cast
<
T
>().
eval
();
auto
temp2
=
(
x
>
threshold
).
template
cast
<
T
>().
eval
();
dx
.
device
(
d
)
=
dy
*
(
temp1
+
temp2
).
template
cast
<
T
>();
}
};
// softshrink(x) = x - lambda, if x > lambda; x + lambda, if x < lambda; 0
// otherwise
template
<
typename
T
>
...
...
@@ -351,8 +384,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
}};
}
...
...
@@ -555,4 +586,5 @@ struct STanhGradFunctor : public BaseActivationFunctor<T> {
__macro(relu6, Relu6Functor, Relu6GradFunctor); \
__macro(leaky_relu, LeakyReluFunctor, LeakyReluGradFunctor); \
__macro(tanh_shrink, TanhShrinkFunctor, TanhShrinkGradFunctor); \
__macro(elu, ELUFunctor, ELUGradFunctor)
__macro(elu, ELUFunctor, ELUGradFunctor); \
__macro(hard_shrink, HardShrinkFunctor, HardShrinkGradFunctor)
python/paddle/v2/framework/tests/test_activation_op.py
浏览文件 @
1397e17f
...
...
@@ -78,6 +78,26 @@ class TestTanhShrink(OpTest):
self
.
check_grad
([
'X'
],
'Y'
,
max_relative_error
=
0.008
)
class
TestHardShrink
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"hard_shrink"
x
=
np
.
random
.
uniform
(
-
1
,
1
,
[
4
,
4
]).
astype
(
"float32"
)
threshold
=
0.5
self
.
inputs
=
{
'X'
:
x
}
self
.
attrs
=
{
'lambda'
:
threshold
}
t
=
np
.
copy
(
x
)
t
[(
t
>=
-
threshold
)
&
(
t
<=
threshold
)]
=
0
self
.
outputs
=
{
'Y'
:
t
}
def
test_check_output
(
self
):
self
.
check_output
()
def
test_check_grad
(
self
):
self
.
check_grad
([
'X'
],
'Y'
,
max_relative_error
=
0.005
)
class
TestSoftShrink
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"softshrink"
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录