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 kernelWidth,
48  const size_t kernelHeight,
49  const size_t strideWidth = 1,
50  const size_t strideHeight = 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  OutputDataType const& OutputParameter() const { return outputParameter; }
80  OutputDataType& OutputParameter() { return outputParameter; }
81 
83  OutputDataType const& Delta() const { return delta; }
85  OutputDataType& Delta() { return delta; }
86 
88  size_t const& InputWidth() const { return inputWidth; }
90  size_t& InputWidth() { return inputWidth; }
91 
93  size_t const& InputHeight() const { return inputHeight; }
95  size_t& InputHeight() { return inputHeight; }
96 
98  size_t const& OutputWidth() const { return outputWidth; }
100  size_t& OutputWidth() { return outputWidth; }
101 
103  size_t const& OutputHeight() const { return outputHeight; }
105  size_t& OutputHeight() { return outputHeight; }
106 
108  size_t InputSize() const { return inSize; }
109 
111  size_t OutputSize() const { return outSize; }
112 
114  size_t KernelWidth() const { return kernelWidth; }
116  size_t& KernelWidth() { return kernelWidth; }
117 
119  size_t KernelHeight() const { return kernelHeight; }
121  size_t& KernelHeight() { return kernelHeight; }
122 
124  size_t StrideWidth() const { return strideWidth; }
126  size_t& StrideWidth() { return strideWidth; }
127 
129  size_t StrideHeight() const { return strideHeight; }
131  size_t& StrideHeight() { return strideHeight; }
132 
134  bool const& Floor() const { return floor; }
136  bool& Floor() { return floor; }
137 
139  bool Deterministic() const { return deterministic; }
141  bool& Deterministic() { return deterministic; }
142 
146  template<typename Archive>
147  void serialize(Archive& ar, const unsigned int /* version */);
148 
149  private:
156  template<typename eT>
157  void Pooling(const arma::Mat<eT>& input, arma::Mat<eT>& output)
158  {
159  const size_t rStep = kernelWidth;
160  const size_t cStep = kernelHeight;
161 
162  for (size_t j = 0, colidx = 0; j < output.n_cols;
163  ++j, colidx += strideHeight)
164  {
165  for (size_t i = 0, rowidx = 0; i < output.n_rows;
166  ++i, rowidx += strideWidth)
167  {
168  arma::mat subInput = input(
169  arma::span(rowidx, rowidx + rStep - 1 - offset),
170  arma::span(colidx, colidx + cStep - 1 - offset));
171 
172  output(i, j) = arma::mean(arma::mean(subInput));
173  }
174  }
175  }
176 
183  template<typename eT>
184  void Unpooling(const arma::Mat<eT>& input,
185  const arma::Mat<eT>& error,
186  arma::Mat<eT>& output)
187  {
188  const size_t rStep = input.n_rows / error.n_rows - offset;
189  const size_t cStep = input.n_cols / error.n_cols - offset;
190 
191  arma::Mat<eT> unpooledError;
192  for (size_t j = 0; j < input.n_cols - cStep; j += cStep)
193  {
194  for (size_t i = 0; i < input.n_rows - rStep; i += rStep)
195  {
196  const arma::Mat<eT>& inputArea = input(arma::span(i, i + rStep - 1),
197  arma::span(j, j + cStep - 1));
198 
199  unpooledError = arma::Mat<eT>(inputArea.n_rows, inputArea.n_cols);
200  unpooledError.fill(error(i / rStep, j / cStep) / inputArea.n_elem);
201 
202  output(arma::span(i, i + rStep - 1 - offset),
203  arma::span(j, j + cStep - 1 - offset)) += unpooledError;
204  }
205  }
206  }
207 
209  size_t kernelWidth;
210 
212  size_t kernelHeight;
213 
215  size_t strideWidth;
216 
218  size_t strideHeight;
219 
221  bool floor;
222 
224  size_t inSize;
225 
227  size_t outSize;
228 
230  size_t inputWidth;
231 
233  size_t inputHeight;
234 
236  size_t outputWidth;
237 
239  size_t outputHeight;
240 
242  bool reset;
243 
245  bool deterministic;
246 
248  size_t offset;
249 
251  size_t batchSize;
252 
254  arma::cube outputTemp;
255 
257  arma::cube inputTemp;
258 
260  arma::cube gTemp;
261 
263  OutputDataType delta;
264 
266  OutputDataType gradient;
267 
269  OutputDataType outputParameter;
270 }; // class MeanPooling
271 
272 
273 } // namespace ann
274 } // namespace mlpack
275 
276 // Include implementation.
277 #include "mean_pooling_impl.hpp"
278 
279 #endif
size_t KernelHeight() const
Get the kernel 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.
OutputDataType const & Delta() const
Get the delta.
strip_type.hpp
Definition: add_to_po.hpp:21
size_t & KernelWidth()
Modify the kernel width.
MeanPooling()
Create the MeanPooling object.
size_t & OutputHeight()
Modify the height.
The core includes that mlpack expects; standard C++ includes and Armadillo.
size_t const & OutputHeight() const
Get the height.
size_t const & InputHeight() const
Get the height.
bool Deterministic() const
Get the value of the deterministic parameter.
OutputDataType & Delta()
Modify the delta.
Implementation of the MeanPooling.
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 width.
size_t & InputHeight()
Modify the height.
size_t const & InputWidth() const
Get the width.
OutputDataType & OutputParameter()
Modify the output parameter.
bool const & Floor() const
Get the value of the rounding operation.
size_t & InputWidth()
Modify the width.
bool & Floor()
Modify the value of the rounding operation.
bool & Deterministic()
Modify the value of the deterministic parameter.
size_t & OutputWidth()
Modify the 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.
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()
Modify the stride width.
size_t StrideHeight() const
Get the stride height.
size_t & KernelHeight()
Modify the kernel height.