GestureRecognitionToolkit  Version: 0.2.5
The Gesture Recognition Toolkit (GRT) is a cross-platform, open-source, c++ machine learning library for real-time gesture recognition.
MLP.h
Go to the documentation of this file.
1 
27 #ifndef GRT_MLP_HEADER
28 #define GRT_MLP_HEADER
29 
30 #include "Neuron.h"
31 #include "../../../CoreModules/Regressifier.h"
32 
33 GRT_BEGIN_NAMESPACE
34 
42 class GRT_API MLP : public Regressifier{
43 public:
44  enum TrainingAlgorithm{ONLINE_GRADIENT_DESCENT=0};
45 
49  MLP();
50 
56  MLP(const MLP &rhs);
57 
61  virtual ~MLP();
62 
69  MLP &operator=(const MLP &rhs);
70 
78  virtual bool deepCopyFrom(const Regressifier *regressifier);
79 
86  virtual bool train_(ClassificationData &trainingData);
87 
94  virtual bool train_(RegressionData &trainingData);
95 
103  virtual bool predict_(VectorFloat &inputVector);
104 
110  virtual bool clear();
111 
117  virtual bool print() const;
118 
126  virtual bool save( std::fstream &file ) const;
127 
135  virtual bool load( std::fstream &file );
136 
144  UINT getNumClasses() const;
145 
161  bool init(const UINT numInputNeurons, const UINT numHiddenNeurons, const UINT numOutputNeurons);
162 
180  bool init(const UINT numInputNeurons, const UINT numHiddenNeurons, const UINT numOutputNeurons,
181  const Neuron::Type inputLayerActivationFunction,
182  const Neuron::Type hiddenLayerActivationFunction,
183  const Neuron::Type outputLayerActivationFunction);
184 
189  void printNetwork() const;
190 
196  bool checkForNAN() const;
197 
204  std::string activationFunctionToString(const Neuron::Type activationFunction) const;
205 
212  Neuron::Type activationFunctionFromString(const std::string activationName) const;
213 
220  bool validateActivationFunction(const Neuron::Type avactivationFunction) const;
221 
227  UINT getNumInputNeurons() const;
228 
234  UINT getNumHiddenNeurons() const;
235 
241  UINT getNumOutputNeurons() const;
242 
251  UINT getNumRandomTrainingIterations() const;
252 
258  Neuron::Type getInputLayerActivationFunction() const;
259 
265  Neuron::Type getHiddenLayerActivationFunction() const;
266 
272  Neuron::Type getOutputLayerActivationFunction() const;
273 
279  Float getTrainingRate() const;
280 
286  Float getMomentum() const;
287 
293  Float getGamma() const;
294 
301  Float getTrainingError() const;
302 
308  bool getClassificationModeActive() const;
309 
315  bool getRegressionModeActive() const;
316 
322  Vector< Neuron > getInputLayer() const;
323 
329  Vector< Neuron > getHiddenLayer() const;
330 
336  Vector< Neuron > getOutputLayer() const;
337 
344  Vector< VectorFloat > getTrainingLog() const;
345 
351  bool getNullRejectionEnabled() const;
352 
360  Float getNullRejectionCoeff() const;
361 
369  Float getNullRejectionThreshold() const;
370 
379  Float getMaximumLikelihood() const;
380 
386  VectorFloat getClassLikelihoods() const;
387 
393  VectorFloat getClassDistances() const;
394 
400  UINT getPredictedClassLabel() const;
401 
409  bool setInputLayerActivationFunction(const Neuron::Type activationFunction);
410 
418  bool setHiddenLayerActivationFunction(const Neuron::Type activationFunction);
419 
427  bool setOutputLayerActivationFunction(const Neuron::Type activationFunction);
428 
437  bool setTrainingRate(const Float trainingRate);
438 
446  bool setMomentum(const Float momentum);
447 
455  bool setGamma(const Float gamma);
456 
468  GRT_DEPRECATED_MSG( "setNumRandomTrainingIterations() is deprecated, use setNumRestarts() instead",bool setNumRandomTrainingIterations(const UINT numRandomTrainingIterations));
469 
476  bool setNullRejection(const bool useNullRejection);
477 
487  bool setNullRejectionCoeff(const Float nullRejectionCoeff);
488 
494  static std::string getId();
495 
496  //Tell the compiler we are using the following functions from the MLBase class to stop hidden virtual function warnings
497  using MLBase::save;
498  using MLBase::load;
499  using MLBase::train;
500  using MLBase::train_;
501  using MLBase::predict;
502  using MLBase::predict_;
503 
504 protected:
505  bool inline isNAN(const Float &v) const;
506 
507  bool setOutputTargets();
508 
509  bool trainModel(RegressionData &trainingData);
510 
511  bool trainOnlineGradientDescentClassification(const RegressionData &trainingData,const RegressionData &validationData);
512 
513  bool trainOnlineGradientDescentRegression(const RegressionData &trainingData,const RegressionData &validationData);
514 
515  bool loadLegacyModelFromFile( std::fstream &file );
516 
526  Float back_prop(const VectorFloat &inputVector,const VectorFloat &targetVector,const Float alpha,const Float beta);
527 
534  VectorFloat feedforward(VectorFloat data);
535 
544  void feedforward(const VectorFloat &data,VectorFloat &inputNeuronsOutput,VectorFloat &hiddenNeuronsOutput,VectorFloat &outputNeuronsOutput);
545 
546  UINT numInputNeurons;
547  UINT numHiddenNeurons;
548  UINT numOutputNeurons;
549  Neuron::Type inputLayerActivationFunction;
550  Neuron::Type hiddenLayerActivationFunction;
551  Neuron::Type outputLayerActivationFunction;
552  UINT trainingMode;
553  Float momentum;
554  Float gamma;
555  Float trainingError;
556  MinMax outputTargets;
557  bool initialized;
558 
559  Vector< Neuron > inputLayer;
560  Vector< Neuron > hiddenLayer;
561  Vector< Neuron > outputLayer;
562  Vector< VectorFloat > trainingErrorLog;
563 
564  //Classifier Variables
565  bool classificationModeActive;
566  bool useNullRejection;
567  UINT predictedClassLabel;
568  Float nullRejectionThreshold;
569  Float nullRejectionCoeff;
570  Float maxLikelihood;
571  VectorFloat classLikelihoods;
572 
573  //Temp Training Stuff
574  VectorFloat inputNeuronsOutput;
575  VectorFloat hiddenNeuronsOutput;
576  VectorFloat outputNeuronsOutput;
577  VectorFloat deltaO;
578  VectorFloat deltaH;
579 
580 private:
581  static RegisterRegressifierModule< MLP > registerModule;
582  static const std::string id;
583 
584 };
585 
586 GRT_END_NAMESPACE
587 
588 #endif //GRT_MLP_HEADER
std::string getId() const
Definition: GRTBase.cpp:85
virtual bool predict(VectorFloat inputVector)
Definition: MLBase.cpp:135
This class implements a Neuron that is used by the Multilayer Perceptron.
virtual bool predict_(VectorFloat &inputVector)
Definition: MLBase.cpp:137
virtual bool clear() override
virtual bool train(ClassificationData trainingData)
Definition: MLBase.cpp:107
virtual bool save(const std::string &filename) const
Definition: MLBase.cpp:167
virtual bool deepCopyFrom(const Regressifier *regressifier)
Definition: Regressifier.h:64
virtual bool print() const
Definition: MLBase.cpp:165
Definition: MinMax.h:29
virtual bool train_(ClassificationData &trainingData)
Definition: MLBase.cpp:109
virtual bool load(const std::string &filename)
Definition: MLBase.cpp:190
Definition: MLP.h:42