GestureRecognitionToolkit  Version: 0.2.0
The Gesture Recognition Toolkit (GRT) is a cross-platform, open-source, c++ machine learning library for real-time gesture recognition.
GaussianMixtureModels.h
Go to the documentation of this file.
1 
30 #ifndef GRT_GAUSSIAN_MIXTURE_MODELS_HEADER
31 #define GRT_GAUSSIAN_MIXTURE_MODELS_HEADER
32 
33 #include "../../CoreModules/Clusterer.h"
34 
35 GRT_BEGIN_NAMESPACE
36 
37 class GRT_API GaussianMixtureModels : public Clusterer
38 {
39 public:
43  GaussianMixtureModels(const UINT numClusters=10,const UINT minNumEpochs=5,const UINT maxNumEpochs=1000,const Float minChange=1.0e-5);
44 
51 
55  virtual ~GaussianMixtureModels();
56 
63  GaussianMixtureModels &operator=(const GaussianMixtureModels &rhs);
64 
72  virtual bool deepCopyFrom(const Clusterer *clusterer);
73 
80  virtual bool reset();
81 
87  virtual bool clear();
88 
95  virtual bool train_(MatrixFloat &trainingData);
96 
103  virtual bool train_(ClassificationData &trainingData);
104 
111  virtual bool train_(UnlabelledData &trainingData);
112 
113 
114  virtual bool predict_(VectorDouble &inputVector);
115 
123  virtual bool saveModelToFile( std::fstream &file ) const;
124 
132  virtual bool loadModelFromFile( std::fstream &file );
133 
141  MatrixFloat getMu() const { if( trained ){ return mu; } return MatrixFloat(); }
142 
151  Vector< MatrixFloat > getSigma() const { if( trained ){ return sigma; } return Vector< MatrixFloat >(); }
152 
160  MatrixFloat getSigma(const UINT k) const{
161  if( k < numClusters && trained ){
162  return sigma[k];
163  }
164  return MatrixFloat();
165  }
166 
167  //Tell the compiler we are using the base class train method to stop hidden virtual function warnings
168  using MLBase::saveModelToFile;
169  using MLBase::loadModelFromFile;
170 
171 protected:
172  bool estep( const MatrixFloat &data, VectorDouble &u, VectorDouble &v, Float &change );
173  bool mstep( const MatrixFloat &data );
174  bool computeInvAndDet();
175  inline void SWAP(UINT &a,UINT &b);
176  inline Float SQR(const Float v){ return v*v; }
177 
178  Float gauss(const VectorDouble &x,const UINT clusterIndex,const VectorDouble &det,const MatrixFloat &mu,const Vector< MatrixFloat > &invSigma){
179 
180  Float y = 0;
181  Float sum = 0;
182  UINT i,j = 0;
183  const UINT N = (UINT)x.size();
184  VectorDouble temp(N,0);
185 
186  //Compute the first part of the equation
187  y = (1.0/pow(TWO_PI,N/2.0)) * (1.0/pow(det[clusterIndex],0.5));
188 
189  //Compute the later half
190  for(i=0; i<N; i++){
191  for(j=0; j<N; j++){
192  temp[i] += (x[j]-mu[clusterIndex][j]) * invSigma[clusterIndex][j][i];
193  }
194  sum += (x[i]-mu[clusterIndex][i]) * temp[i];
195  }
196 
197  return ( y*exp( -0.5*sum ) );
198  }
199 
201  Float loglike;
206  VectorDouble det;
207  Vector< MatrixFloat > sigma;
208  Vector< MatrixFloat > invSigma;
209 
210 private:
212 };
213 
214 GRT_END_NAMESPACE
215 
216 #endif //GRT_GAUSSIAN_MIXTURE_MODELS_HEADER
217 
virtual bool predict_(VectorFloat &inputVector)
Definition: MLBase.cpp:115
MatrixFloat getSigma(const UINT k) const
VectorDouble lndets
A vector holding the log detminants of SIGMA'k.
Float loglike
The current loglikelihood value of the models given the data.
virtual bool deepCopyFrom(const Clusterer *clusterer)
Definition: Clusterer.h:58
MatrixFloat getMu() const
MatrixFloat mu
A matrix holding the estimated mean values of each Gaussian.
virtual bool reset()
Definition: Clusterer.cpp:128
Vector< MatrixFloat > getSigma() const
VectorDouble frac
A vector holding the P(k)'s.
virtual bool train_(MatrixFloat &trainingData)
Definition: Clusterer.cpp:114
MatrixFloat resp
The responsibility matrix.
virtual bool clear()
Definition: Clusterer.cpp:142
UINT numTrainingSamples
The number of samples in the training data.