def test_rand(): n = 5 m = 9 d = 2 atmp = numpy.random.random_sample((n, n)) row_sums = atmp.sum(axis=1) a = numpy.array(atmp / row_sums[:, numpy.newaxis], dtype=numpy.double) wtmp = numpy.random.random_sample((n, m)) row_sums = wtmp.sum(axis=1) w = numpy.array(wtmp / row_sums[:, numpy.newaxis], dtype=numpy.double) means = numpy.array((0.6 * numpy.random.random_sample((n, m, d)) - 0.3), dtype=numpy.double) covars = numpy.zeros( (n,m,d,d) ) for i in xrange(n): for j in xrange(m): for k in xrange(d): covars[i][j][k][k] = 1 pitmp = numpy.random.random_sample((n)) pi = numpy.array(pitmp / sum(pitmp), dtype=numpy.double) gmmhmm = GMHMM(n,m,d,a,means,covars,w,pi,init_type='user',verbose=True) obs = numpy.array((0.6 * numpy.random.random_sample((40,d)) - 0.3), dtype=numpy.double) print "Doing Baum-welch" gmmhmm.train(obs,1000) print print "Pi",gmmhmm.pi print "A",gmmhmm.A print "weights", gmmhmm.w print "means", gmmhmm.means print "covars", gmmhmm.covars
def test_rand(): n = 5 m = 4 d = 2 atmp = numpy.random.random_sample((n, n)) row_sums = atmp.sum(axis=1) a = numpy.array(atmp / row_sums[:, numpy.newaxis], dtype=numpy.double) wtmp = numpy.random.random_sample((n, m)) row_sums = wtmp.sum(axis=1) w = numpy.array(wtmp / row_sums[:, numpy.newaxis], dtype=numpy.double) means = numpy.array((0.6 * numpy.random.random_sample((n, m, d)) - 0.3), dtype=numpy.double) covars = numpy.zeros( (n,m,d,d) ) for i in range(n): for j in range(m): for k in range(d): covars[i][j][k][k] = 1 pitmp = numpy.random.random_sample((n)) pi = numpy.array(pitmp / sum(pitmp), dtype=numpy.double) gmmhmm = GMHMM(n,m,d,a,means,covars,w,pi,init_type='user',verbose=True) obs = numpy.array((0.6 * numpy.random.random_sample((40,d)) - 0.3), dtype=numpy.double) print("Doing Baum-welch") gmmhmm.train(obs,1000) print() print("Pi",gmmhmm.pi) print("A",gmmhmm.A) print("weights", gmmhmm.w) print("means", gmmhmm.means) print("covars", gmmhmm.covars)
def test_simple(): n = 2 m = 2 d = 2 pi = numpy.array([0.5, 0.5]) A = numpy.ones((n, n), dtype=numpy.double) / float(n) w = numpy.ones((n, m), dtype=numpy.double) means = numpy.ones((n, m, d), dtype=numpy.double) covars = [[numpy.matrix(numpy.eye(d, d)) for j in xrange(m)] for i in xrange(n)] w[0][0] = 0.5 w[0][1] = 0.5 w[1][0] = 0.5 w[1][1] = 0.5 means[0][0][0] = 0.5 means[0][0][1] = 0.5 means[0][1][0] = 0.5 means[0][1][1] = 0.5 means[1][0][0] = 0.5 means[1][0][1] = 0.5 means[1][1][0] = 0.5 means[1][1][1] = 0.5 gmmhmm = GMHMM(n, m, d, A, means, covars, w, pi, init_type='user', verbose=True) obs = numpy.array([[0.3, 0.3], [0.1, 0.1], [0.2, 0.2]]) print "Doing Baum-welch" gmmhmm.train(obs, 10) print print "Pi", gmmhmm.pi print "A", gmmhmm.A print "weights", gmmhmm.w print "means", gmmhmm.means print "covars", gmmhmm.covars
def trainModel(self, obs): pi = numpy.array([0.2, 0.2, 0.2, 0.2, 0.2]) A = numpy.ones((self.n, self.n), dtype=numpy.double) / float(self.n) w = numpy.ones((self.n, self.m), dtype=numpy.double) means = numpy.ones((self.n, self.m, self.d), dtype=numpy.double) covars = [[ numpy.matrix(numpy.eye(self.d, self.d)) for j in xrange(self.m) ] for i in xrange(self.n)] n_iter = 20 '''w[0][0] = 0.5 w[0][1] = 0.5 w[1][0] = 0.5 w[1][1] = 0.5 means[0][0][0] = 0.5 means[0][0][1] = 0.5 means[0][1][0] = 0.5 means[0][1][1] = 0.5 means[1][0][0] = 0.5 means[1][0][1] = 0.5 means[1][1][0] = 0.5 means[1][1][1] = 0.5 ''' gmmhmm = GMHMM(self.n, self.m, self.d, A, means, covars, w, pi, init_type='user', verbose=True) print "Doing Baum-welch" #gmmhmm.train(obs,10) if len(obs.shape) == 2: gmmhmm.train(obs) return self elif len(obs.shape) == 3: count = obs.shape[0] for n in range(count): gmmhmm.train(obs[n, :, :]) return self
def test_simple(): n = 2 m = 2 d = 2 pi = numpy.array([0.5, 0.5]) A = numpy.ones((n,n),dtype=numpy.double)/float(n) w = numpy.ones((n,m),dtype=numpy.double) means = numpy.ones((n,m,d),dtype=numpy.double) covars = [[ numpy.matrix(numpy.eye(d,d)) for j in xrange(m)] for i in xrange(n)] w[0][0] = 0.5 w[0][1] = 0.5 w[1][0] = 0.5 w[1][1] = 0.5 means[0][0][0] = 0.5 means[0][0][1] = 0.5 means[0][1][0] = 0.5 means[0][1][1] = 0.5 means[1][0][0] = 0.5 means[1][0][1] = 0.5 means[1][1][0] = 0.5 means[1][1][1] = 0.5 gmmhmm = GMHMM(n,m,d,A,means,covars,w,pi,init_type='user',verbose=True) obs = numpy.array([ [0.3,0.3], [0.1,0.1], [0.2,0.2]]) print "Doing Baum-welch" gmmhmm.train(obs,10) print print "Pi",gmmhmm.pi print "A",gmmhmm.A print "weights", gmmhmm.w print "means", gmmhmm.means print "covars", gmmhmm.covars