def test_lstm(s): print('....... testing lstm alone ......') # Hmm to generate sequences hmm_gen = hmm.Hmm() hmm_gen.initialize_weights(3,4) # Pretend World has 3 emotions - [Happy, Sad, Mad] # Pretend World has 4 faces - [Neutral, Genuine Smile, Fake Smile, Frown] hmm_gen.T_kk = np.log(np.array([1,.2,.2, .1,1,.5, .1,.6,1])).reshape([3,3]) hmm_gen.E_kd = np.log(np.array([.3,.6,.1,0, .2,0,.3,.5, .2,0,.1,.7])).reshape([3,4]) hmm_gen.P_k = np.log(np.array([.6,.3,.1])) hidden, observed = hmm_gen.generate_sequences(100, 100) # Display parameters of the generating-Hmm (T_kk diag's are 1.0) print('\nGenerated sequences from...') hmm_gen.print_percents() print('P = ' + str(hmm_gen.p_X_mat(observed))) lstmHmm = LstmHmm() lstmHmm.initialize_weights(3,4) #lstmHmm.print_percents() lstmHmm.X_mat_train = observed lstmHmm.wrap_em_train_v(n_iter=50, n_init=4) print('\n\nTrained to...') lstmHmm.print_percents()
def main(): if (len(sys.argv) != 2): print "usage: python jammed.py <no-of-observations>" exit(-1) num_obs = int(sys.argv[1]) print "Simulating NW HMM with %d observations " % num_obs # build the hmm ''' In Network HMM, N = 2 (jammed, not jammed) M = 2 {packet OK, packet LOSS} pi = {0.05, 0.95} (jammed, not jammed) ''' NW = enum(jammed=0, not_jammed=1) Ob = enum(ok=0, loss=1) print "state notations: " print "Hidden variables: jammed = 0, not_jammed = 1" print "Observations: ok = 0, loss = 1" A_init = {} A_init[NW.jammed] = {} A_init[NW.jammed][NW.jammed] = 0.9 A_init[NW.jammed][NW.not_jammed] = 0.1 A_init[NW.not_jammed] = {} A_init[NW.not_jammed][NW.jammed] = 0.05 A_init[NW.not_jammed][NW.not_jammed] = 0.95 B_init = {} B_init[NW.jammed] = {} B_init[NW.not_jammed] = {} B_init[NW.jammed][Ob.ok] = 0.2 B_init[NW.jammed][Ob.loss] = 0.8 B_init[NW.not_jammed][Ob.ok] = 0.95 B_init[NW.not_jammed][Ob.loss] = 0.05 pi_init = {} pi_init[NW.jammed] = 0.05 pi_init[NW.not_jammed] = 0.95 n = hmm.Hmm(A_init=A_init, B_init=B_init, pi_init=pi_init) X, obs = n.generateObservations(num_obs) # Finding the likelihood of given observations from HMM print "Log of likelihood of observations = ", n.likelihoodOfObservations( obs) print "Likelihood of observations = ", math.exp( n.likelihoodOfObservations(obs)) learned_X = n.mostLikelyStateSequence(obs) #print "Original state sequence = " , X #print "Original length = ", len(X) #print "Most likely state sequence", learned_X #print "Learned length = ", len(learned_X) print "Error in learned sequence = ", n.errorCompare(X, learned_X)
def get_hmm_results(data, name, hidden_states): obs_states = data.shape[1] hmm_obj = hmm.Hmm(data, 'rescaled', hidden_states, obs_states) hmm_obj.compute_proba() hmm_obj.plot_proba(200, 'Conditional proba ({})'.format(name), '{}'.format(name), '1') hmm_obj.EM(True) hmm_obj.print_parameters() hmm_obj.plot_proba(200, 'Conditional proba ({})'.format(name), '{}'.format(name), '2') hmm_obj.plot_likelihood('{}'.format(name), '3') hmm_obj.compute_viterbi_path() hmm_obj.plot_most_likely_state(200, 'Viterbi ({})'.format(name), '{}'.format(name), '4') return hmm_obj
for i in xrange(len(test_transition)): test_transition[i] /= sum(test_transition[i]) # np.random.shuffle(test_transition[i]) # POSTERIOR DECODING TESTING # T = 500 # pos = meth_data['pos'][0:L] # model = hmm.Hmm(states, initial, test_transition) # path, obs, cov = model.generateData(meth_data['cov'], # cov_type="real", seq_length = T, fix_cov_val = 3) # best_path, acc = model.decode(pos, obs, cov, # method='posterior', # real_path = path, # graph=True, # ci_alpha = .90) # Whole run on real dataset T = 1000 pos = meth_data['pos'][0:L] obs = meth_data['count'][0:L] cov = meth_data['cov'][0:L] model = hmm.Hmm(states, initial, transition) Dkl = model.train(pos, obs, cov, maxiter=30) best_path = model.decode(pos, obs, cov, method='posterior', real_path=None, graph=True, ci_alpha=.90)
s = str(l[0]) for x in l[1:]: s += " " + str(x) return (s) def formatString(string_in): """ Replace white spaces by %20 """ return string_in.strip().replace(" ", "%20") print("Get training sample") alphabet, train = readset(open(train_file, "r")) # print train print("Start Learning") hmm_model = hmm.Hmm(no_of_hidden_states, alphabet) hmm_model.train(train, no_of_iterations) print("Learning Ended") # get the test first prefix: the only element of the test set first_prefix = get_first_prefix(prefix_file) prefix_number = 1 # get the next symbol ranking on the first prefix p = first_prefix.split() prefix = [int(i) for i in p[1:len(p)]] ranking = hmm_model.predict(prefix) ranking_string = list_to_string(ranking[:5]) print("Prefix number: " + str(prefix_number) + " Ranking: " + ranking_string + " Prefix: " + first_prefix)
def test_lstmHmm_vs_hmm(s): """ TODO: - Modified always thinks it converges after 1-2 iters probably because the e/m step is not working properly w the modification (I think it actually gets worse instead of better) """ print('....... testing lstm Hmm vs regular Hmm ......') # Hmm to generate sequences hmm_gen = hmm.Hmm() hmm_gen.initialize_weights(3,4) # Pretend World has 3 emotions - [Happy, Sad, Mad] # Pretend World has 4 faces - [Neutral, Genuine Smile, Fake Smile, Frown] hmm_gen.T_kk = np.log(np.array([1,.2,.2, .1,1,.5, .1,.6,1])).reshape([3,3]) hmm_gen.E_kd = np.log(np.array([.3,.6,.1,0, .2,0,.3,.5, .2,0,.1,.7])).reshape([3,4]) hmm_gen.P_k = np.log(np.array([.6,.3,.1])) hidden, observed = hmm_gen.generate_sequences(150, 100) # 150 sequences, 100 long each # Display parameters of the generating-Hmm (T_kk diag's are 1.0) print('\nGenerated sequences from...') hmm_gen.print_percents() print('P = ' + str(hmm_gen.p_X_mat(observed))) # Check how many sequences contain only 1 hidden state all_one_hidden_state = 0 for h in hidden: if 1 in np.bincount(h)/len(h): all_one_hidden_state += 1 print('{}% of sequences contained only 1 hidden state'. format(100 * all_one_hidden_state/float(len(hidden)))) # Train with LstmHmm (modified m_step) print('Training using modified Hmm (LstmHmm)') lstm_hmm_train = LstmHmm() lstm_hmm_train.initialize_weights(3,4) lstm_hmm_train.X_mat_train = observed lstm_hmm_train.wrap_em_train_v(n_init=5, n_iter=250) print('\n\nLstmHmm trained to...') lstm_hmm_train.print_percents() print('LstmHmm Train Score = {}'.format(lstm_hmm_train.p_X_mat(observed))) # Train with normal (non-modified) Hmm print('Training using normal Hmm') hmm_train = hmm.Hmm() hmm_train.initialize_weights(3,4) hmm_train.X_mat_train = observed hmm_train.wrap_em_train_v(n_init=5, n_iter=250) print('\n\nHmm trained to...') hmm_train.print_percents() print('Hmm Train Score = {}'.format(hmm_train.p_X_mat(observed))) # Print these two again print('\n\nLstmHmm trained to...') lstm_hmm_train.print_percents() print('LstmHmm Train Score = {}'.format(lstm_hmm_train.p_X_mat(observed))) print('\n\nGenerated sequences from...') hmm_gen.print_percents()