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 kW,
48  const size_t kH,
49  const size_t dW = 1,
50  const size_t dH = 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  bool Deterministic() const { return deterministic; }
110  bool& Deterministic() { return deterministic; }
111 
115  template<typename Archive>
116  void serialize(Archive& ar, const unsigned int /* version */);
117 
118  private:
125  template<typename eT>
126  void Pooling(const arma::Mat<eT>& input, arma::Mat<eT>& output)
127  {
128  const size_t rStep = kW;
129  const size_t cStep = kH;
130 
131  for (size_t j = 0, colidx = 0; j < output.n_cols; ++j, colidx += dH)
132  {
133  for (size_t i = 0, rowidx = 0; i < output.n_rows; ++i, rowidx += dW)
134  {
135  arma::mat subInput = input(
136  arma::span(rowidx, rowidx + rStep - 1 - offset),
137  arma::span(colidx, colidx + cStep - 1 - offset));
138 
139  output(i, j) = arma::mean(arma::mean(subInput));
140  }
141  }
142  }
143 
150  template<typename eT>
151  void Unpooling(const arma::Mat<eT>& input,
152  const arma::Mat<eT>& error,
153  arma::Mat<eT>& output)
154  {
155  const size_t rStep = input.n_rows / error.n_rows - offset;
156  const size_t cStep = input.n_cols / error.n_cols - offset;
157 
158  arma::Mat<eT> unpooledError;
159  for (size_t j = 0; j < input.n_cols - cStep; j += cStep)
160  {
161  for (size_t i = 0; i < input.n_rows - rStep; i += rStep)
162  {
163  const arma::Mat<eT>& inputArea = input(arma::span(i, i + rStep - 1),
164  arma::span(j, j + cStep - 1));
165 
166  unpooledError = arma::Mat<eT>(inputArea.n_rows, inputArea.n_cols);
167  unpooledError.fill(error(i / rStep, j / cStep) / inputArea.n_elem);
168 
169  output(arma::span(i, i + rStep - 1 - offset),
170  arma::span(j, j + cStep - 1 - offset)) += unpooledError;
171  }
172  }
173  }
174 
176  size_t kW;
177 
179  size_t kH;
180 
182  size_t dW;
183 
185  size_t dH;
186 
188  bool floor;
189 
191  size_t inSize;
192 
194  size_t outSize;
195 
197  size_t inputWidth;
198 
200  size_t inputHeight;
201 
203  size_t outputWidth;
204 
206  size_t outputHeight;
207 
209  bool reset;
210 
212  bool deterministic;
213 
215  size_t offset;
216 
218  size_t batchSize;
219 
221  arma::cube outputTemp;
222 
224  arma::cube inputTemp;
225 
227  arma::cube gTemp;
228 
230  OutputDataType delta;
231 
233  OutputDataType gradient;
234 
236  OutputDataType outputParameter;
237 }; // class MeanPooling
238 
239 
240 } // namespace ann
241 } // namespace mlpack
242 
243 // Include implementation.
244 #include "mean_pooling_impl.hpp"
245 
246 #endif
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.
.hpp
Definition: add_to_po.hpp:21
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.
OutputDataType const & OutputParameter() const
Get the output parameter.
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.
size_t & InputWidth()
Modify the width.
bool & Deterministic()
Modify the value of the deterministic parameter.
size_t & OutputWidth()
Modify the width.
void serialize(Archive &ar, const unsigned int)
Serialize the layer.
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...