mlpack
padding_impl.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_METHODS_ANN_LAYER_PADDING_IMPL_HPP
14 #define MLPACK_METHODS_ANN_LAYER_PADDING_IMPL_HPP
15 
16 // In case it hasn't yet been included.
17 #include "padding.hpp"
18 
19 namespace mlpack {
20 namespace ann {
21 
22 template<typename InputDataType, typename OutputDataType>
24  const size_t padWLeft,
25  const size_t padWRight,
26  const size_t padHTop,
27  const size_t padHBottom,
28  const size_t inputWidth,
29  const size_t inputHeight) :
30  padWLeft(padWLeft),
31  padWRight(padWRight),
32  padHTop(padHTop),
33  padHBottom(padHBottom),
34  nRows(0),
35  nCols(0),
36  inputHeight(inputWidth),
37  inputWidth(inputHeight)
38 {
39  // Nothing to do here.
40 }
41 
42 template<typename InputDataType, typename OutputDataType>
43 template<typename eT>
45  const arma::Mat<eT>& input, arma::Mat<eT>& output)
46 {
47  nRows = input.n_rows;
48  nCols = input.n_cols;
49 
50  if (inputWidth == 0 || inputHeight == 0)
51  {
52  output = arma::zeros(nRows + padWLeft + padWRight,
53  nCols + padHTop + padHBottom);
54  output.submat(padWLeft, padHTop, padWLeft + nRows - 1,
55  padHTop + nCols - 1) = input;
56  }
57  else
58  {
59  inSize = input.n_elem / (inputWidth * inputHeight * nCols);
60  inputTemp = arma::Cube<eT>(const_cast<arma::Mat<eT>&>(input).memptr(),
61  inputWidth, inputHeight, inSize * nCols, false, false);
62  outputTemp = arma::zeros<arma::Cube<eT>>(inputWidth + padWLeft + padWRight,
63  inputHeight + padHTop + padHBottom, inSize * nCols);
64  for (size_t i = 0; i < inputTemp.n_slices; ++i)
65  {
66  outputTemp.slice(i).submat(padWLeft, padHTop, padWLeft + inputWidth - 1,
67  padHTop + inputHeight - 1) = inputTemp.slice(i);
68  }
69 
70  output = arma::Mat<eT>(outputTemp.memptr(), outputTemp.n_elem / nCols,
71  nCols);
72  }
73 
74  outputWidth = inputWidth + padWLeft + padWRight;
75  outputHeight = inputHeight + padHTop + padHBottom;
76 }
77 
78 template<typename InputDataType, typename OutputDataType>
79 template<typename eT>
81  const arma::Mat<eT>& /* input */,
82  const arma::Mat<eT>& gy,
83  arma::Mat<eT>& g)
84 {
85  g = gy.submat(padWLeft, padHTop, padWLeft + nRows - 1,
86  padHTop + nCols - 1);
87 }
88 
89 template<typename InputDataType, typename OutputDataType>
90 template<typename Archive>
92  Archive& ar, const uint32_t /* version */)
93 {
94  ar(CEREAL_NVP(padWLeft));
95  ar(CEREAL_NVP(padWRight));
96  ar(CEREAL_NVP(padHTop));
97  ar(CEREAL_NVP(padHBottom));
98  ar(CEREAL_NVP(inputWidth));
99  ar(CEREAL_NVP(inputHeight));
100 }
101 
102 } // namespace ann
103 } // namespace mlpack
104 
105 #endif
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: padding_impl.hpp:91
void Backward(const arma::Mat< eT > &, const 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...
Definition: padding_impl.hpp:80
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: padding_impl.hpp:44
Padding(const size_t padWLeft=0, const size_t padWRight=0, const size_t padHTop=0, const size_t padHBottom=0, const size_t inputWidth=0, const size_t inputHeight=0)
Create the Padding object using the specified number of output units.
Definition: padding_impl.hpp:23