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  InputDataType const& InputParameter() const { return inputParameter; }
80  InputDataType& InputParameter() { return inputParameter; }
81 
83  OutputDataType const& OutputParameter() const { return outputParameter; }
85  OutputDataType& OutputParameter() { return outputParameter; }
86 
88  OutputDataType const& Delta() const { return delta; }
90  OutputDataType& Delta() { return delta; }
91 
93  size_t const& InputWidth() const { return inputWidth; }
95  size_t& InputWidth() { return inputWidth; }
96 
98  size_t const& InputHeight() const { return inputHeight; }
100  size_t& InputHeight() { return inputHeight; }
101 
103  size_t const& OutputWidth() const { return outputWidth; }
105  size_t& OutputWidth() { return outputWidth; }
106 
108  size_t const& OutputHeight() const { return outputHeight; }
110  size_t& OutputHeight() { return outputHeight; }
111 
113  bool Deterministic() const { return deterministic; }
115  bool& Deterministic() { return deterministic; }
116 
120  template<typename Archive>
121  void serialize(Archive& ar, const unsigned int /* version */);
122 
123  private:
130  template<typename eT>
131  void Pooling(const arma::Mat<eT>& input, arma::Mat<eT>& output)
132  {
133  const size_t rStep = kW;
134  const size_t cStep = kH;
135 
136  for (size_t j = 0, colidx = 0; j < output.n_cols; ++j, colidx += dH)
137  {
138  for (size_t i = 0, rowidx = 0; i < output.n_rows; ++i, rowidx += dW)
139  {
140  arma::mat subInput = input(
141  arma::span(rowidx, rowidx + rStep - 1 - offset),
142  arma::span(colidx, colidx + cStep - 1 - offset));
143 
144  output(i, j) = arma::mean(arma::mean(subInput));
145  }
146  }
147  }
148 
155  template<typename eT>
156  void Unpooling(const arma::Mat<eT>& input,
157  const arma::Mat<eT>& error,
158  arma::Mat<eT>& output)
159  {
160  const size_t rStep = input.n_rows / error.n_rows - offset;
161  const size_t cStep = input.n_cols / error.n_cols - offset;
162 
163  arma::Mat<eT> unpooledError;
164  for (size_t j = 0; j < input.n_cols - cStep; j += cStep)
165  {
166  for (size_t i = 0; i < input.n_rows - rStep; i += rStep)
167  {
168  const arma::Mat<eT>& inputArea = input(arma::span(i, i + rStep - 1),
169  arma::span(j, j + cStep - 1));
170 
171  unpooledError = arma::Mat<eT>(inputArea.n_rows, inputArea.n_cols);
172  unpooledError.fill(error(i / rStep, j / cStep) / inputArea.n_elem);
173 
174  output(arma::span(i, i + rStep - 1 - offset),
175  arma::span(j, j + cStep - 1 - offset)) += unpooledError;
176  }
177  }
178  }
179 
181  size_t inSize;
182 
184  size_t outSize;
185 
187  size_t kW;
188 
190  size_t kH;
191 
193  size_t dW;
194 
196  size_t dH;
197 
199  size_t inputWidth;
200 
202  size_t inputHeight;
203 
205  size_t outputWidth;
206 
208  size_t outputHeight;
209 
211  bool reset;
212 
214  bool floor;
215 
217  bool deterministic;
218 
220  size_t offset;
221 
223  arma::cube outputTemp;
224 
226  arma::cube inputTemp;
227 
229  arma::cube gTemp;
230 
232  OutputDataType delta;
233 
235  OutputDataType gradient;
236 
238  InputDataType inputParameter;
239 
241  OutputDataType outputParameter;
242 }; // class MeanPooling
243 
244 
245 } // namespace ann
246 } // namespace mlpack
247 
248 // Include implementation.
249 #include "mean_pooling_impl.hpp"
250 
251 #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.
InputDataType const & InputParameter() const
Get the input parameter.
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...
InputDataType & InputParameter()
Modify the input parameter.