subview.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_METHODS_ANN_LAYER_SUBVIEW_HPP
13 #define MLPACK_METHODS_ANN_LAYER_SUBVIEW_HPP
14 
15 #include <mlpack/prereqs.hpp>
17 
18 namespace mlpack {
19 namespace ann {
20 
30 template <
31  typename InputDataType = arma::mat,
32  typename OutputDataType = arma::mat
33 >
34 class Subview
35 {
36  public:
47  Subview(const size_t inSize = 1,
48  const size_t beginRow = 0,
49  const size_t endRow = 0,
50  const size_t beginCol = 0,
51  const size_t endCol = 0) :
52  inSize(inSize),
53  beginRow(beginRow),
54  endRow(endRow),
55  beginCol(beginCol),
56  endCol(endCol)
57  {
58  /* Nothing to do here */
59  }
60 
68  template<typename InputType, typename OutputType>
69  void Forward(InputType&& input, OutputType&& output)
70  {
71  size_t batchSize = input.n_cols / inSize;
72 
73  // Check if subview parameters are within the indices of input sample.
74  endRow = ((endRow < input.n_rows) && (endRow >= beginRow))?
75  endRow : (input.n_rows - 1);
76  endCol = ((endCol < inSize) && (endCol >= beginCol)) ?
77  endCol : (inSize - 1);
78 
79  output.set_size(
80  (endRow - beginRow + 1) * (endCol - beginCol + 1), batchSize);
81 
82  size_t batchBegin = beginCol;
83  size_t batchEnd = endCol;
84 
85  // Check whether the input is already in desired form.
86  if ((input.n_rows != ((endRow - beginRow + 1) *
87  (endCol - beginCol + 1))) || (input.n_cols != batchSize))
88  {
89  for (size_t i = 0; i < batchSize; i++)
90  {
91  output.col(i) = arma::vectorise(
92  input.submat(beginRow, batchBegin, endRow, batchEnd));
93 
94  // Move to next batch.
95  batchBegin += inSize;
96  batchEnd += inSize;
97  }
98  }
99  else
100  {
101  output = input;
102  }
103  }
104 
114  template<typename eT>
115  void Backward(arma::Mat<eT>&& /* input */,
116  arma::Mat<eT>&& gy,
117  arma::Mat<eT>&& g)
118  {
119  g = gy;
120  }
121 
123  OutputDataType const& OutputParameter() const { return outputParameter; }
125  OutputDataType& OutputParameter() { return outputParameter; }
126 
128  OutputDataType const& Delta() const { return delta; }
130  OutputDataType& Delta() { return delta; }
131 
135  template<typename Archive>
136  void serialize(Archive& ar, const unsigned int /* version */)
137  {
138  ar & BOOST_SERIALIZATION_NVP(inSize);
139  ar & BOOST_SERIALIZATION_NVP(beginRow);
140  ar & BOOST_SERIALIZATION_NVP(endRow);
141  ar & BOOST_SERIALIZATION_NVP(beginCol);
142  ar & BOOST_SERIALIZATION_NVP(endCol);
143  }
144 
145  private:
147  size_t inSize;
148 
150  size_t beginRow;
151 
153  size_t endRow;
154 
156  size_t beginCol;
157 
159  size_t endCol;
160 
162  OutputDataType delta;
163 
165  OutputDataType outputParameter;
166 }; // class Subview
167 
168 } // namespace ann
169 } // namespace mlpack
170 
171 #endif
OutputDataType & OutputParameter()
Modify the output parameter.
Definition: subview.hpp:125
void Backward(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...
Definition: subview.hpp:115
.hpp
Definition: add_to_po.hpp:21
The core includes that mlpack expects; standard C++ includes and Armadillo.
void serialize(Archive &ar, const unsigned int)
Serialize the layer.
Definition: subview.hpp:136
Implementation of the subview layer.
Definition: subview.hpp:34
OutputDataType & Delta()
Modify the delta.
Definition: subview.hpp:130
OutputDataType const & Delta() const
Get the delta.
Definition: subview.hpp:128
Subview(const size_t inSize=1, const size_t beginRow=0, const size_t endRow=0, const size_t beginCol=0, const size_t endCol=0)
Create the Subview layer object using the specified range of input to accept.
Definition: subview.hpp:47
void Forward(InputType &&input, OutputType &&output)
Ordinary feed forward pass of a neural network, evaluating the function f(x) by propagating the activ...
Definition: subview.hpp:69
OutputDataType const & OutputParameter() const
Get the output parameter.
Definition: subview.hpp:123