mlpack  3.1.0
 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 
82  ~Sequential();
83 
91  template<typename eT>
92  void Forward(arma::Mat<eT>&& input, arma::Mat<eT>&& output);
93 
103  template<typename eT>
104  void Backward(const arma::Mat<eT>&& /* input */,
105  arma::Mat<eT>&& gy,
106  arma::Mat<eT>&& g);
107 
108  /*
109  * Calculate the gradient using the output delta and the input activation.
110  *
111  * @param input The input parameter used for calculating the gradient.
112  * @param error The calculated error.
113  * @param gradient The calculated gradient.
114  */
115  template<typename eT>
116  void Gradient(arma::Mat<eT>&& input,
117  arma::Mat<eT>&& error,
118  arma::Mat<eT>&& /* gradient */);
119 
120  /*
121  * Add a new module to the model.
122  *
123  * @param args The layer parameter.
124  */
125  template <class LayerType, class... Args>
126  void Add(Args... args) { network.push_back(new LayerType(args...)); }
127 
128  /*
129  * Add a new module to the model.
130  *
131  * @param layer The Layer to be added to the model.
132  */
133  void Add(LayerTypes<CustomLayers...> layer) { network.push_back(layer); }
134 
135  /*
136  * Destroy all the modules added to the Sequential object.
137  */
138  void DeleteModules();
139 
141  std::vector<LayerTypes<CustomLayers...> >& Model()
142  {
143  if (model)
144  {
145  return network;
146  }
147 
148  return empty;
149  }
150 
152  const arma::mat& Parameters() const { return parameters; }
154  arma::mat& Parameters() { return parameters; }
155 
157  arma::mat const& InputParameter() const { return inputParameter; }
159  arma::mat& InputParameter() { return inputParameter; }
160 
162  arma::mat const& OutputParameter() const { return outputParameter; }
164  arma::mat& OutputParameter() { return outputParameter; }
165 
167  arma::mat const& Delta() const { return delta; }
169  arma::mat& Delta() { return delta; }
170 
172  arma::mat const& Gradient() const { return gradient; }
174  arma::mat& Gradient() { return gradient; }
175 
179  template<typename Archive>
180  void serialize(Archive& /* ar */, const unsigned int /* version */);
181 
182  private:
184  bool model;
185 
187  bool reset;
188 
190  std::vector<LayerTypes<CustomLayers...> > network;
191 
193  arma::mat parameters;
194 
196  DeltaVisitor deltaVisitor;
197 
199  OutputParameterVisitor outputParameterVisitor;
200 
202  DeleteVisitor deleteVisitor;
203 
205  std::vector<LayerTypes<CustomLayers...> > empty;
206 
208  arma::mat delta;
209 
211  arma::mat inputParameter;
212 
214  arma::mat outputParameter;
215 
217  arma::mat gradient;
218 
220  OutputWidthVisitor outputWidthVisitor;
221 
223  OutputHeightVisitor outputHeightVisitor;
224 
226  size_t width;
227 
229  size_t height;
230 }; // class Sequential
231 
232 /*
233  * Convenience typedef for use as Residual<> layer.
234  */
235 template<
236  typename InputDataType = arma::mat,
237  typename OutputDataType = arma::mat,
238  typename... CustomLayers
239 >
240 using Residual = Sequential<
241  InputDataType, OutputDataType, true, CustomLayers...>;
242 
243 } // namespace ann
244 } // namespace mlpack
245 
246 // Include implementation.
247 #include "sequential_impl.hpp"
248 
249 #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.
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< 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 > *, CReLU< 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 > *, Reparametrization< arma::mat, arma::mat > *, Select< arma::mat, arma::mat > *, Sequential< arma::mat, arma::mat, false > *, Sequential< arma::mat, arma::mat, true > *, Subview< arma::mat, arma::mat > *, VRClassReward< arma::mat, arma::mat > *, CustomLayers *... > LayerTypes
const arma::mat & Parameters() const
Return the initial point for the optimization.
Definition: sequential.hpp:152
arma::mat & Delta()
Modify the delta.
Definition: sequential.hpp:169
arma::mat & OutputParameter()
Modify the output parameter.
Definition: sequential.hpp:164
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:154
void Add(Args...args)
Definition: sequential.hpp:126
arma::mat const & OutputParameter() const
Get the output parameter.
Definition: sequential.hpp:162
~Sequential()
Destroy the Sequential object.
OutputParameterVisitor exposes the output parameter of the given module.
arma::mat & Gradient()
Modify the gradient.
Definition: sequential.hpp:174
Sequential< InputDataType, OutputDataType, true, CustomLayers...> Residual
Definition: sequential.hpp:241
arma::mat const & InputParameter() const
Get the input parameter.
Definition: sequential.hpp:157
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...
void Add(LayerTypes< CustomLayers...> layer)
Definition: sequential.hpp:133
DeltaVisitor exposes the delta parameter of the given module.
arma::mat const & Delta() const
Get the delta.
Definition: sequential.hpp:167
OutputWidthVisitor exposes the OutputWidth() method of the given module.
arma::mat & InputParameter()
Modify the input parameter.
Definition: sequential.hpp:159
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.
std::vector< LayerTypes< CustomLayers...> > & Model()
Return the model modules.
Definition: sequential.hpp:141
arma::mat const & Gradient() const
Get the gradient.
Definition: sequential.hpp:172