MachineIntelligenceCore:NeuralNets
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
Dropout.hpp
Go to the documentation of this file.
1 
23 #ifndef SRC_MLNN_DROPOUT_HPP_
24 #define SRC_MLNN_DROPOUT_HPP_
25 
26 #include <mlnn/layer/Layer.hpp>
27 
28 namespace mic {
29 namespace mlnn {
30 namespace regularisation {
31 
32 
38 template <typename eT=float>
39 class Dropout : public Layer<eT> {
40 public:
41 
45  Dropout<eT>(size_t inputs_, float ratio_, std::string name_ = "Dropout") :
46  Layer<eT>(inputs_, 1, 1,
47  inputs_, 1, 1,
48  LayerTypes::Dropout, name_),
49  keep_ratio(ratio_)
50  {
51  // Create matrices with temporary variables: random and dropout of size [inputxinput], so we can simply calculate: y=mask*x.
52  m.add ("random", inputs_, inputs_);
53  m.add ("dropout_mask", inputs_, inputs_);
54  }
55 
56  virtual ~Dropout() {};
57 
62  virtual void resizeBatch(size_t batch_size_) {
63  // Call base Layer resize.
64  Layer<eT>::resizeBatch(batch_size_);
65 
66  // Reshape pooling mask and map.
67  m["dropout_mask"]->resize(Layer<eT>::inputSize(), batch_size_);
68  }
69 
70 
71  void forward(bool test = false) {
72  if (test) {
73  // In test run copy data as it is.
74  (*s['y']) = (*s['x']);
75 
76  } else {
77  // Get pointers to input and output batches.
78  mic::types::MatrixPtr<eT> batch_x = s['x'];
79  mic::types::MatrixPtr<eT> batch_y = s['y'];
80 
81  // Generate random matrix.
82  mic::types::MatrixPtr<eT> rand = m["random"];
83  rand->rand(0.0f, 1.0f);
84 
85  // Generate the dropout mask.
86  mic::types::MatrixPtr<eT> mask = m["dropout_mask"];
87 
88  #pragma omp parallel for
89  for(size_t i=0; i< (size_t)mask->size(); i++)
90  (*mask)[i] = ((*rand)[i] < keep_ratio);
91 
92  // Apply the dropout_mask - discard the elements where mask is 0.
93  (*batch_y) = (*mask) * (*batch_x);
94 
95  // Normalize, so that we don't have to do anything at test time.
96  (*batch_y) /= keep_ratio;
97 
98  }
99  }
100 
101  void backward() {
102  // Get pointers to input and output batches.
103  mic::types::MatrixPtr<eT> batch_dx = g['x'];
104  mic::types::MatrixPtr<eT> batch_dy = g['y'];
105  mic::types::MatrixPtr<eT> mask = m["dropout_mask"];
106 
107  // Always use dropout mask as backward pass is used only during learning.
108  (*batch_dy) = (*mask).transpose() * (*batch_dx);
109  }
110 
116  virtual void update(eT alpha_, eT decay_ = 0.0f) { };
117 
118  // Unhide the overloaded methods inherited from the template class Layer fields via "using" statement.
119  using Layer<eT>::forward;
120  using Layer<eT>::backward;
121 
122 protected:
123  // Unhide the fields inherited from the template class Layer via "using" statement.
124  using Layer<eT>::g;
125  using Layer<eT>::s;
126  using Layer<eT>::p;
127  using Layer<eT>::m;
128  using Layer<eT>::batch_size;
129 
130 
135 
136 private:
137  // Friend class - required for using boost serialization.
138  template<typename tmp> friend class mic::mlnn::MultiLayerNeuralNetwork;
139 
144 
145 
146 };
147 
148 
149 } /* regularisation */
150 } /* namespace mlnn */
151 } /* namespace mic */
152 
153 #endif /* SRC_MLNN_DROPOUT_HPP_ */
virtual void resizeBatch(size_t batch_size_)
Definition: Dropout.hpp:62
Droput layer - a layer used for the regularization of neural network by randomly dropping neurons dur...
Definition: Dropout.hpp:39
virtual void resizeBatch(size_t batch_size_)
Definition: Layer.hpp:199
virtual void update(eT alpha_, eT decay_=0.0f)
Definition: Dropout.hpp:116
void forward(bool test=false)
Definition: Dropout.hpp:71
Class representing a multi-layer neural network.
Definition: Layer.hpp:86
mic::types::MatrixArray< eT > s
States - contains input [x] and output [y] matrices.
Definition: Layer.hpp:753
mic::types::MatrixArray< eT > g
Gradients - contains input [x] and output [y] matrices.
Definition: Layer.hpp:756
Contains a template class representing a layer.
mic::types::MatrixArray< eT > m
Memory - a list of temporal parameters, to be used by the derived classes.
Definition: Layer.hpp:762