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 #include "padding.hpp"
24 
25 namespace mlpack {
26 namespace ann {
27 
40 template <
41  typename ForwardConvolutionRule = NaiveConvolution<ValidConvolution>,
42  typename BackwardConvolutionRule = NaiveConvolution<FullConvolution>,
43  typename GradientConvolutionRule = NaiveConvolution<ValidConvolution>,
44  typename InputDataType = arma::mat,
45  typename OutputDataType = arma::mat
46 >
48 {
49  public:
51  Convolution();
52 
68  Convolution(const size_t inSize,
69  const size_t outSize,
70  const size_t kW,
71  const size_t kH,
72  const size_t dW = 1,
73  const size_t dH = 1,
74  const size_t padW = 0,
75  const size_t padH = 0,
76  const size_t inputWidth = 0,
77  const size_t inputHeight = 0);
78 
79  /*
80  * Set the weight and bias term.
81  */
82  void Reset();
83 
91  template<typename eT>
92  void Forward(const arma::Mat<eT>&& input, arma::Mat<eT>&& output);
93 
103  template<typename eT>
104  void Backward(const arma::Mat<eT>&& /* input */,
105  arma::Mat<eT>&& gy,
106  arma::Mat<eT>&& g);
107 
108  /*
109  * Calculate the gradient using the output delta and the input activation.
110  *
111  * @param input The input parameter used for calculating the gradient.
112  * @param error The calculated error.
113  * @param gradient The calculated gradient.
114  */
115  template<typename eT>
116  void Gradient(const arma::Mat<eT>&& /* input */,
117  arma::Mat<eT>&& error,
118  arma::Mat<eT>&& gradient);
119 
121  OutputDataType const& Parameters() const { return weights; }
123  OutputDataType& Parameters() { return weights; }
124 
126  InputDataType const& InputParameter() const { return inputParameter; }
128  InputDataType& InputParameter() { return inputParameter; }
129 
131  OutputDataType const& OutputParameter() const { return outputParameter; }
133  OutputDataType& OutputParameter() { return outputParameter; }
134 
136  OutputDataType const& Delta() const { return delta; }
138  OutputDataType& Delta() { return delta; }
139 
141  OutputDataType const& Gradient() const { return gradient; }
143  OutputDataType& Gradient() { return gradient; }
144 
146  size_t const& InputWidth() const { return inputWidth; }
148  size_t& InputWidth() { return inputWidth; }
149 
151  size_t const& InputHeight() const { return inputHeight; }
153  size_t& InputHeight() { return inputHeight; }
154 
156  size_t const& OutputWidth() const { return outputWidth; }
158  size_t& OutputWidth() { return outputWidth; }
159 
161  size_t const& OutputHeight() const { return outputHeight; }
163  size_t& OutputHeight() { return outputHeight; }
164 
166  arma::mat& Bias() { return bias; }
167 
171  template<typename Archive>
172  void serialize(Archive& ar, const unsigned int /* version */);
173 
174  private:
175  /*
176  * Return the convolution output size.
177  *
178  * @param size The size of the input (row or column).
179  * @param k The size of the filter (width or height).
180  * @param s The stride size (x or y direction).
181  * @param p The size of the padding (width or height).
182  * @return The convolution output size.
183  */
184  size_t ConvOutSize(const size_t size,
185  const size_t k,
186  const size_t s,
187  const size_t p)
188  {
189  return std::floor(size + p * 2 - k) / s + 1;
190  }
191 
192  /*
193  * Rotates a 3rd-order tensor counterclockwise by 180 degrees.
194  *
195  * @param input The input data to be rotated.
196  * @param output The rotated output.
197  */
198  template<typename eT>
199  void Rotate180(const arma::Cube<eT>& input, arma::Cube<eT>& output)
200  {
201  output = arma::Cube<eT>(input.n_rows, input.n_cols, input.n_slices);
202 
203  // * left-right flip, up-down flip */
204  for (size_t s = 0; s < output.n_slices; s++)
205  output.slice(s) = arma::fliplr(arma::flipud(input.slice(s)));
206  }
207 
208  /*
209  * Rotates a dense matrix counterclockwise by 180 degrees.
210  *
211  * @param input The input data to be rotated.
212  * @param output The rotated output.
213  */
214  template<typename eT>
215  void Rotate180(const arma::Mat<eT>& input, arma::Mat<eT>& output)
216  {
217  // * left-right flip, up-down flip */
218  output = arma::fliplr(arma::flipud(input));
219  }
220 
222  size_t inSize;
223 
225  size_t outSize;
226 
228  size_t batchSize;
229 
231  size_t kW;
232 
234  size_t kH;
235 
237  size_t dW;
238 
240  size_t dH;
241 
243  size_t padW;
244 
246  size_t padH;
247 
249  OutputDataType weights;
250 
252  arma::cube weight;
253 
255  arma::mat bias;
256 
258  size_t inputWidth;
259 
261  size_t inputHeight;
262 
264  size_t outputWidth;
265 
267  size_t outputHeight;
268 
270  arma::cube outputTemp;
271 
273  arma::cube inputTemp;
274 
276  arma::cube inputPaddedTemp;
277 
279  arma::cube gTemp;
280 
282  arma::cube gradientTemp;
283 
285  Padding<>* padding;
286 
288  OutputDataType delta;
289 
291  OutputDataType gradient;
292 
294  InputDataType inputParameter;
295 
297  OutputDataType outputParameter;
298 }; // class Convolution
299 
300 } // namespace ann
301 } // namespace mlpack
302 
303 // Include implementation.
304 #include "convolution_impl.hpp"
305 
306 #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
Implementation of the Padding module class.
Definition: layer_types.hpp:68
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:47
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.
arma::mat & Bias()
Modify the bias weights of the layer.
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.