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(const 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(const arma::Mat<eT>& /* input */,
116  const 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 
133  size_t InSize() const { return inSize; }
134 
136  size_t const& BeginRow() const { return beginRow; }
138  size_t& BeginRow() { return beginRow; }
139 
141  size_t const& EndRow() const { return endRow; }
143  size_t& EndRow() { return endRow; }
144 
146  size_t const& BeginCol() const { return beginCol; }
148  size_t& BeginCol() { return beginCol; }
149 
151  size_t const& EndCol() const { return endCol; }
153  size_t& EndCol() { return endCol; }
154 
158  template<typename Archive>
159  void serialize(Archive& ar, const uint32_t /* version */)
160  {
161  ar(CEREAL_NVP(inSize));
162  ar(CEREAL_NVP(beginRow));
163  ar(CEREAL_NVP(endRow));
164  ar(CEREAL_NVP(beginCol));
165  ar(CEREAL_NVP(endCol));
166  }
167 
168  private:
170  size_t inSize;
171 
173  size_t beginRow;
174 
176  size_t endRow;
177 
179  size_t beginCol;
180 
182  size_t endCol;
183 
185  OutputDataType delta;
186 
188  OutputDataType outputParameter;
189 }; // class Subview
190 
191 } // namespace ann
192 } // namespace mlpack
193 
194 #endif
OutputDataType & OutputParameter()
Modify the output parameter.
Definition: subview.hpp:125
Linear algebra utility functions, generally performed on matrices or vectors.
The core includes that mlpack expects; standard C++ includes and Armadillo.
size_t const & EndRow() const
Get the ending row index of subview vector or matrix.
Definition: subview.hpp:141
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: subview.hpp:115
size_t InSize() const
Get the width of each sample.
Definition: subview.hpp:133
size_t const & BeginRow() const
Get the starting row index of subview vector or matrix.
Definition: subview.hpp:136
size_t const & BeginCol() const
Get the width of each sample.
Definition: subview.hpp:146
size_t const & EndCol() const
Get the ending column index of subview vector or matrix.
Definition: subview.hpp:151
void Forward(const 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
Implementation of the subview layer.
Definition: subview.hpp:34
OutputDataType & Delta()
Modify the delta.
Definition: subview.hpp:130
void serialize(Archive &ar, const uint32_t)
Serialize the layer.
Definition: subview.hpp:159
OutputDataType const & Delta() const
Get the delta.
Definition: subview.hpp:128
size_t & EndCol()
Modify the width of each sample.
Definition: subview.hpp:153
size_t & EndRow()
Modify the width of each sample.
Definition: subview.hpp:143
size_t & BeginRow()
Modify the width of each sample.
Definition: subview.hpp:138
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
OutputDataType const & OutputParameter() const
Get the output parameter.
Definition: subview.hpp:123
size_t & BeginCol()
Modify the width of each sample.
Definition: subview.hpp:148