/*---------------------------------------------------------------------------* * IT++ * *---------------------------------------------------------------------------* * Copyright (c) 1995-2004 by Tony Ottosson, Thomas Eriksson, Pål Frenger, * * Tobias Ringström, and Jonas Samuelsson. * * * * Permission to use, copy, modify, and distribute this software and its * * documentation under the terms of the GNU General Public License is hereby * * granted. No representations are made about the suitability of this * * software for any purpose. It is provided "as is" without expressed or * * implied warranty. See the GNU General Public License for more details. * *---------------------------------------------------------------------------*/ /*! \file \brief Gaussian mixture model 1.3 2004/09/28 07:21:31 */ #include "base/random.h" #include "base/timing.h" #include "srccode/vqtrain.h" #include "base/matfunc.h" #include "base/specmat.h" #include "srccode/gmm.h" #include #include using std::ifstream; using std::ofstream; using std::cout; using std::endl; namespace itpp { GMM::GMM() { d=0; M=0; } GMM::GMM(string filename) { load(filename); } GMM::GMM(int M_in, int d_in) { M=M_in; d=d_in; m=zeros(M*d); sigma=zeros(M*d); w=1./M*ones(M); for (int i=0;id,"GMM.marginalize: cannot change to a larger dimension"); vec mnew(d_new*M),sigmanew(d_new*M); int i,j; for (i=0;i> M >> d ; w.set_length(M); for (i=0;i> w(i) ; } m.set_length(M*d); for (i=0;i> m(i*d+j) ; } } sigma.set_length(M*d); for (i=0;i> sigma(i*d+j) ; } } compute_internals(); cout << " mixtures:" << M << " dim:" << d << endl ; } double GMM::likelihood(const vec &x) { double fx=0; int i; for (i=0;i1e-6,"weight does not sum to 0"); cumweight(length(cumweight)-1)=1; } k=0; while (u>cumweight(k)) k++; return elem_mult(sqrt(sigma.mid(k*d,d)),randn(d))+m.mid(k*d,d); } GMM gmmtrain(Array &TrainingData, int M, int NOITER, bool VERBOSE) { mat mean; int i,j,d=TrainingData(0).length(); vec sig; GMM gmm(M,d); vec m(d*M); vec sigma(d*M); vec w(M); vec normweight(M); vec normexp(d*M); double LL=0,LLold,fx; double constant=1.0/std::pow(2*pi,d/2.0); int T=TrainingData.length(); vec x1; int t,n; vec msum(d*M); vec sigmasum(d*M); vec wsum(M); vec p_aposteriori(M); vec x2; double s; vec temp1,temp2; //double MINIMUM_VARIANCE=0.03; //-----------initialization----------------------------------- mean=vqtrain(TrainingData,M,200000,0.5,VERBOSE); for (i=0;i