rnn.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_METHODS_ANN_RNN_HPP
13 #define MLPACK_METHODS_ANN_RNN_HPP
14 
15 #include <mlpack/prereqs.hpp>
16 
21 
23 
28 
29 #include <ensmallen.hpp>
30 
31 namespace mlpack {
32 namespace ann {
33 
40 template<
41  typename OutputLayerType = NegativeLogLikelihood<>,
42  typename InitializationRuleType = RandomInitialization,
43  typename... CustomLayers
44 >
45 class RNN
46 {
47  public:
49  using NetworkType = RNN<OutputLayerType,
50  InitializationRuleType,
51  CustomLayers...>;
52 
68  RNN(const size_t rho,
69  const bool single = false,
70  OutputLayerType outputLayer = OutputLayerType(),
71  InitializationRuleType initializeRule = InitializationRuleType());
72 
74  ~RNN();
75 
85  template<typename OptimizerType>
86  typename std::enable_if<
87  HasMaxIterations<OptimizerType, size_t&(OptimizerType::*)()>
88  ::value, void>::type
89  WarnMessageMaxIterations(OptimizerType& optimizer, size_t samples) const;
90 
99  template<typename OptimizerType>
100  typename std::enable_if<
101  !HasMaxIterations<OptimizerType, size_t&(OptimizerType::*)()>
102  ::value, void>::type
103  WarnMessageMaxIterations(OptimizerType& optimizer, size_t samples) const;
104 
132  template<typename OptimizerType, typename... CallbackTypes>
133  double Train(arma::cube predictors,
134  arma::cube responses,
135  OptimizerType& optimizer,
136  CallbackTypes&&... callbacks);
137 
165  template<typename OptimizerType = ens::StandardSGD, typename... CallbackTypes>
166  double Train(arma::cube predictors,
167  arma::cube responses,
168  CallbackTypes&&... callbacks);
169 
189  void Predict(arma::cube predictors,
190  arma::cube& results,
191  const size_t batchSize = 256);
192 
205  double Evaluate(const arma::mat& parameters,
206  const size_t begin,
207  const size_t batchSize,
208  const bool deterministic);
209 
221  double Evaluate(const arma::mat& parameters,
222  const size_t begin,
223  const size_t batchSize);
224 
236  template<typename GradType>
237  double EvaluateWithGradient(const arma::mat& parameters,
238  const size_t begin,
239  GradType& gradient,
240  const size_t batchSize);
241 
255  void Gradient(const arma::mat& parameters,
256  const size_t begin,
257  arma::mat& gradient,
258  const size_t batchSize);
259 
264  void Shuffle();
265 
266  /*
267  * Add a new module to the model.
268  *
269  * @param args The layer parameter.
270  */
271  template <class LayerType, class... Args>
272  void Add(Args... args) { network.push_back(new LayerType(args...)); }
273 
274  /*
275  * Add a new module to the model.
276  *
277  * @param layer The Layer to be added to the model.
278  */
279  void Add(LayerTypes<CustomLayers...> layer) { network.push_back(layer); }
280 
282  size_t NumFunctions() const { return numFunctions; }
283 
285  const arma::mat& Parameters() const { return parameter; }
287  arma::mat& Parameters() { return parameter; }
288 
290  const size_t& Rho() const { return rho; }
292  size_t& Rho() { return rho; }
293 
295  const arma::cube& Responses() const { return responses; }
297  arma::cube& Responses() { return responses; }
298 
300  const arma::cube& Predictors() const { return predictors; }
302  arma::cube& Predictors() { return predictors; }
303 
309  void Reset();
310 
314  void ResetParameters();
315 
317  template<typename Archive>
318  void serialize(Archive& ar, const unsigned int /* version */);
319 
320  private:
321  // Helper functions.
328  template<typename InputType>
329  void Forward(const InputType& input);
330 
334  void ResetCells();
335 
340  void Backward();
341 
346  template<typename InputType>
347  void Gradient(const InputType& input);
348 
353  void ResetDeterministic();
354 
358  void ResetGradients(arma::mat& gradient);
359 
361  size_t rho;
362 
364  OutputLayerType outputLayer;
365 
368  InitializationRuleType initializeRule;
369 
371  size_t inputSize;
372 
374  size_t outputSize;
375 
377  size_t targetSize;
378 
380  bool reset;
381 
383  bool single;
384 
386  std::vector<LayerTypes<CustomLayers...> > network;
387 
389  arma::cube predictors;
390 
392  arma::cube responses;
393 
395  arma::mat parameter;
396 
398  size_t numFunctions;
399 
401  arma::mat error;
402 
404  DeltaVisitor deltaVisitor;
405 
407  OutputParameterVisitor outputParameterVisitor;
408 
410  std::vector<arma::mat> moduleOutputParameter;
411 
413  WeightSizeVisitor weightSizeVisitor;
414 
416  ResetVisitor resetVisitor;
417 
419  DeleteVisitor deleteVisitor;
420 
422  bool deterministic;
423 
425  arma::mat currentGradient;
426 
427  // The BRN class should have access to internal members.
428  template<
429  typename OutputLayerType1,
430  typename MergeLayerType1,
431  typename MergeOutputType1,
432  typename InitializationRuleType1,
433  typename... CustomLayers1
434  >
435  friend class BRNN;
436 }; // class RNN
437 
438 } // namespace ann
439 } // namespace mlpack
440 
443 namespace boost {
444 namespace serialization {
445 
446 template<typename OutputLayerType,
447  typename InitializationRuleType,
448  typename... CustomLayer>
449 struct version<
450  mlpack::ann::RNN<OutputLayerType, InitializationRuleType, CustomLayer...>>
451 {
452  BOOST_STATIC_CONSTANT(int, value = 1);
453 };
454 
455 } // namespace serialization
456 } // namespace boost
457 
458 // Include implementation.
459 #include "rnn_impl.hpp"
460 
461 #endif
DeleteVisitor executes the destructor of the instantiated object.
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 > *, TransposedConvolution< NaiveConvolution< ValidConvolution >, NaiveConvolution< ValidConvolution >, NaiveConvolution< ValidConvolution >, arma::mat, arma::mat > *, WeightNorm< arma::mat, arma::mat > *, MoreTypes, CustomLayers *... > LayerTypes
RNN(const size_t rho, const bool single=false, OutputLayerType outputLayer=OutputLayerType(), InitializationRuleType initializeRule=InitializationRuleType())
Create the RNN object.
void ResetParameters()
Reset the module information (weights/parameters).
Set the serialization version of the adaboost class.
Definition: adaboost.hpp:198
double Train(arma::cube predictors, arma::cube responses, OptimizerType &optimizer, CallbackTypes &&... callbacks)
Train the recurrent neural network on the given input data using the given optimizer.
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: add_to_po.hpp:21
void Predict(arma::cube predictors, arma::cube &results, const size_t batchSize=256)
Predict the responses to a given set of predictors.
void serialize(Archive &ar, const unsigned int)
Serialize the model.
The core includes that mlpack expects; standard C++ includes and Armadillo.
void Reset()
Reset the state of the network.
size_t & Rho()
Modify the maximum length of backpropagation through time.
Definition: rnn.hpp:292
WeightSizeVisitor returns the number of weights of the given module.
const arma::mat & Parameters() const
Return the initial point for the optimization.
Definition: rnn.hpp:285
arma::mat & Parameters()
Modify the initial point for the optimization.
Definition: rnn.hpp:287
double Evaluate(const arma::mat &parameters, const size_t begin, const size_t batchSize, const bool deterministic)
Evaluate the recurrent neural network with the given parameters.
Implementation of a standard recurrent neural network container.
Definition: rnn.hpp:45
Implementation of the base layer.
Definition: base_layer.hpp:65
arma::cube & Predictors()
Modify the matrix of data points (predictors).
Definition: rnn.hpp:302
ResetVisitor executes the Reset() function.
OutputParameterVisitor exposes the output parameter of the given module.
const arma::cube & Predictors() const
Get the matrix of data points (predictors).
Definition: rnn.hpp:300
void Add(Args... args)
Definition: rnn.hpp:272
size_t NumFunctions() const
Return the number of separable functions (the number of predictor points).
Definition: rnn.hpp:282
void Gradient(const arma::mat &parameters, const size_t begin, arma::mat &gradient, const size_t batchSize)
Evaluate the gradient of the recurrent neural network with the given parameters, and with respect to ...
Implementation of a standard bidirectional recurrent neural network container.
Definition: brnn.hpp:48
std::enable_if< HasMaxIterations< OptimizerType, size_t &(OptimizerType::*)()>::value, void >::type WarnMessageMaxIterations(OptimizerType &optimizer, size_t samples) const
Check if the optimizer has MaxIterations() parameter, if it does then check if it&#39;s value is less tha...
double EvaluateWithGradient(const arma::mat &parameters, const size_t begin, GradType &gradient, const size_t batchSize)
Evaluate the recurrent neural network with the given parameters.
DeltaVisitor exposes the delta parameter of the given module.
const size_t & Rho() const
Return the maximum length of backpropagation through time.
Definition: rnn.hpp:290
void Add(LayerTypes< CustomLayers... > layer)
Definition: rnn.hpp:279
arma::cube & Responses()
Modify the matrix of responses to the input data points.
Definition: rnn.hpp:297
~RNN()
Destructor to release allocated memory.
const arma::cube & Responses() const
Get the matrix of responses to the input data points.
Definition: rnn.hpp:295
void Shuffle()
Shuffle the order of function visitation.