12 #ifndef MLPACK_METHODS_ANN_LAYER_CONVOLUTION_HPP 13 #define MLPACK_METHODS_ANN_LAYER_CONVOLUTION_HPP 40 typename ForwardConvolutionRule = NaiveConvolution<ValidConvolution>,
41 typename BackwardConvolutionRule = NaiveConvolution<FullConvolution>,
42 typename GradientConvolutionRule = NaiveConvolution<ValidConvolution>,
43 typename InputDataType = arma::mat,
44 typename OutputDataType = arma::mat
73 const size_t padW = 0,
74 const size_t padH = 0,
75 const size_t inputWidth = 0,
76 const size_t inputHeight = 0);
91 void Forward(
const arma::Mat<eT>&& input, arma::Mat<eT>&& output);
102 template<
typename eT>
103 void Backward(
const arma::Mat<eT>&& ,
114 template<
typename eT>
115 void Gradient(
const arma::Mat<eT>&& ,
116 arma::Mat<eT>&& error,
117 arma::Mat<eT>&& gradient);
135 OutputDataType
const&
Delta()
const {
return delta; }
137 OutputDataType&
Delta() {
return delta; }
140 OutputDataType
const&
Gradient()
const {
return gradient; }
167 template<
typename Archive>
168 void serialize(Archive& ar,
const unsigned int );
180 size_t ConvOutSize(
const size_t size,
185 return std::floor(size + p * 2 - k) / s + 1;
194 template<
typename eT>
195 void Rotate180(
const arma::Cube<eT>& input, arma::Cube<eT>& output)
197 output = arma::Cube<eT>(input.n_rows, input.n_cols, input.n_slices);
200 for (
size_t s = 0; s < output.n_slices; s++)
201 output.slice(s) = arma::fliplr(arma::flipud(input.slice(s)));
210 template<
typename eT>
211 void Rotate180(
const arma::Mat<eT>& input, arma::Mat<eT>& output)
214 output = arma::fliplr(arma::flipud(input));
225 template<
typename eT>
226 void Pad(
const arma::Mat<eT>& input,
229 arma::Mat<eT>& output)
231 if (output.n_rows != input.n_rows + wPad * 2 ||
232 output.n_cols != input.n_cols + hPad * 2)
234 output = arma::zeros(input.n_rows + wPad * 2, input.n_cols + hPad * 2);
237 output.submat(wPad, hPad, wPad + input.n_rows - 1,
238 hPad + input.n_cols - 1) = input;
249 template<
typename eT>
250 void Pad(
const arma::Cube<eT>& input,
253 arma::Cube<eT>& output)
255 output = arma::zeros(input.n_rows + wPad * 2,
256 input.n_cols + hPad * 2, input.n_slices);
258 for (
size_t i = 0; i < input.n_slices; ++i)
260 Pad<eT>(input.slice(i), wPad, hPad, output.slice(i));
289 OutputDataType weights;
310 arma::cube outputTemp;
313 arma::cube inputTemp;
316 arma::cube inputPaddedTemp;
322 arma::cube gradientTemp;
325 OutputDataType delta;
328 OutputDataType gradient;
331 InputDataType inputParameter;
334 OutputDataType outputParameter;
341 #include "convolution_impl.hpp" InputDataType const & InputParameter() const
Get the input parameter.
void Backward(const arma::Mat< eT > &&, arma::Mat< eT > &&gy, arma::Mat< eT > &&g)
Ordinary feed backward pass of a neural network, calculating the function f(x) by propagating x backw...
OutputDataType const & Parameters() const
Get the parameters.
size_t & OutputHeight()
Modify the output height.
The core includes that mlpack expects; standard C++ includes and Armadillo.
OutputDataType const & OutputParameter() const
Get the output parameter.
Implementation of the Convolution class.
size_t const & InputWidth() const
Get the input width.
OutputDataType & Gradient()
Modify the gradient.
InputDataType & InputParameter()
Modify the input parameter.
size_t const & InputHeight() const
Get the input height.
size_t & InputHeight()
Modify the input height.
size_t const & OutputWidth() const
Get the output width.
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 const & Delta() const
Get the delta.
size_t & InputWidth()
Modify input the width.
OutputDataType & Parameters()
Modify the parameters.
size_t const & OutputHeight() const
Get the output height.
size_t & OutputWidth()
Modify the output width.
OutputDataType & Delta()
Modify the delta.
OutputDataType & OutputParameter()
Modify the output parameter.
Convolution()
Create the Convolution object.
void serialize(Archive &ar, const unsigned int)
Serialize the layer.
OutputDataType const & Gradient() const
Get the gradient.