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  for (size_t j = 0, colidx = 0; j < output.n_cols;
165  ++j, colidx += strideHeight)
166  {
167  for (size_t i = 0, rowidx = 0; i < output.n_rows;
168  ++i, rowidx += strideWidth)
169  {
170  arma::mat subInput = input(
171  arma::span(rowidx, rowidx + kernelWidth - 1 - offset),
172  arma::span(colidx, colidx + kernelHeight - 1 - offset));
173 
174  output(i, j) = pow(arma::accu(arma::pow(subInput,
175  normType)), 1.0 / normType);
176  }
177  }
178  }
179 
186  template<typename eT>
187  void Unpooling(const arma::Mat<eT>& input,
188  const arma::Mat<eT>& error,
189  arma::Mat<eT>& output)
190  {
191  const size_t rStep = input.n_rows / error.n_rows - offset;
192  const size_t cStep = input.n_cols / error.n_cols - offset;
193 
194  arma::Mat<eT> unpooledError;
195  for (size_t j = 0; j < input.n_cols - cStep; j += cStep)
196  {
197  for (size_t i = 0; i < input.n_rows - rStep; i += rStep)
198  {
199  const arma::Mat<eT>& inputArea = input(arma::span(i, i + rStep - 1),
200  arma::span(j, j + cStep - 1));
201  size_t sum = pow(arma::accu(arma::pow(inputArea, normType)),
202  (normType - 1) / normType);
203  unpooledError = arma::Mat<eT>(inputArea.n_rows, inputArea.n_cols);
204  unpooledError.fill(error(i / rStep, j / cStep));
205  unpooledError %= arma::pow(inputArea, normType - 1);
206  unpooledError /= sum;
207  output(arma::span(i, i + rStep - 1 - offset),
208  arma::span(j, j + cStep - 1 - offset)) += unpooledError;
209  }
210  }
211  }
212 
214  size_t normType;
215 
217  size_t kernelWidth;
218 
220  size_t kernelHeight;
221 
223  size_t strideWidth;
224 
226  size_t strideHeight;
227 
229  bool floor;
230 
232  size_t inSize;
233 
235  size_t outSize;
236 
238  size_t inputWidth;
239 
241  size_t inputHeight;
242 
244  size_t outputWidth;
245 
247  size_t outputHeight;
248 
250  bool reset;
251 
253  size_t offset;
254 
256  size_t batchSize;
257 
259  arma::cube outputTemp;
260 
262  arma::cube inputTemp;
263 
265  arma::cube gTemp;
266 
268  OutputDataType delta;
269 
271  OutputDataType gradient;
272 
274  OutputDataType outputParameter;
275 }; // class LpPooling
276 
277 
278 } // namespace ann
279 } // namespace mlpack
280 
281 // Include implementation.
282 #include "lp_pooling_impl.hpp"
283 
284 #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