Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
9a44f3d6
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
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看板
提交
9a44f3d6
编写于
9月 02, 2017
作者:
X
Xinghai Sun
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add dropout operator.
上级
2d31ab5f
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
174 addition
and
0 deletion
+174
-0
paddle/operators/dropout_op.cc
paddle/operators/dropout_op.cc
+81
-0
paddle/operators/dropout_op.cu
paddle/operators/dropout_op.cu
+22
-0
paddle/operators/dropout_op.h
paddle/operators/dropout_op.h
+70
-0
paddle/pybind/pybind.cc
paddle/pybind/pybind.cc
+1
-0
未找到文件。
paddle/operators/dropout_op.cc
0 → 100644
浏览文件 @
9a44f3d6
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include "paddle/operators/dropout_op.h"
namespace
paddle
{
namespace
operators
{
using
framework
::
Tensor
;
class
DropoutOp
:
public
framework
::
OperatorWithKernel
{
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
protected:
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{
PADDLE_ENFORCE_NOT_NULL
(
ctx
.
InputVar
(
"X"
),
"Input(X) must not be null."
);
auto
dims
=
ctx
.
Input
<
Tensor
>
(
"X"
)
->
dims
();
ctx
.
Output
<
Tensor
>
(
"Out"
)
->
Resize
(
dims
);
ctx
.
Output
<
Tensor
>
(
"Mask"
)
->
Resize
(
dims
);
}
};
class
DropoutOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
DropoutOpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
"The input of dropout op."
);
AddOutput
(
"Out"
,
"The output of dropout op."
);
AddOutput
(
"Mask"
,
"The dropout mask."
).
AsIntermediate
();
AddComment
(
R"DOC(Dropout Operator.)DOC"
);
}
};
class
DropoutOpGrad
:
public
framework
::
OperatorWithKernel
{
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
protected:
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{
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."
);
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 must be the same."
);
PADDLE_ENFORCE_EQ
(
x_dims
,
mask_dims
,
"Dimensions of Input(X) and Mask must be the same."
);
auto
*
x_grad
=
ctx
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"X"
));
x_grad
->
Resize
(
x_dims
);
}
};
}
// namespace operators
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
REGISTER_OP
(
dropout
,
ops
::
DropoutOp
,
ops
::
DropoutOpMaker
,
dropout_grad
,
ops
::
DropoutOpGrad
);
REGISTER_OP_CPU_KERNEL
(
dropout
,
ops
::
DropoutKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
REGISTER_OP_CPU_KERNEL
(
dropout_grad
,
ops
::
DropoutGradKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
paddle/operators/dropout_op.cu
0 → 100644
浏览文件 @
9a44f3d6
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#define EIGEN_USE_GPU
#include "paddle/operators/dropout_op.h"
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_GPU_KERNEL
(
dropout
,
ops
::
DropoutKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
);
REGISTER_OP_GPU_KERNEL
(
dropout_grad
,
ops
::
DropoutGradKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
);
paddle/operators/dropout_op.h
0 → 100644
浏览文件 @
9a44f3d6
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#pragma once
#include "paddle/framework/eigen.h"
#include "paddle/framework/op_registry.h"
namespace
paddle
{
namespace
operators
{
using
Tensor
=
framework
::
Tensor
;
template
<
typename
T
,
int
MajorType
=
Eigen
::
RowMajor
,
typename
IndexType
=
Eigen
::
DenseIndex
>
using
EigenMatrix
=
framework
::
EigenMatrix
<
T
,
MajorType
,
IndexType
>
;
template
<
typename
Place
,
typename
T
>
class
DropoutKernel
:
public
framework
::
OpKernel
{
public:
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"
);
mask
->
mutable_data
<
T
>
(
context
.
GetPlace
());
y
->
mutable_data
<
T
>
(
context
.
GetPlace
());
auto
dims
=
x
->
dims
();
auto
X
=
EigenMatrix
<
T
>::
From
(
*
x
);
auto
Y
=
EigenMatrix
<
T
>::
From
(
*
y
);
auto
M
=
EigenMatrix
<
T
>::
From
(
*
mask
);
auto
place
=
context
.
GetEigenDevice
<
Place
>
();
M
.
device
(
place
).
setRandom
<
UniformRandomGenerator
>
();
float
dropout_prob
=
context
.
op_
.
GetAttr
<
float
>
(
"dropout_prob"
);
M
.
device
(
place
)
=
(
M
>
dropout_prob
).
cast
<
float
>
();
Y
.
device
(
place
)
=
X
*
Y
;
}
};
template
<
typename
Place
,
typename
T
>
class
DropoutGradKernel
:
public
framework
::
OpKernel
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
*
grad_x
=
context
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
grad_y
=
context
.
Input
<
Tensor
>
(
framework
::
GradVarName
(
"Out"
));
auto
*
mask
=
context
.
Input
<
Tensor
>
(
"Mask"
);
grad_x
->
mutable_data
<
T
>
(
context
.
GetPlace
());
auto
dims
=
grad_x
->
dims
();
auto
M
=
EigenMatrix
<
T
>::
From
(
*
mask
);
auto
dX
=
EigenMatrix
<
T
>::
From
(
*
grad_x
);
auto
dY
=
EigenMatrix
<
T
>::
From
(
*
grad_y
);
auto
place
=
context
.
GetEigenDevice
<
Place
>
();
dX
.
device
(
place
)
=
dY
*
M
;
}
};
}
// namespace operators
}
// namespace paddle
paddle/pybind/pybind.cc
浏览文件 @
9a44f3d6
...
@@ -46,6 +46,7 @@ USE_OP(lookup_table);
...
@@ -46,6 +46,7 @@ USE_OP(lookup_table);
USE_OP
(
scale
);
USE_OP
(
scale
);
USE_OP_ITSELF
(
identity
);
USE_OP_ITSELF
(
identity
);
USE_OP
(
minus
);
USE_OP
(
minus
);
USE_OP
(
dropout
);
USE_CPU_ONLY_OP
(
gather
);
USE_CPU_ONLY_OP
(
gather
);
USE_CPU_ONLY_OP
(
scatter
);
USE_CPU_ONLY_OP
(
scatter
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录