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.
SelfOrganizingMap.h
Go to the documentation of this file.
1 
29 #ifndef GRT_SELF_ORGANIZING_MAP_HEADER
30 #define GRT_SELF_ORGANIZING_MAP_HEADER
31 
32 #include "../../Util/GRTCommon.h"
33 #include "../../CoreModules/Clusterer.h"
34 #include "../../Util/Random.h"
35 
36 namespace GRT{
37 
39 public:
40  GaussNeuron(){
41  numInputs = 0;
42  sigma = 0;
43  initialized = false;
44  }
45 
46  ~GaussNeuron(){
47 
48  }
49 
50  double& operator[](const UINT index){
51  return weights[ index ];
52  }
53 
54  bool init(const UINT numInputs,const double sigma = 2.0){
55 
56  this->numInputs = numInputs;
57  this->sigma = sigma;
58  weights.resize( numInputs );
59 
60  //Set the random seed
61  Random random;
62  random.setSeed( (unsigned long long)time(NULL) );
63 
64  //Randomise the weights between [0.4 0.6]
65  for(unsigned int i=0; i<numInputs; i++){
66  weights[i] = random.getRandomNumberUniform(0.4,0.6);
67  }
68 
69  initialized = true;
70 
71  return true;
72  }
73 
74  bool clear(){
75 
76  numInputs = 0;
77  weights.clear();
78  initialized = false;
79  return true;
80 
81  }
82 
83  bool getInitialized() const {
84  return initialized;
85  }
86 
87  double getWeightDistance( const VectorDouble &x ) const {
88 
89  double dist = 0;
90 
91  for(UINT i=0; i<numInputs; i++){
92  dist += x[i]- weights[i];
93  }
94 
95  return dist;
96  }
97 
98  double getSquaredWeightDistance( const VectorDouble &x ) const {
99 
100  double dist = 0;
101 
102  for(UINT i=0; i<numInputs; i++){
103  dist += SQR( x[i]- weights[i] );
104  }
105 
106  return dist;
107  }
108 
109  double fire( const VectorDouble &x ) const {
110  double y = 0;
111 
112  for(UINT i=0; i<numInputs; i++){
113  y += SQR( x[i]- weights[i] );
114  }
115 
116  return exp( - (y/(2*SQR(sigma))) );
117  }
118 
119  bool saveNeuronToFile(fstream &file) const {
120 
121  if( !file.is_open() ){
122  return false;
123  }
124 
125  if( !initialized ){
126  return false;
127  }
128 
129  file << "GAUSS_NEURON\n";
130  file << "NumInputs: " << numInputs << endl;
131  file << "Weights: ";
132  for(UINT i=0; i<numInputs; i++){
133  file << weights[i];
134  if( i < numInputs-1 ) file << "\t";
135  }
136  file << endl;
137  file << "Sigma: " << sigma << endl;
138 
139  return true;
140  }
141 
142  bool loadNeuronFromFile(fstream &file){
143 
144  if( !file.is_open() ){
145  return false;
146  }
147 
148  clear();
149 
150  string word;
151 
152  //Read the header
153  file >> word;
154  if( word != "GAUSS_NEURON" ){
155  return false;
156  }
157 
158  //Read the num inputs
159  file >> word;
160  if( word != "NumInputs:" ){
161  return false;
162  }
163  file >> numInputs;
164 
165  //Resize the weight vector
166  weights.resize( numInputs );
167 
168  //Read the weights header
169  file >> word;
170  if( word != "Weights:" ){
171  return false;
172  }
173 
174  //Load the weights
175  for(UINT i=0; i<numInputs; i++){
176  file >> weights[i];
177  }
178 
179  //Load the sigma value
180  file >> word;
181  if( word != "Sigma:" ){
182  return false;
183  }
184  file >> sigma;
185 
186  initialized = true;
187 
188  return true;
189  }
190 
191  UINT numInputs;
192  VectorDouble weights;
193  double sigma;
194  bool initialized;
195 };
196 
198 
199 public:
203  SelfOrganizingMap(const UINT networkSize = 20, const UINT networkTypology = RANDOM_NETWORK, const UINT maxNumEpochs = 1000,const double alphaStart = 0.8, const double alphaEnd = 0.1);
204 
212 
216  virtual ~SelfOrganizingMap();
217 
225 
233  virtual bool deepCopyFrom(const Clusterer *clusterer);
234 
241  virtual bool reset();
242 
248  virtual bool clear();
249 
257  virtual bool train_(MatrixDouble &data);
258 
265  virtual bool train_(ClassificationData &trainingData);
266 
273  virtual bool train_(UnlabelledData &trainingData);
274 
285  virtual bool map_( VectorDouble &x );
286 
294  virtual bool saveModelToFile(fstream &file) const;
295 
303  virtual bool loadModelFromFile(fstream &file);
304 
311  bool validateNetworkTypology( const UINT networkTypology );
312 
319  UINT getNetworkSize() const;
320 
321  double getAlphaStart() const;
322 
323  double getAlphaEnd() const;
324 
325  VectorDouble getMappedData() const;
326 
327  vector< GaussNeuron > getNeurons() const;
328 
329  const vector< GaussNeuron > &getNeuronsRef() const;
330 
331  MatrixDouble getNetworkWeights() const;
332 
333  bool setNetworkSize( const UINT networkSize );
334 
335  bool setNetworkTypology( const UINT networkTypology );
336 
337  bool setAlphaStart( const double alphaStart );
338 
339  bool setAlphaEnd( const double alphaEnd );
340 
341  //Tell the compiler we are using the base class train method to stop hidden virtual function warnings
344 
345 protected:
346  UINT networkTypology;
347  double alphaStart;
348  double alphaEnd;
349  VectorDouble mappedData;
350  vector< GaussNeuron > neurons;
351  MatrixDouble networkWeights;
352 
353 private:
354  static RegisterClustererModule< SelfOrganizingMap > registerModule;
355 
356 public:
357 
358  enum NetworkTypology{RANDOM_NETWORK=0};
359 
360 };
361 
362 }//End of namespace GRT
363 
364 #endif //GRT_SELF_ORGANIZING_MAP_HEADER
virtual bool saveModelToFile(string filename) const
Definition: MLBase.cpp:135
virtual bool train_(MatrixDouble &data)
virtual bool loadModelFromFile(fstream &file)
virtual bool loadModelFromFile(string filename)
Definition: MLBase.cpp:157
Definition: AdaBoost.cpp:25
virtual bool map_(VectorDouble &x)
SelfOrganizingMap(const UINT networkSize=20, const UINT networkTypology=RANDOM_NETWORK, const UINT maxNumEpochs=1000, const double alphaStart=0.8, const double alphaEnd=0.1)
void setSeed(unsigned long long seed=0)
Definition: Random.h:67
virtual bool saveModelToFile(fstream &file) const
double getRandomNumberUniform(double minRange=0.0, double maxRange=1.0)
Definition: Random.h:197
virtual bool deepCopyFrom(const Clusterer *clusterer)
bool validateNetworkTypology(const UINT networkTypology)
SelfOrganizingMap & operator=(const SelfOrganizingMap &rhs)