mlpack  master
ffn.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_METHODS_ANN_FFN_HPP
13 #define MLPACK_METHODS_ANN_FFN_HPP
14 
15 #include <mlpack/prereqs.hpp>
16 
24 
29 
30 namespace mlpack {
31 namespace ann {
32 
39 template<
40  typename OutputLayerType = NegativeLogLikelihood<>,
41  typename InitializationRuleType = RandomInitialization
42 >
43 class FFN
44 {
45  public:
48 
59  FFN(OutputLayerType&& outputLayer = OutputLayerType(),
60  InitializationRuleType initializeRule = InitializationRuleType());
61 
74  FFN(const arma::mat& predictors,
75  const arma::mat& responses,
76  OutputLayerType&& outputLayer = OutputLayerType(),
77  InitializationRuleType initializeRule = InitializationRuleType());
78 
80  ~FFN();
81 
95  template<
96  template<typename> class OptimizerType = mlpack::optimization::RMSprop
97  >
98  void Train(const arma::mat& predictors,
99  const arma::mat& responses,
100  OptimizerType<NetworkType>& optimizer);
101 
115  template<
116  template<typename> class OptimizerType = mlpack::optimization::RMSprop
117  >
118  void Train(const arma::mat& predictors, const arma::mat& responses);
119 
128  void Predict(arma::mat& predictors, arma::mat& responses);
129 
139  double Evaluate(const arma::mat& parameters,
140  const size_t i,
141  const bool deterministic = true);
142 
152  void Gradient(const arma::mat& parameters,
153  const size_t i,
154  arma::mat& gradient);
155 
156  /*
157  * Add a new module to the model.
158  *
159  * @param args The layer parameter.
160  */
161  template <class LayerType, class... Args>
162  void Add(Args... args) { network.push_back(new LayerType(args...)); }
163 
164  /*
165  * Add a new module to the model.
166  *
167  * @param layer The Layer to be added to the model.
168  */
169  void Add(LayerTypes layer) { network.push_back(layer); }
170 
172  size_t NumFunctions() const { return numFunctions; }
173 
175  const arma::mat& Parameters() const { return parameter; }
177  arma::mat& Parameters() { return parameter; }
178 
180  template<typename Archive>
181  void Serialize(Archive& ar, const unsigned int /* version */);
182 
183 private:
184  // Helper functions.
191  void Forward(arma::mat&& input);
192 
197  void Backward();
198 
203  void Gradient();
204 
208  void ResetParameters();
209 
214  void ResetDeterministic();
215 
219  void ResetGradients(arma::mat& gradient);
220 
222  OutputLayerType outputLayer;
223 
226  InitializationRuleType initializeRule;
227 
229  size_t width;
230 
232  size_t height;
233 
235  bool reset;
236 
238  std::vector<LayerTypes> network;
239 
241  arma::mat predictors;
242 
244  arma::mat responses;
245 
247  arma::mat parameter;
248 
250  size_t numFunctions;
251 
253  arma::mat error;
254 
256  arma::mat currentInput;
257 
259  arma::mat currentTarget;
260 
263 
266 
269 
272 
275 
278 
281 
284 
286  arma::mat delta;
287 
289  arma::mat inputParameter;
290 
292  arma::mat outputParameter;
293 
295  arma::mat gradient;
296 }; // class FFN
297 
298 } // namespace ann
299 } // namespace mlpack
300 
301 // Include implementation.
302 #include "ffn_impl.hpp"
303 
304 #endif
void Train(const arma::mat &predictors, const arma::mat &responses, OptimizerType< NetworkType > &optimizer)
Train the feedforward network on the given input data using the given optimizer.
DeleteVisitor executes the destructor of the instantiated object.
OutputWidthVisitor exposes the OutputHeight() method of the given module.
void Backward()
The Backward algorithm (part of the Forward-Backward algorithm).
OutputWidthVisitor outputWidthVisitor
Locally-stored output width visitor.
Definition: ffn.hpp:271
FFN(OutputLayerType &&outputLayer=OutputLayerType(), InitializationRuleType initializeRule=InitializationRuleType())
Create the FFN object with the given predictors and responses set (this is the set that is used to tr...
arma::mat parameter
Matrix of (trained) parameters.
Definition: ffn.hpp:247
arma::mat delta
Locally-stored delta object.
Definition: ffn.hpp:286
arma::mat error
The current error for the backward pass.
Definition: ffn.hpp:253
size_t height
The input height.
Definition: ffn.hpp:232
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: binarize.hpp:18
arma::mat currentInput
THe current input of the forward/backward pass.
Definition: ffn.hpp:256
void ResetDeterministic()
Reset the module status by setting the current deterministic parameter for all modules that implement...
OutputHeightVisitor outputHeightVisitor
Locally-stored output height visitor.
Definition: ffn.hpp:274
arma::mat predictors
The matrix of data points (predictors).
Definition: ffn.hpp:241
The core includes that mlpack expects; standard C++ includes and Armadillo.
const arma::mat & Parameters() const
Return the initial point for the optimization.
Definition: ffn.hpp:175
std::vector< LayerTypes > network
Locally-stored model modules.
Definition: ffn.hpp:238
WeightSizeVisitor returns the number of weights of the given module.
arma::mat currentTarget
THe current target of the forward/backward pass.
Definition: ffn.hpp:259
OutputLayerType outputLayer
Instantiated outputlayer used to evaluate the network.
Definition: ffn.hpp:222
void ResetParameters()
Reset the module infomration (weights/parameters).
arma::mat inputParameter
Locally-stored input parameter object.
Definition: ffn.hpp:289
void Serialize(Archive &ar, const unsigned int)
Serialize the model.
DeltaVisitor deltaVisitor
Locally-stored delta visitor.
Definition: ffn.hpp:262
void Add(Args...args)
Definition: ffn.hpp:162
~FFN()
Destructor to release allocated memory.
arma::mat responses
The matrix of responses to the input data points.
Definition: ffn.hpp:244
size_t width
The input width.
Definition: ffn.hpp:229
ResetVisitor executes the Reset() function.
void Add(LayerTypes layer)
Definition: ffn.hpp:169
OutputParameterVisitor exposes the output parameter of the given module.
void Forward(arma::mat &&input)
The Forward algorithm (part of the Forward-Backward algorithm).
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
size_t NumFunctions() const
Return the number of separable functions (the number of predictor points).
Definition: ffn.hpp:172
DeleteVisitor deleteVisitor
Locally-stored delete visitor.
Definition: ffn.hpp:280
void Predict(arma::mat &predictors, arma::mat &responses)
Predict the responses to a given set of predictors.
arma::mat outputParameter
Locally-stored output parameter object.
Definition: ffn.hpp:292
bool reset
Indicator if we already trained the model.
Definition: ffn.hpp:235
DeltaVisitor exposes the delta parameter of the given module.
void ResetGradients(arma::mat &gradient)
Reset the gradient for all modules that implement the Gradient function.
InitializationRuleType initializeRule
Instantiated InitializationRule object for initializing the network parameter.
Definition: ffn.hpp:226
Implementation of a standard feed forward network.
Definition: ffn.hpp:43
OutputParameterVisitor outputParameterVisitor
Locally-stored output parameter visitor.
Definition: ffn.hpp:265
OutputWidthVisitor exposes the OutputWidth() method of the given module.
void Gradient()
Iterate through all layer modules and update the the gradient using the layer defined optimizer...
arma::mat & Parameters()
Modify the initial point for the optimization.
Definition: ffn.hpp:177
bool deterministic
The current evaluation mode (training or testing).
Definition: ffn.hpp:283
double Evaluate(const arma::mat &parameters, const size_t i, const bool deterministic=true)
Evaluate the feedforward network with the given parameters.
ResetVisitor resetVisitor
Locally-stored reset visitor.
Definition: ffn.hpp:277
arma::mat gradient
Locally-stored gradient parameter.
Definition: ffn.hpp:295
size_t numFunctions
The number of separable functions (the number of predictor points).
Definition: ffn.hpp:250
RMSprop is an optimizer that utilizes the magnitude of recent gradients to normalize the gradients...
Definition: rmsprop.hpp:64
WeightSizeVisitor weightSizeVisitor
Locally-stored weight size visitor.
Definition: ffn.hpp:268