GestureRecognitionToolkit  Version: 0.1.0
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 
33 #ifndef GRT_MLP_HEADER
34 #define GRT_MLP_HEADER
35 
36 #include "Neuron.h"
37 #include "../../../CoreModules/Regressifier.h"
38 
39 GRT_BEGIN_NAMESPACE
40 
41 class MLP : public Regressifier{
42 public:
46  MLP();
47 
53  MLP(const MLP &rhs);
54 
58  virtual ~MLP();
59 
66  MLP &operator=(const MLP &rhs);
67 
75  virtual bool deepCopyFrom(const Regressifier *regressifier);
76 
83  virtual bool train_(ClassificationData &trainingData);
84 
91  virtual bool train_(RegressionData &trainingData);
92 
100  virtual bool predict_(VectorFloat &inputVector);
101 
107  virtual bool clear();
108 
114  virtual bool print() const;
115 
123  virtual bool saveModelToFile( std::fstream &file ) const;
124 
132  virtual bool loadModelFromFile( std::fstream &file );
133 
141  UINT getNumClasses() const;
142 
158  bool init(const UINT numInputNeurons, const UINT numHiddenNeurons, const UINT numOutputNeurons);
159 
177  bool init(const UINT numInputNeurons, const UINT numHiddenNeurons, const UINT numOutputNeurons, const UINT inputLayerActivationFunction,
178  const UINT hiddenLayerActivationFunction, const UINT outputLayerActivationFunction);
179 
184  void printNetwork() const;
185 
191  bool checkForNAN() const;
192 
199  std::string activationFunctionToString(const UINT activationFunction) const;
200 
207  UINT activationFunctionFromString(const std::string activationName) const;
208 
215  bool validateActivationFunction(const UINT avactivationFunction) const;
216 
222  UINT getNumInputNeurons() const;
223 
229  UINT getNumHiddenNeurons() const;
230 
236  UINT getNumOutputNeurons() const;
237 
243  UINT getInputLayerActivationFunction() const;
244 
251 
258 
267  UINT getNumRandomTrainingIterations() const;
268 
274  Float getTrainingRate() const;
275 
281  Float getMomentum() const;
282 
288  Float getGamma() const;
289 
296  Float getTrainingError() const;
297 
303  bool getClassificationModeActive() const;
304 
310  bool getRegressionModeActive() const;
311 
318 
325 
332 
340 
346  bool getNullRejectionEnabled() const;
347 
355  Float getNullRejectionCoeff() const;
356 
364  Float getNullRejectionThreshold() const;
365 
374  Float getMaximumLikelihood() const;
375 
382 
389 
395  UINT getPredictedClassLabel() const;
396 
404  bool setInputLayerActivationFunction(const UINT activationFunction);
405 
413  bool setHiddenLayerActivationFunction(const UINT activationFunction);
414 
422  bool setOutputLayerActivationFunction(const UINT activationFunction);
423 
432  bool setTrainingRate(const Float trainingRate);
433 
441  bool setMomentum(const Float momentum);
442 
450  bool setGamma(const Float gamma);
451 
462  bool setNumRandomTrainingIterations(const UINT numRandomTrainingIterations);
463 
470  bool setNullRejection(const bool useNullRejection);
471 
481  bool setNullRejectionCoeff(const Float nullRejectionCoeff);
482 
483  //Tell the compiler we are using the following functions from the MLBase class to stop hidden virtual function warnings
486  using MLBase::train;
487  using MLBase::train_;
488  using MLBase::predict;
489  using MLBase::predict_;
490 
491 protected:
492  bool inline isNAN(const Float v) const;
493 
494  bool trainModel(RegressionData &trainingData);
495 
496  bool trainOnlineGradientDescentClassification(const RegressionData &trainingData,const RegressionData &validationData);
497 
498  bool trainOnlineGradientDescentRegression(const RegressionData &trainingData,const RegressionData &validationData);
499 
500  bool loadLegacyModelFromFile( std::fstream &file );
501 
511  Float back_prop(const VectorFloat &inputVector,const VectorFloat &targetVector,const Float alpha,const Float beta);
512 
520 
529  void feedforward(const VectorFloat &data,VectorFloat &inputNeuronsOuput,VectorFloat &hiddenNeuronsOutput,VectorFloat &outputNeuronsOutput);
530 
531  UINT numInputNeurons;
532  UINT numHiddenNeurons;
533  UINT numOutputNeurons;
534  UINT inputLayerActivationFunction;
535  UINT hiddenLayerActivationFunction;
536  UINT outputLayerActivationFunction;
537  UINT numRandomTrainingIterations;
538  UINT trainingMode;
539  Float momentum;
540  Float gamma;
541  Float trainingError;
542  bool initialized;
543  Random random;
544 
545  Vector< Neuron > inputLayer;
546  Vector< Neuron > hiddenLayer;
547  Vector< Neuron > outputLayer;
548  Vector< VectorFloat > trainingErrorLog;
549 
550  //Classifier Variables
551  bool classificationModeActive;
552  bool useNullRejection;
553  UINT predictedClassLabel;
554  Float nullRejectionThreshold;
555  Float nullRejectionCoeff;
556  Float maxLikelihood;
557  VectorFloat classLikelihoods;
558 
559  static RegisterRegressifierModule< MLP > registerModule;
560 
561  //Temp Training Stuff
562  VectorFloat inputNeuronsOuput;
563  VectorFloat hiddenNeuronsOutput;
564  VectorFloat outputNeuronsOutput;
565  VectorFloat deltaO;
566  VectorFloat deltaH;
567 
568 public:
569  enum TrainingModes{ONLINE_GRADIENT_DESCENT};
570 
571 };
572 
573 GRT_END_NAMESPACE
574 
575 #endif //GRT_MLP_HEADER
UINT getNumHiddenNeurons() const
Definition: MLP.cpp:1498
virtual bool predict(VectorFloat inputVector)
Definition: MLBase.cpp:112
MLP & operator=(const MLP &rhs)
Definition: MLP.cpp:75
VectorFloat feedforward(VectorFloat data)
Definition: MLP.cpp:939
VectorFloat getClassDistances() const
Definition: MLP.cpp:1584
bool setTrainingRate(const Float trainingRate)
Definition: MLP.cpp:1686
Float getGamma() const
Definition: MLP.cpp:1530
virtual ~MLP()
Definition: MLP.cpp:71
This class implements a Neuron that is used by the Multilayer Perceptron.
virtual bool predict_(VectorFloat &inputVector)
Definition: MLBase.cpp:114
bool getNullRejectionEnabled() const
Definition: MLP.cpp:1562
Definition: Random.h:40
bool validateActivationFunction(const UINT avactivationFunction) const
Definition: MLP.cpp:1634
Float back_prop(const VectorFloat &inputVector, const VectorFloat &targetVector, const Float alpha, const Float beta)
Definition: MLP.cpp:851
virtual bool train(ClassificationData trainingData)
Definition: MLBase.cpp:88
UINT getInputLayerActivationFunction() const
Definition: MLP.cpp:1506
Vector< Neuron > getHiddenLayer() const
Definition: MLP.cpp:1550
bool init(const UINT numInputNeurons, const UINT numHiddenNeurons, const UINT numOutputNeurons)
Definition: MLP.cpp:222
std::string activationFunctionToString(const UINT activationFunction) const
Definition: MLP.cpp:1595
Vector< VectorFloat > getTrainingLog() const
Definition: MLP.cpp:1558
UINT getNumRandomTrainingIterations() const
Definition: MLP.cpp:1518
bool checkForNAN() const
Definition: MLP.cpp:1037
virtual bool train_(ClassificationData &trainingData)
Definition: MLP.cpp:129
bool setInputLayerActivationFunction(const UINT activationFunction)
Definition: MLP.cpp:1639
Vector< Neuron > getOutputLayer() const
Definition: MLP.cpp:1554
bool setHiddenLayerActivationFunction(const UINT activationFunction)
Definition: MLP.cpp:1655
MLP()
Definition: MLP.cpp:31
virtual bool saveModelToFile(std::string filename) const
Definition: MLBase.cpp:146
virtual bool saveModelToFile(std::fstream &file) const
Definition: MLP.cpp:1072
virtual bool clear()
Definition: MLP.cpp:296
Vector< Neuron > getInputLayer() const
Definition: MLP.cpp:1546
UINT getNumOutputNeurons() const
Definition: MLP.cpp:1502
void printNetwork() const
Definition: MLP.cpp:1005
UINT activationFunctionFromString(const std::string activationName) const
Definition: MLP.cpp:1616
Float getMomentum() const
Definition: MLP.cpp:1526
virtual bool print() const
Definition: MLP.cpp:312
bool setNullRejection(const bool useNullRejection)
Definition: MLP.cpp:1721
VectorFloat getClassLikelihoods() const
Definition: MLP.cpp:1579
virtual bool loadModelFromFile(std::string filename)
Definition: MLBase.cpp:168
UINT getOutputLayerActivationFunction() const
Definition: MLP.cpp:1514
bool setMomentum(const Float momentum)
Definition: MLP.cpp:1690
Float getTrainingError() const
Definition: MLP.cpp:1534
Float getMaximumLikelihood() const
Definition: MLP.cpp:1574
virtual bool train_(ClassificationData &trainingData)
Definition: MLBase.cpp:90
UINT getHiddenLayerActivationFunction() const
Definition: MLP.cpp:1510
Float getNullRejectionCoeff() const
Definition: MLP.cpp:1566
virtual bool deepCopyFrom(const Regressifier *regressifier)
Definition: MLP.cpp:111
bool setNumRandomTrainingIterations(const UINT numRandomTrainingIterations)
Definition: MLP.cpp:1713
bool setGamma(const Float gamma)
Definition: MLP.cpp:1698
UINT getNumClasses() const
Definition: MLP.cpp:1488
Float getNullRejectionThreshold() const
Definition: MLP.cpp:1570
virtual bool loadModelFromFile(std::fstream &file)
Definition: MLP.cpp:1146
bool setNullRejectionCoeff(const Float nullRejectionCoeff)
Definition: MLP.cpp:1726
bool getRegressionModeActive() const
Definition: MLP.cpp:1542
Definition: MLP.h:41
Float getTrainingRate() const
Definition: MLP.cpp:1522
bool setOutputLayerActivationFunction(const UINT activationFunction)
Definition: MLP.cpp:1671
UINT getPredictedClassLabel() const
Definition: MLP.cpp:1590
bool getClassificationModeActive() const
Definition: MLP.cpp:1538
virtual bool predict_(VectorFloat &inputVector)
Definition: MLP.cpp:163
UINT getNumInputNeurons() const
Definition: MLP.cpp:1494