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 "../visitor/delete_visitor.hpp"
19 #include "../visitor/copy_visitor.hpp"
20 #include "../visitor/delta_visitor.hpp"
21 #include "../visitor/output_height_visitor.hpp"
22 #include "../visitor/output_parameter_visitor.hpp"
23 #include "../visitor/output_width_visitor.hpp"
24 #include "../visitor/input_shape_visitor.hpp"
25 
26 #include "layer_types.hpp"
27 #include "add_merge.hpp"
28 
29 namespace mlpack {
30 namespace ann {
31 
65 template <
66  typename InputDataType = arma::mat,
67  typename OutputDataType = arma::mat,
68  bool Residual = false,
69  typename... CustomLayers
70 >
71 class Sequential
72 {
73  public:
79  Sequential(const bool model = true);
80 
88  Sequential(const bool model, const bool ownsLayers);
89 
91  Sequential(const Sequential& layer);
92 
94  Sequential& operator = (const Sequential& layer);
95 
97  ~Sequential();
98 
106  template<typename eT>
107  void Forward(const arma::Mat<eT>& input, arma::Mat<eT>& output);
108 
118  template<typename eT>
119  void Backward(const arma::Mat<eT>& /* input */,
120  const arma::Mat<eT>& gy,
121  arma::Mat<eT>& g);
122 
123  /*
124  * Calculate the gradient using the output delta and the input activation.
125  *
126  * @param input The input parameter used for calculating the gradient.
127  * @param error The calculated error.
128  * @param gradient The calculated gradient.
129  */
130  template<typename eT>
131  void Gradient(const arma::Mat<eT>& input,
132  const arma::Mat<eT>& error,
133  arma::Mat<eT>& /* gradient */);
134 
135  /*
136  * Add a new module to the model.
137  *
138  * @param args The layer parameter.
139  */
140  template <class LayerType, class... Args>
141  void Add(Args... args) { network.push_back(new LayerType(args...)); }
142 
143  /*
144  * Add a new module to the model.
145  *
146  * @param layer The Layer to be added to the model.
147  */
148  void Add(LayerTypes<CustomLayers...> layer) { network.push_back(layer); }
149 
151  std::vector<LayerTypes<CustomLayers...> >& Model()
152  {
153  if (model)
154  {
155  return network;
156  }
157 
158  return empty;
159  }
160 
162  const arma::mat& Parameters() const { return parameters; }
164  arma::mat& Parameters() { return parameters; }
165 
167  arma::mat const& InputParameter() const { return inputParameter; }
169  arma::mat& InputParameter() { return inputParameter; }
170 
172  arma::mat const& OutputParameter() const { return outputParameter; }
174  arma::mat& OutputParameter() { return outputParameter; }
175 
177  arma::mat const& Delta() const { return delta; }
179  arma::mat& Delta() { return delta; }
180 
182  arma::mat const& Gradient() const { return gradient; }
184  arma::mat& Gradient() { return gradient; }
185 
186  size_t InputShape() const;
187 
191  template<typename Archive>
192  void serialize(Archive& ar, const uint32_t /* version */);
193 
194  private:
196  bool model;
197 
199  bool reset;
200 
202  std::vector<LayerTypes<CustomLayers...> > network;
203 
205  arma::mat parameters;
206 
208  DeltaVisitor deltaVisitor;
209 
211  OutputParameterVisitor outputParameterVisitor;
212 
214  DeleteVisitor deleteVisitor;
215 
217  std::vector<LayerTypes<CustomLayers...> > empty;
218 
220  arma::mat delta;
221 
223  arma::mat inputParameter;
224 
226  arma::mat outputParameter;
227 
229  arma::mat gradient;
230 
232  OutputWidthVisitor outputWidthVisitor;
233 
235  OutputHeightVisitor outputHeightVisitor;
236 
238  CopyVisitor<CustomLayers...> copyVisitor;
239 
241  size_t width;
242 
244  size_t height;
245 
247  bool ownsLayers;
248 }; // class Sequential
249 
250 /*
251  * Convenience typedef for use as Residual<> layer.
252  */
253 template<
254  typename InputDataType = arma::mat,
255  typename OutputDataType = arma::mat,
256  typename... CustomLayers
257 >
258 using Residual = Sequential<
259  InputDataType, OutputDataType, true, CustomLayers...>;
260 
261 } // namespace ann
262 } // namespace mlpack
263 
264 // Include implementation.
265 #include "sequential_impl.hpp"
266 
267 #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:179
arma::mat & OutputParameter()
Modify the output parameter.
Definition: sequential.hpp:174
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.
arma::mat & Parameters()
Modify the initial point for the optimization.
Definition: sequential.hpp:164
arma::mat const & Delta() const
Get the delta.
Definition: sequential.hpp:177
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:162
arma::mat const & OutputParameter() const
Get the output parameter.
Definition: sequential.hpp:172
arma::mat const & Gradient() const
Get the gradient.
Definition: sequential.hpp:182
arma::mat & Gradient()
Modify the gradient.
Definition: sequential.hpp:184
void serialize(Archive &ar, const uint32_t)
Serialize the layer.
size_t InputShape() const
void Add(LayerTypes< CustomLayers... > layer)
Definition: sequential.hpp:148
Sequential< InputDataType, OutputDataType, true, CustomLayers... > Residual
Definition: sequential.hpp:259
DeltaVisitor exposes the delta parameter of the given module.
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 > *, 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 > *, Sequential< arma::mat, arma::mat, false > *, Sequential< arma::mat, arma::mat, true > *, Softmax< arma::mat, arma::mat > *, TransposedConvolution< NaiveConvolution< ValidConvolution >, NaiveConvolution< ValidConvolution >, NaiveConvolution< ValidConvolution >, arma::mat, arma::mat > *, WeightNorm< arma::mat, arma::mat > *, MoreTypes, CustomLayers *... > LayerTypes
Sequential & operator=(const Sequential &layer)
Copy assignment operator.
OutputWidthVisitor exposes the OutputWidth() method of the given module.
void Add(Args... args)
Definition: sequential.hpp:141
Implementation of the Sequential class.
arma::mat & InputParameter()
Modify the input parameter.
Definition: sequential.hpp:169
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:167
std::vector< LayerTypes< CustomLayers... > > & Model()
Return the model modules.
Definition: sequential.hpp:151