def _test11(): # A reduced version of test10 ret = "" # GmmMgr setup num_states = 2 dimension = 2 models = [] for i in xrange(num_states): dm = DummyModel(dimension, 1.0) models.append(dm) gmm_mgr = GmmMgr(models) gb = GraphBuilder() node_id0 = gb.new_node((0, 0)) node_id1 = gb.new_node((1, 1)) node_id2 = gb.new_node((2, 1)) node_id3 = gb.new_node((3, 1)) node_id4 = gb.new_node((4, 2)) # The topology here is slightly complex than the previous example arc_id = gb.new_arc(node_id0, node_id1) arc_id = gb.new_arc(node_id1, node_id4) arc_id = gb.new_arc(node_id0, node_id2) arc_id = gb.new_arc(node_id2, node_id3) arc_id = gb.new_arc(node_id3, node_id4) arc_id = gb.new_arc(node_id2, node_id4) gr0 = FrozenGraph(gb) # Make two Hmms with 3 states and order 2 (self loop, forward 1) # The models in the middle are special and can skip. seed(0) hmm0 = make_forward_hmm(gmm_mgr, num_states, order=2, exact=False) hmm1 = Hmm(1) trans = array(((0.0, 0.5, 0.5), (0.0, 0.5, 0.5), (0.0, 0.0, 0.0))) hmm1.build_model(gmm_mgr, (0, ), 1, 1, trans) hmm2 = make_forward_hmm(gmm_mgr, num_states, order=2, exact=True) hmm_mgr = HmmMgr((hmm0, hmm1, hmm2)) spd = {} spd[(0, 1)] = (0.4, ) spd[(0, 2)] = (0.6, ) spd[(2, 3)] = (0.4, ) spd[(2, 4)] = (0.6, ) tg0 = TrainingGraph(gr0, hmm_mgr, split_prob_dict=spd) if do_display: tg0.dot_display() tg0.dot_display(expand_hmms=True) with DebugPrint("bwt_ctsh") if True else DebugPrint(): result_hmm = tg0.convert_to_standalone_hmm() ret += "\n\n========= TG CONVERTED TO Hmm =========\n\n" + result_hmm.to_string( full=True) return ret
def _test11(): # A reduced version of test10 ret = "" # GmmMgr setup num_states = 2 dimension = 2 models = [] for i in xrange(num_states): dm = DummyModel(dimension, 1.0) models.append(dm) gmm_mgr = GmmMgr(models) gb = GraphBuilder() node_id0 = gb.new_node((0, 0)) node_id1 = gb.new_node((1, 1)) node_id2 = gb.new_node((2, 1)) node_id3 = gb.new_node((3, 1)) node_id4 = gb.new_node((4, 2)) # The topology here is slightly complex than the previous example arc_id = gb.new_arc(node_id0, node_id1) arc_id = gb.new_arc(node_id1, node_id4) arc_id = gb.new_arc(node_id0, node_id2) arc_id = gb.new_arc(node_id2, node_id3) arc_id = gb.new_arc(node_id3, node_id4) arc_id = gb.new_arc(node_id2, node_id4) gr0 = FrozenGraph(gb) # Make two Hmms with 3 states and order 2 (self loop, forward 1) # The models in the middle are special and can skip. seed(0) hmm0 = make_forward_hmm(gmm_mgr, num_states, order=2, exact=False) hmm1 = Hmm(1) trans = array(((0.0, 0.5, 0.5), (0.0, 0.5, 0.5), (0.0, 0.0, 0.0))) hmm1.build_model(gmm_mgr, (0,), 1, 1, trans) hmm2 = make_forward_hmm(gmm_mgr, num_states, order=2, exact=True) hmm_mgr = HmmMgr((hmm0, hmm1, hmm2)) spd = {} spd[(0, 1)] = (0.4,) spd[(0, 2)] = (0.6,) spd[(2, 3)] = (0.4,) spd[(2, 4)] = (0.6,) tg0 = TrainingGraph(gr0, hmm_mgr, split_prob_dict=spd) if do_display: tg0.dot_display() tg0.dot_display(expand_hmms=True) with DebugPrint("bwt_ctsh") if True else DebugPrint(): result_hmm = tg0.convert_to_standalone_hmm() ret += "\n\n========= TG CONVERTED TO Hmm =========\n\n" + result_hmm.to_string(full=True) return ret
def _test9(): # Like test8, but now HMMs have multiple inputs and outputs. ret = "" # GmmMgr setup num_states = 3 dimension = 2 models = [] for i in xrange(num_states): dm = DummyModel(dimension, 1.0) models.append(dm) gmm_mgr = GmmMgr(models) gb = GraphBuilder() node_id0 = gb.new_node((0, 0)) node_id1 = gb.new_node((1, 1)) node_id2 = gb.new_node((2, 1)) node_id3 = gb.new_node((3, 1)) node_id4 = gb.new_node((4, 1)) node_id5 = gb.new_node((5, 2)) arc_id = gb.new_arc(node_id0, node_id1) arc_id = gb.new_arc(node_id1, node_id2) arc_id = gb.new_arc(node_id2, node_id3) arc_id = gb.new_arc(node_id3, node_id4) arc_id = gb.new_arc(node_id4, node_id5) gr0 = FrozenGraph(gb) # Make two Hmms with 3 states and order 3 (self loop, forward 1, forward 2) # The models in the middle are special and can skip directly seed(0) hmm0 = make_forward_hmm(gmm_mgr, num_states, order=3, exact=True) hmm1 = Hmm(1) trans = array( ( (0.0, 0.0, 0.0, 0.5, 0.5, 0.0, 0.0), (0.0, 0.0, 0.0, 0.5, 0.0, 0.5, 0.0), (0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.5), (0.0, 0.0, 0.0, 0.5, 0.35, 0.1, 0.05), (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), ) ) hmm1.build_model(gmm_mgr, (0,), 3, 3, trans) hmm2 = make_forward_hmm(gmm_mgr, num_states, order=3, exact=True) hmm_mgr = HmmMgr((hmm0, hmm1, hmm2)) with DebugPrint("bwt_vrfy") if False else DebugPrint(): tg0 = TrainingGraph(gr0, hmm_mgr, split_prob_dict=dict()) result_hmm = tg0.convert_to_standalone_hmm() ret += "\n\n========= TG CONVERTED TO Hmm =========\n\n" + result_hmm.to_string(full=True) return ret
def make_forward_hmm(gmm_mgr, num_states, order, models=None, exact=False): hmm0 = Hmm(num_states) # generate a set of random indices from the GmmMgr models = tuple(randint(0, gmm_mgr.num_models - 1) for i in xrange(num_states)) if models is None else models trans = tuple([p] * num_states for p in toy_probs(order)) if exact: hmm0.build_forward_model_exact(gmm_mgr, models, order, trans) else: hmm0.build_forward_model_compact(gmm_mgr, models, order, trans) return hmm0
def read_hmm(stream, user_data, header_tokens): gmm_mgr, log_domain = user_data v,num_inputs = stream.read_scalar("num_inputs", int) v,num_states = stream.read_scalar("num_states", int) v,num_outputs = stream.read_scalar("num_outputs", int) s = num_inputs + num_states + num_outputs v,trans_array = stream.read_array("transition_matrix", rtype=float, dim=2, shape=(s,s)) v,models = stream.read_list("models", rtype=int, count=num_states) # Construct and return Hmm object ret = Hmm(num_states, log_domain=log_domain) ret.build_model(gmm_mgr, models, num_inputs, num_outputs, trans_array) return ret
def _test9(): # Like test8, but now HMMs have multiple inputs and outputs. ret = "" # GmmMgr setup num_states = 3 dimension = 2 models = [] for i in xrange(num_states): dm = DummyModel(dimension, 1.0) models.append(dm) gmm_mgr = GmmMgr(models) gb = GraphBuilder() node_id0 = gb.new_node((0, 0)) node_id1 = gb.new_node((1, 1)) node_id2 = gb.new_node((2, 1)) node_id3 = gb.new_node((3, 1)) node_id4 = gb.new_node((4, 1)) node_id5 = gb.new_node((5, 2)) arc_id = gb.new_arc(node_id0, node_id1) arc_id = gb.new_arc(node_id1, node_id2) arc_id = gb.new_arc(node_id2, node_id3) arc_id = gb.new_arc(node_id3, node_id4) arc_id = gb.new_arc(node_id4, node_id5) gr0 = FrozenGraph(gb) # Make two Hmms with 3 states and order 3 (self loop, forward 1, forward 2) # The models in the middle are special and can skip directly seed(0) hmm0 = make_forward_hmm(gmm_mgr, num_states, order=3, exact=True) hmm1 = Hmm(1) trans = array(((0.0, 0.0, 0.0, 0.5, 0.5, 0.0, 0.0), (0.0, 0.0, 0.0, 0.5, 0.0, 0.5, 0.0), (0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.5), (0.0, 0.0, 0.0, 0.5, 0.35, 0.1, 0.05), (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0))) hmm1.build_model(gmm_mgr, (0, ), 3, 3, trans) hmm2 = make_forward_hmm(gmm_mgr, num_states, order=3, exact=True) hmm_mgr = HmmMgr((hmm0, hmm1, hmm2)) with DebugPrint("bwt_vrfy") if False else DebugPrint(): tg0 = TrainingGraph(gr0, hmm_mgr, split_prob_dict=dict()) result_hmm = tg0.convert_to_standalone_hmm() ret += "\n\n========= TG CONVERTED TO Hmm =========\n\n" + result_hmm.to_string( full=True) return ret
def add_epsilon_model(self, gmm_mgr, arity, log_domain=False): """ Add an epsilon model with the given arity if it doesn't already exist. arity is an int with value > 0. Returns the index of the model added. This call can only be made when the adaptation state is NOT_ADAPTING. >>> hmm_mgr0 = HmmMgr(12) >>> hmm_mgr0.add_epsilon_model(None, 1) 0 >>> hmm_mgr0.add_epsilon_model(None, 2) 1 >>> hmm_mgr0.add_epsilon_model(None, 4) 2 >>> print hmm_mgr0 HmmMgr with 3 models >>> hmm_mgr0.input_arity_set set([1, 2, 4]) >>> hmm_mgr0.output_arity_set set([1, 2, 4]) >>> idx = hmm_mgr0.get_epsilon_model_index(2) # >>> hmm_mgr0[idx].dot_display() """ self._require_state("NOT_ADAPTING") if arity <= 0: raise ValueError("Expected arity to be > 0, but got %d" % (arity,)) if self.has_epsilon_model(arity): return self.get_epsilon_model_index() epsilon_model = Hmm(0, log_domain) order = arity + 1 epsilon_model.build_forward_model_compact(gmm_mgr, (), order, repeat((), order)) temp_models = list(self._models) temp_models.append(epsilon_model) self._models = tuple(temp_models) new_index = len(self._models) - 1 self._epsilon_model_map[arity] = new_index return new_index
def test5(num_obs, do_display=False): # A test in which one of the HMMs has a transition from an input directly to # an output, so it can behave as an epsilon. This node is between two other # nodes in a linear arrangement. # Data generator setup and data generation dimension = 2 obs_gen = make_data_generator(dimension) obs_list = [obs_gen.next() for i in xrange(num_obs)] # GmmMgr setup num_models = 20 models = make_standard_gmms(dimension, num_models) gmm_mgr1 = GmmMgr(models[0:10]) gmm_mgr2 = GmmMgr(models[10:20]) # Hmm setup # Make two Hmms with 2 states and order 2 (self loop, forward 1) The model # in the middle is special in that it can skip directly from the input state # to the output state. seed(0) num_states = 2 hmm0 = make_forward_hmm(gmm_mgr1, num_states, 2, exact=False) hmm1 = Hmm(1) trans = array(((0.0, 0.5, 0.5), (0.0, 0.5, 0.5), (0.0, 0.0, 0.0))) hmm1.build_model(gmm_mgr1, (0, ), 1, 1, trans) hmm2 = make_forward_hmm(gmm_mgr1, num_states, 2, exact=False) hmm_mgr = HmmMgr((hmm0, hmm1, hmm2)) # TrainingGraph setup gb = GraphBuilder() node_id0 = gb.new_node((0, 0)) node_id1 = gb.new_node((1, 1)) # node_id2 = gb.new_node((2,2)) arc_id = gb.new_arc(node_id0, node_id1) # arc_id = gb.new_arc(node_id1, node_id2) gr0 = FrozenGraph(gb) tg0 = TrainingGraph(gr0, hmm_mgr, split_prob_dict=dict()) if do_display: tg0.dot_display() tg0.dot_display(expand_hmms=True) valid, ret = validate_training_graph(tg0, gmm_mgr1, hmm_mgr, obs_list, 1, gmm_mgr2) return ret
def test5(num_obs, do_display=False): # A test in which one of the HMMs has a transition from an input directly to # an output, so it can behave as an epsilon. This node is between two other # nodes in a linear arrangement. # Data generator setup and data generation dimension = 2 obs_gen = make_data_generator(dimension) obs_list = [obs_gen.next() for i in xrange(num_obs)] # GmmMgr setup num_models = 20 models = make_standard_gmms(dimension, num_models) gmm_mgr1 = GmmMgr(models[0:10]) gmm_mgr2 = GmmMgr(models[10:20]) # Hmm setup # Make two Hmms with 2 states and order 2 (self loop, forward 1) The model # in the middle is special in that it can skip directly from the input state # to the output state. seed(0) num_states = 2 hmm0 = make_forward_hmm(gmm_mgr1, num_states, 2, exact=False) hmm1 = Hmm(1) trans = array(((0.0, 0.5, 0.5), (0.0, 0.5, 0.5), (0.0, 0.0, 0.0))) hmm1.build_model(gmm_mgr1, (0,), 1, 1, trans) hmm2 = make_forward_hmm(gmm_mgr1, num_states, 2, exact=False) hmm_mgr = HmmMgr((hmm0, hmm1, hmm2)) # TrainingGraph setup gb = GraphBuilder() node_id0 = gb.new_node((0, 0)) node_id1 = gb.new_node((1, 1)) # node_id2 = gb.new_node((2,2)) arc_id = gb.new_arc(node_id0, node_id1) # arc_id = gb.new_arc(node_id1, node_id2) gr0 = FrozenGraph(gb) tg0 = TrainingGraph(gr0, hmm_mgr, split_prob_dict=dict()) if do_display: tg0.dot_display() tg0.dot_display(expand_hmms=True) valid, ret = validate_training_graph(tg0, gmm_mgr1, hmm_mgr, obs_list, 1, gmm_mgr2) return ret
def make_forward_hmm(gmm_mgr, num_states, order, models=None, exact=False): hmm0 = Hmm(num_states) # generate a set of random indices from the GmmMgr models = tuple( randint(0, gmm_mgr.num_models - 1) for i in xrange(num_states)) if models is None else models trans = tuple([p] * num_states for p in toy_probs(order)) if exact: hmm0.build_forward_model_exact(gmm_mgr, models, order, trans) else: hmm0.build_forward_model_compact(gmm_mgr, models, order, trans) return hmm0
def test9(num_obs): """ Test sequence scoring interface. """ 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) obs_gen = obs_generator(generators, target_durations) # 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(full=True) for i in xrange(num_obs): obs = obs_gen.next() scores = hmm0.forward_score_sequence(obs) print scores
def _test10(): # Like test9, but now HMMs are arranged in a diamond pattern so inter-HMM # probabilities come into play ret = "" # GmmMgr setup num_states = 3 dimension = 2 models = [] for i in xrange(num_states): dm = DummyModel(dimension, 1.0) models.append(dm) gmm_mgr = GmmMgr(models) gb = GraphBuilder() node_id0 = gb.new_node((0, 0)) node_id1 = gb.new_node((1, 1)) node_id2 = gb.new_node((2, 1)) node_id3 = gb.new_node((3, 1)) node_id4 = gb.new_node((4, 1)) node_id5 = gb.new_node((5, 2)) # The topology here is more complex than previous examples arc_id = gb.new_arc(node_id0, node_id1) arc_id = gb.new_arc(node_id1, node_id5) arc_id = gb.new_arc(node_id0, node_id2) arc_id = gb.new_arc(node_id2, node_id3) arc_id = gb.new_arc(node_id3, node_id4) arc_id = gb.new_arc(node_id3, node_id5) arc_id = gb.new_arc(node_id4, node_id5) gr0 = FrozenGraph(gb) # Make two Hmms with 3 states and order 3 (self loop, forward 1, forward 2) # The models in the middle are special and can skip. seed(0) hmm0 = make_forward_hmm(gmm_mgr, num_states, order=3, exact=True) hmm1 = Hmm(1) trans = array(((0.0, 0.0, 0.0, 0.5, 0.5, 0.0, 0.0), (0.0, 0.0, 0.0, 0.5, 0.0, 0.5, 0.0), (0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.5), (0.0, 0.0, 0.0, 0.5, 0.35, 0.1, 0.05), (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0))) hmm1.build_model(gmm_mgr, (0, ), 3, 3, trans) hmm2 = make_forward_hmm(gmm_mgr, num_states, order=3, exact=True) hmm_mgr = HmmMgr((hmm0, hmm1, hmm2)) spd = {} spd[(0, 1)] = (0.4, 0.3, 0.8) spd[(0, 2)] = (0.6, 0.7, 0.2) spd[(3, 4)] = (0.4, 0.3, 0.8) spd[(3, 5)] = (0.6, 0.7, 0.2) tg0 = TrainingGraph(gr0, hmm_mgr, split_prob_dict=spd) with DebugPrint("bwt_ctsh") if True else DebugPrint(): result_hmm = tg0.convert_to_standalone_hmm() ret += "\n\n========= TG CONVERTED TO Hmm =========\n\n" + result_hmm.to_string( full=True) return ret
hmm_mgr = HmmMgr(dim) gmm_mgr = GmmMgr(dim) hmms = [] names = [] unnamed_index = 0 for label, m in models: assert label == 'HMM' dc and dc("m = \n%s" % (pformat(m),)) dc and dc("m.keys() = \n%s" % (m.keys(),)) if m.hasattr.decl: name = m.decl else: name = ("UnnamedModel%d" % unnamed_index) unnamed_index += 1 n = m.numstates - 2 # HTK numstates counts virtual entry and exit states hmm = Hmm(n, log_domain) gmms = [] for s_label, state in m.states: assert s_label == 'state' dc and dc("state.keys() = \n%s" % (state.keys(),)) num_mixtures = 1 weights = array((1.0,), dtype = float) gmm = GaussianMixtureModel(dim, covar_type, num_mixtures) gmm.set_weights(weights) gmm.set_model(state.mean, state.var) dc and dc("gmm = %s" % (gmm,)) gmms.append(gmm) model_indices = gmm_mgr.add_models(gmms) hmm.build_model(gmm_mgr, model_indices, 1, 1, m.transp) hmms.append(hmm)
def test7(): """ This test builds an Hmm with dummy models which always give a score of 1, but with a somewhat unusual topology in which there are 6 actual states chained together with 2 virtual inputs and 3 virtual outputs. The point is to make sure we can handle this asymetric case correctly. This is the same as test6 except that now we'll use the network adaptation interface instead. """ import pprint num_states = 6 dimension = 2 # GmmMgr setup models = [] for i in xrange(num_states): dm = DummyModel(dimension, 1.0) models.append(dm) mm = GmmMgr(models) # Hmm setup T0: i1 i2 1 2 3 4 5 6 o1 o2 o3 FROM: trans = array(((0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), # i1 (0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), # i2 (0.0, 0.0, 0.5, 0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), # 1 (0.0, 0.0, 0.0, 0.5, 0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), # 2 (0.0, 0.0, 0.0, 0.0, 0.5, 0.5, 0.0, 0.0, 0.0, 0.0, 0.0), # 3 (0.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.4, 0.0, 0.1, 0.0, 0.0), # 4 (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.4, 0.0, 0.1, 0.0), # 5 (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.5), # 6 (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), # o1 (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), # o2 (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0))) # o3 hmm0 = Hmm(num_states, log_domain=True) models = range(num_states) hmm0.build_model(mm, models, 2, 3, trans) print hmm0.to_string(True) num_passes = 1 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") hmm0.begin_adapt("NETWORK") mm.set_adaptation_state("ACCUMULATING") num_obs = 11 obs = [array((0,0))] * num_obs # Dummy sequence context = hmm0.init_for_forward_pass(obs, terminal = True) # Add some mass into the system for the forward pass. To match the behavior of # standalone adaptation, we divide an initial mass of 1 evenly across the inputs hmm0.accum_input_alphas(context, array([1.0/hmm0.num_inputs] * hmm0.num_inputs)) # Actually do the forward pass. Note that we must process one more frame than the number of # observations - this is because an extra frame is automatically added which scores 1 on the exit # states of the Hmm (and 0 on all real states). XXX we might want clients to do this for # themselves at some point rather than this automatic behavior: for frame in xrange(num_obs + 1): output_alphas = hmm0.process_one_frame_forward(context) print output_alphas # Likewise, we initialize and then make the backward pass: hmm0.init_for_backward_pass(context) hmm0.accum_input_betas(context, array([1.0] * hmm0.num_outputs)) for frame in xrange(num_obs + 1): output_betas = hmm0.process_one_frame_backward(context) print output_betas # Now collect all the gamma sums; here there's only one: norm = hmm0.get_initial_gamma_sum() hmm0.add_to_gamma_sum(norm, context) # Here's where the actual accumulation happens: hmm0.do_accumulation(context, norm) mm.set_adaptation_state("APPLYING") hmm0.end_adapt() mm.apply_all_accumulators() mm.set_adaptation_state("NOT_ADAPTING") print hmm0.to_string(True)
def test8_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. This is identical to test5 except that here I have built the Hmm with only one Gmm, which is shared by all three states. """ 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 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 = (gmm,) mm = GmmMgr(models) # Here's where we're using the same Gmm in all three states of this Hmm. models = (0, 0, 0) # 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, log_domain=True) hmm0.build_model(mm, models, 1, 1, trans) print hmm0.to_string(True) 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) mm.set_adaptation_state("APPLYING") hmm0.end_adapt() mm.apply_all_accumulators() mm.set_adaptation_state("NOT_ADAPTING") print hmm0.to_string(True)
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, log_domain=True) hmm0.build_model(mm, models, 1, 1, trans) print hmm0.to_string(True) 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)
def test6(): """ This test builds an Hmm with dummy models which always give a score of 1, but with a somewhat unusual topology in which there are 6 actual states chained together with 2 virtual inputs and 3 virtual outputs. The point is to make sure we can handle this asymetric case correctly. """ import pprint num_states = 6 dimension = 2 # GmmMgr setup models = [] for i in xrange(num_states): dm = DummyModel(dimension, 1.0) models.append(dm) mm = GmmMgr(models) models = range(num_states) # Hmm setup T0: i1 i2 1 2 3 4 5 6 o1 o2 o3 FROM: trans = array(((0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), # i1 (0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), # i2 (0.0, 0.0, 0.5, 0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), # 1 (0.0, 0.0, 0.0, 0.5, 0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), # 2 (0.0, 0.0, 0.0, 0.0, 0.5, 0.5, 0.0, 0.0, 0.0, 0.0, 0.0), # 3 (0.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.4, 0.0, 0.1, 0.0, 0.0), # 4 (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.4, 0.0, 0.1, 0.0), # 5 (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.5), # 6 (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), # o1 (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), # o2 (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0))) # o3 hmm0 = Hmm(num_states, log_domain=True) hmm0.build_model(mm, models, 2, 3, trans) print hmm0.to_string(True) num_passes = 1 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 = [array((0,0))] * 11 # Dummy sequence of length 11 hmm0.adapt_one_sequence(obs) mm.set_adaptation_state("APPLYING") hmm0.end_adapt() mm.apply_all_accumulators() mm.set_adaptation_state("NOT_ADAPTING") print hmm0.to_string(True)
def test3_helper(dataset_idx, num_passes): """ This tests mimics a run ChrisW did with HTK. The models are 2-D single-mode Gaussians embedded in a 1-state Hmm. Each data point is taken as a sequence of length 1. """ dimension = 2 # Gmm setup 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) mm = GmmMgr((gmm,)) # 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. trans = array(((0.0, 1.0, 0.0), (0.0, 0.0, 1.0), (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) # adaptation data = datasets[dataset_idx] for p in xrange(num_passes): mm.set_adaptation_state("INITIALIZING") mm.clear_all_accumulators() hmm0.begin_adapt("STANDALONE") mm.set_adaptation_state("ACCUMULATING") for point in data: s = array(point) # We treat each point as an entire sequence hmm0.adapt_one_sequence((s,)) mm.set_adaptation_state("APPLYING") hmm0.end_adapt() mm.apply_all_accumulators() mm.set_adaptation_state("NOT_ADAPTING") print hmm0.to_string(True)
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)
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 test2(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 # A transition probability matrix with a p=1 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.0, 1.0), (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 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") for point in data: # We treat each point as an entire sequence hmm0.adapt_one_sequence((point,)) 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) + '\n' print gmm0
def _test10(): # Like test9, but now HMMs are arranged in a diamond pattern so inter-HMM # probabilities come into play ret = "" # GmmMgr setup num_states = 3 dimension = 2 models = [] for i in xrange(num_states): dm = DummyModel(dimension, 1.0) models.append(dm) gmm_mgr = GmmMgr(models) gb = GraphBuilder() node_id0 = gb.new_node((0, 0)) node_id1 = gb.new_node((1, 1)) node_id2 = gb.new_node((2, 1)) node_id3 = gb.new_node((3, 1)) node_id4 = gb.new_node((4, 1)) node_id5 = gb.new_node((5, 2)) # The topology here is more complex than previous examples arc_id = gb.new_arc(node_id0, node_id1) arc_id = gb.new_arc(node_id1, node_id5) arc_id = gb.new_arc(node_id0, node_id2) arc_id = gb.new_arc(node_id2, node_id3) arc_id = gb.new_arc(node_id3, node_id4) arc_id = gb.new_arc(node_id3, node_id5) arc_id = gb.new_arc(node_id4, node_id5) gr0 = FrozenGraph(gb) # Make two Hmms with 3 states and order 3 (self loop, forward 1, forward 2) # The models in the middle are special and can skip. seed(0) hmm0 = make_forward_hmm(gmm_mgr, num_states, order=3, exact=True) hmm1 = Hmm(1) trans = array( ( (0.0, 0.0, 0.0, 0.5, 0.5, 0.0, 0.0), (0.0, 0.0, 0.0, 0.5, 0.0, 0.5, 0.0), (0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.5), (0.0, 0.0, 0.0, 0.5, 0.35, 0.1, 0.05), (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), ) ) hmm1.build_model(gmm_mgr, (0,), 3, 3, trans) hmm2 = make_forward_hmm(gmm_mgr, num_states, order=3, exact=True) hmm_mgr = HmmMgr((hmm0, hmm1, hmm2)) spd = {} spd[(0, 1)] = (0.4, 0.3, 0.8) spd[(0, 2)] = (0.6, 0.7, 0.2) spd[(3, 4)] = (0.4, 0.3, 0.8) spd[(3, 5)] = (0.6, 0.7, 0.2) tg0 = TrainingGraph(gr0, hmm_mgr, split_prob_dict=spd) with DebugPrint("bwt_ctsh") if True else DebugPrint(): result_hmm = tg0.convert_to_standalone_hmm() ret += "\n\n========= TG CONVERTED TO Hmm =========\n\n" + result_hmm.to_string(full=True) return ret