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(const RNN&);
75 
77  RNN(RNN&&);
78 
80  RNN& operator=(const RNN&);
81 
83  RNN& operator=(RNN&&);
84 
86  ~RNN();
87 
97  template<typename OptimizerType>
98  typename std::enable_if<
99  HasMaxIterations<OptimizerType, size_t&(OptimizerType::*)()>
100  ::value, void>::type
101  WarnMessageMaxIterations(OptimizerType& optimizer, size_t samples) const;
102 
111  template<typename OptimizerType>
112  typename std::enable_if<
113  !HasMaxIterations<OptimizerType, size_t&(OptimizerType::*)()>
114  ::value, void>::type
115  WarnMessageMaxIterations(OptimizerType& optimizer, size_t samples) const;
116 
144  template<typename OptimizerType, typename... CallbackTypes>
145  double Train(arma::cube predictors,
146  arma::cube responses,
147  OptimizerType& optimizer,
148  CallbackTypes&&... callbacks);
149 
177  template<typename OptimizerType = ens::StandardSGD, typename... CallbackTypes>
178  double Train(arma::cube predictors,
179  arma::cube responses,
180  CallbackTypes&&... callbacks);
181 
201  void Predict(arma::cube predictors,
202  arma::cube& results,
203  const size_t batchSize = 256);
204 
217  double Evaluate(const arma::mat& parameters,
218  const size_t begin,
219  const size_t batchSize,
220  const bool deterministic);
221 
233  double Evaluate(const arma::mat& parameters,
234  const size_t begin,
235  const size_t batchSize);
236 
248  template<typename GradType>
249  double EvaluateWithGradient(const arma::mat& parameters,
250  const size_t begin,
251  GradType& gradient,
252  const size_t batchSize);
253 
267  void Gradient(const arma::mat& parameters,
268  const size_t begin,
269  arma::mat& gradient,
270  const size_t batchSize);
271 
276  void Shuffle();
277 
278  /*
279  * Add a new module to the model.
280  *
281  * @param args The layer parameter.
282  */
283  template <class LayerType, class... Args>
284  void Add(Args... args) { network.push_back(new LayerType(args...)); }
285 
286  /*
287  * Add a new module to the model.
288  *
289  * @param layer The Layer to be added to the model.
290  */
291  void Add(LayerTypes<CustomLayers...> layer) { network.push_back(layer); }
292 
294  size_t NumFunctions() const { return numFunctions; }
295 
297  const arma::mat& Parameters() const { return parameter; }
299  arma::mat& Parameters() { return parameter; }
300 
302  const size_t& Rho() const { return rho; }
304  size_t& Rho() { return rho; }
305 
307  const arma::cube& Responses() const { return responses; }
309  arma::cube& Responses() { return responses; }
310 
312  const arma::cube& Predictors() const { return predictors; }
314  arma::cube& Predictors() { return predictors; }
315 
321  void Reset();
322 
326  void ResetParameters();
327 
329  template<typename Archive>
330  void serialize(Archive& ar, const uint32_t /* version */);
331 
332  private:
333  // Helper functions.
340  template<typename InputType>
341  void Forward(const InputType& input);
342 
346  void ResetCells();
347 
352  void Backward();
353 
358  template<typename InputType>
359  void Gradient(const InputType& input);
360 
365  void ResetDeterministic();
366 
370  void ResetGradients(arma::mat& gradient);
371 
373  size_t rho;
374 
376  OutputLayerType outputLayer;
377 
380  InitializationRuleType initializeRule;
381 
383  size_t inputSize;
384 
386  size_t outputSize;
387 
389  size_t targetSize;
390 
392  bool reset;
393 
395  bool single;
396 
398  std::vector<LayerTypes<CustomLayers...> > network;
399 
401  arma::cube predictors;
402 
404  arma::cube responses;
405 
407  arma::mat parameter;
408 
410  size_t numFunctions;
411 
413  arma::mat error;
414 
416  DeltaVisitor deltaVisitor;
417 
419  OutputParameterVisitor outputParameterVisitor;
420 
422  std::vector<arma::mat> moduleOutputParameter;
423 
425  WeightSizeVisitor weightSizeVisitor;
426 
428  CopyVisitor<CustomLayers...> copyVisitor;
429 
431  ResetVisitor resetVisitor;
432 
434  DeleteVisitor deleteVisitor;
435 
437  bool deterministic;
438 
440  arma::mat currentGradient;
441 
442  // The BRN class should have access to internal members.
443  template<
444  typename OutputLayerType1,
445  typename MergeLayerType1,
446  typename MergeOutputType1,
447  typename InitializationRuleType1,
448  typename... CustomLayers1
449  >
450  friend class BRNN;
451 }; // class RNN
452 
453 } // namespace ann
454 } // namespace mlpack
455 
456 // Include implementation.
457 #include "rnn_impl.hpp"
458 
459 #endif
DeleteVisitor executes the destructor of the instantiated object.
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).
void serialize(Archive &ar, const uint32_t)
Serialize the model.
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.
void Predict(arma::cube predictors, arma::cube &results, const size_t batchSize=256)
Predict the responses to a given set of predictors.
This visitor is to support copy constructor for neural network module.
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:304
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:297
arma::mat & Parameters()
Modify the initial point for the optimization.
Definition: rnn.hpp:299
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
RNN & operator=(const RNN &)
Copy assignment operator.
arma::cube & Predictors()
Modify the matrix of data points (predictors).
Definition: rnn.hpp:314
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:312
void Add(Args... args)
Definition: rnn.hpp:284
size_t NumFunctions() const
Return the number of separable functions (the number of predictor points).
Definition: rnn.hpp:294
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:302
void Add(LayerTypes< CustomLayers... > layer)
Definition: rnn.hpp:291
arma::cube & Responses()
Modify the matrix of responses to the input data points.
Definition: rnn.hpp:309
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 > *, 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 > *, Sequential< arma::mat, arma::mat, false > *, Sequential< arma::mat, arma::mat, true > *, 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()
Destructor to release allocated memory.
const arma::cube & Responses() const
Get the matrix of responses to the input data points.
Definition: rnn.hpp:307
void Shuffle()
Shuffle the order of function visitation.