Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
3c3a6d90
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看板
提交
3c3a6d90
编写于
9月 18, 2017
作者:
Z
zchen0211
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
prelu finalize
上级
1b797468
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
18 addition
and
15 deletion
+18
-15
paddle/operators/prelu_op.h
paddle/operators/prelu_op.h
+12
-11
python/paddle/v2/framework/tests/test_prelu_op.py
python/paddle/v2/framework/tests/test_prelu_op.py
+6
-4
未找到文件。
paddle/operators/prelu_op.h
浏览文件 @
3c3a6d90
...
...
@@ -26,17 +26,17 @@ using platform::Transform;
template
<
typename
T
>
class
PReluFunctor
{
public:
explicit
PReluFunctor
(
const
T
&
alpha
)
:
alpha_
(
alpha
)
{}
explicit
PReluFunctor
(
const
T
*
alpha
)
:
alpha_
(
alpha
)
{}
HOSTDEVICE
T
operator
()(
const
T
&
x
)
const
{
if
(
x
>
0
)
return
x
;
else
return
x
*
alpha_
;
return
x
*
(
*
alpha_
)
;
}
private:
T
alpha_
;
const
T
*
alpha_
;
};
template
<
typename
Place
,
typename
T
>
...
...
@@ -50,30 +50,29 @@ class PReluKernel : public framework::OpKernel {
const
T
*
x_ptr
=
x
->
data
<
T
>
();
T
*
o_ptr
=
out
->
mutable_data
<
T
>
(
context
.
GetPlace
());
auto
alpha_val
=
alpha
->
data
<
T
>
()[
0
];
// auto alpha = static_cast<T>(context.Attr<AttrType>("alpha"));
auto
*
alpha_ptr
=
alpha
->
data
<
T
>
();
int
numel
=
x
->
numel
();
auto
place
=
context
.
GetPlace
();
Transform
(
place
,
x_ptr
,
x_ptr
+
numel
,
o_ptr
,
PReluFunctor
<
T
>
(
alpha_
val
));
Transform
(
place
,
x_ptr
,
x_ptr
+
numel
,
o_ptr
,
PReluFunctor
<
T
>
(
alpha_
ptr
));
}
};
template
<
typename
T
>
class
PReluGradFunctor
{
public:
explicit
PReluGradFunctor
(
const
T
&
alpha
)
:
alpha_
(
alpha
)
{}
explicit
PReluGradFunctor
(
const
T
*
alpha
)
:
alpha_
(
alpha
)
{}
HOSTDEVICE
T
operator
()(
const
T
&
out
,
const
T
&
dout
)
const
{
if
(
out
>
0
)
return
dout
;
else
return
dout
*
alpha_
;
return
dout
*
(
*
alpha_
)
;
}
private:
T
alpha_
;
const
T
*
alpha_
;
};
template
<
typename
Place
,
typename
T
>
...
...
@@ -85,7 +84,7 @@ class PReluGradKernel : public framework::OpKernel {
auto
*
out
=
context
.
Input
<
Tensor
>
(
"Out"
);
auto
*
alpha
=
context
.
Input
<
Tensor
>
(
"Alpha"
);
auto
alpha_val
=
alpha
->
data
<
T
>
()[
0
]
;
auto
*
alpha_ptr
=
alpha
->
data
<
T
>
()
;
T
*
dx_ptr
=
dx
->
mutable_data
<
T
>
(
context
.
GetPlace
());
const
T
*
dout_ptr
=
dout
->
data
<
T
>
();
...
...
@@ -94,7 +93,9 @@ class PReluGradKernel : public framework::OpKernel {
auto
place
=
context
.
GetPlace
();
Transform
(
place
,
out_ptr
,
out_ptr
+
numel
,
dout_ptr
,
dx_ptr
,
PReluGradFunctor
<
T
>
(
alpha_val
));
PReluGradFunctor
<
T
>
(
alpha_ptr
));
// TODO (Zhuoyuan): add dalpha upgrade when GPU kernels ready
}
};
...
...
python/paddle/v2/framework/tests/test_prelu_op.py
浏览文件 @
3c3a6d90
...
...
@@ -3,13 +3,15 @@ import numpy as np
from
op_test
import
OpTest
class
P
r
eluTest
(
OpTest
):
class
P
R
eluTest
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
"prelu"
self
.
inputs
=
{
'X'
:
np
.
random
.
normal
(
size
=
(
10
,
10
)).
astype
(
"float32"
)}
self
.
attrs
=
{
'alpha'
:
0.1
}
x_np
=
np
.
random
.
normal
(
size
=
(
10
,
10
)).
astype
(
"float32"
)
alpha_np
=
np
.
array
([.
1
])
self
.
inputs
=
{
'X'
:
x_np
,
'Alpha'
:
alpha_np
}
out_np
=
np
.
maximum
(
self
.
inputs
[
'X'
],
0.
)
out_np
=
out_np
+
np
.
minimum
(
self
.
inputs
[
'X'
],
0.
)
*
self
.
attrs
[
'alpha'
]
out_np
=
out_np
+
np
.
minimum
(
self
.
inputs
[
'X'
],
0.
)
*
self
.
inputs
[
'Alpha'
]
assert
out_np
is
not
self
.
inputs
[
'X'
]
self
.
outputs
=
{
'Out'
:
out_np
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录