Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
ffeeef82
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
ffeeef82
编写于
9月 19, 2017
作者:
X
Xinghai Sun
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Remove unnecessary mask operations in test phase for dropout operator.
上级
a2798ff2
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
20 addition
and
27 deletion
+20
-27
paddle/operators/dropout_op.cc
paddle/operators/dropout_op.cc
+9
-6
paddle/operators/dropout_op.cu
paddle/operators/dropout_op.cu
+4
-6
paddle/operators/dropout_op.h
paddle/operators/dropout_op.h
+5
-7
python/paddle/v2/framework/tests/test_dropout_op.py
python/paddle/v2/framework/tests/test_dropout_op.py
+2
-8
未找到文件。
paddle/operators/dropout_op.cc
浏览文件 @
ffeeef82
...
...
@@ -26,7 +26,6 @@ class DropoutOp : public framework::OperatorWithKernel {
protected:
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{
// validity check
PADDLE_ENFORCE_NOT_NULL
(
ctx
.
InputVar
(
"X"
),
"Input(X) must not be null."
);
PADDLE_ENFORCE_GE
(
ctx
.
Attr
<
float
>
(
"dropout_prob"
),
0
);
PADDLE_ENFORCE_LE
(
ctx
.
Attr
<
float
>
(
"dropout_prob"
),
1
);
...
...
@@ -34,10 +33,11 @@ class DropoutOp : public framework::OperatorWithKernel {
PADDLE_ENFORCE
(
ctx
.
Attr
<
int
>
(
"is_training"
)
==
0
||
ctx
.
Attr
<
int
>
(
"is_training"
)
==
1
);
// resize
auto
dims
=
ctx
.
Input
<
Tensor
>
(
"X"
)
->
dims
();
ctx
.
Output
<
LoDTensor
>
(
"Out"
)
->
Resize
(
dims
);
ctx
.
Output
<
LoDTensor
>
(
"Mask"
)
->
Resize
(
dims
);
if
(
ctx
.
Attr
<
int
>
(
"is_training"
)
==
1
)
{
ctx
.
Output
<
LoDTensor
>
(
"Mask"
)
->
Resize
(
dims
);
}
}
};
...
...
@@ -75,24 +75,27 @@ class DropoutOpGrad : public framework::OperatorWithKernel {
protected:
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{
// validity check
PADDLE_ENFORCE_EQ
(
ctx
.
Attr
<
int
>
(
"is_training"
),
1
,
"GradOp is only callable when is_training is true"
);
PADDLE_ENFORCE_NOT_NULL
(
ctx
.
InputVar
(
"X"
),
"Input(X) must not be null."
);
PADDLE_ENFORCE_NOT_NULL
(
ctx
.
InputVar
(
"Mask"
),
"Mask must not be null."
);
PADDLE_ENFORCE_NOT_NULL
(
ctx
.
InputVar
(
framework
::
GradVarName
(
"Out"
)),
"Input(Out@GRAD) must not be null."
);
PADDLE_ENFORCE_GE
(
ctx
.
Attr
<
AttrType
>
(
"dropout_prob"
),
0
);
PADDLE_ENFORCE_LE
(
ctx
.
Attr
<
AttrType
>
(
"dropout_prob"
),
1
);
// TODO(xinghai-sun): remove this check after swtiching to bool
PADDLE_ENFORCE
(
ctx
.
Attr
<
int
>
(
"is_training"
)
==
0
||
ctx
.
Attr
<
int
>
(
"is_training"
)
==
1
);
auto
x_dims
=
ctx
.
Input
<
Tensor
>
(
"X"
)
->
dims
();
auto
mask_dims
=
ctx
.
Input
<
Tensor
>
(
"Mask"
)
->
dims
();
auto
out_dims
=
ctx
.
Input
<
Tensor
>
(
framework
::
GradVarName
(
"Out"
))
->
dims
();
PADDLE_ENFORCE_EQ
(
x_dims
,
out_dims
,
"Dimensions of Input(X) and Out@Grad must be the same."
);
auto
mask_dims
=
ctx
.
Input
<
Tensor
>
(
"Mask"
)
->
dims
();
PADDLE_ENFORCE_EQ
(
x_dims
,
mask_dims
,
"Dimensions of Input(X) and Mask must be the same."
);
// resize
auto
*
x_grad
=
ctx
.
Output
<
LoDTensor
>
(
framework
::
GradVarName
(
"X"
));
x_grad
->
Resize
(
x_dims
);
}
...
...
paddle/operators/dropout_op.cu
浏览文件 @
ffeeef82
...
...
@@ -53,26 +53,24 @@ class GPUDropoutKernel : public framework::OpKernel {
auto
*
x
=
context
.
Input
<
Tensor
>
(
"X"
);
auto
*
y
=
context
.
Output
<
Tensor
>
(
"Out"
);
y
->
mutable_data
<
T
>
(
context
.
GetPlace
());
auto
*
mask
=
context
.
Output
<
Tensor
>
(
"Mask"
);
auto
*
mask_data
=
mask
->
mutable_data
<
T
>
(
context
.
GetPlace
());
AttrType
dropout_prob
=
context
.
Attr
<
AttrType
>
(
"dropout_prob"
);
auto
X
=
EigenMatrix
<
T
>::
Reshape
(
*
x
,
1
);
auto
Y
=
EigenMatrix
<
T
>::
Reshape
(
*
y
,
1
);
auto
M
=
EigenMatrix
<
T
>::
Reshape
(
*
mask
,
1
);
auto
place
=
context
.
GetEigenDevice
<
Place
>
();
int
size
=
framework
::
product
(
mask
->
dims
());
if
(
context
.
Attr
<
int
>
(
"is_training"
)
==
1
)
{
auto
*
mask
=
context
.
Output
<
Tensor
>
(
"Mask"
);
auto
*
mask_data
=
mask
->
mutable_data
<
T
>
(
context
.
GetPlace
());
int
size
=
framework
::
product
(
mask
->
dims
());
int
seed
=
context
.
Attr
<
int
>
(
"seed"
);
thrust
::
counting_iterator
<
unsigned
int
>
index_sequence_begin
(
0
);
thrust
::
transform
(
index_sequence_begin
,
index_sequence_begin
+
size
,
thrust
::
device_ptr
<
T
>
(
mask_data
),
MaskGenerator
<
T
,
AttrType
>
(
dropout_prob
,
seed
));
auto
M
=
EigenMatrix
<
T
>::
Reshape
(
*
mask
,
1
);
Y
.
device
(
place
)
=
X
*
M
;
}
else
{
cudaMemset
(
mask_data
,
0
,
sizeof
(
T
)
*
size
);
Y
.
device
(
place
)
=
X
*
dropout_prob
;
}
}
...
...
paddle/operators/dropout_op.h
浏览文件 @
ffeeef82
...
...
@@ -31,14 +31,13 @@ class CPUDropoutKernel : public framework::OpKernel {
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
*
x
=
context
.
Input
<
Tensor
>
(
"X"
);
auto
*
y
=
context
.
Output
<
Tensor
>
(
"Out"
);
auto
*
mask
=
context
.
Output
<
Tensor
>
(
"Mask"
);
auto
*
mask_data
=
mask
->
mutable_data
<
T
>
(
context
.
GetPlace
());
auto
*
y_data
=
y
->
mutable_data
<
T
>
(
context
.
GetPlace
());
const
auto
*
x_data
=
x
->
data
<
T
>
();
auto
*
y_data
=
y
->
mutable_data
<
T
>
(
context
.
GetPlace
());
AttrType
dropout_prob
=
context
.
Attr
<
AttrType
>
(
"dropout_prob"
);
if
(
context
.
Attr
<
int
>
(
"is_training"
)
==
1
)
{
auto
*
mask
=
context
.
Output
<
Tensor
>
(
"Mask"
);
auto
*
mask_data
=
mask
->
mutable_data
<
T
>
(
context
.
GetPlace
());
int
seed
=
context
.
Attr
<
int
>
(
"seed"
);
std
::
minstd_rand
engine
;
engine
.
seed
(
seed
);
...
...
@@ -54,8 +53,6 @@ class CPUDropoutKernel : public framework::OpKernel {
}
}
}
else
{
size_t
size
=
framework
::
product
(
mask
->
dims
());
memset
(
mask_data
,
0
,
sizeof
(
T
)
*
size
);
auto
X
=
EigenMatrix
<
T
>::
Reshape
(
*
x
,
1
);
auto
Y
=
EigenMatrix
<
T
>::
Reshape
(
*
y
,
1
);
auto
place
=
context
.
GetEigenDevice
<
Place
>
();
...
...
@@ -69,7 +66,8 @@ class DropoutGradKernel : public framework::OpKernel {
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
PADDLE_ENFORCE_EQ
(
context
.
Attr
<
int
>
(
"is_training"
),
1
,
"Only callable when is_training is true"
);
"GradOp is only callable when is_training is true"
);
auto
*
grad_x
=
context
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
grad_y
=
context
.
Input
<
Tensor
>
(
framework
::
GradVarName
(
"Out"
));
auto
*
mask
=
context
.
Input
<
Tensor
>
(
"Mask"
);
...
...
python/paddle/v2/framework/tests/test_dropout_op.py
浏览文件 @
ffeeef82
...
...
@@ -38,10 +38,7 @@ class TestDropoutOp4(OpTest):
self
.
op_type
=
"dropout"
self
.
inputs
=
{
'X'
:
np
.
random
.
random
((
32
,
64
)).
astype
(
"float32"
)}
self
.
attrs
=
{
'dropout_prob'
:
0.35
,
'is_training'
:
0
}
self
.
outputs
=
{
'Out'
:
self
.
inputs
[
'X'
]
*
self
.
attrs
[
'dropout_prob'
],
'Mask'
:
np
.
zeros
((
32
,
64
))
}
self
.
outputs
=
{
'Out'
:
self
.
inputs
[
'X'
]
*
self
.
attrs
[
'dropout_prob'
]}
def
test_check_output
(
self
):
self
.
check_output
()
...
...
@@ -52,10 +49,7 @@ class TestDropoutOp5(OpTest):
self
.
op_type
=
"dropout"
self
.
inputs
=
{
'X'
:
np
.
random
.
random
((
32
,
64
,
3
)).
astype
(
"float32"
)}
self
.
attrs
=
{
'dropout_prob'
:
0.75
,
'is_training'
:
0
}
self
.
outputs
=
{
'Out'
:
self
.
inputs
[
'X'
]
*
self
.
attrs
[
'dropout_prob'
],
'Mask'
:
np
.
zeros
((
32
,
64
,
3
))
}
self
.
outputs
=
{
'Out'
:
self
.
inputs
[
'X'
]
*
self
.
attrs
[
'dropout_prob'
]}
def
test_check_output
(
self
):
self
.
check_output
()
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录