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.
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 GRT_BEGIN_NAMESPACE
37 
39 public:
40  GaussNeuron(){
41  numInputs = 0;
42  sigma = 0;
43  initialized = false;
44  }
45 
46  ~GaussNeuron(){
47 
48  }
49 
50  Float& operator[](const UINT index){
51  return weights[ index ];
52  }
53 
54  const Float& operator[](const UINT index) const{
55  return weights[ index ];
56  }
57 
58  bool init(const UINT numInputs,const Float sigma = 2.0,const Float minWeightRange = -1.0,const Float maxWeightRange = 1.0){
59 
60  this->numInputs = numInputs;
61  this->sigma = sigma;
62  weights.resize( numInputs );
63 
64  //Set the random seed
65  Random random;
66  random.setSeed( (unsigned long long)time(NULL) );
67 
68  //Randomise the weights between [minWeightRange maxWeightRange]
69  for(unsigned int i=0; i<numInputs; i++){
70  weights[i] = random.getRandomNumberUniform(minWeightRange,maxWeightRange);
71  }
72 
73  initialized = true;
74 
75  return true;
76  }
77 
78  bool clear(){
79 
80  numInputs = 0;
81  weights.clear();
82  initialized = false;
83  return true;
84 
85  }
86 
87  bool getInitialized() const {
88  return initialized;
89  }
90 
91  Float getWeightDistance( const VectorFloat &x ) const {
92 
93  Float dist = 0;
94 
95  for(UINT i=0; i<numInputs; i++){
96  dist += x[i]- weights[i];
97  }
98 
99  return dist;
100  }
101 
102  Float getSquaredWeightDistance( const VectorFloat &x ) const {
103 
104  Float dist = 0;
105 
106  for(UINT i=0; i<numInputs; i++){
107  dist += grt_sqr( x[i]- weights[i] );
108  }
109 
110  return dist;
111  }
112 
113  Float fire( const VectorFloat &x ) const {
114  Float y = 0;
115 
116  for(UINT i=0; i<numInputs; i++){
117  y += grt_sqr( x[i]- weights[i] );
118  }
119 
120  return exp( - (y/(2*grt_sqr(sigma))) );
121  }
122 
123  bool save( std::fstream &file ) const {
124 
125  if( !file.is_open() ){
126  return false;
127  }
128 
129  if( !initialized ){
130  return false;
131  }
132 
133  file << "GAUSS_NEURON\n";
134  file << "NumInputs: " << numInputs << std::endl;
135  file << "Weights: ";
136  for(UINT i=0; i<numInputs; i++){
137  file << weights[i];
138  if( i < numInputs-1 ) file << "\t";
139  }
140  file << std::endl;
141  file << "Sigma: " << sigma << std::endl;
142 
143  return true;
144  }
145 
146  bool load( std::fstream &file ){
147 
148  if( !file.is_open() ){
149  return false;
150  }
151 
152  clear();
153 
154  std::string word;
155 
156  //Read the header
157  file >> word;
158  if( word != "GAUSS_NEURON" ){
159  return false;
160  }
161 
162  //Read the num inputs
163  file >> word;
164  if( word != "NumInputs:" ){
165  return false;
166  }
167  file >> numInputs;
168 
169  //Resize the weight Vector
170  weights.resize( numInputs );
171 
172  //Read the weights header
173  file >> word;
174  if( word != "Weights:" ){
175  return false;
176  }
177 
178  //Load the weights
179  for(UINT i=0; i<numInputs; i++){
180  file >> weights[i];
181  }
182 
183  //Load the sigma value
184  file >> word;
185  if( word != "Sigma:" ){
186  return false;
187  }
188  file >> sigma;
189 
190  initialized = true;
191 
192  return true;
193  }
194 
195  UINT numInputs;
196  VectorFloat weights;
197  Float sigma;
198  bool initialized;
199 };
200 
201 class GRT_API SelfOrganizingMap : public Clusterer{
202 
203 public:
204  enum NetworkTypology{RANDOM_NETWORK=0};
205 
209  SelfOrganizingMap(const UINT networkSize = 5, const UINT networkTypology = RANDOM_NETWORK, const UINT maxNumEpochs = 1000,const Float sigmaWeight = 0.2, const Float alphaStart = 0.3, const Float alphaEnd = 0.1);
210 
218 
222  virtual ~SelfOrganizingMap();
223 
230  SelfOrganizingMap &operator=(const SelfOrganizingMap &rhs);
231 
239  virtual bool deepCopyFrom(const Clusterer *clusterer);
240 
247  virtual bool reset();
248 
254  virtual bool clear();
255 
263  virtual bool train_(MatrixFloat &trainingData);
264 
271  virtual bool train_(ClassificationData &trainingData);
272 
279  virtual bool train_(UnlabelledData &trainingData);
280 
291  virtual bool map_( VectorFloat &x );
292 
300  virtual bool save( std::fstream &file ) const;
301 
309  virtual bool load( std::fstream &file );
310 
317  bool validateNetworkTypology( const UINT networkTypology );
318 
325  UINT getNetworkSize() const;
326 
327  Float getAlphaStart() const;
328 
329  Float getAlphaEnd() const;
330 
331  VectorFloat getMappedData() const;
332 
333  Matrix< GaussNeuron > getNeurons() const;
334 
335  const Matrix< GaussNeuron > &getNeuronsRef() const;
336 
337  Matrix< VectorFloat > getWeightsMatrix() const;
338 
339  bool setNetworkSize( const UINT networkSize );
340 
341  bool setNetworkTypology( const UINT networkTypology );
342 
343  bool setAlphaStart( const Float alphaStart );
344 
345  bool setAlphaEnd( const Float alphaEnd );
346 
347  bool setSigmaWeight( const Float sigmaWeight );
348 
349  //Tell the compiler we are using the base class train method to stop hidden virtual function warnings
350  using MLBase::save;
351  using MLBase::load;
352 
358  static std::string getId();
359 
360 protected:
361  UINT networkTypology;
362  Float sigmaWeight;
363  Float alphaStart;
364  Float alphaEnd;
365  VectorFloat mappedData;
366  Matrix< GaussNeuron > neurons;
367 
368 private:
369  static RegisterClustererModule< SelfOrganizingMap > registerModule;
370  static const std::string id;
371 };
372 
373 GRT_END_NAMESPACE
374 
375 #endif //GRT_SELF_ORGANIZING_MAP_HEADER
This file contains the Random class, a useful wrapper for generating cross platform random functions...
Definition: Random.h:46
virtual bool resize(const unsigned int size)
Definition: Vector.h:133
virtual bool save(const std::string &filename) const
Definition: MLBase.cpp:167
bool setSeed(const unsigned long long seed=0)
Definition: Random.cpp:40
Float getRandomNumberUniform(Float minRange=0.0, Float maxRange=1.0)
Definition: Random.cpp:129
virtual bool load(const std::string &filename)
Definition: MLBase.cpp:190