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
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 
27 #include "layer_types.hpp"
28 #include "add_merge.hpp"
29 
30 namespace mlpack {
31 namespace ann {
32 
66 template <
67  typename InputDataType = arma::mat,
68  typename OutputDataType = arma::mat,
69  bool Residual = false,
70  typename... CustomLayers
71 >
72 class Sequential
73 {
74  public:
80  Sequential(const bool model = true);
81 
89  Sequential(const bool model, const bool ownsLayers);
90 
92  Sequential(const Sequential& layer);
93 
95  Sequential& operator = (const Sequential& layer);
96 
98  ~Sequential();
99 
107  template<typename eT>
108  void Forward(const arma::Mat& input, arma::Mat& output);
109 
119  template<typename eT>
120  void Backward(const arma::Mat& /* input */,
121  const arma::Mat& gy,
122  arma::Mat& g);
123 
124  /*
125  * Calculate the gradient using the output delta and the input activation.
126  *
127  * @param input The input parameter used for calculating the gradient.
128  * @param error The calculated error.
129  * @param gradient The calculated gradient.
130  */
131  template<typename eT>
132  void Gradient(const arma::Mat& input,
133  const arma::Mat& error,
134  arma::Mat& /* gradient */);
135 
136  /*
137  * Add a new module to the model.
138  *
139  * @param args The layer parameter.
140  */
141  template <class LayerType, class... Args>
142  void Add(Args... args) { network.push_back(new LayerType(args...)); }
143 
144  /*
145  * Add a new module to the model.
146  *
147  * @param layer The Layer to be added to the model.
148  */
149  void Add(LayerTypes layer) { network.push_back(layer); }
150 
152  std::vector<LayerTypes >& Model()
153  {
154  if (model)
155  {
156  return network;
157  }
158 
159  return empty;
160  }
161 
163  const arma::mat& Parameters() const { return parameters; }
165  arma::mat& Parameters() { return parameters; }
166 
168  arma::mat const& InputParameter() const { return inputParameter; }
170  arma::mat& InputParameter() { return inputParameter; }
171 
173  arma::mat const& OutputParameter() const { return outputParameter; }
175  arma::mat& OutputParameter() { return outputParameter; }
176 
178  arma::mat const& Delta() const { return delta; }
180  arma::mat& Delta() { return delta; }
181 
183  arma::mat const& Gradient() const { return gradient; }
185  arma::mat& Gradient() { return gradient; }
186 
190  template<typename Archive>
191  void serialize(Archive& /* ar */, const unsigned int /* version */);
192 
193  private:
195  bool model;
196 
198  bool reset;
199 
201  std::vector<LayerTypes > network;
202 
204  arma::mat parameters;
205 
207  DeltaVisitor deltaVisitor;
208 
210  OutputParameterVisitor outputParameterVisitor;
211 
213  DeleteVisitor deleteVisitor;
214 
216  std::vector<LayerTypes > empty;
217 
219  arma::mat delta;
220 
222  arma::mat inputParameter;
223 
225  arma::mat outputParameter;
226 
228  arma::mat gradient;
229 
231  OutputWidthVisitor outputWidthVisitor;
232 
234  OutputHeightVisitor outputHeightVisitor;
235 
237  CopyVisitor copyVisitor;
238 
240  size_t width;
241 
243  size_t height;
244 
246  bool ownsLayers;
247 }; // class Sequential
248 
249 /*
250  * Convenience typedef for use as Residual<> layer.
251  */
252 template<
253  typename InputDataType = arma::mat,
254  typename OutputDataType = arma::mat,
255  typename... CustomLayers
256 >
257 using Residual = Sequential<
258  InputDataType, OutputDataType, true, CustomLayers...>;
259 
260 } // namespace ann
261 } // namespace mlpack
262 
264 namespace boost {
265 namespace serialization {
266 
267 template <
268  typename InputDataType,
269  typename OutputDataType,
270  bool Residual,
271  typename... CustomLayers
272 >
273 struct version<mlpack::ann::Sequential<
274  InputDataType, OutputDataType, Residual, CustomLayers...>>
275 {
276  BOOST_STATIC_CONSTANT(int, value = 1);
277 };
278 
279 } // namespace serialization
280 } // namespace boost
281 
282 // Include implementation.
283 #include "sequential_impl.hpp"
284 
285 #endif
DeleteVisitor executes the destructor of the instantiated object.
void serialize(Archive &, const unsigned int)
Serialize the layer.
OutputHeightVisitor exposes the OutputHeight() method of the given module.
Set the serialization version of the adaboost class.
Definition: adaboost.hpp:198
Linear algebra utility functions, generally performed on matrices or vectors.
arma::mat & Delta()
Modify the delta.
Definition: sequential.hpp:180
arma::mat & OutputParameter()
Modify the output parameter.
Definition: sequential.hpp:175
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:165
arma::mat const & Delta() const
Get the delta.
Definition: sequential.hpp:178
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:163
arma::mat const & OutputParameter() const
Get the output parameter.
Definition: sequential.hpp:173
arma::mat const & Gradient() const
Get the gradient.
Definition: sequential.hpp:183
arma::mat & Gradient()
Modify the gradient.
Definition: sequential.hpp:185
void Add(LayerTypes< CustomLayers... > layer)
Definition: sequential.hpp:149
Sequential< InputDataType, OutputDataType, true, CustomLayers... > Residual
Definition: sequential.hpp:258
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:142
Implementation of the Sequential class.
arma::mat & InputParameter()
Modify the input parameter.
Definition: sequential.hpp:170
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:168
std::vector< LayerTypes< CustomLayers... > > & Model()
Return the model modules.
Definition: sequential.hpp:152