mlpack 3.4.2
brnn.hpp
Go to the documentation of this file.
1
13#ifndef MLPACK_METHODS_ANN_BRNN_HPP
14#define MLPACK_METHODS_ANN_BRNN_HPP
15
16#include <mlpack/prereqs.hpp>
17
23
29
30#include <ensmallen.hpp>
31
32namespace mlpack {
33namespace ann {
34
41template<
42 typename OutputLayerType = NegativeLogLikelihood<>,
43 typename MergeLayerType = Concat<>,
44 typename MergeOutputType = LogSoftMax<>,
45 typename InitializationRuleType = RandomInitialization,
46 typename... CustomLayers
47>
48class BRNN
49{
50 public:
52 using NetworkType = BRNN<OutputLayerType,
53 MergeLayerType,
54 MergeOutputType,
55 InitializationRuleType,
56 CustomLayers...>;
57
75 BRNN(const size_t rho,
76 const bool single = false,
77 OutputLayerType outputLayer = OutputLayerType(),
78 MergeLayerType* mergeLayer = new MergeLayerType(),
79 MergeOutputType* mergeOutput = new MergeOutputType(),
80 InitializationRuleType initializeRule = InitializationRuleType());
81
83
93 template<typename OptimizerType>
94 typename std::enable_if<
95 HasMaxIterations<OptimizerType, size_t&(OptimizerType::*)()>
96 ::value, void>::type
97 WarnMessageMaxIterations(OptimizerType& optimizer, size_t samples) const;
98
107 template<typename OptimizerType>
108 typename std::enable_if<
109 !HasMaxIterations<OptimizerType, size_t&(OptimizerType::*)()>
110 ::value, void>::type
111 WarnMessageMaxIterations(OptimizerType& optimizer, size_t samples) const;
112
136 template<typename OptimizerType>
137 double Train(arma::cube predictors,
138 arma::cube responses,
139 OptimizerType& optimizer);
140
164 template<typename OptimizerType = ens::StandardSGD>
165 double Train(arma::cube predictors, arma::cube responses);
166
186 void Predict(arma::cube predictors,
187 arma::cube& results,
188 const size_t batchSize = 256);
189
203 double Evaluate(const arma::mat& parameters,
204 const size_t begin,
205 const size_t batchSize,
206 const bool deterministic);
207
220 double Evaluate(const arma::mat& parameters,
221 const size_t begin,
222 const size_t batchSize);
223
237 template<typename GradType>
238 double EvaluateWithGradient(const arma::mat& parameters,
239 const size_t begin,
240 GradType& gradient,
241 const size_t batchSize);
242
256 void Gradient(const arma::mat& parameters,
257 const size_t begin,
258 arma::mat& gradient,
259 const size_t batchSize);
260
265 void Shuffle();
266
267 /*
268 * Add a new module to the model.
269 *
270 * @param args The layer parameter.
271 */
272 template <class LayerType, class... Args>
273 void Add(Args... args);
274
275 /*
276 * Add a new module to the model.
277 *
278 * @param layer The Layer to be added to the model.
279 */
281
283 size_t NumFunctions() const { return numFunctions; }
284
286 const arma::mat& Parameters() const { return parameter; }
288 arma::mat& Parameters() { return parameter; }
289
291 const size_t& Rho() const { return rho; }
293 size_t& Rho() { return rho; }
294
296 const arma::cube& Responses() const { return responses; }
298 arma::cube& Responses() { return responses; }
299
301 const arma::cube& Predictors() const { return predictors; }
303 arma::cube& Predictors() { return predictors; }
304
310 void Reset();
311
316
318 template<typename Archive>
319 void serialize(Archive& ar, const unsigned int /* version */);
320
321 private:
322 // Helper functions.
327 void ResetDeterministic();
328
330 size_t rho;
331
333 OutputLayerType outputLayer;
334
336 LayerTypes<CustomLayers...> mergeLayer;
337
339 LayerTypes<CustomLayers...> mergeOutput;
340
343 InitializationRuleType initializeRule;
344
346 size_t inputSize;
347
349 size_t outputSize;
350
352 size_t targetSize;
353
355 bool reset;
356
358 bool single;
359
361 arma::cube predictors;
362
364 arma::cube responses;
365
367 arma::mat parameter;
368
370 size_t numFunctions;
371
373 arma::mat error;
374
376 DeltaVisitor deltaVisitor;
377
379 OutputParameterVisitor outputParameterVisitor;
380
382 std::vector<arma::mat> forwardRNNOutputParameter;
383
385 std::vector<arma::mat> backwardRNNOutputParameter;
386
388 WeightSizeVisitor weightSizeVisitor;
389
391 ResetVisitor resetVisitor;
392
394 DeleteVisitor deleteVisitor;
395
397 CopyVisitor<CustomLayers...> copyVisitor;
398
400 bool deterministic;
401
403 arma::mat forwardGradient;
404
406 arma::mat backwardGradient;
407
409 arma::mat totalGradient;
410
412 RNN<OutputLayerType, InitializationRuleType, CustomLayers...> forwardRNN;
413
415 RNN<OutputLayerType, InitializationRuleType, CustomLayers...> backwardRNN;
416}; // class BRNN
417
418} // namespace ann
419} // namespace mlpack
420
422namespace boost {
423namespace serialization {
424
425template<typename OutputLayerType,
426 typename InitializationRuleType,
427 typename MergeLayerType,
428 typename MergeOutputType,
429 typename... CustomLayer>
430struct version<
431 mlpack::ann::BRNN<OutputLayerType, MergeLayerType, MergeOutputType,
432 InitializationRuleType, CustomLayer...>>
433{
434 BOOST_STATIC_CONSTANT(int, value = 1);
435};
436
437} // namespace serialization
438} // namespace boost
439
440// Include implementation.
441#include "brnn_impl.hpp"
442
443#endif
Implementation of a standard bidirectional recurrent neural network container.
Definition: brnn.hpp:49
void Predict(arma::cube predictors, arma::cube &results, const size_t batchSize=256)
Predict the responses to a given set of predictors.
arma::mat & Parameters()
Modify the initial point for the optimization.
Definition: brnn.hpp:288
size_t & Rho()
Modify the maximum length of backpropagation through time.
Definition: brnn.hpp:293
const arma::mat & Parameters() const
Return the initial point for the optimization.
Definition: brnn.hpp:286
size_t NumFunctions() const
Return the number of separable functions. (number of predictor points).
Definition: brnn.hpp:283
void Shuffle()
Shuffle the order of function visitation.
void Reset()
Reset the state of the network.
double EvaluateWithGradient(const arma::mat &parameters, const size_t begin, GradType &gradient, const size_t batchSize)
Evaluate the bidirectional recurrent neural network with the given parameters.
double Evaluate(const arma::mat &parameters, const size_t begin, const size_t batchSize, const bool deterministic)
Evaluate the bidirectional recurrent neural network with the given parameters.
double Train(arma::cube predictors, arma::cube responses, OptimizerType &optimizer)
Train the bidirectional recurrent neural network on the given input data using the given optimizer.
void Add(LayerTypes< CustomLayers... > layer)
arma::cube & Predictors()
Modify the matrix of data points (predictors).
Definition: brnn.hpp:303
void ResetParameters()
Reset the module information (weights/parameters).
const arma::cube & Predictors() const
Get the matrix of data points (predictors).
Definition: brnn.hpp:301
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 doesn't then simply return from the funct...
double Train(arma::cube predictors, arma::cube responses)
Train the bidirectional recurrent neural network on the given input data.
double Evaluate(const arma::mat &parameters, const size_t begin, const size_t batchSize)
Evaluate the bidirectional recurrent neural network with the given parameters.
void Add(Args... args)
const arma::cube & Responses() const
Get the matrix of responses to the input data points.
Definition: brnn.hpp:296
void Gradient(const arma::mat &parameters, const size_t begin, arma::mat &gradient, const size_t batchSize)
Evaluate the gradient of the bidirectional recurrent neural network with the given parameters,...
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's value is less tha...
arma::cube & Responses()
Modify the matrix of responses to the input data points.
Definition: brnn.hpp:298
BRNN(const size_t rho, const bool single=false, OutputLayerType outputLayer=OutputLayerType(), MergeLayerType *mergeLayer=new MergeLayerType(), MergeOutputType *mergeOutput=new MergeOutputType(), InitializationRuleType initializeRule=InitializationRuleType())
Create the BRNN object.
const size_t & Rho() const
Return the maximum length of backpropagation through time.
Definition: brnn.hpp:291
void serialize(Archive &ar, const unsigned int)
Serialize the model.
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.
OutputParameterVisitor exposes the output parameter of the given module.
Implementation of a standard recurrent neural network container.
Definition: rnn.hpp:46
ResetVisitor executes the Reset() function.
WeightSizeVisitor returns the number of weights of the given module.
Set the serialization version of the adaboost class.
Definition: adaboost.hpp:198
BaseLayer< ActivationFunction, InputDataType, OutputDataType > CustomLayer
Standard Sigmoid layer.
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.