ffn.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_METHODS_ANN_FFN_HPP
14 #define MLPACK_METHODS_ANN_FFN_HPP
15 
16 #include <mlpack/prereqs.hpp>
17 
25 #include "visitor/copy_visitor.hpp"
26 
28 
32 
33 namespace mlpack {
34 namespace ann {
35 
44 template<
45  typename OutputLayerType = NegativeLogLikelihood<>,
46  typename InitializationRuleType = RandomInitialization,
47  typename... CustomLayers
48 >
49 class FFN
50 {
51  public:
54 
68  FFN(OutputLayerType outputLayer = OutputLayerType(),
69  InitializationRuleType initializeRule = InitializationRuleType());
70 
72  FFN(const FFN&);
73 
75  FFN(FFN&&);
76 
79 
81  ~FFN();
82 
99  template<typename OptimizerType>
100  void Train(arma::mat predictors,
101  arma::mat responses,
102  OptimizerType& optimizer);
103 
120  template<typename OptimizerType = mlpack::optimization::RMSProp>
121  void Train(arma::mat predictors, arma::mat responses);
122 
134  void Predict(arma::mat predictors, arma::mat& results);
135 
144  double Evaluate(const arma::mat& parameters);
145 
159  double Evaluate(const arma::mat& parameters,
160  const size_t begin,
161  const size_t batchSize,
162  const bool deterministic);
163 
176  double Evaluate(const arma::mat& parameters,
177  const size_t begin,
178  const size_t batchSize)
179  {
180  return Evaluate(parameters, begin, batchSize, true);
181  }
182 
195  void Gradient(const arma::mat& parameters,
196  const size_t begin,
197  arma::mat& gradient,
198  const size_t batchSize);
199 
204  void Shuffle();
205 
206  /*
207  * Add a new module to the model.
208  *
209  * @param args The layer parameter.
210  */
211  template <class LayerType, class... Args>
212  void Add(Args... args) { network.push_back(new LayerType(args...)); }
213 
214  /*
215  * Add a new module to the model.
216  *
217  * @param layer The Layer to be added to the model.
218  */
219  void Add(LayerTypes<CustomLayers...> layer) { network.push_back(layer); }
220 
222  size_t NumFunctions() const { return numFunctions; }
223 
225  const arma::mat& Parameters() const { return parameter; }
227  arma::mat& Parameters() { return parameter; }
228 
230  const arma::mat& Responses() const { return responses; }
232  arma::mat& Responses() { return responses; }
233 
235  const arma::mat& Predictors() const { return predictors; }
237  arma::mat& Predictors() { return predictors; }
238 
242  void ResetParameters();
243 
245  template<typename Archive>
246  void serialize(Archive& ar, const unsigned int /* version */);
247 
258  void Forward(arma::mat inputs, arma::mat& results);
259 
271  double Backward(arma::mat targets, arma::mat& gradients);
272 
273  private:
274  // Helper functions.
281  void Forward(arma::mat&& input);
282 
290  void ResetData(arma::mat predictors, arma::mat responses);
291 
296  void Backward();
297 
302  void Gradient(arma::mat&& input);
303 
308  void ResetDeterministic();
309 
313  void ResetGradients(arma::mat& gradient);
314 
320  void Swap(FFN& network);
321 
323  OutputLayerType outputLayer;
324 
327  InitializationRuleType initializeRule;
328 
330  size_t width;
331 
333  size_t height;
334 
336  bool reset;
337 
339  std::vector<LayerTypes<CustomLayers...> > network;
340 
342  arma::mat predictors;
343 
345  arma::mat responses;
346 
348  arma::mat parameter;
349 
351  size_t numFunctions;
352 
354  arma::mat error;
355 
357  arma::mat currentInput;
358 
360  DeltaVisitor deltaVisitor;
361 
363  OutputParameterVisitor outputParameterVisitor;
364 
366  WeightSizeVisitor weightSizeVisitor;
367 
369  OutputWidthVisitor outputWidthVisitor;
370 
372  OutputHeightVisitor outputHeightVisitor;
373 
375  ResetVisitor resetVisitor;
376 
378  DeleteVisitor deleteVisitor;
379 
381  bool deterministic;
382 
384  arma::mat delta;
385 
387  arma::mat inputParameter;
388 
390  arma::mat outputParameter;
391 
393  arma::mat gradient;
394 
396  CopyVisitor<CustomLayers...> copyVisitor;
397 }; // class FFN
398 
399 } // namespace ann
400 } // namespace mlpack
401 
402 // Include implementation.
403 #include "ffn_impl.hpp"
404 
405 #endif
DeleteVisitor executes the destructor of the instantiated object.
void Gradient(const arma::mat &parameters, const size_t begin, arma::mat &gradient, const size_t batchSize)
Evaluate the gradient of the feedforward network with the given parameters, and with respect to only ...
OutputWidthVisitor exposes the OutputHeight() method of the given module.
arma::mat & Responses()
Modify the matrix of responses to the input data points.
Definition: ffn.hpp:232
void serialize(Archive &ar, const unsigned int)
Serialize the model.
size_t NumFunctions() const
Return the number of separable functions (the number of predictor points).
Definition: ffn.hpp:222
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
double Evaluate(const arma::mat &parameters)
Evaluate the feedforward network with the given parameters.
void Predict(arma::mat predictors, arma::mat &results)
Predict the responses to a given set of predictors.
.hpp
Definition: add_to_po.hpp:21
This visitor is to support copy constructor for neural network module.
void Add(Args... args)
Definition: ffn.hpp:212
const arma::mat & Predictors() const
Get the matrix of data points (predictors).
Definition: ffn.hpp:235
The core includes that mlpack expects; standard C++ includes and Armadillo.
WeightSizeVisitor returns the number of weights of the given module.
void Forward(arma::mat inputs, arma::mat &results)
Perform the forward pass of the data in real batch mode.
void Train(arma::mat predictors, arma::mat responses, OptimizerType &optimizer)
Train the feedforward network on the given input data using the given optimizer.
FFN & operator=(FFN)
Copy/move assignment operator.
void Shuffle()
Shuffle the order of function visitation.
~FFN()
Destructor to release allocated memory.
double Evaluate(const arma::mat &parameters, const size_t begin, const size_t batchSize)
Evaluate the feedforward network with the given parameters, but using only one data point...
Definition: ffn.hpp:176
const arma::mat & Responses() const
Get the matrix of responses to the input data points.
Definition: ffn.hpp:230
ResetVisitor executes the Reset() function.
OutputParameterVisitor exposes the output parameter of the given module.
void Add(LayerTypes< CustomLayers... > layer)
Definition: ffn.hpp:219
arma::mat & Parameters()
Modify the initial point for the optimization.
Definition: ffn.hpp:227
void ResetParameters()
Reset the module infomration (weights/parameters).
arma::mat & Predictors()
Modify the matrix of data points (predictors).
Definition: ffn.hpp:237
const arma::mat & Parameters() const
Return the initial point for the optimization.
Definition: ffn.hpp:225
DeltaVisitor exposes the delta parameter of the given module.
Implementation of a standard feed forward network.
Definition: ffn.hpp:49
OutputWidthVisitor exposes the OutputWidth() method of the given module.
double Backward(arma::mat targets, arma::mat &gradients)
Perform the backward pass of the data in real batch mode.
FFN(OutputLayerType outputLayer=OutputLayerType(), InitializationRuleType initializeRule=InitializationRuleType())
Create the FFN object.