sequential.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_METHODS_ANN_LAYER_SEQUENTIAL_HPP
14 #define MLPACK_METHODS_ANN_LAYER_SEQUENTIAL_HPP
15 
16 #include <mlpack/prereqs.hpp>
17 
18 #include <boost/ptr_container/ptr_vector.hpp>
19 
20 #include "../visitor/delete_visitor.hpp"
21 #include "../visitor/copy_visitor.hpp"
22 #include "../visitor/delta_visitor.hpp"
23 #include "../visitor/output_height_visitor.hpp"
24 #include "../visitor/output_parameter_visitor.hpp"
25 #include "../visitor/output_width_visitor.hpp"
26 #include "../visitor/input_shape_visitor.hpp"
27 
28 #include "layer_types.hpp"
29 #include "add_merge.hpp"
30 
31 namespace mlpack {
32 namespace ann {
33 
67 template <
68  typename InputDataType = arma::mat,
69  typename OutputDataType = arma::mat,
70  bool Residual = false,
71  typename... CustomLayers
72 >
73 class Sequential
74 {
75  public:
81  Sequential(const bool model = true);
82 
90  Sequential(const bool model, const bool ownsLayers);
91 
93  Sequential(const Sequential& layer);
94 
96  Sequential& operator = (const Sequential& layer);
97 
99  ~Sequential();
100 
108  template<typename eT>
109  void Forward(const arma::Mat<eT>& input, arma::Mat<eT>& output);
110 
120  template<typename eT>
121  void Backward(const arma::Mat<eT>& /* input */,
122  const arma::Mat<eT>& gy,
123  arma::Mat<eT>& g);
124 
125  /*
126  * Calculate the gradient using the output delta and the input activation.
127  *
128  * @param input The input parameter used for calculating the gradient.
129  * @param error The calculated error.
130  * @param gradient The calculated gradient.
131  */
132  template<typename eT>
133  void Gradient(const arma::Mat<eT>& input,
134  const arma::Mat<eT>& error,
135  arma::Mat<eT>& /* gradient */);
136 
137  /*
138  * Add a new module to the model.
139  *
140  * @param args The layer parameter.
141  */
142  template <class LayerType, class... Args>
143  void Add(Args... args) { network.push_back(new LayerType(args...)); }
144 
145  /*
146  * Add a new module to the model.
147  *
148  * @param layer The Layer to be added to the model.
149  */
150  void Add(LayerTypes<CustomLayers...> layer) { network.push_back(layer); }
151 
153  std::vector<LayerTypes<CustomLayers...> >& Model()
154  {
155  if (model)
156  {
157  return network;
158  }
159 
160  return empty;
161  }
162 
164  const arma::mat& Parameters() const { return parameters; }
166  arma::mat& Parameters() { return parameters; }
167 
169  arma::mat const& InputParameter() const { return inputParameter; }
171  arma::mat& InputParameter() { return inputParameter; }
172 
174  arma::mat const& OutputParameter() const { return outputParameter; }
176  arma::mat& OutputParameter() { return outputParameter; }
177 
179  arma::mat const& Delta() const { return delta; }
181  arma::mat& Delta() { return delta; }
182 
184  arma::mat const& Gradient() const { return gradient; }
186  arma::mat& Gradient() { return gradient; }
187 
188  size_t InputShape() const;
189 
193  template<typename Archive>
194  void serialize(Archive& ar, const uint32_t /* version */);
195 
196  private:
198  bool model;
199 
201  bool reset;
202 
204  std::vector<LayerTypes<CustomLayers...> > network;
205 
207  arma::mat parameters;
208 
210  DeltaVisitor deltaVisitor;
211 
213  OutputParameterVisitor outputParameterVisitor;
214 
216  DeleteVisitor deleteVisitor;
217 
219  std::vector<LayerTypes<CustomLayers...> > empty;
220 
222  arma::mat delta;
223 
225  arma::mat inputParameter;
226 
228  arma::mat outputParameter;
229 
231  arma::mat gradient;
232 
234  OutputWidthVisitor outputWidthVisitor;
235 
237  OutputHeightVisitor outputHeightVisitor;
238 
240  CopyVisitor<CustomLayers...> copyVisitor;
241 
243  size_t width;
244 
246  size_t height;
247 
249  bool ownsLayers;
250 }; // class Sequential
251 
252 /*
253  * Convenience typedef for use as Residual<> layer.
254  */
255 template<
256  typename InputDataType = arma::mat,
257  typename OutputDataType = arma::mat,
258  typename... CustomLayers
259 >
260 using Residual = Sequential<
261  InputDataType, OutputDataType, true, CustomLayers...>;
262 
263 } // namespace ann
264 } // namespace mlpack
265 
266 // Include implementation.
267 #include "sequential_impl.hpp"
268 
269 #endif
DeleteVisitor executes the destructor of the instantiated object.
OutputHeightVisitor exposes the OutputHeight() method of the given module.
Linear algebra utility functions, generally performed on matrices or vectors.
arma::mat & Delta()
Modify the delta.
Definition: sequential.hpp:181
arma::mat & OutputParameter()
Modify the output parameter.
Definition: sequential.hpp:176
Sequential(const bool model=true)
Create the Sequential object using the specified parameters.
This visitor is to support copy constructor for neural network module.
The core includes that mlpack expects; standard C++ includes and Armadillo.
boost::variant< AdaptiveMaxPooling< arma::mat, arma::mat > *, AdaptiveMeanPooling< arma::mat, arma::mat > *, Add< arma::mat, arma::mat > *, AddMerge< arma::mat, arma::mat > *, AlphaDropout< arma::mat, arma::mat > *, AtrousConvolution< NaiveConvolution< ValidConvolution >, NaiveConvolution< FullConvolution >, NaiveConvolution< ValidConvolution >, arma::mat, arma::mat > *, BaseLayer< LogisticFunction, arma::mat, arma::mat > *, BaseLayer< IdentityFunction, arma::mat, arma::mat > *, BaseLayer< TanhFunction, arma::mat, arma::mat > *, BaseLayer< SoftplusFunction, arma::mat, arma::mat > *, BaseLayer< RectifierFunction, arma::mat, arma::mat > *, BatchNorm< arma::mat, arma::mat > *, BilinearInterpolation< arma::mat, arma::mat > *, CELU< arma::mat, arma::mat > *, Concat< arma::mat, arma::mat > *, Concatenate< arma::mat, arma::mat > *, ConcatPerformance< NegativeLogLikelihood< arma::mat, arma::mat >, arma::mat, arma::mat > *, Constant< arma::mat, arma::mat > *, Convolution< NaiveConvolution< ValidConvolution >, NaiveConvolution< FullConvolution >, NaiveConvolution< ValidConvolution >, arma::mat, arma::mat > *, CReLU< arma::mat, arma::mat > *, DropConnect< arma::mat, arma::mat > *, Dropout< arma::mat, arma::mat > *, ELU< arma::mat, arma::mat > *, FastLSTM< arma::mat, arma::mat > *, FlexibleReLU< arma::mat, arma::mat > *, GRU< arma::mat, arma::mat > *, HardTanH< arma::mat, arma::mat > *, Join< arma::mat, arma::mat > *, LayerNorm< arma::mat, arma::mat > *, LeakyReLU< arma::mat, arma::mat > *, Linear< arma::mat, arma::mat, NoRegularizer > *, LinearNoBias< arma::mat, arma::mat, NoRegularizer > *, LogSoftMax< arma::mat, arma::mat > *, Lookup< arma::mat, arma::mat > *, LSTM< arma::mat, arma::mat > *, MaxPooling< arma::mat, arma::mat > *, MeanPooling< arma::mat, arma::mat > *, MiniBatchDiscrimination< arma::mat, arma::mat > *, MultiplyConstant< arma::mat, arma::mat > *, MultiplyMerge< arma::mat, arma::mat > *, NegativeLogLikelihood< arma::mat, arma::mat > *, NoisyLinear< arma::mat, arma::mat > *, Padding< arma::mat, arma::mat > *, PReLU< arma::mat, arma::mat > *, Softmax< arma::mat, arma::mat > *, SpatialDropout< arma::mat, arma::mat > *, TransposedConvolution< NaiveConvolution< ValidConvolution >, NaiveConvolution< ValidConvolution >, NaiveConvolution< ValidConvolution >, arma::mat, arma::mat > *, WeightNorm< arma::mat, arma::mat > *, MoreTypes, CustomLayers *... > LayerTypes
arma::mat & Parameters()
Modify the initial point for the optimization.
Definition: sequential.hpp:166
arma::mat const & Delta() const
Get the delta.
Definition: sequential.hpp:179
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...
~Sequential()
Destroy the Sequential object.
OutputParameterVisitor exposes the output parameter of the given module.
const arma::mat & Parameters() const
Return the initial point for the optimization.
Definition: sequential.hpp:164
arma::mat const & OutputParameter() const
Get the output parameter.
Definition: sequential.hpp:174
arma::mat const & Gradient() const
Get the gradient.
Definition: sequential.hpp:184
arma::mat & Gradient()
Modify the gradient.
Definition: sequential.hpp:186
void serialize(Archive &ar, const uint32_t)
Serialize the layer.
size_t InputShape() const
void Add(LayerTypes< CustomLayers... > layer)
Definition: sequential.hpp:150
Sequential< InputDataType, OutputDataType, true, CustomLayers... > Residual
Definition: sequential.hpp:261
DeltaVisitor exposes the delta parameter of the given module.
Sequential & operator=(const Sequential &layer)
Copy assignment operator.
OutputWidthVisitor exposes the OutputWidth() method of the given module.
void Add(Args... args)
Definition: sequential.hpp:143
Implementation of the Sequential class.
arma::mat & InputParameter()
Modify the input parameter.
Definition: sequential.hpp:171
void Backward(const arma::Mat< eT > &, const arma::Mat< eT > &gy, arma::Mat< eT > &g)
Ordinary feed backward pass of a neural network, using 3rd-order tensors as input, calculating the function f(x) by propagating x backwards through f.
arma::mat const & InputParameter() const
Get the input parameter.
Definition: sequential.hpp:169
std::vector< LayerTypes< CustomLayers... > > & Model()
Return the model modules.
Definition: sequential.hpp:153