mlpack  master
mean_pooling.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_METHODS_ANN_LAYER_MEAN_POOLING_HPP
14 #define MLPACK_METHODS_ANN_LAYER_MEAN_POOLING_HPP
15 
16 #include <mlpack/prereqs.hpp>
17 
18 namespace mlpack {
19 namespace ann {
20 
29 template <
30  typename InputDataType = arma::mat,
31  typename OutputDataType = arma::mat
32 >
34 {
35 public:
37  MeanPooling();
38 
47  MeanPooling(const size_t kW,
48  const size_t kH,
49  const size_t dW = 1,
50  const size_t dH = 1,
51  const bool floor = true);
52 
60  template<typename eT>
61  void Forward(const arma::Mat<eT>&& input, arma::Mat<eT>&& output);
62 
72  template<typename eT>
73  void Backward(const arma::Mat<eT>&& /* input */,
74  arma::Mat<eT>&& gy,
75  arma::Mat<eT>&& g);
76 
78  InputDataType const& InputParameter() const { return inputParameter; }
80  InputDataType& InputParameter() { return inputParameter; }
81 
83  OutputDataType const& OutputParameter() const { return outputParameter; }
85  OutputDataType& OutputParameter() { return outputParameter; }
86 
88  OutputDataType const& Delta() const { return delta; }
90  OutputDataType& Delta() { return delta; }
91 
93  size_t const& InputWidth() const { return inputWidth; }
95  size_t& InputWidth() { return inputWidth; }
96 
98  size_t const& InputHeight() const { return inputHeight; }
100  size_t& InputHeight() { return inputHeight; }
101 
103  size_t const& OutputWidth() const { return outputWidth; }
105  size_t& OutputWidth() { return outputWidth; }
106 
108  size_t const& OutputHeight() const { return outputHeight; }
110  size_t& OutputHeight() { return outputHeight; }
111 
113  bool Deterministic() const { return deterministic; }
115  bool& Deterministic() { return deterministic; }
116 
120  template<typename Archive>
121  void Serialize(Archive& ar, const unsigned int /* version */);
122 
123  private:
124 
131  template<typename eT>
132  void Pooling(const arma::Mat<eT>& input, arma::Mat<eT>& output)
133  {
134  const size_t rStep = kW;
135  const size_t cStep = kH;
136 
137  for (size_t j = 0, colidx = 0; j < output.n_cols; ++j, colidx += dH)
138  {
139  for (size_t i = 0, rowidx = 0; i < output.n_rows; ++i, rowidx += dW)
140  {
141  arma::mat subInput = input(
142  arma::span(rowidx, rowidx + rStep - 1 - offset),
143  arma::span(colidx, colidx + cStep - 1 - offset));
144 
145  output(i, j) = arma::mean(arma::mean(subInput));
146  }
147  }
148  }
149 
156  template<typename eT>
157  void Unpooling(const arma::Mat<eT>& input,
158  const arma::Mat<eT>& error,
159  arma::Mat<eT>& output)
160  {
161  const size_t rStep = input.n_rows / error.n_rows - offset;
162  const size_t cStep = input.n_cols / error.n_cols - offset;
163 
164  arma::Mat<eT> unpooledError;
165  for (size_t j = 0; j < input.n_cols - cStep; j += cStep)
166  {
167  for (size_t i = 0; i < input.n_rows - rStep; i += rStep)
168  {
169  const arma::Mat<eT>& inputArea = input(arma::span(i, i + rStep - 1),
170  arma::span(j, j + cStep - 1));
171 
172  unpooledError = arma::Mat<eT>(inputArea.n_rows, inputArea.n_cols);
173  unpooledError.fill(error(i / rStep, j / cStep) / inputArea.n_elem);
174 
175  output(arma::span(i, i + rStep - 1 - offset),
176  arma::span(j, j + cStep - 1 - offset)) += unpooledError;
177  }
178  }
179  }
180 
182  size_t inSize;
183 
185  size_t outSize;
186 
188  size_t kW;
189 
191  size_t kH;
192 
194  size_t dW;
195 
197  size_t dH;
198 
200  size_t inputWidth;
201 
203  size_t inputHeight;
204 
206  size_t outputWidth;
207 
209  size_t outputHeight;
210 
212  bool reset;
213 
215  bool floor;
216 
219 
221  size_t offset;
222 
224  arma::cube outputTemp;
225 
227  arma::cube inputTemp;
228 
230  arma::cube gTemp;
231 
233  OutputDataType delta;
234 
236  OutputDataType gradient;
237 
239  InputDataType inputParameter;
240 
242  OutputDataType outputParameter;
243 }; // class MeanPooling
244 
245 
246 } // namespace ann
247 } // namespace mlpack
248 
249 // Include implementation.
250 #include "mean_pooling_impl.hpp"
251 
252 #endif
arma::cube gTemp
Locally-stored transformed output parameter.
size_t & OutputHeight()
Modify the height.
OutputDataType delta
Locally-stored delta object.
size_t const & OutputWidth() const
Get the width.
OutputDataType gradient
Locally-stored gradient object.
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: binarize.hpp:18
OutputDataType const & OutputParameter() const
Get the output parameter.
MeanPooling()
Create the MeanPooling object.
size_t & InputWidth()
Modify the width.
The core includes that mlpack expects; standard C++ includes and Armadillo.
size_t kW
Locally-stored width of the pooling window.
size_t inputHeight
Locally-stored input height.
arma::cube outputTemp
Locally-stored output parameter.
Implementation of the MeanPooling.
InputDataType const & InputParameter() const
Get the input parameter.
size_t kH
Locally-stored height of the pooling window.
void Pooling(const arma::Mat< eT > &input, arma::Mat< eT > &output)
Apply pooling to the input and store the results.
void Unpooling(const arma::Mat< eT > &input, const arma::Mat< eT > &error, arma::Mat< eT > &output)
Apply unpooling to the input and store the results.
size_t outputWidth
Locally-stored output width.
bool deterministic
If true use maximum a posteriori during the forward pass.
bool & Deterministic()
Modify the value of the deterministic parameter.
size_t outputHeight
Locally-stored output height.
bool Deterministic() const
Get the value of the deterministic parameter.
bool reset
Locally-stored reset parameter used to initialize the module once.
size_t offset
Locally-stored stored rounding offset.
size_t const & InputHeight() const
Get the height.
size_t & InputHeight()
Modify the height.
bool floor
Rounding operation used.
size_t inputWidth
Locally-stored input width.
void Forward(const arma::Mat< eT > &&input, arma::Mat< eT > &&output)
Ordinary feed forward pass of a neural network, evaluating the function f(x) by propagating the activ...
size_t const & InputWidth() const
Get the width.
OutputDataType & OutputParameter()
Modify the output parameter.
size_t const & OutputHeight() const
Get the height.
void Backward(const arma::Mat< eT > &&, arma::Mat< eT > &&gy, arma::Mat< eT > &&g)
Ordinary feed backward pass of a neural network, using 3rd-order tensors as input, calculating the function f(x) by propagating x backwards through f.
size_t inSize
Locally-stored number of input units.
InputDataType & InputParameter()
Modify the input parameter.
size_t dW
Locally-stored width of the stride operation.
size_t outSize
Locally-stored number of output units.
void Serialize(Archive &ar, const unsigned int)
Serialize the layer.
arma::cube inputTemp
Locally-stored transformed input parameter.
OutputDataType outputParameter
Locally-stored output parameter object.
OutputDataType & Delta()
Modify the delta.
size_t dH
Locally-stored height of the stride operation.
OutputDataType const & Delta() const
Get the delta.
InputDataType inputParameter
Locally-stored input parameter object.
size_t & OutputWidth()
Modify the width.