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 
147  template<typename Archive>
148  void serialize(Archive& ar, const unsigned int /* version */);
149 
150  private:
157  template<typename eT>
158  void Pooling(const arma::Mat<eT>& input, arma::Mat<eT>& output)
159  {
160  for (size_t j = 0, colidx = 0; j < output.n_cols;
161  ++j, colidx += strideHeight)
162  {
163  for (size_t i = 0, rowidx = 0; i < output.n_rows;
164  ++i, rowidx += strideWidth)
165  {
166  arma::mat subInput = input(
167  arma::span(rowidx, rowidx + kernelWidth - 1 - offset),
168  arma::span(colidx, colidx + kernelHeight - 1 - offset));
169 
170  output(i, j) = arma::mean(arma::mean(subInput));
171  }
172  }
173  }
174 
181  template<typename eT>
182  void Unpooling(const arma::Mat<eT>& input,
183  const arma::Mat<eT>& error,
184  arma::Mat<eT>& output)
185  {
186  const size_t rStep = input.n_rows / error.n_rows - offset;
187  const size_t cStep = input.n_cols / error.n_cols - offset;
188 
189  arma::Mat<eT> unpooledError;
190  for (size_t j = 0; j < input.n_cols - cStep; j += cStep)
191  {
192  for (size_t i = 0; i < input.n_rows - rStep; i += rStep)
193  {
194  const arma::Mat<eT>& inputArea = input(arma::span(i, i + rStep - 1),
195  arma::span(j, j + cStep - 1));
196 
197  unpooledError = arma::Mat<eT>(inputArea.n_rows, inputArea.n_cols);
198  unpooledError.fill(error(i / rStep, j / cStep) / inputArea.n_elem);
199 
200  output(arma::span(i, i + rStep - 1 - offset),
201  arma::span(j, j + cStep - 1 - offset)) += unpooledError;
202  }
203  }
204  }
205 
207  size_t kernelWidth;
208 
210  size_t kernelHeight;
211 
213  size_t strideWidth;
214 
216  size_t strideHeight;
217 
219  bool floor;
220 
222  size_t inSize;
223 
225  size_t outSize;
226 
228  size_t inputWidth;
229 
231  size_t inputHeight;
232 
234  size_t outputWidth;
235 
237  size_t outputHeight;
238 
240  bool reset;
241 
243  bool deterministic;
244 
246  size_t offset;
247 
249  size_t batchSize;
250 
252  arma::cube outputTemp;
253 
255  arma::cube inputTemp;
256 
258  arma::cube gTemp;
259 
261  OutputDataType delta;
262 
264  OutputDataType gradient;
265 
267  OutputDataType outputParameter;
268 }; // class MeanPooling
269 
270 
271 } // namespace ann
272 } // namespace mlpack
273 
274 // Include implementation.
275 #include "mean_pooling_impl.hpp"
276 
277 #endif
size_t KernelHeight() const
Get the kernel height.
OutputDataType const & Delta() const
Get the delta.
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: add_to_po.hpp:21
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.
void serialize(Archive &ar, const unsigned int)
Serialize the layer.
size_t OutputSize() const
Get the output size.
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.