Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
24100e1f
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
24100e1f
编写于
3月 29, 2018
作者:
C
chengduo
提交者:
GitHub
3月 29, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #9449 from chengduoZH/feature/add_cos
Add cos and sin
上级
241f3c98
bdda08d9
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
116 addition
and
0 deletion
+116
-0
paddle/fluid/operators/activation_op.cc
paddle/fluid/operators/activation_op.cc
+36
-0
paddle/fluid/operators/activation_op.h
paddle/fluid/operators/activation_op.h
+50
-0
python/paddle/fluid/layers/ops.py
python/paddle/fluid/layers/ops.py
+2
-0
python/paddle/fluid/tests/unittests/test_activation_op.py
python/paddle/fluid/tests/unittests/test_activation_op.py
+28
-0
未找到文件。
paddle/fluid/operators/activation_op.cc
浏览文件 @
24100e1f
...
@@ -260,6 +260,36 @@ $out = floor(x)$
...
@@ -260,6 +260,36 @@ $out = floor(x)$
}
}
};
};
class
CosOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
CosOpMaker
(
OpProto
*
proto
,
OpAttrChecker
*
op_checker
)
:
framework
::
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
"Input of Cosine operator"
);
AddOutput
(
"Out"
,
"Output of Cosine operator"
);
AddComment
(
R"DOC(
Cosine Activation Operator.
$out = cos(x)$
)DOC"
);
}
};
class
SinOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
SinOpMaker
(
OpProto
*
proto
,
OpAttrChecker
*
op_checker
)
:
framework
::
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
"Input of Sine operator"
);
AddOutput
(
"Out"
,
"Output of Sine operator"
);
AddComment
(
R"DOC(
Sine Activation Operator.
$out = sin(x)$
)DOC"
);
}
};
class
RoundOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
class
RoundOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
public:
RoundOpMaker
(
OpProto
*
proto
,
OpAttrChecker
*
op_checker
)
RoundOpMaker
(
OpProto
*
proto
,
OpAttrChecker
*
op_checker
)
...
@@ -561,6 +591,12 @@ REGISTER_OP(ceil, ops::ActivationOp, ops::CeilOpMaker, ceil_grad,
...
@@ -561,6 +591,12 @@ REGISTER_OP(ceil, ops::ActivationOp, ops::CeilOpMaker, ceil_grad,
REGISTER_OP
(
floor
,
ops
::
ActivationOp
,
ops
::
FloorOpMaker
,
floor_grad
,
REGISTER_OP
(
floor
,
ops
::
ActivationOp
,
ops
::
FloorOpMaker
,
floor_grad
,
ops
::
ActivationOpGrad
);
ops
::
ActivationOpGrad
);
REGISTER_OP
(
cos
,
ops
::
ActivationOp
,
ops
::
CosOpMaker
,
cos_grad
,
ops
::
ActivationOpGrad
);
REGISTER_OP
(
sin
,
ops
::
ActivationOp
,
ops
::
SinOpMaker
,
sin_grad
,
ops
::
ActivationOpGrad
);
REGISTER_OP
(
round
,
ops
::
ActivationOp
,
ops
::
RoundOpMaker
,
round_grad
,
REGISTER_OP
(
round
,
ops
::
ActivationOp
,
ops
::
RoundOpMaker
,
round_grad
,
ops
::
ActivationOpGrad
);
ops
::
ActivationOpGrad
);
...
...
paddle/fluid/operators/activation_op.h
浏览文件 @
24100e1f
...
@@ -331,6 +331,54 @@ struct FloorFunctor : public BaseActivationFunctor<T> {
...
@@ -331,6 +331,54 @@ struct FloorFunctor : public BaseActivationFunctor<T> {
}
}
};
};
template
<
typename
T
>
struct
Sine
{
HOSTDEVICE
T
operator
()(
const
T
&
val
)
const
{
return
sin
(
val
);
}
};
template
<
typename
T
>
struct
Cosine
{
HOSTDEVICE
T
operator
()(
const
T
&
val
)
const
{
return
cos
(
val
);
}
};
// cosine'(x) = -sin(x)
template
<
typename
T
>
struct
CosGradFunctor
:
public
BaseActivationFunctor
<
T
>
{
template
<
typename
Device
,
typename
X
,
typename
Out
,
typename
dOut
,
typename
dX
>
void
operator
()(
Device
d
,
X
x
,
Out
out
,
dOut
dout
,
dX
dx
)
const
{
dx
.
device
(
d
)
=
-
dout
*
x
.
unaryExpr
(
Sine
<
T
>
());
}
};
// cosine(x) = cos(x)
template
<
typename
T
>
struct
CosFunctor
:
public
BaseActivationFunctor
<
T
>
{
template
<
typename
Device
,
typename
X
,
typename
Out
>
void
operator
()(
Device
d
,
X
x
,
Out
out
)
const
{
out
.
device
(
d
)
=
x
.
unaryExpr
(
Cosine
<
T
>
());
}
};
// sine'(x) = cos(x)
template
<
typename
T
>
struct
SinGradFunctor
:
public
BaseActivationFunctor
<
T
>
{
template
<
typename
Device
,
typename
X
,
typename
Out
,
typename
dOut
,
typename
dX
>
void
operator
()(
Device
d
,
X
x
,
Out
out
,
dOut
dout
,
dX
dx
)
const
{
dx
.
device
(
d
)
=
dout
*
x
.
unaryExpr
(
Cosine
<
T
>
());
}
};
// sine(x) = sin(x)
template
<
typename
T
>
struct
SinFunctor
:
public
BaseActivationFunctor
<
T
>
{
template
<
typename
Device
,
typename
X
,
typename
Out
>
void
operator
()(
Device
d
,
X
x
,
Out
out
)
const
{
out
.
device
(
d
)
=
x
.
unaryExpr
(
Sine
<
T
>
());
}
};
// round(x) = [x]
// round(x) = [x]
template
<
typename
T
>
template
<
typename
T
>
struct
RoundFunctor
:
public
BaseActivationFunctor
<
T
>
{
struct
RoundFunctor
:
public
BaseActivationFunctor
<
T
>
{
...
@@ -782,6 +830,8 @@ struct SwishGradFunctor : public BaseActivationFunctor<T> {
...
@@ -782,6 +830,8 @@ struct SwishGradFunctor : public BaseActivationFunctor<T> {
__macro(abs, AbsFunctor, AbsGradFunctor); \
__macro(abs, AbsFunctor, AbsGradFunctor); \
__macro(ceil, CeilFunctor, ZeroGradFunctor); \
__macro(ceil, CeilFunctor, ZeroGradFunctor); \
__macro(floor, FloorFunctor, ZeroGradFunctor); \
__macro(floor, FloorFunctor, ZeroGradFunctor); \
__macro(cos, CosFunctor, CosGradFunctor); \
__macro(sin, SinFunctor, SinGradFunctor); \
__macro(round, RoundFunctor, ZeroGradFunctor); \
__macro(round, RoundFunctor, ZeroGradFunctor); \
__macro(reciprocal, ReciprocalFunctor, ReciprocalGradFunctor); \
__macro(reciprocal, ReciprocalFunctor, ReciprocalGradFunctor); \
__macro(log, LogFunctor, LogGradFunctor); \
__macro(log, LogFunctor, LogGradFunctor); \
...
...
python/paddle/fluid/layers/ops.py
浏览文件 @
24100e1f
...
@@ -25,6 +25,8 @@ __activations__ = [
...
@@ -25,6 +25,8 @@ __activations__ = [
'abs'
,
'abs'
,
'ceil'
,
'ceil'
,
'floor'
,
'floor'
,
'cos'
,
'sin'
,
'round'
,
'round'
,
'reciprocal'
,
'reciprocal'
,
'log'
,
'log'
,
...
...
python/paddle/fluid/tests/unittests/test_activation_op.py
浏览文件 @
24100e1f
...
@@ -196,6 +196,34 @@ class TestFloor(OpTest):
...
@@ -196,6 +196,34 @@ class TestFloor(OpTest):
self
.
check_grad
([
'X'
],
'Out'
,
max_relative_error
=
0.007
)
self
.
check_grad
([
'X'
],
'Out'
,
max_relative_error
=
0.007
)
class
TestCos
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"cos"
x
=
np
.
random
.
uniform
(
-
1
,
1
,
[
4
,
4
]).
astype
(
"float32"
)
self
.
inputs
=
{
'X'
:
x
}
self
.
outputs
=
{
'Out'
:
np
.
cos
(
self
.
inputs
[
'X'
])}
def
test_check_output
(
self
):
self
.
check_output
()
def
test_check_grad
(
self
):
self
.
check_grad
([
'X'
],
'Out'
,
max_relative_error
=
0.007
)
class
TestSin
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"sin"
x
=
np
.
random
.
uniform
(
-
1
,
1
,
[
4
,
4
]).
astype
(
"float32"
)
self
.
inputs
=
{
'X'
:
x
}
self
.
outputs
=
{
'Out'
:
np
.
sin
(
self
.
inputs
[
'X'
])}
def
test_check_output
(
self
):
self
.
check_output
()
def
test_check_grad
(
self
):
self
.
check_grad
([
'X'
],
'Out'
,
max_relative_error
=
0.007
)
class
TestRound
(
OpTest
):
class
TestRound
(
OpTest
):
def
setUp
(
self
):
def
setUp
(
self
):
self
.
op_type
=
"round"
self
.
op_type
=
"round"
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录