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.
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,const UINT numRestarts=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 
167  static std::string getId();
168 
177  bool setNumRestarts(const UINT numRestarts);
178 
179  //Tell the compiler we are using the base class train method to stop hidden virtual function warnings
180  using MLBase::saveModelToFile;
181  using MLBase::loadModelFromFile;
182 
183 protected:
184  bool train_( const UINT numTrainingSamples, const MatrixFloat &data );
185  bool estep( const MatrixFloat &data, VectorDouble &u, VectorDouble &v, Float &change );
186  bool mstep( const MatrixFloat &data );
187  bool computeInvAndDet();
188  inline void SWAP(UINT &a,UINT &b);
189  inline Float SQR(const Float v){ return v*v; }
190 
191  Float gauss(const VectorDouble &x,const UINT clusterIndex,const VectorDouble &det,const MatrixFloat &mu,const Vector< MatrixFloat > &invSigma){
192 
193  Float y = 0;
194  Float sum = 0;
195  UINT i,j = 0;
196  const UINT N = (UINT)x.size();
197  VectorDouble temp(N,0);
198 
199  //Compute the first part of the equation
200  y = (1.0/pow(TWO_PI,N/2.0)) * (1.0/pow(det[clusterIndex],0.5));
201 
202  //Compute the later half
203  for(i=0; i<N; i++){
204  for(j=0; j<N; j++){
205  temp[i] += (x[j]-mu[clusterIndex][j]) * invSigma[clusterIndex][j][i];
206  }
207  sum += (x[i]-mu[clusterIndex][i]) * temp[i];
208  }
209 
210  return ( y*exp( -0.5*sum ) );
211  }
212 
214  UINT numRestarts;
215  Float loglike;
220  VectorDouble det;
221  Vector< MatrixFloat > sigma;
222  Vector< MatrixFloat > invSigma;
223 
224 private:
226  static const std::string id;
227 };
228 
229 GRT_END_NAMESPACE
230 
231 #endif //GRT_GAUSSIAN_MIXTURE_MODELS_HEADER
232 
std::string getId() const
Definition: GRTBase.cpp:85
virtual bool reset() override
Definition: Clusterer.cpp:130
virtual bool predict_(VectorFloat &inputVector)
Definition: MLBase.cpp:137
bool setNumRestarts(const UINT numRestarts)
Definition: MLBase.cpp:339
VectorDouble lndets
A vector holding the log detminants of SIGMA&#39;k.
Float loglike
The current loglikelihood value of the models given the data.
virtual bool clear() override
Definition: Clusterer.cpp:144
virtual bool deepCopyFrom(const Clusterer *clusterer)
Definition: Clusterer.h:59
virtual bool train_(MatrixFloat &trainingData) override
Definition: Clusterer.cpp:116
MatrixFloat getMu() const
MatrixFloat mu
A matrix holding the estimated mean values of each Gaussian.
Vector< MatrixFloat > getSigma() const
VectorDouble frac
A vector holding the P(k)&#39;s.
UINT numRestarts
The number of times the learning algorithm can reattempt to train a model.
MatrixFloat resp
The responsibility matrix.
UINT numTrainingSamples
The number of samples in the training data.