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