mlpack  master
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 >
46 class Sequential
47 {
48  public:
49 
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 layer) { network.push_back(layer); }
110 
112  std::vector<LayerTypes>& 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> network;
161 
163  arma::mat parameters;
164 
167 
170 
173 
175  std::vector<LayerTypes> empty;
176 
178  arma::mat delta;
179 
181  arma::mat inputParameter;
182 
184  arma::mat outputParameter;
185 
187  arma::mat gradient;
188 
191 
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
arma::mat inputParameter
Locally-stored input parameter object.
Definition: sequential.hpp:181
DeleteVisitor executes the destructor of the instantiated object.
arma::mat outputParameter
Locally-stored output parameter object.
Definition: sequential.hpp:184
OutputWidthVisitor exposes the OutputHeight() method of the given module.
bool model
Parameter which indicates if the modules should be exposed.
Definition: sequential.hpp:154
arma::mat const & OutputParameter() const
Get the output parameter.
Definition: sequential.hpp:132
bool reset
Indicator if we already initialized the model.
Definition: sequential.hpp:157
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: binarize.hpp:18
void Serialize(Archive &, const unsigned int)
Serialize the layer.
arma::mat & InputParameter()
Modify the input parameter.
Definition: sequential.hpp:129
The core includes that mlpack expects; standard C++ includes and Armadillo.
arma::mat const & Gradient() const
Get the gradient.
Definition: sequential.hpp:142
void Add(Args...args)
Definition: sequential.hpp:102
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...
~Sequential()
Destroy the Sequential object.
arma::mat & Parameters()
Modify the initial point for the optimization.
Definition: sequential.hpp:125
arma::mat parameters
Locally-stored model parameters.
Definition: sequential.hpp:163
std::vector< LayerTypes > network
Locally-stored network modules.
Definition: sequential.hpp:160
arma::mat & Delta()
Modify the delta.
Definition: sequential.hpp:139
Sequential(const bool model=true)
Create the Sequential object using the specified parameters.
arma::mat & Gradient()
Modify the gradient.
Definition: sequential.hpp:144
OutputParameterVisitor exposes the output parameter of the given module.
arma::mat & OutputParameter()
Modify the output parameter.
Definition: sequential.hpp:134
size_t height
The input height.
Definition: sequential.hpp:199
arma::mat const & InputParameter() const
Definition: sequential.hpp:127
boost::variant< Add< arma::mat, arma::mat > *, AddMerge< 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 > *, 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 > *, DropConnect< arma::mat, arma::mat > *, Dropout< arma::mat, arma::mat > *, Glimpse< arma::mat, arma::mat > *, HardTanH< arma::mat, arma::mat > *, Join< 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 > *, MaxPooling< arma::mat, arma::mat > *, MeanPooling< arma::mat, arma::mat > *, MeanSquaredError< arma::mat, arma::mat > *, MultiplyConstant< 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 > * > LayerTypes
const arma::mat & Parameters() const
Return the initial point for the optimization.
Definition: sequential.hpp:123
void Add(LayerTypes layer)
Definition: sequential.hpp:109
OutputWidthVisitor outputWidthVisitor
Locally-stored output width visitor.
Definition: sequential.hpp:190
DeleteVisitor deleteVisitor
Locally-stored delete visitor.
Definition: sequential.hpp:172
OutputParameterVisitor outputParameterVisitor
Locally-stored output parameter visitor.
Definition: sequential.hpp:169
std::vector< LayerTypes > empty
Locally-stored empty list of modules.
Definition: sequential.hpp:175
DeltaVisitor exposes the delta parameter of the given module.
arma::mat const & Delta() const
Get the delta.e.
Definition: sequential.hpp:137
arma::mat delta
Locally-stored delta object.
Definition: sequential.hpp:178
OutputWidthVisitor exposes the OutputWidth() method of the given module.
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.
DeltaVisitor deltaVisitor
Locally-stored delta visitor.
Definition: sequential.hpp:166
size_t width
The input width.
Definition: sequential.hpp:196
OutputHeightVisitor outputHeightVisitor
Locally-stored output height visitor.
Definition: sequential.hpp:193
arma::mat gradient
Locally-stored gradient object.
Definition: sequential.hpp:187
std::vector< LayerTypes > & Model()
Return the model modules.
Definition: sequential.hpp:112