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.
MLBase.cpp
1 
21 #include "MLBase.h"
22 
23 GRT_BEGIN_NAMESPACE
24 
26  trained = false;
27  useScaling = false;
28  baseType = BASE_TYPE_NOT_SET;
29  inputType = DATA_TYPE_UNKNOWN;
30  outputType = DATA_TYPE_UNKNOWN;
31  numInputDimensions = 0;
32  numOutputDimensions = 0;
33  minNumEpochs = 0;
34  maxNumEpochs = 100;
35  validationSetSize = 20;
36  validationSetAccuracy = 0;
37  minChange = 1.0e-5;
38  learningRate = 0.1;
39  useValidationSet = false;
40  randomiseTrainingOrder = true;
41  rootMeanSquaredTrainingError = 0;
42  totalSquaredTrainingError = 0;
43 }
44 
46  clear();
47 }
48 
49 bool MLBase::copyMLBaseVariables(const MLBase *mlBase){
50 
51  if( mlBase == NULL ){
52  errorLog << "copyMLBaseVariables(MLBase *mlBase) - mlBase pointer is NULL!" << std::endl;
53  return false;
54  }
55 
56  if( !copyGRTBaseVariables( mlBase ) ){
57  errorLog << "copyMLBaseVariables(MLBase *mlBase) - Failed to copy GRT Base variables!" << std::endl;
58  return false;
59  }
60 
61  this->trained = mlBase->trained;
62  this->useScaling = mlBase->useScaling;
63  this->baseType = mlBase->baseType;
64  this->inputType = mlBase->inputType;
65  this->outputType = mlBase->outputType;
66  this->numInputDimensions = mlBase->numInputDimensions;
67  this->numOutputDimensions = mlBase->numOutputDimensions;
68  this->minNumEpochs = mlBase->minNumEpochs;
69  this->maxNumEpochs = mlBase->maxNumEpochs;
70  this->validationSetSize = mlBase->validationSetSize;
71  this->validationSetAccuracy = mlBase->validationSetAccuracy;
72  this->validationSetPrecision = mlBase->validationSetPrecision;
73  this->validationSetRecall = mlBase->validationSetRecall;
74  this->minChange = mlBase->minChange;
75  this->learningRate = mlBase->learningRate;
76  this->rootMeanSquaredTrainingError = mlBase->rootMeanSquaredTrainingError;
77  this->totalSquaredTrainingError = mlBase->totalSquaredTrainingError;
78  this->useValidationSet = mlBase->useValidationSet;
79  this->randomiseTrainingOrder = mlBase->randomiseTrainingOrder;
80  this->numTrainingIterationsToConverge = mlBase->numTrainingIterationsToConverge;
81  this->trainingResults = mlBase->trainingResults;
82  this->trainingResultsObserverManager = mlBase->trainingResultsObserverManager;
83  this->testResultsObserverManager = mlBase->testResultsObserverManager;
84 
85  return true;
86 }
87 
88 bool MLBase::train(ClassificationData trainingData){ return train_( trainingData ); }
89 
90 bool MLBase::train_(ClassificationData &trainingData){ return false; }
91 
92 bool MLBase::train(RegressionData trainingData){ return train_( trainingData ); }
93 
94 bool MLBase::train_(RegressionData &trainingData){ return false; }
95 
96 bool MLBase::train(TimeSeriesClassificationData trainingData){ return train_( trainingData ); }
97 
98 bool MLBase::train_(TimeSeriesClassificationData &trainingData){ return false; }
99 
100 bool MLBase::train(ClassificationDataStream trainingData){ return train_( trainingData ); }
101 
102 bool MLBase::train_(ClassificationDataStream &trainingData){ return false; }
103 
104 bool MLBase::train(UnlabelledData trainingData){ return train_( trainingData ); }
105 
106 bool MLBase::train_(UnlabelledData &trainingData){ return false; }
107 
108 bool MLBase::train(MatrixFloat data){ return train_( data ); }
109 
110 bool MLBase::train_(MatrixFloat &data){ return false; }
111 
112 bool MLBase::predict(VectorFloat inputVector){ return predict_( inputVector ); }
113 
114 bool MLBase::predict_(VectorFloat &inputVector){ return false; }
115 
116 bool MLBase::predict(MatrixFloat inputMatrix){ return predict_( inputMatrix ); }
117 
118 bool MLBase::predict_(MatrixFloat &inputMatrix){ return false; }
119 
120 bool MLBase::map(VectorFloat inputVector){ return map_( inputVector ); }
121 
122 bool MLBase::map_(VectorFloat &inputVector){ return false; }
123 
124 bool MLBase::reset(){ return true; }
125 
127  trained = false;
128  numInputDimensions = 0;
129  numOutputDimensions = 0;
130  numTrainingIterationsToConverge = 0;
131  rootMeanSquaredTrainingError = 0;
132  totalSquaredTrainingError = 0;
133  trainingResults.clear();
134  validationSetPrecision.clear();
135  validationSetRecall.clear();
136  validationSetAccuracy = 0;
137  return true;
138 }
139 
140 bool MLBase::print() const { std::cout << getModelAsString(); return true; }
141 
142 bool MLBase::save(const std::string filename) const {
143  return saveModelToFile( filename );
144 }
145 
146 bool MLBase::saveModelToFile(std::string filename) const{
147 
148  if( !trained ) return false;
149 
150  std::fstream file;
151  file.open(filename.c_str(), std::ios::out);
152 
153  if( !saveModelToFile( file ) ){
154  return false;
155  }
156 
157  file.close();
158 
159  return true;
160 }
161 
162 bool MLBase::saveModelToFile(std::fstream &file) const { return false; }
163 
164 bool MLBase::load(const std::string filename){
165  return loadModelFromFile(filename);
166 }
167 
168 bool MLBase::loadModelFromFile(std::string filename){
169 
170  std::fstream file;
171  file.open(filename.c_str(), std::ios::in);
172 
173  if( !loadModelFromFile( file ) ){
174  return false;
175  }
176 
177  //Close the file
178  file.close();
179 
180  return true;
181 }
182 
183 bool MLBase::loadModelFromFile(std::fstream &file){ return false; }
184 
185 bool MLBase::getModel(std::ostream &stream) const { return true; }
186 
187 std::string MLBase::getModelAsString() const{
188  std::stringstream stream;
189  if( getModel( stream ) ){
190  return stream.str();
191  }
192  return "";
193 }
194 
195 DataType MLBase::getInputType() const {
196  return inputType;
197 }
198 
199 DataType MLBase::getOutputType() const {
200  return outputType;
201 }
202 
203 UINT MLBase::getBaseType() const{ return baseType; }
204 
206 
207 UINT MLBase::getNumInputDimensions() const{ return numInputDimensions; }
208 
209 UINT MLBase::getNumOutputDimensions() const{ return numOutputDimensions; }
210 
212  if( trained ){
213  return numTrainingIterationsToConverge;
214  }
215  return 0;
216 }
217 
219  return minNumEpochs;
220 }
221 
223  return maxNumEpochs;
224 }
225 
227  return validationSetSize;
228 }
229 
231  return learningRate;
232 }
233 
235  return rootMeanSquaredTrainingError;
236 }
237 
239  return totalSquaredTrainingError;
240 }
241 
243  return validationSetAccuracy;
244 }
245 
247  return validationSetPrecision;
248 }
249 
251  return validationSetRecall;
252 }
253 
254 bool MLBase::getTrained() const{ return trained; }
255 
256 bool MLBase::getModelTrained() const{ return getTrained(); }
257 
258 bool MLBase::getScalingEnabled() const{ return useScaling; }
259 
260 bool MLBase::getIsBaseTypeClassifier() const{ return baseType==CLASSIFIER; }
261 
262 bool MLBase::getIsBaseTypeRegressifier() const{ return baseType==REGRESSIFIER; }
263 
264 bool MLBase::getIsBaseTypeClusterer() const{ return baseType==CLUSTERER; }
265 
266 bool MLBase::enableScaling(bool useScaling){ this->useScaling = useScaling; return true; }
267 
268 bool MLBase::setMaxNumEpochs(const UINT maxNumEpochs){
269  if( maxNumEpochs == 0 ){
270  warningLog << "setMaxNumEpochs(const UINT maxNumEpochs) - The maxNumEpochs must be greater than 0!" << std::endl;
271  return false;
272  }
273  this->maxNumEpochs = maxNumEpochs;
274  return true;
275 }
276 
277 bool MLBase::setMinNumEpochs(const UINT minNumEpochs){
278  this->minNumEpochs = minNumEpochs;
279  return true;
280 }
281 
282 bool MLBase::setMinChange(const Float minChange){
283  if( minChange < 0 ){
284  warningLog << "setMinChange(const Float minChange) - The minChange must be greater than or equal to 0!" << std::endl;
285  return false;
286  }
287  this->minChange = minChange;
288  return true;
289 }
290 
291 bool MLBase::setLearningRate(const Float learningRate){
292  if( learningRate > 0 ){
293  this->learningRate = learningRate;
294  return true;
295  }
296  return false;
297 }
298 
299 bool MLBase::setValidationSetSize(const UINT validationSetSize){
300 
301  if( validationSetSize > 0 && validationSetSize < 100 ){
302  this->validationSetSize = validationSetSize;
303  return true;
304  }
305 
306  warningLog << "setValidationSetSize(const UINT validationSetSize) - The validation size must be in the range [1 99]!" << std::endl;
307 
308  return false;
309 }
310 
311 bool MLBase::setUseValidationSet(const bool useValidationSet){
312  this->useValidationSet = useValidationSet;
313  return true;
314 }
315 
316 bool MLBase::setRandomiseTrainingOrder(const bool randomiseTrainingOrder){
317  this->randomiseTrainingOrder = randomiseTrainingOrder;
318  return true;
319 }
320 
321 bool MLBase::setTrainingLoggingEnabled(const bool loggingEnabled){
322  this->trainingLog.setEnableInstanceLogging( loggingEnabled );
323  return true;
324 }
325 
327  return trainingResultsObserverManager.registerObserver( observer );
328 }
329 
331  return testResultsObserverManager.registerObserver( observer );
332 }
333 
335  return trainingResultsObserverManager.removeObserver( observer );
336 }
337 
339  return testResultsObserverManager.removeObserver( observer );
340 }
341 
343  return trainingResultsObserverManager.removeAllObservers();
344 }
345 
347  return testResultsObserverManager.removeAllObservers();
348 }
349 
350 bool MLBase::notifyTrainingResultsObservers( const TrainingResult &data ){
351  return trainingResultsObserverManager.notifyObservers( data );
352 }
353 
354 bool MLBase::notifyTestResultsObservers( const TestInstanceResult &data ){
355  return testResultsObserverManager.notifyObservers( data );
356 }
357 
359  return this;
360 }
361 
363  return this;
364 }
365 
367  return trainingResults;
368 }
369 
370 bool MLBase::saveBaseSettingsToFile( std::fstream &file ) const{
371 
372  if( !file.is_open() ){
373  errorLog << "saveBaseSettingsToFile(fstream &file) - The file is not open!" << std::endl;
374  return false;
375  }
376 
377  file << "Trained: " << trained << std::endl;
378  file << "UseScaling: " << useScaling << std::endl;
379  file << "NumInputDimensions: " << numInputDimensions << std::endl;
380  file << "NumOutputDimensions: " << numOutputDimensions << std::endl;
381  file << "NumTrainingIterationsToConverge: " << numTrainingIterationsToConverge << std::endl;
382  file << "MinNumEpochs: " << minNumEpochs << std::endl;
383  file << "MaxNumEpochs: " << maxNumEpochs << std::endl;
384  file << "ValidationSetSize: " << validationSetSize << std::endl;
385  file << "LearningRate: " << learningRate << std::endl;
386  file << "MinChange: " << minChange << std::endl;
387  file << "UseValidationSet: " << useValidationSet << std::endl;
388  file << "RandomiseTrainingOrder: " << randomiseTrainingOrder << std::endl;
389 
390  return true;
391 }
392 
393 bool MLBase::loadBaseSettingsFromFile( std::fstream &file ){
394 
395  //Clear any previous setup
396  clear();
397 
398  if( !file.is_open() ){
399  errorLog << "loadBaseSettingsFromFile(fstream &file) - The file is not open!" << std::endl;
400  return false;
401  }
402 
403  std::string word;
404 
405  //Load the trained state
406  file >> word;
407  if( word != "Trained:" ){
408  errorLog << "loadBaseSettingsFromFile(fstream &file) - Failed to read Trained header!" << std::endl;
409  return false;
410  }
411  file >> trained;
412 
413  //Load the scaling state
414  file >> word;
415  if( word != "UseScaling:" ){
416  errorLog << "loadBaseSettingsFromFile(fstream &file) - Failed to read UseScaling header!" << std::endl;
417  return false;
418  }
419  file >> useScaling;
420 
421  //Load the NumInputDimensions
422  file >> word;
423  if( word != "NumInputDimensions:" ){
424  errorLog << "loadBaseSettingsFromFile(fstream &file) - Failed to read NumInputDimensions header!" << std::endl;
425  return false;
426  }
427  file >> numInputDimensions;
428 
429  //Load the NumOutputDimensions
430  file >> word;
431  if( word != "NumOutputDimensions:" ){
432  errorLog << "loadBaseSettingsFromFile(fstream &file) - Failed to read NumOutputDimensions header!" << std::endl;
433  return false;
434  }
435  file >> numOutputDimensions;
436 
437  //Load the numTrainingIterationsToConverge
438  file >> word;
439  if( word != "NumTrainingIterationsToConverge:" ){
440  errorLog << "loadBaseSettingsFromFile(fstream &file) - Failed to read NumTrainingIterationsToConverge header!" << std::endl;
441  return false;
442  }
443  file >> numTrainingIterationsToConverge;
444 
445  //Load the MinNumEpochs
446  file >> word;
447  if( word != "MinNumEpochs:" ){
448  errorLog << "loadBaseSettingsFromFile(fstream &file) - Failed to read MinNumEpochs header!" << std::endl;
449  return false;
450  }
451  file >> minNumEpochs;
452 
453  //Load the maxNumEpochs
454  file >> word;
455  if( word != "MaxNumEpochs:" ){
456  errorLog << "loadBaseSettingsFromFile(fstream &file) - Failed to read MaxNumEpochs header!" << std::endl;
457  return false;
458  }
459  file >> maxNumEpochs;
460 
461  //Load the ValidationSetSize
462  file >> word;
463  if( word != "ValidationSetSize:" ){
464  errorLog << "loadBaseSettingsFromFile(fstream &file) - Failed to read ValidationSetSize header!" << std::endl;
465  return false;
466  }
467  file >> validationSetSize;
468 
469  //Load the LearningRate
470  file >> word;
471  if( word != "LearningRate:" ){
472  errorLog << "loadBaseSettingsFromFile(fstream &file) - Failed to read LearningRate header!" << std::endl;
473  return false;
474  }
475  file >> learningRate;
476 
477  //Load the MinChange
478  file >> word;
479  if( word != "MinChange:" ){
480  errorLog << "loadBaseSettingsFromFile(fstream &file) - Failed to read MinChange header!" << std::endl;
481  return false;
482  }
483  file >> minChange;
484 
485  //Load the UseValidationSet
486  file >> word;
487  if( word != "UseValidationSet:" ){
488  errorLog << "loadBaseSettingsFromFile(fstream &file) - Failed to read UseValidationSet header!" << std::endl;
489  return false;
490  }
491  file >> useValidationSet;
492 
493  //Load the RandomiseTrainingOrder
494  file >> word;
495  if( word != "RandomiseTrainingOrder:" ){
496  errorLog << "loadBaseSettingsFromFile(fstream &file) - Failed to read RandomiseTrainingOrder header!" << std::endl;
497  return false;
498  }
499  file >> randomiseTrainingOrder;
500 
501  return true;
502 }
503 
504 GRT_END_NAMESPACE
bool saveBaseSettingsToFile(std::fstream &file) const
Definition: MLBase.cpp:370
bool setLearningRate(const Float learningRate)
Definition: MLBase.cpp:291
virtual bool predict(VectorFloat inputVector)
Definition: MLBase.cpp:112
bool setRandomiseTrainingOrder(const bool randomiseTrainingOrder)
Definition: MLBase.cpp:316
bool notifyTrainingResultsObservers(const TrainingResult &data)
Definition: MLBase.cpp:350
bool registerTrainingResultsObserver(Observer< TrainingResult > &observer)
Definition: MLBase.cpp:326
virtual bool predict_(VectorFloat &inputVector)
Definition: MLBase.cpp:114
virtual bool reset()
Definition: MLBase.cpp:124
bool setTrainingLoggingEnabled(const bool loggingEnabled)
Definition: MLBase.cpp:321
Float getLearningRate() const
Definition: MLBase.cpp:230
bool removeAllTestObservers()
Definition: MLBase.cpp:346
bool enableScaling(const bool useScaling)
Definition: MLBase.cpp:266
DataType getOutputType() const
Definition: MLBase.cpp:199
virtual bool getModel(std::ostream &stream) const
Definition: MLBase.cpp:185
virtual bool train(ClassificationData trainingData)
Definition: MLBase.cpp:88
bool getTrained() const
Definition: MLBase.cpp:254
Float getRootMeanSquaredTrainingError() const
Definition: MLBase.cpp:234
UINT getMinNumEpochs() const
Definition: MLBase.cpp:218
UINT getNumOutputDimensions() const
Definition: MLBase.cpp:209
bool getScalingEnabled() const
Definition: MLBase.cpp:258
bool registerTestResultsObserver(Observer< TestInstanceResult > &observer)
Definition: MLBase.cpp:330
bool setMinChange(const Float minChange)
Definition: MLBase.cpp:282
UINT getValidationSetSize() const
Definition: MLBase.cpp:226
virtual bool save(const std::string filename) const
Definition: MLBase.cpp:142
virtual bool load(const std::string filename)
Definition: MLBase.cpp:164
UINT getMaxNumEpochs() const
Definition: MLBase.cpp:222
This is the main base class that all GRT machine learning algorithms should inherit from...
bool getModelTrained() const
Definition: MLBase.cpp:256
Float getTotalSquaredTrainingError() const
Definition: MLBase.cpp:238
bool setValidationSetSize(const UINT validationSetSize)
Definition: MLBase.cpp:299
virtual bool saveModelToFile(std::string filename) const
Definition: MLBase.cpp:146
bool copyMLBaseVariables(const MLBase *mlBase)
Definition: MLBase.cpp:49
virtual bool print() const
Definition: MLBase.cpp:140
UINT getBaseType() const
Definition: MLBase.cpp:203
Float getValidationSetAccuracy() const
Definition: MLBase.cpp:242
bool getIsBaseTypeClusterer() const
Definition: MLBase.cpp:264
virtual std::string getModelAsString() const
Definition: MLBase.cpp:187
virtual bool loadModelFromFile(std::string filename)
Definition: MLBase.cpp:168
bool setMinNumEpochs(const UINT minNumEpochs)
Definition: MLBase.cpp:277
MLBase(void)
Definition: MLBase.cpp:25
bool loadBaseSettingsFromFile(std::fstream &file)
Definition: MLBase.cpp:393
MLBase * getMLBasePointer()
Definition: MLBase.cpp:358
bool removeAllTrainingObservers()
Definition: MLBase.cpp:342
UINT getNumInputFeatures() const
Definition: MLBase.cpp:205
virtual bool clear()
Definition: MLBase.cpp:126
virtual ~MLBase(void)
Definition: MLBase.cpp:45
virtual bool train_(ClassificationData &trainingData)
Definition: MLBase.cpp:90
bool notifyTestResultsObservers(const TestInstanceResult &data)
Definition: MLBase.cpp:354
bool copyGRTBaseVariables(const GRTBase *GRTBase)
Definition: GRTBase.cpp:32
bool getIsBaseTypeClassifier() const
Definition: MLBase.cpp:260
VectorFloat getValidationSetPrecision() const
Definition: MLBase.cpp:246
DataType getInputType() const
Definition: MLBase.cpp:195
bool removeTrainingResultsObserver(const Observer< TrainingResult > &observer)
Definition: MLBase.cpp:334
Definition: Vector.h:41
UINT getNumInputDimensions() const
Definition: MLBase.cpp:207
bool removeTestResultsObserver(const Observer< TestInstanceResult > &observer)
Definition: MLBase.cpp:338
virtual bool map_(VectorFloat &inputVector)
Definition: MLBase.cpp:122
UINT getNumTrainingIterationsToConverge() const
Definition: MLBase.cpp:211
bool setUseValidationSet(const bool useValidationSet)
Definition: MLBase.cpp:311
bool setMaxNumEpochs(const UINT maxNumEpochs)
Definition: MLBase.cpp:268
Vector< TrainingResult > getTrainingResults() const
Definition: MLBase.cpp:366
virtual bool map(VectorFloat inputVector)
Definition: MLBase.cpp:120
VectorFloat getValidationSetRecall() const
Definition: MLBase.cpp:250
Definition: MLBase.h:70
bool getIsBaseTypeRegressifier() const
Definition: MLBase.cpp:262