mlpack  3.1.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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/delta_visitor.hpp"
22 #include "../visitor/output_height_visitor.hpp"
23 #include "../visitor/output_parameter_visitor.hpp"
24 #include "../visitor/output_width_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();
92 
100  template<typename eT>
101  void Forward(const arma::Mat<eT>& input, arma::Mat<eT>& output);
102 
112  template<typename eT>
113  void Backward(const arma::Mat<eT>& /* input */,
114  const arma::Mat<eT>& gy,
115  arma::Mat<eT>& g);
116 
117  /*
118  * Calculate the gradient using the output delta and the input activation.
119  *
120  * @param input The input parameter used for calculating the gradient.
121  * @param error The calculated error.
122  * @param gradient The calculated gradient.
123  */
124  template<typename eT>
125  void Gradient(const arma::Mat<eT>& input,
126  const arma::Mat<eT>& error,
127  arma::Mat<eT>& /* gradient */);
128 
129  /*
130  * Add a new module to the model.
131  *
132  * @param args The layer parameter.
133  */
134  template <class LayerType, class... Args>
135  void Add(Args... args) { network.push_back(new LayerType(args...)); }
136 
137  /*
138  * Add a new module to the model.
139  *
140  * @param layer The Layer to be added to the model.
141  */
142  void Add(LayerTypes<CustomLayers...> layer) { network.push_back(layer); }
143 
145  std::vector<LayerTypes<CustomLayers...> >& Model()
146  {
147  if (model)
148  {
149  return network;
150  }
151 
152  return empty;
153  }
154 
156  const arma::mat& Parameters() const { return parameters; }
158  arma::mat& Parameters() { return parameters; }
159 
161  arma::mat const& InputParameter() const { return inputParameter; }
163  arma::mat& InputParameter() { return inputParameter; }
164 
166  arma::mat const& OutputParameter() const { return outputParameter; }
168  arma::mat& OutputParameter() { return outputParameter; }
169 
171  arma::mat const& Delta() const { return delta; }
173  arma::mat& Delta() { return delta; }
174 
176  arma::mat const& Gradient() const { return gradient; }
178  arma::mat& Gradient() { return gradient; }
179 
183  template<typename Archive>
184  void serialize(Archive& /* ar */, const unsigned int /* version */);
185 
186  private:
188  bool model;
189 
191  bool reset;
192 
194  std::vector<LayerTypes<CustomLayers...> > network;
195 
197  arma::mat parameters;
198 
200  DeltaVisitor deltaVisitor;
201 
203  OutputParameterVisitor outputParameterVisitor;
204 
206  DeleteVisitor deleteVisitor;
207 
209  std::vector<LayerTypes<CustomLayers...> > empty;
210 
212  arma::mat delta;
213 
215  arma::mat inputParameter;
216 
218  arma::mat outputParameter;
219 
221  arma::mat gradient;
222 
224  OutputWidthVisitor outputWidthVisitor;
225 
227  OutputHeightVisitor outputHeightVisitor;
228 
230  size_t width;
231 
233  size_t height;
234 
236  bool ownsLayers;
237 }; // class Sequential
238 
239 /*
240  * Convenience typedef for use as Residual<> layer.
241  */
242 template<
243  typename InputDataType = arma::mat,
244  typename OutputDataType = arma::mat,
245  typename... CustomLayers
246 >
247 using Residual = Sequential<
248  InputDataType, OutputDataType, true, CustomLayers...>;
249 
250 } // namespace ann
251 } // namespace mlpack
252 
254 namespace boost {
255 namespace serialization {
256 
257 template <
258  typename InputDataType,
259  typename OutputDataType,
260  bool Residual,
261  typename... CustomLayers
262 >
263 struct version<mlpack::ann::Sequential<
264  InputDataType, OutputDataType, Residual, CustomLayers...>>
265 {
266  BOOST_STATIC_CONSTANT(int, value = 1);
267 };
268 
269 } // namespace serialization
270 } // namespace boost
271 
272 // Include implementation.
273 #include "sequential_impl.hpp"
274 
275 #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.
const arma::mat & Parameters() const
Return the initial point for the optimization.
Definition: sequential.hpp:156
arma::mat & Delta()
Modify the delta.
Definition: sequential.hpp:173
arma::mat & OutputParameter()
Modify the output parameter.
Definition: sequential.hpp:168
Sequential(const bool model=true)
Create the Sequential object using the specified parameters.
The core includes that mlpack expects; standard C++ includes and Armadillo.
boost::variant< Add< arma::mat, arma::mat > *, AddMerge< 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< RectifierFunction, arma::mat, arma::mat > *, BaseLayer< SoftplusFunction, arma::mat, arma::mat > *, BatchNorm< arma::mat, arma::mat > *, BilinearInterpolation< 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 > *, TransposedConvolution< NaiveConvolution< ValidConvolution >, NaiveConvolution< ValidConvolution >, NaiveConvolution< ValidConvolution >, arma::mat, arma::mat > *, DropConnect< arma::mat, arma::mat > *, Dropout< arma::mat, arma::mat > *, AlphaDropout< arma::mat, arma::mat > *, ELU< arma::mat, arma::mat > *, FlexibleReLU< arma::mat, arma::mat > *, Glimpse< arma::mat, arma::mat > *, HardTanH< arma::mat, arma::mat > *, Highway< arma::mat, arma::mat > *, Join< arma::mat, arma::mat > *, LayerNorm< arma::mat, arma::mat > *, LeakyReLU< arma::mat, arma::mat > *, CReLU< 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 > *, GRU< arma::mat, arma::mat > *, FastLSTM< 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 > *, Padding< arma::mat, arma::mat > *, PReLU< arma::mat, arma::mat > *, WeightNorm< arma::mat, arma::mat > *, CELU< arma::mat, arma::mat > *, MoreTypes, CustomLayers *... > LayerTypes
arma::mat & Parameters()
Modify the initial point for the optimization.
Definition: sequential.hpp:158
void Add(Args...args)
Definition: sequential.hpp:135
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...
arma::mat const & OutputParameter() const
Get the output parameter.
Definition: sequential.hpp:166
~Sequential()
Destroy the Sequential object.
OutputParameterVisitor exposes the output parameter of the given module.
arma::mat & Gradient()
Modify the gradient.
Definition: sequential.hpp:178
Sequential< InputDataType, OutputDataType, true, CustomLayers...> Residual
Definition: sequential.hpp:248
arma::mat const & InputParameter() const
Get the input parameter.
Definition: sequential.hpp:161
void Add(LayerTypes< CustomLayers...> layer)
Definition: sequential.hpp:142
DeltaVisitor exposes the delta parameter of the given module.
arma::mat const & Delta() const
Get the delta.
Definition: sequential.hpp:171
OutputWidthVisitor exposes the OutputWidth() method of the given module.
arma::mat & InputParameter()
Modify the input parameter.
Definition: sequential.hpp:163
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.
std::vector< LayerTypes< CustomLayers...> > & Model()
Return the model modules.
Definition: sequential.hpp:145
arma::mat const & Gradient() const
Get the gradient.
Definition: sequential.hpp:176