示例#1
0
def test5_helper(num_obs, num_passes):
    """
    This tests mimics a run ChrisW did with HTK.  The models are 2-D single-mode Gaussians
    embedded in a 3-state Hmm.  Each observation is a sequence of length 11, taken by sampling
    2, 3, and 6 times, respectively, from three target distributions.    
    """
    import pprint
    num_states = 3
    dimension = 2
    
    # Data generator setup
    target_means = ((1,1), (2,2), (3,3))
    target_vars = ((0.1,0.1), (0.2,0.2), (0.3,0.3))
    target_durations = (2, 3, 6)
    num_steps = sum(target_durations)
    generators = [SimpleGaussianModel(dimension, SimpleGaussianModel.DIAGONAL_COVARIANCE) for i in xrange(num_states)]
    [m.set_model(tm, tv) for (m, tm, tv) in izip(generators, target_means, target_vars)]
    SimpleGaussianModel.seed(0)

    # Gmm setup
    num_states = 3
    models = []
    for i in xrange(num_states):
        gmm = GaussianMixtureModel(dimension, GaussianMixtureModel.DIAGONAL_COVARIANCE, 1)
        gmm.set_weights(array((1.0,)))
        mu = array(((0.0,0.0),))
        v = array(((1.0,1.0),))
        gmm.set_model(mu, v)
        models.append(gmm)
    
    mm = GmmMgr(models)
    models = range(num_states)

    # Hmm setup
    trans = array(((0.0, 1.0, 0.0, 0.0, 0.0),
                   (0.0, 0.5, 0.5, 0.0, 0.0),
                   (0.0, 0.0, 0.5, 0.5, 0.0),
                   (0.0, 0.0, 0.0, 0.5, 0.5),
                   (0.0, 0.0, 0.0, 0.0, 0.0)))
    hmm0 = Hmm(num_states)
    hmm0.build_model(mm, models, 1, 1, trans)
    print hmm0.to_string(True)

    # Now sample the adapted Hmm
    import sys
    for i in xrange(10):
        sample = hmm0.sample()
        print sample

    for p in xrange(num_passes):
        # Reseeding here ensures we are repeating the same observations in each pass
        SimpleGaussianModel.seed(0)
        mm.set_adaptation_state("INITIALIZING")
        mm.clear_all_accumulators()
        hmm0.begin_adapt("STANDALONE")
        mm.set_adaptation_state("ACCUMULATING")
        obs_gen = obs_generator(generators, target_durations)
        for i in xrange(num_obs):
            obs = obs_gen.next()
            hmm0.adapt_one_sequence(obs)
        
            obs2 = [tuple(a) for a in obs]
            # Uncomment these lines to show observations as nicely formatted sequences; this
            # is what I gave ChrisW to use with his HTK runs.
            # pprint.pprint(obs2)
            # print
        mm.set_adaptation_state("APPLYING")
        hmm0.end_adapt()
        mm.apply_all_accumulators()
        mm.set_adaptation_state("NOT_ADAPTING")
        print hmm0.to_string(True)