Exemplo n.º 1
0
def hmm_test():

    st_time = time.time()

    model_file = "hmm_model.json"

    # load data
    with open(model_file, 'r') as f:
        data = json.load(f)
    A = np.array(data['A'])
    B = np.array(data['B'])
    pi = np.array(data['pi'])
    # observation symbols
    obs_dict = data['observations']
    # state symbols
    states_symbols = dict()
    for idx, item in enumerate(data['states']):
        states_symbols[item] = idx
    Osequence = np.array(data['Osequence'])
    N = len(Osequence)
    model = HMM(pi, A, B, obs_dict, states_symbols)

    delta = model.forward(Osequence)
    m_delta = np.array([[3.5000e-01, 1.3600e-01, 0.0000e+00, 0.0000e+00, 1.1136e-05, 1.1136e-05, 0.0000e+00],
               [1.5000e-01, 3.2000e-02, 4.6400e-03, 2.7840e-04, 3.3408e-05, 1.1136e-05, 8.9088e-07]])

    print("Your forward function output:", delta)
    print("My forward function output:", m_delta)

    gamma = model.backward(Osequence)
    m_gamma = np.array([[1.6896e-06, 3.8400e-06, 6.4000e-05, 2.0000e-03, 1.4000e-02, 2.0000e-02, 1.0000e+00],
               [1.9968e-06, 1.1520e-05, 1.9200e-04, 3.2000e-03, 2.2000e-02, 6.0000e-02, 1.0000e+00]])

    print("Your backward function output:", gamma)
    print("My backward function output:", m_gamma)

    prob1 = model.sequence_prob(Osequence)
    m_prob1 = 8.908800000000002e-07

    print("Your sequence_prob function output:", prob1)
    print("My sequence_prob function output:", m_prob1)

    prob2 = model.posterior_prob(Osequence)
    m_prob2 = np.array([[0.6637931, 0.5862069, 0., 0., 0.175, 0.25, 0.],
               [0.3362069, 0.4137931, 1., 1., 0.825, 0.75, 1.]])

    print("Your posterior_prob function output:", prob2)
    print("My posterior_prob function output:", m_prob2)

    viterbi_path = model.viterbi(Osequence)
    m_viterbi_path = ['1', '1', '2', '2', '2', '2', '2']

    print('Your viterbi function output: ', viterbi_path)
    print('My viterbi function output: ', m_viterbi_path)

    en_time = time.time()
    print()
    print("hmm total time: ", en_time - st_time)
Exemplo n.º 2
0
def test02():
    A = np.array([[0.5, 0.2, 0.3], [0.3, 0.5, 0.2], [0.2, 0.3, 0.5]])
    B = np.array([[0.5, 0.5], [0.4, 0.6], [0.7, 0.3]])
    pi = np.array([0.2, 0.4, 0.4])
    hmm = HMM(A, B, pi)

    pro = hmm.forward([0, 1, 0])
    # 0.130218
    print('前向算法:', pro)
    pro = hmm.backward([0, 1, 0])
    print('后向算法:', pro)
def hmm_test():

    st_time = time.time()

    model_file = "hmm_model.json"

    # load data
    with open(model_file, 'r') as f:
        data = json.load(f)
    A = np.array(data['A'])
    B = np.array(data['B'])
    pi = np.array(data['pi'])
    # observation symbols
    obs_dict = data['observations']
    # state symbols
    states_symbols = dict()
    for idx, item in enumerate(data['states']):
        states_symbols[item] = idx
    Osequence = np.array(data['Osequence'])
    N = len(Osequence)
    model = HMM(pi, A, B, obs_dict, states_symbols)

    delta = model.forward(Osequence)
    print("Forward function output:", delta)

    gamma = model.backward(Osequence)
    print("Backward function output:", gamma)

    prob1 = model.sequence_prob(Osequence)
    print("Sequence_prob function output:", prob1)

    prob2 = model.posterior_prob(Osequence)
    print("Posterior_prob function output:", prob2)

    prob3 = model.likelihood_prob(Osequence)
    print("Likelihood_prob function output:", prob3)

    viterbi_path = model.viterbi(Osequence)
    print('Viterbi function output: ', viterbi_path)

    en_time = time.time()
    print()
    print("hmm total time: ", en_time - st_time)
def hmm_test():

    st_time = time.time()

    model_file = "hmm_model.json"

    # load data
    with open(model_file, 'r') as f:
        data = json.load(f)
    A = np.array(data['A'])
    B = np.array(data['B'])
    pi = np.array(data['pi'])
    # observation symbols
    obs_dict = data['observations']
    # state symbols
    states_symbols = dict()

    for idx, item in enumerate(data['states']):
        states_symbols[item] = idx
    Osequence = np.array(data['Osequence'])
    N = len(Osequence)
    # print("pi",pi,"\nobsdict",obs_dict,"\n","B", B,"\n states_symbols",states_symbols,"\n A",A,"\n osequence",Osequence)
    model = HMM(pi, A, B, obs_dict, states_symbols)

    alpha = model.forward(Osequence)
    m_alpha = np.array([[
        3.5000e-01, 1.3600e-01, 0.0000e+00, 0.0000e+00, 1.1136e-05, 1.1136e-05,
        0.0000e+00
    ],
                        [
                            1.5000e-01, 3.2000e-02, 4.6400e-03, 2.7840e-04,
                            3.3408e-05, 1.1136e-05, 8.9088e-07
                        ]])

    print("Your forward function output:\n", alpha)
    print("Correct forward function output:\n", m_alpha)

    beta = model.backward(Osequence)
    m_beta = np.array([[
        1.6896e-06, 3.8400e-06, 6.4000e-05, 2.0000e-03, 1.4000e-02, 2.0000e-02,
        1.0000e+00
    ],
                       [
                           1.9968e-06, 1.1520e-05, 1.9200e-04, 3.2000e-03,
                           2.2000e-02, 6.0000e-02, 1.0000e+00
                       ]])

    print("Your backward function output:\n", beta)
    print("Correct backward function output:\n", m_beta)

    prob1 = model.sequence_prob(Osequence)
    m_prob1 = 8.908800000000002e-07

    print("Your sequence_prob function output:\n", prob1)
    print("Correct sequence_prob function output:\n", m_prob1)

    prob2 = model.posterior_prob(Osequence)
    m_prob2 = np.array([[0.6637931, 0.5862069, 0., 0., 0.175, 0.25, 0.],
                        [0.3362069, 0.4137931, 1., 1., 0.825, 0.75, 1.]])

    print("Your posterior_prob function output:\n", prob2)
    print("Correct posterior_prob function output:\n", m_prob2)

    prob3 = model.likelihood_prob(Osequence)
    m_prob3 = np.array([[[0.48275862, 0, 0, 0, 0.1, 0.],
                         [0.18103448, 0.5862069, 0, 0, 0.075, 0.25]],
                        [[0.10344828, 0., 0, 0.175, 0.15, 0.],
                         [0.23275862, 0.4137931, 1, 0.825, 0.675, 0.75]]])

    print("Your likelihood_prob function output:\n", prob3)
    print("Correct likelihood_prob function output:\n", m_prob3)

    viterbi_path = model.viterbi(Osequence)
    m_viterbi_path = ['1', '1', '2', '2', '2', '2', '2']

    print('Your viterbi function output: \n', viterbi_path)
    print('Correct viterbi function output: \n', m_viterbi_path)

    en_time = time.time()
    print()
    print("HMM total time: ", en_time - st_time)
Exemplo n.º 5
0
# -*- coding:utf-8 -*-
import numpy as np
from hmm import HMM

A = np.array([[0.7, 0.3], [0.4, 0.6]])
B = np.array([[0.5, 0.4, 0.1], [0.1, 0.3, 0.6]])
hmm = HMM(A, B, [0.3, 0.7])
# print(hmm.generate_data(5, seed=2018))
observations, states = hmm.generate_data(T=10, seed=2019)
print('observations: {}'.format(observations))
print('hidden states: {}'.format(states))
#  概率计算问题
print('backward prob: {}'.format(hmm.backward(observations)[1]))
print('forward prob: {}'.format(hmm.forward(observations)[1]))

# 学习问题
model = HMM(np.array([[0.5, 0.5], [0.5, 0.5]]),
            np.array([[0.4, 0.4, 0.2], [0.2, 0.3, 0.5]]), np.array([0.5, 0.5]))
a, b, pi, count = model.baum_welch(observations, threshold=0.1)
print('EM iteration: {}'.format(count))
print('a: {}'.format(a))
print('b: {}'.format(b))
print('pi: {}'.format(pi))

# 预测问题
print("predict: {}".format(hmm.viterbi(observations)))
Exemplo n.º 6
0
hmmfile = "test.hmm"
seqfile = "test.seq"

M, N, pi, A, B = read_hmm(hmmfile)
T, obs = read_sequence(seqfile)
	
hmm_object = HMM(pi, A, B)

#test forward algorithm
prob, alpha = hmm_object.forward(obs)
print "forward probability is %f" % np.log(prob)
prob, alpha, scale = hmm_object.forward_with_scale(obs)
print "forward probability with scale is %f" % prob

# test backward algorithm
prob, beta = hmm_object.backward(obs)
print "backward probability is %f" % prob 
beta = hmm_object.backward_with_scale(obs, scale)

# test baum-welch algorithm
logprobinit, logprobfinal = hmm_object.baum_welch(obs)
print "------------------------------------------------"
print "estimated parameters are: "
print "pi is:"
print hmm_object.pi
print "A is:"
print hmm_object.A
print "B is:"
print hmm_object.B
print "------------------------------------------------"
print "initial log probability is:"