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 
48  MeanPooling(const size_t kernelWidth,
49  const size_t kernelHeight,
50  const size_t strideWidth = 1,
51  const size_t strideHeight = 1,
52  const bool floor = true);
53 
61  template<typename eT>
62  void Forward(const arma::Mat<eT>& input, arma::Mat<eT>& output);
63 
73  template<typename eT>
74  void Backward(const arma::Mat<eT>& /* input */,
75  const arma::Mat<eT>& gy,
76  arma::Mat<eT>& g);
77 
79  OutputDataType const& OutputParameter() const { return outputParameter; }
81  OutputDataType& OutputParameter() { return outputParameter; }
82 
84  OutputDataType const& Delta() const { return delta; }
86  OutputDataType& Delta() { return delta; }
87 
89  size_t const& InputWidth() const { return inputWidth; }
91  size_t& InputWidth() { return inputWidth; }
92 
94  size_t const& InputHeight() const { return inputHeight; }
96  size_t& InputHeight() { return inputHeight; }
97 
99  size_t const& OutputWidth() const { return outputWidth; }
101  size_t& OutputWidth() { return outputWidth; }
102 
104  size_t const& OutputHeight() const { return outputHeight; }
106  size_t& OutputHeight() { return outputHeight; }
107 
109  size_t InputSize() const { return inSize; }
110 
112  size_t OutputSize() const { return outSize; }
113 
115  size_t KernelWidth() const { return kernelWidth; }
117  size_t& KernelWidth() { return kernelWidth; }
118 
120  size_t KernelHeight() const { return kernelHeight; }
122  size_t& KernelHeight() { return kernelHeight; }
123 
125  size_t StrideWidth() const { return strideWidth; }
127  size_t& StrideWidth() { return strideWidth; }
128 
130  size_t StrideHeight() const { return strideHeight; }
132  size_t& StrideHeight() { return strideHeight; }
133 
135  bool const& Floor() const { return floor; }
137  bool& Floor() { return floor; }
138 
140  bool Deterministic() const { return deterministic; }
142  bool& Deterministic() { return deterministic; }
143 
145  size_t WeightSize() const { return 0; }
146 
150  template<typename Archive>
151  void serialize(Archive& ar, const uint32_t /* version */);
152 
153  private:
160  template<typename eT>
161  void Pooling(const arma::Mat<eT>& input, arma::Mat<eT>& output)
162  {
163  for (size_t j = 0, colidx = 0; j < output.n_cols;
164  ++j, colidx += strideHeight)
165  {
166  for (size_t i = 0, rowidx = 0; i < output.n_rows;
167  ++i, rowidx += strideWidth)
168  {
169  size_t rowEnd = rowidx + kernelWidth - 1;
170  size_t colEnd = colidx + kernelHeight - 1;
171 
172  if (rowEnd > input.n_rows - 1)
173  rowEnd = input.n_rows - 1;
174  if (colEnd > input.n_cols - 1)
175  colEnd = input.n_cols - 1;
176 
177  arma::mat subInput = input(
178  arma::span(rowidx, rowEnd),
179  arma::span(colidx, colEnd));
180 
181  output(i, j) = arma::mean(arma::mean(subInput));
182  }
183  }
184  }
185 
192  template<typename eT>
193  void Unpooling(const arma::Mat<eT>& input,
194  const arma::Mat<eT>& error,
195  arma::Mat<eT>& output)
196  {
197 
198  arma::Mat<eT> unpooledError;
199  for (size_t j = 0, colidx = 0; j < input.n_cols; j += strideHeight, colidx++)
200  {
201  for (size_t i = 0, rowidx = 0; i < input.n_rows; i += strideWidth, rowidx++)
202  {
203  size_t rowEnd = i + kernelWidth - 1;
204  size_t colEnd = j + kernelHeight - 1;
205 
206  if (rowEnd > input.n_rows - 1)
207  {
208  if (floor)
209  continue;
210  rowEnd = input.n_rows - 1;
211  }
212 
213  if (colEnd > input.n_cols - 1)
214  {
215  if (floor)
216  continue;
217  colEnd = input.n_cols - 1;
218  }
219 
220  arma::mat InputArea = input(arma::span(i, rowEnd), arma::span(j, colEnd));
221 
222  unpooledError = arma::Mat<eT>(InputArea.n_rows, InputArea.n_cols);
223  unpooledError.fill(error(rowidx, colidx) / InputArea.n_elem);
224 
225  output(arma::span(i, i + InputArea.n_rows - 1),
226  arma::span(j, j + InputArea.n_cols - 1)) += unpooledError;
227  }
228  }
229  }
230 
232  size_t kernelWidth;
233 
235  size_t kernelHeight;
236 
238  size_t strideWidth;
239 
241  size_t strideHeight;
242 
244  bool floor;
245 
247  size_t inSize;
248 
250  size_t outSize;
251 
253  size_t inputWidth;
254 
256  size_t inputHeight;
257 
259  size_t outputWidth;
260 
262  size_t outputHeight;
263 
265  bool reset;
266 
268  bool deterministic;
269 
271  size_t batchSize;
272 
274  arma::cube outputTemp;
275 
277  arma::cube inputTemp;
278 
280  arma::cube gTemp;
281 
283  OutputDataType delta;
284 
286  OutputDataType gradient;
287 
289  OutputDataType outputParameter;
290 }; // class MeanPooling
291 
292 
293 } // namespace ann
294 } // namespace mlpack
295 
296 // Include implementation.
297 #include "mean_pooling_impl.hpp"
298 
299 #endif
size_t KernelHeight() const
Get the kernel height.
OutputDataType const & Delta() const
Get the delta.
void serialize(Archive &ar, const uint32_t)
Serialize the layer.
Linear algebra utility functions, generally performed on matrices or vectors.
size_t & KernelWidth()
Modify the kernel width.
MeanPooling()
Create the MeanPooling object.
size_t & OutputHeight()
Modify the output height.
The core includes that mlpack expects; standard C++ includes and Armadillo.
size_t const & OutputHeight() const
Get the output height.
size_t const & InputHeight() const
Get the input height.
bool Deterministic() const
Get the value of the deterministic parameter.
OutputDataType & Delta()
Modify the delta.
Implementation of the MeanPooling.
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 StrideWidth() const
Get the stride width.
OutputDataType const & OutputParameter() const
Get the output parameter.
size_t InputSize() const
Get the input size.
size_t KernelWidth() const
Get the kernel width.
size_t const & OutputWidth() const
Get the output width.
size_t & InputHeight()
Modify the input height.
size_t const & InputWidth() const
Get the intput width.
OutputDataType & OutputParameter()
Modify the output parameter.
bool const & Floor() const
Get the value of the rounding operation.
size_t & InputWidth()
Modify the input width.
bool & Floor()
Modify the value of the rounding operation.
bool & Deterministic()
Modify the value of the deterministic parameter.
size_t & OutputWidth()
Modify the output width.
size_t & StrideHeight()
Modify the stride height.
size_t OutputSize() const
Get the output size.
size_t WeightSize() const
Get the size of the weights.
size_t & StrideWidth()
Modify the stride width.
size_t StrideHeight() const
Get the stride height.
size_t & KernelHeight()
Modify the kernel height.
void Backward(const arma::Mat< eT > &, const 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.