13 #ifndef MLPACK_METHODS_ANN_LAYER_LP_POOLING_IMPL_HPP 14 #define MLPACK_METHODS_ANN_LAYER_LP_POOLING_IMPL_HPP 22 template<
typename InputDataType,
typename OutputDataType>
28 template<
typename InputDataType,
typename OutputDataType>
30 const size_t normType,
31 const size_t kernelWidth,
32 const size_t kernelHeight,
33 const size_t strideWidth,
34 const size_t strideHeight,
37 kernelWidth(kernelWidth),
38 kernelHeight(kernelHeight),
39 strideWidth(strideWidth),
40 strideHeight(strideHeight),
54 template<
typename InputDataType,
typename OutputDataType>
57 const arma::Mat<eT>& input, arma::Mat<eT>& output)
59 batchSize = input.n_cols;
60 inSize = input.n_elem / (inputWidth * inputHeight * batchSize);
61 inputTemp = arma::cube(
const_cast<arma::Mat<eT>&
>(input).memptr(),
62 inputWidth, inputHeight, batchSize * inSize,
false,
false);
66 outputWidth = std::floor((inputWidth -
67 (
double) kernelWidth) / (
double) strideWidth + 1);
68 outputHeight = std::floor((inputHeight -
69 (
double) kernelHeight) / (
double) strideHeight + 1);
73 outputWidth = std::ceil((inputWidth -
74 (
double) kernelWidth) / (
double) strideWidth + 1);
75 outputHeight = std::ceil((inputHeight -
76 (
double) kernelHeight) / (
double) strideHeight + 1);
79 outputTemp = arma::zeros<arma::Cube<eT> >(outputWidth, outputHeight,
82 for (
size_t s = 0; s < inputTemp.n_slices; s++)
83 Pooling(inputTemp.slice(s), outputTemp.slice(s));
85 output = arma::Mat<eT>(outputTemp.memptr(), outputTemp.n_elem / batchSize,
88 outputWidth = outputTemp.n_rows;
89 outputHeight = outputTemp.n_cols;
90 outSize = batchSize * inSize;
93 template<
typename InputDataType,
typename OutputDataType>
96 const arma::Mat<eT>& ,
97 const arma::Mat<eT>& gy,
100 arma::cube mappedError = arma::cube(((arma::Mat<eT>&) gy).memptr(),
101 outputWidth, outputHeight, outSize,
false,
false);
103 gTemp = arma::zeros<arma::cube>(inputTemp.n_rows,
104 inputTemp.n_cols, inputTemp.n_slices);
106 for (
size_t s = 0; s < mappedError.n_slices; s++)
108 Unpooling(inputTemp.slice(s), mappedError.slice(s), gTemp.slice(s));
111 g = arma::mat(gTemp.memptr(), gTemp.n_elem / batchSize, batchSize);
114 template<
typename InputDataType,
typename OutputDataType>
115 template<
typename Archive>
120 ar(CEREAL_NVP(normType));
121 ar(CEREAL_NVP(kernelWidth));
122 ar(CEREAL_NVP(kernelHeight));
123 ar(CEREAL_NVP(strideWidth));
124 ar(CEREAL_NVP(strideHeight));
125 ar(CEREAL_NVP(batchSize));
126 ar(CEREAL_NVP(floor));
127 ar(CEREAL_NVP(inputWidth));
128 ar(CEREAL_NVP(inputHeight));
129 ar(CEREAL_NVP(outputWidth));
130 ar(CEREAL_NVP(outputHeight));
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.
Definition: lp_pooling_impl.hpp:95
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
void serialize(Archive &ar, const uint32_t)
Serialize the layer.
Definition: lp_pooling_impl.hpp:116
LpPooling()
Create the LpPooling object.
Definition: lp_pooling_impl.hpp:23
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...
Definition: lp_pooling_impl.hpp:56