Caffe2 - C++ API
A deep learning, cross platform ML framework
dropout_op.cc
1 #include "caffe2/operators/dropout_op.h"
2 
3 namespace caffe2 {
4 
5 template <>
6 bool DropoutOp<float, CPUContext>::RunOnDevice() {
7  auto& X = Input(0);
8  auto* Y = Output(0, X.sizes(), at::dtype<float>());
9 
10  if (is_test_) {
11  if (!IsInputOutputAlias(0, 0)) {
12  context_.CopyFromCPU<float>(
13  X.numel(), X.data<float>(), Y->template mutable_data<float>());
14  }
15  return true;
16  } else {
17  float scale = 1. / (1. - ratio_);
18  // mask=true means keep, and mask=false means not keep, so we will
19  // generate probability depending on 1-ratio.
20  std::bernoulli_distribution dist(1. - ratio_);
21  const float* Xdata = X.data<float>();
22  float* Ydata = Y->template mutable_data<float>();
23 
24  auto mask = Output(1, X.sizes(), at::dtype<bool>());
25  bool* mask_data = mask->template mutable_data<bool>();
26  auto& gen = context_.RandGenerator();
27  for (int i = 0; i < X.numel(); ++i) {
28  mask_data[i] = dist(gen);
29  Ydata[i] = Xdata[i] * scale * mask_data[i];
30  }
31  return true;
32  }
33 }
34 
35 template <>
36 bool DropoutGradientOp<float, CPUContext>::RunOnDevice() {
37  auto& dY = Input(0);
38 
39  auto* dX = Output(0, dY.sizes(), at::dtype<float>());
40  if (is_test_) {
41  if (dX != &dY) {
42  context_.CopyFromCPU<float>(
43  dY.numel(), dY.data<float>(), dX->template mutable_data<float>());
44  }
45  return true;
46  } else {
47  auto& mask = Input(1);
48  CAFFE_ENFORCE_EQ(dY.numel(), mask.numel());
49  const float* dYdata = dY.data<float>();
50  const bool* mask_data = mask.data<bool>();
51  float* dXdata = dX->template mutable_data<float>();
52  float scale = 1. / (1. - ratio_);
53  for (int i = 0; i < dY.numel(); ++i) {
54  dXdata[i] = dYdata[i] * mask_data[i] * scale;
55  }
56  return true;
57  }
58 }
59 
60 REGISTER_CPU_OPERATOR(Dropout, DropoutOp<float, CPUContext>);
61 REGISTER_CPU_GRADIENT_OPERATOR(
62  DropoutGrad,
63  DropoutGradientOp<float, CPUContext>);
64 
65 OPERATOR_SCHEMA(Dropout)
66  .NumInputs(1)
67  .NumOutputs(1, 2)
68  .AllowInplace({{0, 0}})
69  .TensorInferenceFunction([](const OperatorDef& def,
70  const vector<TensorShape>& in) {
71  CAFFE_ENFORCE_EQ(1, in.size());
72  vector<TensorShape> out;
73  ArgumentHelper argsHelper(def);
74  out.push_back(in[0]);
75  if (def.output().size() == 2) {
76  out.push_back(in[0]);
77  out[1].set_data_type(TensorProto_DataType_BOOL);
78  }
79  return out;
80  })
81  .SetDoc(R"DOC(
82 
83 `Dropout` takes one input data tensor (`X`) and produces two tensor outputs, `Y` and
84 `mask`. If the `is_test` argument is zero (default=0), the output `Y` will be the input
85 with random elements zeroed. The probability that a given element is zeroed is
86 determined by the `ratio` argument.
87 
88 If the `is_test` argument is set to non-zero, the output `Y` is exactly the same as the
89 input `X`. Note that outputs are scaled by a factor of $\frac{1}{1-ratio}$ during
90 training, so that during test time, we can simply compute an identity function. This
91 scaling is important because we want the output at test time to equal the expected value
92 at training time. Dropout has been proven to be an effective regularization technique to
93 prevent overfitting during training.
94 
95 
96 Github Links:
97 
98 - https://github.com/pytorch/pytorch/blob/master/caffe2/operators/dropout_op.h
99 - https://github.com/pytorch/pytorch/blob/master/caffe2/operators/dropout_op.cc
100 
101 
102 <details>
104 <summary> <b>Example</b> </summary>
105 
106 **Code**
107 
108 ```
109 workspace.ResetWorkspace()
110 
111 op = core.CreateOperator(
112  "Dropout",
113  ["X"],
114  ["Y"] + ["mask"],
115  ratio=0.5,
116  is_test=0
117 )
118 
119 workspace.FeedBlob("X", np.random.randint(10, size=(5, 5)).astype(np.float32))
120 print("X:", workspace.FetchBlob("X"))
121 workspace.RunOperatorOnce(op)
122 print("Y:", workspace.FetchBlob("Y"))
123 print("mask:", workspace.FetchBlob("mask"))
124 ```
125 
126 **Result**
127 
128 ```
129 X: [[5. 4. 3. 6. 9.]
130  [2. 1. 8. 0. 9.]
131  [7. 3. 0. 6. 3.]
132  [1. 8. 2. 6. 4.]
133  [6. 2. 6. 4. 0.]]
134 Y: [[ 0. 0. 0. 12. 18.]
135  [ 0. 0. 16. 0. 0.]
136  [ 0. 0. 0. 12. 6.]
137  [ 0. 0. 4. 0. 0.]
138  [12. 0. 0. 0. 0.]]
139 mask: [[False False False True True]
140  [False False True True False]
141  [False False True True True]
142  [False False True False False]
143  [ True False False False False]]
144 ```
145 
146 </details>
147 
148 )DOC")
149  .Arg(
150  "ratio",
151  "*(type: float; default: 0.5)* Probability of an element to be zeroed.")
152  .ArgIsTest(
153  "*(type: int; default: 0)* If zero (train mode), perform dropout. If non-zero"
154  "(test mode), Y = X.")
155  .Input(0, "X", "*(type: Tensor`<float>`)* Input data tensor.")
156  .Output(0, "Y", "*(type: Tensor`<float>`)* Output tensor.")
157  .Output(
158  1,
159  "mask",
160  "*(type: Tensor`<bool>`)* The output mask containing boolean values for"
161  "each element, signifying which elements are dropped out. If `is_test` is"
162  "nonzero, this output is not filled.")
163  .InheritOnnxSchema();
164 
165 GRADIENT_OPERATOR_SCHEMA(DropoutGrad)
166  .NumInputs(1, 2)
167  .NumOutputs(1)
168  .AllowInplace({{0, 0}});
169 
170 class GetDropoutGradient : public GradientMakerBase {
171  using GradientMakerBase::GradientMakerBase;
172  vector<OperatorDef> GetGradientDefs() override {
173  ArgumentHelper argshelper(def_);
174  auto is_test = argshelper.GetSingleArgument<bool>("is_test", 0);
175  if (is_test) {
176  return SingleGradientDef(
177  "DropoutGrad", "", vector<string>{GO(0)}, vector<string>{GI(0)});
178  } else {
179  return SingleGradientDef(
180  "DropoutGrad",
181  "",
182  vector<string>{GO(0), O(1)},
183  vector<string>{GI(0)});
184  }
185  }
186 };
187 REGISTER_GRADIENT(Dropout, GetDropoutGradient);
188 } // namespace caffe2
A helper class to index into arguments.
Definition: proto_utils.h:200
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13
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 ...