def main(): # # Train classifier and make predications # m = classifier.train('knn') train_x = json.loads(open('data/hist/train_x.json').read()) train_y = json.loads(open('data/hist/train_y.json').read()) test_x = json.loads(open('data/hist/semantic1.json').read()) test_y = json.loads(open('data/hist/truth1.json').read()) # m = hmm.create(train_x) # create or fit then predict # hmm.predict_all(m, test_x, test_y, 'map') hmm.train(train_x, train_y, test_x, test_y, training='baum-welch', decoder='map')
def simulate(membership, TM, TM0, TI0, Z, T1, s0, rho, _actions=None): # natural (true) transition TMn = membership[0, 0] * TM[0] + membership[0, 1] * TM[1] + membership[ 0, 2] * TM[2] TIn = util.interaction_effect(TMn, rho) # t=1...T1 actions = [] observations = [] s = s0 # warm up loop if T1 > 0: for t in range(T1): if _actions is not None: a = _actions[t] else: a = np.random.binomial(1, 0.3, 1)[0] actions.append(a) # print s0, TMn[s0] s = np.random.choice(3, 1, p=TMn[s])[0] # assumes 3 states # print expandZ(Z[s],s) o = np.random.choice(3, 1, p=Z[s])[0] observations.append(o) # print t,a,s,o hmm = hmm.HMM() hmm.pi = np.array([0.5, 0.3, 0.2]) # ASSUMPTION hmm.A = np.array([TM0, TI0]) hmm.B = np.copy(Z) hmm.train(observations, actions, 0.01) T_hat = hmm.A Z_hat = hmm.B else: o = np.random.choice(3, 1, p=Z[s])[0] T_hat = np.array([TMn, TIn]) Z_hat = Z b = Z[:, o] b = b / b.sum() # initialize belief # personalize T, Z return TMn, TIn, actions, observations, s, b, T_hat, Z_hat
def parallel(A, B, prior, observationSequence): # parallel HMM training with graphlab g = SGraph() vertices = map(lambda i: Vertex(str(i) + "a", attr={'i': i, 'ait': [prior[i]] + ([0] * OBSERVATION_LENGTH), 'bit': ([0] * OBSERVATION_LENGTH) + [1], 'b': B[i, :], 'git': [0] * (OBSERVATION_LENGTH + 1), 'self': A[i, i], 'git_sum': 0.0}), xrange(NUM_STATES)) g = g.add_vertices(vertices) edges = [] for i in xrange(NUM_STATES): for j in xrange(NUM_STATES): if i != j: edges.append(Edge(str(i) + "a", str(j) + "a", attr={'aij': A[i, j], 'xi': 0.0})) g = g.add_edges(edges) g = hmm.train(g, observationSequence, NITERS, NUM_STATES, NUM_OBSERVATIONS) print g.vertices print g.edges
def op_train(filelist): print("Train file list:", filelist) datas = [] i = 0 for train_file in filelist: with open('brown/' + train_file, "r") as f: lines = f.readlines() for line in lines: if len(line.strip()) == 0: continue splited = line.split() data = [mysplit(s) for s in splited] datas.append(list(zip(*data))) i += 1 print("data count:", str(len(datas))) starttime = time.time() model = hmm.train(datas) endtime = time.time() print("Training finished:", endtime - starttime, "s elapsed") # dump trained model model.dump('trained')
def solve(TMn, TIn, Z, actions, observations, s, b, T_hat, Z_hat, TM0, TI0, T2, utility, cost, discount, if_update, policy_type, threshold, model_file): # t = T1+1...T2 # print Z_hat # hmm_ = HMM() # hmm_.pi = np.array([0.5, 0.3, 0.2]) # ASSUMPTION # hmm_.A = np.array([T_hat]) # hmm_.B = np.copy(Z_hat) # hmm_ = copy.copy(hmm) actions = copy.copy(actions) observations = copy.copy(observations) err = 0 rwd = 0 A = [] # record the action of all periods B = [] # record the belief of all periods O = [] # record the observation of all periods S = [] # record the true state of all periods for t in range(T2): if policy_type == 0: a = random_policy() elif policy_type == 1: if t == 0: update_model(MODEL_DIR + model_file, T_hat, Z_hat, utility, cost) node, pomdp_actions, pomdp_alphas, pomdp_next_nodes = pomdp_policy( model_file, b, horizon=T2, discount=discount) a = pomdp_actions[node] # for n in range(len(pomdp_actions)): # print pomdp_actions[n], pomdp_alphas[n], pomdp_next_nodes[n] # print '%2d %2d' %(t, a) else: # print '%2d %2d %2d' %(t, a, o) node = pomdp_next_nodes[node][o] a = pomdp_actions[node] elif policy_type == 2: if t == 0: update_model(MODEL_DIR + model_file, T_hat, Z_hat, utility, cost) else: update_model(MODEL_DIR + model_file, T_hat, Z_hat) a = pomdp_policy(model_file, b, horizon=None, discount=discount) # print t, a elif policy_type == 3: a = H_policy(b, threshold) elif policy_type == 4: a = S_policy(b, threshold) actions.append(a) s = np.random.choice(3, 1, p=[TMn, TIn][a][s])[0] o = np.random.choice(3, 1, p=Z[s])[0] observations.append(o) _b = Z_hat[:, o] * b.dot(T_hat[a]) # update belief if _b.sum() > 0: b = _b / _b.sum() else: b = np.array([1. / 3., 1. / 3., 1. / 3.]) # print np.array_str(Z_hat, precision=4, suppress_small=True) # print np.array_str(b, precision=4, suppress_small=True) if if_update > 0: hmm = HMM() hmm.pi = np.array([0.5, 0.3, 0.2]) # ASSUMPTION hmm.A = np.copy(T_hat) hmm.B = np.copy(Z) hmm.train(observations, actions, 0.01) T_hat = hmm.A Z_hat = hmm.B # print t,a,s,o rwd += reward(s, a, utility, cost) * np.power(discount, t) err += (1 - b[s])**2 A.append(a) S.append(s) O.append(o) B.append(b) return 1. * err / T2, 1. * rwd / T2, A, S, O, B
elif a == M[1]: OBSERVED_STATE[j] = OBSERVED_STATE[j] + 'R' elif a == M[2]: OBSERVED_STATE[j] = OBSERVED_STATE[j] + 'O' elif a == M[3]: OBSERVED_STATE[j] = OBSERVED_STATE[j] + 'G' print OBSERVED_STATE for i in range(0, 119): if pixsum[i] > 7500: HIDDEN_STATE[i] = "OPEN" elif pixsum[i] > 6600: HIDDEN_STATE[i] = "MILDLY CONGESTED" elif pixsum[i] > 5545: HIDDEN_STATE[i] = "MODERATELY CONGESTED" else: HIDDEN_STATE[i] = "HEAVILY CONGESTED" print HIDDEN_STATE[i] sequence = [(HIDDEN_STATE, OBSERVED_STATE)] for i in range(5): print '_' * 80 print '' * 80 print "THE HIDDEN STATE AND OBSERVED STATE PREDICTED FOR THE NEXT TIME INTERVAL IS\n" train(sequence)
partition, observed_seq, given_seq) count = [[0 for i in range(num_of_symbol)], [0 for i in range(num_of_symbol)]] for i in range(len(observed_seq)): count[given_seq[i]][observed_seq[i]]+=1 for k in count: s = sum(k) for j in range(len(k)): k[j] /= s k[j] = float(int(k[j]*10000))/100 #print(partition, count) model = hmm.train([(given_seq, observed_seq)]) ##Analysis HMM right_count = 0 for data in data_set: observed_seq = [] given_seq = [] given_state = data[2] observed_seq, given_seq = extra.get_sequences(data, type, aver_num,\ partition, observed_seq, given_seq) forward_seq = model._forward(observed_seq) last_state = extra.last_max_state(forward_seq) if last_state == given_state:
print("=" * 70) print("(TRAIN )") # sequences = [ # (['hot', 'cold', 'hot'], ['3', '1', '3']), # (['cold', 'cold', 'cold'], ['1', '1', '1']), # (['cold', 'hot', 'hot'], ['1', '2', '2']) # ] sequences = [(['hot', 'cold', 'hot'], ['3', '1', '3']), (['hot', 'hot', 'hot'], ['3', '3', '3']), (['cold', 'cold', 'cold'], ['1', '1', '1']), (['cold', 'hot', 'hot'], ['1', '3', '3'])] model = hmm.train(sequences, delta=1e-10, smoothing=1e-10) print("=" * 70) print("(PREDICT)") # In[37]: # model._start_prob['hot'] = 0.5 # model._start_prob['cold'] = 0.5 print("=" * 70) sequence = ['3', '1', '3'] print(model.evaluate(sequence)) # Likelihood 계산 print(model.decode(sequence)) # 최적상태열 추정 print("=" * 70)
import hmm hmm.train("../dataset/brown.txt", 2, 27, 50000, 1, 10)
'1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '2', '2' ], #6PM [ 'MROO', 'MROO', 'MRRO', 'MRRO', 'MRRO', 'MRRO', 'MRRO', 'MRRO', 'MRRO', 'MRRR', 'MRRR', 'MRRR', 'MMRO', 'MRRO', 'RRRO', 'RRRO', 'MROO', 'MROO', 'MROG', 'MRGG', 'MROG', 'RROG', 'MROG', 'MROG', 'MRRO', 'MROO', 'MOOO', 'RRRO', 'RRRO', 'RROO', 'RROO', 'RROG', 'RROG', 'ROOO', 'ROOO', 'RROG', 'ROOG', 'RRGG', 'MGGG', 'RGGG', 'RGGG', 'ROGG', 'OOGG', 'OOGG', 'ROGG', 'ROOO', 'OOOO', 'OOOO', 'ROOO', 'ROOG', 'ROOG', 'ROGG', 'RGGG', 'ROGG', 'OOOG', 'OOOG', 'OOOG', 'OOOG', 'OOOG', 'OOGG', 'OOGG', 'OGGG', 'GGGG', 'GGGG', 'OGGG', 'RGGG', 'ROGG', 'OOOG', 'OOGG', 'OGGG', 'RGOG', 'OOGG', 'OOGG', 'OOOG', 'ROOG', 'ROOG', 'ROOG', 'ROGG', 'OOOG', 'OOOO', 'OOOO', 'ROOO', 'ROOG', 'ROOO', 'RROG', 'RROO', 'MROO', 'MROO', 'MROG', 'MROG', 'MROO', 'MMOO', 'MMOO', 'RRRO', 'RRRR', 'RRRR', 'MRRO', 'MRRO', 'MRRO', 'MMMR', 'MROO', 'MRRO', 'MRRO', 'MRRO', 'MMOO', 'MMMO', 'MMMM', 'MMMM', 'MMMR', 'MRRR', 'MRRO', 'MROO', 'MROO', 'RRRO', 'MRRO', 'RRRO', 'MROO', 'RROO', 'RROG', 'RROG' ])] train(sequences)