lp_pooling.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_METHODS_ANN_LAYER_LP_POOLING_HPP
13 #define MLPACK_METHODS_ANN_LAYER_LP_POOLING_HPP
14 
15 #include <mlpack/prereqs.hpp>
16 
17 namespace mlpack {
18 namespace ann {
19 
28 template <
29  typename InputDataType = arma::mat,
30  typename OutputDataType = arma::mat
31 >
32 class LpPooling
33 {
34  public:
36  LpPooling();
37 
48  LpPooling(const size_t normType,
49  const size_t kernelWidth,
50  const size_t kernelHeight,
51  const size_t strideWidth = 1,
52  const size_t strideHeight = 1,
53  const bool floor = true);
54 
62  template<typename eT>
63  void Forward(const arma::Mat<eT>& input, arma::Mat<eT>& output);
64 
74  template<typename eT>
75  void Backward(const arma::Mat<eT>& /* input */,
76  const arma::Mat<eT>& gy,
77  arma::Mat<eT>& g);
78 
80  OutputDataType const& OutputParameter() const { return outputParameter; }
82  OutputDataType& OutputParameter() { return outputParameter; }
83 
85  OutputDataType const& Delta() const { return delta; }
87  OutputDataType& Delta() { return delta; }
88 
90  size_t const& InputWidth() const { return inputWidth; }
92  size_t& InputWidth() { return inputWidth; }
93 
95  size_t const& InputHeight() const { return inputHeight; }
97  size_t& InputHeight() { return inputHeight; }
98 
100  size_t const& OutputWidth() const { return outputWidth; }
102  size_t& OutputWidth() { return outputWidth; }
103 
105  size_t const& OutputHeight() const { return outputHeight; }
107  size_t& OutputHeight() { return outputHeight; }
108 
110  size_t InputSize() const { return inSize; }
111 
113  size_t OutputSize() const { return outSize; }
114 
116  size_t NormType() const { return normType; }
118  size_t& NormType() { return normType; }
119 
121  size_t KernelWidth() const { return kernelWidth; }
123  size_t& KernelWidth() { return kernelWidth; }
124 
126  size_t KernelHeight() const { return kernelHeight; }
128  size_t& KernelHeight() { return kernelHeight; }
129 
131  size_t StrideWidth() const { return strideWidth; }
133  size_t& StrideWidth() { return strideWidth; }
134 
136  size_t StrideHeight() const { return strideHeight; }
138  size_t& StrideHeight() { return strideHeight; }
139 
141  bool const& Floor() const { return floor; }
143  bool& Floor() { return floor; }
144 
146  size_t WeightSize() const { return 0; }
147 
151  template<typename Archive>
152  void serialize(Archive& ar, const uint32_t /* version */);
153 
154  private:
161  template<typename eT>
162  void Pooling(const arma::Mat<eT>& input, arma::Mat<eT>& output)
163  {
164  arma::Mat<eT> inputPre = input;
165  inputPre = arma::pow(inputPre, normType);
166 
167  for (size_t i = 1; i < input.n_cols; ++i)
168  inputPre.col(i) += inputPre.col(i - 1);
169 
170  for (size_t i = 1; i < input.n_rows; ++i)
171  inputPre.row(i) += inputPre.row(i - 1);
172 
173  for (size_t j = 0, colidx = 0; j < output.n_cols;
174  ++j, colidx += strideHeight)
175  {
176  for (size_t i = 0, rowidx = 0; i < output.n_rows;
177  ++i, rowidx += strideWidth)
178  {
179  double val = 0.0;
180  size_t rowEnd = rowidx + kernelWidth - 1;
181  size_t colEnd = colidx + kernelHeight - 1;
182 
183  if (rowEnd > input.n_rows - 1)
184  rowEnd = input.n_rows - 1;
185  if (colEnd > input.n_cols - 1)
186  colEnd = input.n_cols - 1;
187 
188  val += inputPre(rowEnd, colEnd);
189  if (rowidx >= 1)
190  {
191  if (colidx >= 1)
192  val += inputPre(rowidx - 1, colidx - 1);
193  val -= inputPre(rowidx - 1, colEnd);
194  }
195 
196  if (colidx >= 1)
197  val -= inputPre(rowEnd, colidx - 1);
198 
199  output(i, j) = val;
200  }
201  }
202 
203  output = arma::pow(output, 1.0 / normType);
204  }
205 
212  template<typename eT>
213  void Unpooling(const arma::Mat<eT>& input,
214  const arma::Mat<eT>& error,
215  arma::Mat<eT>& output)
216  {
217  arma::Mat<eT> unpooledError;
218  for (size_t j = 0, colidx = 0; j < input.n_cols; j += strideHeight,
219  colidx++)
220  {
221  for (size_t i = 0, rowidx = 0; i < input.n_rows; i += strideWidth,
222  rowidx++)
223  {
224  size_t rowEnd = i + kernelWidth - 1;
225  size_t colEnd = j + kernelHeight - 1;
226 
227  if (rowEnd > input.n_rows - 1)
228  {
229  if (floor)
230  continue;
231  rowEnd = input.n_rows - 1;
232  }
233 
234  if (colEnd > input.n_cols - 1)
235  {
236  if (floor)
237  continue;
238  colEnd = input.n_cols - 1;
239  }
240 
241  arma::mat InputArea = input(arma::span(i, rowEnd),
242  arma::span(j, colEnd));
243 
244  size_t sum = pow(arma::accu(arma::pow(InputArea, normType)),
245  (normType - 1) / normType);
246  unpooledError = arma::Mat<eT>(InputArea.n_rows, InputArea.n_cols);
247  unpooledError.fill(error(rowidx, colidx) / InputArea.n_elem);
248  unpooledError %= arma::pow(InputArea, normType - 1);
249  unpooledError /= sum;
250  output(arma::span(i, i + InputArea.n_rows - 1),
251  arma::span(j, j + InputArea.n_cols - 1)) += unpooledError;
252  }
253  }
254  }
255 
257  size_t normType;
258 
260  size_t kernelWidth;
261 
263  size_t kernelHeight;
264 
266  size_t strideWidth;
267 
269  size_t strideHeight;
270 
272  bool floor;
273 
275  size_t inSize;
276 
278  size_t outSize;
279 
281  size_t inputWidth;
282 
284  size_t inputHeight;
285 
287  size_t outputWidth;
288 
290  size_t outputHeight;
291 
293  bool reset;
294 
296  size_t batchSize;
297 
299  arma::cube outputTemp;
300 
302  arma::cube inputTemp;
303 
305  arma::cube gTemp;
306 
308  OutputDataType delta;
309 
311  OutputDataType gradient;
312 
314  OutputDataType outputParameter;
315 }; // class LpPooling
316 
317 
318 } // namespace ann
319 } // namespace mlpack
320 
321 // Include implementation.
322 #include "lp_pooling_impl.hpp"
323 
324 #endif
size_t const & InputWidth() const
Get the intput width.
Definition: lp_pooling.hpp:90
void serialize(Archive &ar, const uint32_t)
Serialize the layer.
size_t StrideWidth() const
Get the stride width.
Definition: lp_pooling.hpp:131
LpPooling()
Create the LpPooling object.
Linear algebra utility functions, generally performed on matrices or vectors.
size_t & StrideWidth()
Modify the stride width.
Definition: lp_pooling.hpp:133
Implementation of the LPPooling.
Definition: lp_pooling.hpp:32
size_t & KernelWidth()
Modify the kernel width.
Definition: lp_pooling.hpp:123
size_t KernelHeight() const
Get the kernel height.
Definition: lp_pooling.hpp:126
size_t OutputSize() const
Get the output size.
Definition: lp_pooling.hpp:113
The core includes that mlpack expects; standard C++ includes and Armadillo.
size_t WeightSize() const
Get the size of the weights.
Definition: lp_pooling.hpp:146
OutputDataType const & Delta() const
Get the delta.
Definition: lp_pooling.hpp:85
size_t & InputWidth()
Modify the input width.
Definition: lp_pooling.hpp:92
size_t StrideHeight() const
Get the stride height.
Definition: lp_pooling.hpp:136
bool & Floor()
Modify the value of the rounding operation.
Definition: lp_pooling.hpp:143
size_t KernelWidth() const
Get the kernel width.
Definition: lp_pooling.hpp:121
size_t & NormType()
Modify the normType.
Definition: lp_pooling.hpp:118
OutputDataType const & OutputParameter() const
Get the output parameter.
Definition: lp_pooling.hpp:80
size_t const & OutputWidth() const
Get the output width.
Definition: lp_pooling.hpp:100
size_t & OutputWidth()
Modify the output width.
Definition: lp_pooling.hpp:102
OutputDataType & OutputParameter()
Modify the output parameter.
Definition: lp_pooling.hpp:82
size_t & StrideHeight()
Modify the stride height.
Definition: lp_pooling.hpp:138
size_t & KernelHeight()
Modify the kernel height.
Definition: lp_pooling.hpp:128
size_t const & InputHeight() const
Get the input height.
Definition: lp_pooling.hpp:95
size_t const & OutputHeight() const
Get the output height.
Definition: lp_pooling.hpp:105
size_t & OutputHeight()
Modify the output height.
Definition: lp_pooling.hpp:107
size_t NormType() const
Get the normType.
Definition: lp_pooling.hpp:116
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...
OutputDataType & Delta()
Modify the delta.
Definition: lp_pooling.hpp:87
size_t & InputHeight()
Modify the input height.
Definition: lp_pooling.hpp:97
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.
size_t InputSize() const
Get the input size.
Definition: lp_pooling.hpp:110
bool const & Floor() const
Get the value of the rounding operation.
Definition: lp_pooling.hpp:141