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 
42 template <
43  typename InputDataType = arma::mat,
44  typename OutputDataType = arma::mat,
45  typename... CustomLayers
46 >
47 class Sequential
48 {
49  public:
55  Sequential(const bool model = true);
56 
58  ~Sequential();
59 
67  template<typename eT>
68  void Forward(arma::Mat<eT>&& input, arma::Mat<eT>&& output);
69 
79  template<typename eT>
80  void Backward(const arma::Mat<eT>&& /* input */,
81  arma::Mat<eT>&& gy,
82  arma::Mat<eT>&& g);
83 
84  /*
85  * Calculate the gradient using the output delta and the input activation.
86  *
87  * @param input The input parameter used for calculating the gradient.
88  * @param error The calculated error.
89  * @param gradient The calculated gradient.
90  */
91  template<typename eT>
92  void Gradient(arma::Mat<eT>&& input,
93  arma::Mat<eT>&& error,
94  arma::Mat<eT>&& /* gradient */);
95 
96  /*
97  * Add a new module to the model.
98  *
99  * @param args The layer parameter.
100  */
101  template <class LayerType, class... Args>
102  void Add(Args... args) { network.push_back(new LayerType(args...)); }
103 
104  /*
105  * Add a new module to the model.
106  *
107  * @param layer The Layer to be added to the model.
108  */
109  void Add(LayerTypes<CustomLayers...> layer) { network.push_back(layer); }
110 
112  std::vector<LayerTypes<CustomLayers...> >& Model()
113  {
114  if (model)
115  {
116  return network;
117  }
118 
119  return empty;
120  }
121 
123  const arma::mat& Parameters() const { return parameters; }
125  arma::mat& Parameters() { return parameters; }
126 
127  arma::mat const& InputParameter() const { return inputParameter; }
129  arma::mat& InputParameter() { return inputParameter; }
130 
132  arma::mat const& OutputParameter() const { return outputParameter; }
134  arma::mat& OutputParameter() { return outputParameter; }
135 
137  arma::mat const& Delta() const { return delta; }
139  arma::mat& Delta() { return delta; }
140 
142  arma::mat const& Gradient() const { return gradient; }
144  arma::mat& Gradient() { return gradient; }
145 
149  template<typename Archive>
150  void serialize(Archive& /* ar */, const unsigned int /* version */);
151 
152  private:
154  bool model;
155 
157  bool reset;
158 
160  std::vector<LayerTypes<CustomLayers...> > network;
161 
163  arma::mat parameters;
164 
166  DeltaVisitor deltaVisitor;
167 
169  OutputParameterVisitor outputParameterVisitor;
170 
172  DeleteVisitor deleteVisitor;
173 
175  std::vector<LayerTypes<CustomLayers...> > empty;
176 
178  arma::mat delta;
179 
181  arma::mat inputParameter;
182 
184  arma::mat outputParameter;
185 
187  arma::mat gradient;
188 
190  OutputWidthVisitor outputWidthVisitor;
191 
193  OutputHeightVisitor outputHeightVisitor;
194 
196  size_t width;
197 
199  size_t height;
200 }; // class Sequential
201 
202 } // namespace ann
203 } // namespace mlpack
204 
205 // Include implementation.
206 #include "sequential_impl.hpp"
207 
208 #endif
DeleteVisitor executes the destructor of the instantiated object.
void serialize(Archive &, const unsigned int)
Serialize the layer.
OutputWidthVisitor exposes the OutputHeight() method of the given module.
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 > *, BatchNorm< arma::mat, arma::mat > *, BilinearInterpolation< arma::mat, arma::mat > *, Concat< 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< FullConvolution >, 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 > *, Join< arma::mat, arma::mat > *, LayerNorm< arma::mat, arma::mat > *, LeakyReLU< arma::mat, arma::mat > *, Linear< arma::mat, arma::mat > *, LinearNoBias< arma::mat, arma::mat > *, 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 > *, MultiplyConstant< arma::mat, arma::mat > *, MultiplyMerge< arma::mat, arma::mat > *, NegativeLogLikelihood< arma::mat, arma::mat > *, PReLU< arma::mat, arma::mat > *, Recurrent< arma::mat, arma::mat > *, RecurrentAttention< arma::mat, arma::mat > *, ReinforceNormal< arma::mat, arma::mat > *, Select< arma::mat, arma::mat > *, Sequential< arma::mat, arma::mat > *, VRClassReward< arma::mat, arma::mat > *, CustomLayers *... > LayerTypes
.hpp
Definition: add_to_po.hpp:21
arma::mat & Delta()
Modify the delta.
Definition: sequential.hpp:139
arma::mat & OutputParameter()
Modify the output parameter.
Definition: sequential.hpp:134
Sequential(const bool model=true)
Create the Sequential object using the specified parameters.
The core includes that mlpack expects; standard C++ includes and Armadillo.
arma::mat & Parameters()
Modify the initial point for the optimization.
Definition: sequential.hpp:125
arma::mat const & Delta() const
Get the delta.e.
Definition: sequential.hpp:137
~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:123
arma::mat const & OutputParameter() const
Get the output parameter.
Definition: sequential.hpp:132
arma::mat const & Gradient() const
Get the gradient.
Definition: sequential.hpp:142
arma::mat & Gradient()
Modify the gradient.
Definition: sequential.hpp:144
void Add(LayerTypes< CustomLayers... > layer)
Definition: sequential.hpp:109
void Forward(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...
DeltaVisitor exposes the delta parameter of the given module.
OutputWidthVisitor exposes the OutputWidth() method of the given module.
void Add(Args... args)
Definition: sequential.hpp:102
arma::mat & InputParameter()
Modify the input parameter.
Definition: sequential.hpp:129
void Backward(const arma::Mat< eT > &&, 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
Definition: sequential.hpp:127
std::vector< LayerTypes< CustomLayers... > > & Model()
Return the model modules.
Definition: sequential.hpp:112