13 #ifndef MLPACK_METHODS_ANN_LAYER_MEAN_POOLING_HPP 14 #define MLPACK_METHODS_ANN_LAYER_MEAN_POOLING_HPP 30 typename InputDataType = arma::mat,
31 typename OutputDataType = arma::mat
49 const size_t kernelHeight,
50 const size_t strideWidth = 1,
51 const size_t strideHeight = 1,
52 const bool floor =
true);
62 void Forward(
const arma::Mat<eT>& input, arma::Mat<eT>& output);
75 const arma::Mat<eT>& gy,
84 OutputDataType
const&
Delta()
const {
return delta; }
86 OutputDataType&
Delta() {
return delta; }
135 bool const&
Floor()
const {
return floor; }
150 template<
typename Archive>
151 void serialize(Archive& ar,
const uint32_t );
160 template<
typename eT>
161 void Pooling(
const arma::Mat<eT>& input, arma::Mat<eT>& output)
163 arma::Mat<eT> inputPre = input;
165 for (
size_t i = 1; i < input.n_cols; ++i)
166 inputPre.col(i) += inputPre.col(i - 1);
168 for (
size_t i = 1; i < input.n_rows; ++i)
169 inputPre.row(i) += inputPre.row(i - 1);
171 for (
size_t j = 0, colidx = 0; j < output.n_cols;
172 ++j, colidx += strideHeight)
174 for (
size_t i = 0, rowidx = 0; i < output.n_rows;
175 ++i, rowidx += strideWidth)
178 size_t rowEnd = rowidx + kernelWidth - 1;
179 size_t colEnd = colidx + kernelHeight - 1;
181 if (rowEnd > input.n_rows - 1)
182 rowEnd = input.n_rows - 1;
183 if (colEnd > input.n_cols - 1)
184 colEnd = input.n_cols - 1;
186 const size_t kernalArea = (rowEnd - rowidx + 1) * (colEnd - colidx + 1);
187 val += inputPre(rowEnd, colEnd);
191 val += inputPre(rowidx - 1, colidx - 1);
192 val -= inputPre(rowidx - 1, colEnd);
195 val -= inputPre(rowEnd, colidx - 1);
197 output(i, j) = val / kernalArea;
208 template<
typename eT>
209 void Unpooling(
const arma::Mat<eT>& input,
210 const arma::Mat<eT>& error,
211 arma::Mat<eT>& output)
221 const bool condition = (error.n_elem * kernelHeight * kernelWidth) >
222 (4 * error.n_elem + 2 * input.n_elem);
259 for (
size_t j = 0, colidx = 0; j < input.n_cols; j += strideHeight, ++colidx)
261 for (
size_t i = 0, rowidx = 0; i < input.n_rows; i += strideWidth, ++rowidx)
275 size_t rowEnd = i + kernelWidth - 1;
276 size_t colEnd = j + kernelHeight - 1;
278 if (rowEnd > input.n_rows - 1)
282 rowEnd = input.n_rows - 1;
285 if (colEnd > input.n_cols - 1)
289 colEnd = input.n_cols - 1;
292 size_t kernalArea = (rowEnd - i + 1) * (colEnd - j + 1);
293 output(i, j) += error(rowidx, colidx) / kernalArea;
295 if (rowEnd + 1 < input.n_rows)
297 output(rowEnd + 1, j) -= error(rowidx, colidx) / kernalArea;
299 if (colEnd + 1 < input.n_cols)
300 output(rowEnd + 1, colEnd + 1) += error(rowidx, colidx) / kernalArea;
303 if (colEnd + 1 < input.n_cols)
304 output(i, colEnd + 1) -= error(rowidx, colidx) / kernalArea;
308 for (
size_t i = 1; i < input.n_rows; ++i)
309 output.row(i) += output.row(i - 1);
311 for (
size_t j = 1; j < input.n_cols; ++j)
312 output.col(j) += output.col(j - 1);
316 arma::Mat<eT> unpooledError;
317 for (
size_t j = 0, colidx = 0; j < input.n_cols; j += strideHeight, ++colidx)
319 for (
size_t i = 0, rowidx = 0; i < input.n_rows; i += strideWidth, ++rowidx)
321 size_t rowEnd = i + kernelWidth - 1;
322 size_t colEnd = j + kernelHeight - 1;
324 if (rowEnd > input.n_rows - 1)
328 rowEnd = input.n_rows - 1;
331 if (colEnd > input.n_cols - 1)
335 colEnd = input.n_cols - 1;
338 arma::mat InputArea = input(arma::span(i, rowEnd), arma::span(j, colEnd));
340 unpooledError = arma::Mat<eT>(InputArea.n_rows, InputArea.n_cols);
341 unpooledError.fill(error(rowidx, colidx) / InputArea.n_elem);
343 output(arma::span(i, i + InputArea.n_rows - 1),
344 arma::span(j, j + InputArea.n_cols - 1)) += unpooledError;
393 arma::cube outputTemp;
396 arma::cube inputTemp;
402 OutputDataType delta;
405 OutputDataType gradient;
408 OutputDataType outputParameter;
416 #include "mean_pooling_impl.hpp" size_t KernelHeight() const
Get the kernel height.
OutputDataType const & Delta() const
Get the delta.
void serialize(Archive &ar, const uint32_t)
Serialize the layer.
Linear algebra utility functions, generally performed on matrices or vectors.
size_t & KernelWidth()
Modify the kernel width.
MeanPooling()
Create the MeanPooling object.
size_t & OutputHeight()
Modify the output height.
The core includes that mlpack expects; standard C++ includes and Armadillo.
size_t const & OutputHeight() const
Get the output height.
size_t const & InputHeight() const
Get the input height.
bool Deterministic() const
Get the value of the deterministic parameter.
OutputDataType & Delta()
Modify the delta.
Implementation of the MeanPooling.
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() 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 output width.
size_t & InputHeight()
Modify the input height.
size_t const & InputWidth() const
Get the intput width.
OutputDataType & OutputParameter()
Modify the output parameter.
bool const & Floor() const
Get the value of the rounding operation.
size_t & InputWidth()
Modify the input width.
bool & Floor()
Modify the value of the rounding operation.
bool & Deterministic()
Modify the value of the deterministic parameter.
size_t & OutputWidth()
Modify the output width.
size_t & StrideHeight()
Modify the stride height.
size_t OutputSize() const
Get the output size.
size_t WeightSize() const
Get the size of the weights.
size_t & StrideWidth()
Modify the stride width.
size_t StrideHeight() const
Get the stride height.
size_t & KernelHeight()
Modify the kernel height.
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.