1 #include "caffe2/operators/dropout_op.h" 6 bool DropoutOp<float, CPUContext>::RunOnDevice() {
12 context_.Copy<float, CPUContext, CPUContext>(
13 X.size(), X.data<
float>(), Y->mutable_data<
float>());
17 float scale = 1. / (1. - ratio_);
20 std::bernoulli_distribution dist(1. - ratio_);
21 const float* Xdata = X.data<
float>();
22 float* Ydata = Y->mutable_data<
float>();
23 auto mask = Output(1);
24 mask->Resize(X.dims());
25 bool* mask_data = mask->mutable_data<
bool>();
26 auto& gen = context_.RandGenerator();
27 for (
int i = 0; i < X.size(); ++i) {
28 mask_data[i] = dist(gen);
29 Ydata[i] = Xdata[i] * scale * mask_data[i];
36 bool DropoutGradientOp<float, CPUContext>::RunOnDevice() {
39 dX->Resize(dY.dims());
42 context_.Copy<float, CPUContext, CPUContext>(
43 dY.size(), dY.data<
float>(), dX->mutable_data<
float>());
47 auto& mask = Input(1);
48 CAFFE_ENFORCE_EQ(dY.size(), mask.size());
49 const float* dYdata = dY.data<
float>();
50 const bool* mask_data = mask.data<
bool>();
51 float* dXdata = dX->mutable_data<
float>();
52 float scale = 1. / (1. - ratio_);
53 for (
int i = 0; i < dY.size(); ++i) {
54 dXdata[i] = dYdata[i] * mask_data[i] * scale;
60 REGISTER_CPU_OPERATOR(Dropout, DropoutOp<float, CPUContext>);
61 REGISTER_CPU_OPERATOR(DropoutGrad, DropoutGradientOp<float, CPUContext>);
63 OPERATOR_SCHEMA(Dropout)
66 .AllowInplace({{0, 0}})
67 .TensorInferenceFunction([](
const OperatorDef& def,
68 const vector<TensorShape>& in) {
69 CAFFE_ENFORCE_EQ(1, in.size());
70 vector<TensorShape> out;
71 ArgumentHelper argsHelper(def);
73 auto output_mask = !argsHelper.GetSingleArgument<
bool>(
"is_test", 0);
76 out[1].set_data_type(TensorProto_DataType_BOOL);
81 Dropout takes one input data (Tensor<float>) and produces two Tensor outputs, 82 output (Tensor<float>) and mask (Tensor<bool>). Depending on whether it is in 83 test mode or not, the output Y will either be a random dropout, or a simple 84 copy of the input. Note that our implementation of Dropout does scaling in 85 the training phase, so during testing nothing needs to be done. 87 .Arg("ratio",
"(float, default 0.5) the ratio of random dropout")
89 "(int) if nonzero, run dropout in test mode where " 90 "the output is simply Y = X.")
91 .Input(0,
"data",
"The input data as Tensor.")
92 .Output(0,
"output",
"The output.")
96 "The output mask. If is_test is nonzero, this output is not filled.")
97 .InheritOnnxSchema(
"Dropout");
99 OPERATOR_SCHEMA(DropoutGrad)
102 .AllowInplace({{0, 0}});
105 using GradientMakerBase::GradientMakerBase;
106 vector<OperatorDef> GetGradientDefs()
override {
108 auto is_test = argshelper.GetSingleArgument<
bool>(
"is_test", 0);
111 "DropoutGrad",
"", vector<string>{GO(0)}, vector<string>{GI(0)});
116 vector<string>{GO(0), O(1)},
117 vector<string>{GI(0)});
A helper class to index into arguments.
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
static vector< OperatorDef > SingleGradientDef(const Args &...args)
a helper function to allow one to create one single operator def, which is usually the case for many ...