def test2a(num_obs, num_passes): dimension = 2 # Data generator setup target_means = (1,1) target_vars = (0.1,0.1) generator = SimpleGaussianModel(dimension, SimpleGaussianModel.DIAGONAL_COVARIANCE) generator.set_model(target_means, target_vars) SimpleGaussianModel.seed(0) data = [generator.sample() for i in xrange(num_obs)] # Gmm setup num_mixtures = 2 gmm0 = make_gmm_diag(dimension, num_mixtures) gmm1 = make_gmm_diag(dimension, num_mixtures) mm = GmmMgr((gmm1,)) # Hmm setup # A transition probability matrix with a p ~= 1 self-loop for the real state. # The entry state feeds into the real state with p=1. We use p ~= 1 for the # second self loop since we need *some* probability of finishing. trans = array(((0.0, 1.0, 0.0), (0.0, 0.999999999999, 0.000000000001), (0.0, 0.0, 0.0))) hmm0 = Hmm(1, log_domain=True) hmm0.build_model(mm, (0,), 1, 1, trans) print hmm0.to_string(True) + '\n' print gmm0 print '\n\n' # Try some adaptation for p in xrange(num_passes): mm.set_adaptation_state("INITIALIZING") mm.clear_all_accumulators() hmm0.begin_adapt("STANDALONE") mm.set_adaptation_state("ACCUMULATING") hmm0.adapt_one_sequence(data) mm.set_adaptation_state("APPLYING") hmm0.end_adapt() mm.apply_all_accumulators() mm.set_adaptation_state("NOT_ADAPTING") really_print = False with DebugPrint("gaussian", "gaussian_pt", "gaussian_gmm_score") if really_print else DebugPrint(): gmm0.adapt(data, max_iters = num_passes) print hmm0.to_string(True) + '\n' print gmm0
def test1(num_obs, num_passes): dimension = 2 # Data generator setup target_means = (1,1) target_vars = (0.1,0.1) generator = SimpleGaussianModel(dimension, SimpleGaussianModel.DIAGONAL_COVARIANCE) generator.set_model(target_means, target_vars) SimpleGaussianModel.seed(0) GaussianMixtureModel.seed(0) # Gmm setup num_mixtures = 2 gmm0 = make_gmm(dimension, num_mixtures) gmm1 = make_gmm(dimension, num_mixtures) mm = GmmMgr((gmm1,)) # Hmm setup hmm0 = Hmm(1, log_domain=True) # A transition probability matrix with a p=1/2 exit for the real state. # The entry state feeds into the real state with p=1. trans = array(((0.0, 1.0, 0.0), (0.0, 0.5, 0.5), (0.0, 0.0, 0.0))) hmm0.build_model(mm, (0,), 1, 1, trans) print hmm0.to_string(True) print gmm0 # Try some adaptation. Note that we are feeding the entire data set as one stream # to the Hmm adaption call. data = [generator.sample() for i in xrange(num_obs)] for p in xrange(num_passes): mm.set_adaptation_state("INITIALIZING") mm.clear_all_accumulators() hmm0.begin_adapt("STANDALONE") mm.set_adaptation_state("ACCUMULATING") hmm0.adapt_one_sequence(data) mm.set_adaptation_state("APPLYING") hmm0.end_adapt() mm.apply_all_accumulators() mm.set_adaptation_state("NOT_ADAPTING") gmm0.adapt(data, max_iters = num_passes) print hmm0.to_string(True) print gmm0
def test0(num_obs, num_passes): dimension = 2 # Data generator setup target_means = (1,1) target_vars = (0.1,0.1) generator = SimpleGaussianModel(dimension, SimpleGaussianModel.DIAGONAL_COVARIANCE) generator.set_model(target_means, target_vars) SimpleGaussianModel.seed(0) GaussianMixtureModel.seed(0) mm = GmmMgr(dimension) # Hmm setup hmm0 = Hmm(0, log_domain=True) # A transition probability matrix with no real state. # The entry state feeds into the exit state with p=1. trans = array(((0.0, 1.0), (0.0, 0.0))) hmm0.build_model(mm, (), 1, 1, trans) print hmm0.to_string(True) # Try some adaptation. Note that we are feeding the entire data set as one stream # to the Hmm adaption call. data = [generator.sample() for i in xrange(num_obs)] for p in xrange(num_passes): mm.set_adaptation_state("INITIALIZING") mm.clear_all_accumulators() hmm0.begin_adapt("STANDALONE") mm.set_adaptation_state("ACCUMULATING") with DebugPrint("hmm_gxfs", "hmm_aos") if False else DebugPrint(): hmm0.adapt_one_sequence(data) mm.set_adaptation_state("APPLYING") hmm0.end_adapt() mm.apply_all_accumulators() mm.set_adaptation_state("NOT_ADAPTING") print hmm0.to_string(True)