|
|
|
@ -29,13 +29,10 @@ class DropoutOp : public framework::OperatorWithKernel {
|
|
|
|
|
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);
|
|
|
|
|
// 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 dims = ctx.Input<Tensor>("X")->dims();
|
|
|
|
|
ctx.Output<LoDTensor>("Out")->Resize(dims);
|
|
|
|
|
if (ctx.Attr<int>("is_training") == 1) {
|
|
|
|
|
if (ctx.Attr<bool>("is_training")) {
|
|
|
|
|
ctx.Output<LoDTensor>("Mask")->Resize(dims);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -49,8 +46,7 @@ class DropoutOpMaker : public framework::OpProtoAndCheckerMaker {
|
|
|
|
|
: OpProtoAndCheckerMaker(proto, op_checker) {
|
|
|
|
|
AddAttr<AttrType>("dropout_prob", "Probability of setting units to zero.")
|
|
|
|
|
.SetDefault(.5f);
|
|
|
|
|
// TODO(xinghai-sun): use bool for is_training after bool is supported.
|
|
|
|
|
AddAttr<int>("is_training", "Whether in training phase.").SetDefault(1);
|
|
|
|
|
AddAttr<bool>("is_training", "Whether in training phase.").SetDefault(true);
|
|
|
|
|
AddAttr<int>("seed", "Dropout random seed.").SetDefault(0);
|
|
|
|
|
AddInput("X", "The input of dropout op.");
|
|
|
|
|
AddOutput("Out", "The output of dropout op.");
|
|
|
|
@ -59,7 +55,7 @@ class DropoutOpMaker : public framework::OpProtoAndCheckerMaker {
|
|
|
|
|
AddComment(R"DOC(
|
|
|
|
|
Dropout Operator.
|
|
|
|
|
|
|
|
|
|
"Dropout" refers to randomly dropping out units in a nerual network. It is a
|
|
|
|
|
'Dropout' refers to randomly dropping out units in a nerual network. It is a
|
|
|
|
|
regularization technique for reducing overfitting by preventing neuron
|
|
|
|
|
co-adaption during training. The dropout operator randomly set (according to
|
|
|
|
|
the given dropout probability) the outputs of some units to zero, while others
|
|
|
|
@ -75,8 +71,8 @@ class DropoutOpGrad : public framework::OperatorWithKernel {
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
void InferShape(const framework::InferShapeContext &ctx) const override {
|
|
|
|
|
PADDLE_ENFORCE_EQ(ctx.Attr<int>("is_training"), 1,
|
|
|
|
|
"GradOp is only callable when is_training is true");
|
|
|
|
|
PADDLE_ENFORCE(ctx.Attr<bool>("is_training"),
|
|
|
|
|
"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.");
|
|
|
|
@ -85,9 +81,6 @@ class DropoutOpGrad : public framework::OperatorWithKernel {
|
|
|
|
|
|
|
|
|
|
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 out_dims = ctx.Input<Tensor>(framework::GradVarName("Out"))->dims();
|
|
|
|
|
PADDLE_ENFORCE_EQ(x_dims, out_dims,
|
|
|
|
|