-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHMM.py
More file actions
69 lines (61 loc) · 2.05 KB
/
HMM.py
File metadata and controls
69 lines (61 loc) · 2.05 KB
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
69
import numpy as np
import torch
class HMM(object):
def __init__(self):
self.alpha = None
self.beta = None
def forward(self, Q, V, A, B, PI, O):
T = len(O)
N = len(Q)
alpha = np.zeros((T,N))
for t in range(T):
IndexOfO = V.index(O[t])
if t==0:
alpha[t] = PI * B[:,IndexOfO]
else:
for i in range(N):
alpha[t][i] = np.dot(alpha[t-1][:],A[:,i])*B[i,IndexOfO]
self.alpha = alpha
return np.sum(alpha[-1][:])
def backward(self, Q, V, A, B, PI, O):
T = len(O)
N = len(Q)
beta = np.ones((T,N))
for t in range(T-2,-1,-1):
for i in range(N):
IndexOfOP1 = V.index(O[t+1])
beta[t][i] = np.sum(A[i,:] * B[:,IndexOfOP1] * beta[t+1,:])
IndexOfOP1 = V.index(O[0])
self.beta = beta
return np.sum(PI * B[:,IndexOfOP1] * beta[0,:])
def viterbi(self, Q, V, A, B, O, PI):
T = len(O)
N = len(Q)
delta = np.ones((T,N))
phi = np.zeros((T,N))
IndexOfO = V.index(O[0])
delta[0,:] = PI * B[:,IndexOfO]
for t in range(1,T):
IndexOfO = V.index(O[t])
for i in range(N):
da = delta[t-1,:] * A[:,i]
delta[t,i] = np.max(da)*B[i,IndexOfO]
phi[t,i] = da.tolist().index(np.max(da))
path = [0] * T
for t in range(T-1,-1,-1):
if t==T-1:
tmp = delta[t,:].tolist()
path[t] = int(tmp.index(np.max(delta[t,:])))+1
else:
path[t] = int(phi[t+1,path[t+1]-1])+1
return path
Q = np.array([1, 2, 3])
V = ['红', '白']
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]])
O = np.array(['红', '白', '红','白'])
PI = np.array([0.2, 0.4, 0.4])
hmm = HMM()
p = hmm.forward(Q, V, A, B, PI, O)
p = hmm.backward(Q, V, A, B, PI, O)
print(hmm.viterbi(Q, V, A, B, O, PI))