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.
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  bool init(const UINT numInputs,const Float 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  Float getWeightDistance( const VectorFloat &x ) const {
88 
89  Float 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  Float getSquaredWeightDistance( const VectorFloat &x ) const {
99 
100  Float dist = 0;
101 
102  for(UINT i=0; i<numInputs; i++){
103  dist += grt_sqr( x[i]- weights[i] );
104  }
105 
106  return dist;
107  }
108 
109  Float fire( const VectorFloat &x ) const {
110  Float y = 0;
111 
112  for(UINT i=0; i<numInputs; i++){
113  y += grt_sqr( x[i]- weights[i] );
114  }
115 
116  return exp( - (y/(2*grt_sqr(sigma))) );
117  }
118 
119  bool saveNeuronToFile( std::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 << std::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 << std::endl;
137  file << "Sigma: " << sigma << std::endl;
138 
139  return true;
140  }
141 
142  bool loadNeuronFromFile( std::fstream &file ){
143 
144  if( !file.is_open() ){
145  return false;
146  }
147 
148  clear();
149 
150  std::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  VectorFloat weights;
193  Float 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 Float alphaStart = 0.8, const Float 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_(MatrixFloat &trainingData);
258 
265  virtual bool train_(ClassificationData &trainingData);
266 
273  virtual bool train_(UnlabelledData &trainingData);
274 
285  virtual bool map_( VectorFloat &x );
286 
294  virtual bool saveModelToFile( std::fstream &file ) const;
295 
303  virtual bool loadModelFromFile( std::fstream &file );
304 
311  bool validateNetworkTypology( const UINT networkTypology );
312 
319  UINT getNetworkSize() const;
320 
321  Float getAlphaStart() const;
322 
323  Float getAlphaEnd() const;
324 
325  VectorFloat getMappedData() const;
326 
327  Vector< GaussNeuron > getNeurons() const;
328 
329  const Vector< GaussNeuron > &getNeuronsRef() const;
330 
331  MatrixFloat getNetworkWeights() const;
332 
333  bool setNetworkSize( const UINT networkSize );
334 
335  bool setNetworkTypology( const UINT networkTypology );
336 
337  bool setAlphaStart( const Float alphaStart );
338 
339  bool setAlphaEnd( const Float 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  Float alphaStart;
348  Float alphaEnd;
349  VectorFloat mappedData;
350  Vector< GaussNeuron > neurons;
351  MatrixFloat networkWeights;
352 
353 private:
354  static RegisterClustererModule< SelfOrganizingMap > registerModule;
355 
356 public:
357 
358  enum NetworkTypology{RANDOM_NETWORK=0};
359 
360 };
361 
362 GRT_END_NAMESPACE
363 
364 #endif //GRT_SELF_ORGANIZING_MAP_HEADER
virtual bool deepCopyFrom(const Clusterer *clusterer)
Definition: Random.h:40
virtual bool resize(const unsigned int size)
Definition: Vector.h:133
virtual bool train_(MatrixFloat &trainingData)
UINT getNetworkSize() const
bool validateNetworkTypology(const UINT networkTypology)
virtual bool map_(VectorFloat &x)
SelfOrganizingMap(const UINT networkSize=20, const UINT networkTypology=RANDOM_NETWORK, const UINT maxNumEpochs=1000, const Float alphaStart=0.8, const Float alphaEnd=0.1)
virtual bool saveModelToFile(std::string filename) const
Definition: MLBase.cpp:146
virtual bool loadModelFromFile(std::string filename)
Definition: MLBase.cpp:168
virtual bool saveModelToFile(std::fstream &file) const
virtual bool loadModelFromFile(std::fstream &file)
Float getRandomNumberUniform(Float minRange=0.0, Float maxRange=1.0)
Definition: Random.h:198
void setSeed(unsigned long long seed=0)
Definition: Random.h:68
SelfOrganizingMap & operator=(const SelfOrganizingMap &rhs)