convolution.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_METHODS_ANN_LAYER_CONVOLUTION_HPP
13 #define MLPACK_METHODS_ANN_LAYER_CONVOLUTION_HPP
14 
15 #include <mlpack/prereqs.hpp>
16 
21 
22 #include "layer_types.hpp"
23 
24 namespace mlpack {
25 namespace ann {
26 
39 template <
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
45 >
47 {
48  public:
50  Convolution();
51 
67  Convolution(const size_t inSize,
68  const size_t outSize,
69  const size_t kW,
70  const size_t kH,
71  const size_t dW = 1,
72  const size_t dH = 1,
73  const size_t padW = 0,
74  const size_t padH = 0,
75  const size_t inputWidth = 0,
76  const size_t inputHeight = 0);
77 
78  /*
79  * Set the weight and bias term.
80  */
81  void Reset();
82 
90  template<typename eT>
91  void Forward(const arma::Mat<eT>&& input, arma::Mat<eT>&& output);
92 
102  template<typename eT>
103  void Backward(const arma::Mat<eT>&& /* input */,
104  arma::Mat<eT>&& gy,
105  arma::Mat<eT>&& g);
106 
107  /*
108  * Calculate the gradient using the output delta and the input activation.
109  *
110  * @param input The input parameter used for calculating the gradient.
111  * @param error The calculated error.
112  * @param gradient The calculated gradient.
113  */
114  template<typename eT>
115  void Gradient(const arma::Mat<eT>&& /* input */,
116  arma::Mat<eT>&& error,
117  arma::Mat<eT>&& gradient);
118 
120  OutputDataType const& Parameters() const { return weights; }
122  OutputDataType& Parameters() { return weights; }
123 
125  InputDataType const& InputParameter() const { return inputParameter; }
127  InputDataType& InputParameter() { return inputParameter; }
128 
130  OutputDataType const& OutputParameter() const { return outputParameter; }
132  OutputDataType& OutputParameter() { return outputParameter; }
133 
135  OutputDataType const& Delta() const { return delta; }
137  OutputDataType& Delta() { return delta; }
138 
140  OutputDataType const& Gradient() const { return gradient; }
142  OutputDataType& Gradient() { return gradient; }
143 
145  size_t const& InputWidth() const { return inputWidth; }
147  size_t& InputWidth() { return inputWidth; }
148 
150  size_t const& InputHeight() const { return inputHeight; }
152  size_t& InputHeight() { return inputHeight; }
153 
155  size_t const& OutputWidth() const { return outputWidth; }
157  size_t& OutputWidth() { return outputWidth; }
158 
160  size_t const& OutputHeight() const { return outputHeight; }
162  size_t& OutputHeight() { return outputHeight; }
163 
167  template<typename Archive>
168  void serialize(Archive& ar, const unsigned int /* version */);
169 
170  private:
171  /*
172  * Return the convolution output size.
173  *
174  * @param size The size of the input (row or column).
175  * @param k The size of the filter (width or height).
176  * @param s The stride size (x or y direction).
177  * @param p The size of the padding (width or height).
178  * @return The convolution output size.
179  */
180  size_t ConvOutSize(const size_t size,
181  const size_t k,
182  const size_t s,
183  const size_t p)
184  {
185  return std::floor(size + p * 2 - k) / s + 1;
186  }
187 
188  /*
189  * Rotates a 3rd-order tensor counterclockwise by 180 degrees.
190  *
191  * @param input The input data to be rotated.
192  * @param output The rotated output.
193  */
194  template<typename eT>
195  void Rotate180(const arma::Cube<eT>& input, arma::Cube<eT>& output)
196  {
197  output = arma::Cube<eT>(input.n_rows, input.n_cols, input.n_slices);
198 
199  // * left-right flip, up-down flip */
200  for (size_t s = 0; s < output.n_slices; s++)
201  output.slice(s) = arma::fliplr(arma::flipud(input.slice(s)));
202  }
203 
204  /*
205  * Rotates a dense matrix counterclockwise by 180 degrees.
206  *
207  * @param input The input data to be rotated.
208  * @param output The rotated output.
209  */
210  template<typename eT>
211  void Rotate180(const arma::Mat<eT>& input, arma::Mat<eT>& output)
212  {
213  // * left-right flip, up-down flip */
214  output = arma::fliplr(arma::flipud(input));
215  }
216 
217  /*
218  * Pad the given input data.
219  *
220  * @param input The input to be padded.
221  * @param wPad Padding width of the input.
222  * @param hPad Padding height of the input.
223  * @param output The padded output data.
224  */
225  template<typename eT>
226  void Pad(const arma::Mat<eT>& input,
227  size_t wPad,
228  size_t hPad,
229  arma::Mat<eT>& output)
230  {
231  if (output.n_rows != input.n_rows + wPad * 2 ||
232  output.n_cols != input.n_cols + hPad * 2)
233  {
234  output = arma::zeros(input.n_rows + wPad * 2, input.n_cols + hPad * 2);
235  }
236 
237  output.submat(wPad, hPad, wPad + input.n_rows - 1,
238  hPad + input.n_cols - 1) = input;
239  }
240 
241  /*
242  * Pad the given input data.
243  *
244  * @param input The input to be padded.
245  * @param wPad Padding width of the input.
246  * @param hPad Padding height of the input.
247  * @param output The padded output data.
248  */
249  template<typename eT>
250  void Pad(const arma::Cube<eT>& input,
251  size_t wPad,
252  size_t hPad,
253  arma::Cube<eT>& output)
254  {
255  output = arma::zeros(input.n_rows + wPad * 2,
256  input.n_cols + hPad * 2, input.n_slices);
257 
258  for (size_t i = 0; i < input.n_slices; ++i)
259  {
260  Pad<double>(input.slice(i), wPad, hPad, output.slice(i));
261  }
262  }
263 
265  size_t inSize;
266 
268  size_t outSize;
269 
271  size_t kW;
272 
274  size_t kH;
275 
277  size_t dW;
278 
280  size_t dH;
281 
283  size_t padW;
284 
286  size_t padH;
287 
289  OutputDataType weights;
290 
292  arma::cube weight;
293 
295  arma::mat bias;
296 
298  size_t inputWidth;
299 
301  size_t inputHeight;
302 
304  size_t outputWidth;
305 
307  size_t outputHeight;
308 
310  arma::cube outputTemp;
311 
313  arma::cube inputTemp;
314 
316  arma::cube inputPaddedTemp;
317 
319  arma::cube gTemp;
320 
322  arma::cube gradientTemp;
323 
325  OutputDataType delta;
326 
328  OutputDataType gradient;
329 
331  InputDataType inputParameter;
332 
334  OutputDataType outputParameter;
335 }; // class Convolution
336 
337 } // namespace ann
338 } // namespace mlpack
339 
340 // Include implementation.
341 #include "convolution_impl.hpp"
342 
343 #endif
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.
.hpp
Definition: add_to_po.hpp:21
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.
Definition: convolution.hpp:46
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.