-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_conv_lstm
69 lines (51 loc) · 1.65 KB
/
test_conv_lstm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import numpy as np
import IPython
# transition model and observation model
A = np.array([[0.2, 0.8, 0], [0., 0.2, 0.8], [0, 0., 1]])
O = np.array([[0.8, 0.2, 0], [0.1, 0.8, 0.1], [0.0, 0.2, 0.8]])
# O = O.transpose()
# initial setup
pi = np.array([1, 0, 0])
pi = np.transpose(pi)
# sequence
S = [1, 0, 1, 2]
# target: argmax P{{Zi} | pi, A, O, S}
# Xi
bt = []
for step, s in enumerate(S[1:]):
# for every state, find the most possible ancestor by max func
temp = []
bt_row = []
for i in range(len(pi)):
a = A[:, i] # the array of possible states to i state probability
temp.append(np.max(pi*a))
bt_row.append(np.argmax(pi*a))
# print bt_row
pi = temp
print 'step {}: the predicted state based on transition:\n {}'.format(step, pi)
# P{X_k|O_k} = P{O_k = s|X_k}P{X_k} and P{X_k} = pi
print 'observation:'+str(s)
ita = 0
for i in range(len(pi)):
pi[i] = O[i, s]*pi[i]
# print O[i, s]
ita += pi[i]
pi = pi/ita
print 'step {}: the predicted state updated with observation:\n {}'.format(step, pi)
# print bt_row
bt.append(bt_row)
# IPython.embed()
print '===================================================='
# IPython.embed()
print 'backtracing matrix:\n {}'.format(bt)
# backtracing
print '----------------------------------------------'
print 'start to backtrace from the max at terminal...'
Ind_T = np.argmax(pi)
ind = Ind_T
print 'the state at termination is: {}'.format(ind)
# IPython.embed()
for j in np.flip(range(len(bt))):
print 'step {}:'.format(j)
print 'the state backwards are: {}'.format(bt[j][ind])
ind = bt[j][ind]