mlpack 3.4.2
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/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
30namespace mlpack {
31namespace ann {
32
66template <
67 typename InputDataType = arma::mat,
68 typename OutputDataType = arma::mat,
69 bool Residual = false,
70 typename... CustomLayers
71>
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
96
99
107 template<typename eT>
108 void Forward(const arma::Mat<eT>& input, arma::Mat<eT>& output);
109
119 template<typename eT>
120 void Backward(const arma::Mat<eT>& /* input */,
121 const arma::Mat<eT>& gy,
122 arma::Mat<eT>& 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<eT>& input,
133 const arma::Mat<eT>& error,
134 arma::Mat<eT>& /* 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<CustomLayers...> layer) { network.push_back(layer); }
150
152 std::vector<LayerTypes<CustomLayers...> >& 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<CustomLayers...> > network;
202
204 arma::mat parameters;
205
207 DeltaVisitor deltaVisitor;
208
210 OutputParameterVisitor outputParameterVisitor;
211
213 DeleteVisitor deleteVisitor;
214
216 std::vector<LayerTypes<CustomLayers...> > 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<CustomLayers...> 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 */
252template<
253 typename InputDataType = arma::mat,
254 typename OutputDataType = arma::mat,
255 typename... CustomLayers
256>
258 InputDataType, OutputDataType, true, CustomLayers...>;
259
260} // namespace ann
261} // namespace mlpack
262
264namespace boost {
265namespace serialization {
266
267template <
268 typename InputDataType,
269 typename OutputDataType,
270 bool Residual,
271 typename... CustomLayers
272>
273struct 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
This visitor is to support copy constructor for neural network module.
DeleteVisitor executes the destructor of the instantiated object.
DeltaVisitor exposes the delta parameter of the given module.
OutputHeightVisitor exposes the OutputHeight() method of the given module.
OutputParameterVisitor exposes the output parameter of the given module.
OutputWidthVisitor exposes the OutputWidth() method of the given module.
Implementation of the Sequential class.
Definition: sequential.hpp:73
Sequential(const bool model=true)
Create the Sequential object using the specified parameters.
arma::mat const & Gradient() const
Get the gradient.
Definition: sequential.hpp:183
~Sequential()
Destroy the Sequential object.
arma::mat & Parameters()
Modify the initial point for the optimization.
Definition: sequential.hpp:165
Sequential(const Sequential &layer)
Copy constructor.
arma::mat & OutputParameter()
Modify the output parameter.
Definition: sequential.hpp:175
const arma::mat & Parameters() const
Return the initial point for the optimization.
Definition: sequential.hpp:163
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...
void Add(LayerTypes< CustomLayers... > layer)
Definition: sequential.hpp:149
void Gradient(const arma::Mat< eT > &input, const arma::Mat< eT > &error, arma::Mat< eT > &)
Sequential(const bool model, const bool ownsLayers)
Create the Sequential object using the specified parameters.
arma::mat & Delta()
Modify the delta.
Definition: sequential.hpp:180
std::vector< LayerTypes< CustomLayers... > > & Model()
Return the model modules.
Definition: sequential.hpp:152
void Add(Args... args)
Definition: sequential.hpp:142
arma::mat const & InputParameter() const
Get the input parameter.
Definition: sequential.hpp:168
Sequential & operator=(const Sequential &layer)
Copy assignment operator.
arma::mat const & Delta() const
Get the delta.
Definition: sequential.hpp:178
void serialize(Archive &, const unsigned int)
Serialize the layer.
arma::mat & InputParameter()
Modify the input parameter.
Definition: sequential.hpp:170
arma::mat const & OutputParameter() const
Get the output parameter.
Definition: sequential.hpp:173
arma::mat & Gradient()
Modify the gradient.
Definition: sequential.hpp:185
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,...
Set the serialization version of the adaboost class.
Definition: adaboost.hpp:198
Sequential< InputDataType, OutputDataType, true, CustomLayers... > Residual
Definition: sequential.hpp:258
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
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
The core includes that mlpack expects; standard C++ includes and Armadillo.