-
Notifications
You must be signed in to change notification settings - Fork 4
/
hmm.py
283 lines (240 loc) · 8.87 KB
/
hmm.py
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import numpy as np
from abc import ABCMeta, abstractmethod
class _BaseHMM:
"""
n_state : number of hidden states
n_iter :
x_size : dimension of obs
start_prob : initial prob
transmat_prob : transition prob
"""
__metaclass__ = ABCMeta
def __init__(self, n_state=1, x_size=1, iter=20):
self.n_state = n_state
self.x_size = x_size
# initialize
self.start_prob = np.ones(n_state) * (1.0 / n_state)
self.transmat_prob = np.ones((n_state, n_state)) * (1.0 / n_state)
self.trained = False
self.n_iter = iter
@abstractmethod
def _init(self, X):
pass
@abstractmethod
def emit_prob(self, x): # emission prob
return np.array([0])
# 虚函数
@abstractmethod
def generate_x(self, z): # generate x according to p(x|z)
return np.array([0])
# 虚函数:发射概率的更新
@abstractmethod
def emit_prob_updated(self, X, post_state):
pass
# 通过HMM生成序列
def generate_seq(self, seq_length):
X = np.zeros((seq_length, self.x_size))
Z = np.zeros(seq_length)
Z_pre = np.random.choice(self.n_state, 1, p=self.start_prob)
X[0] = self.generate_x(Z_pre)
Z[0] = Z_pre
for i in range(seq_length):
if i == 0:
continue
# P(Zn+1)=P(Zn+1|Zn)P(Zn)
Z_next = np.random.choice(
self.n_state, 1, p=self.transmat_prob[Z_pre, :][0]
)
Z_pre = Z_next
# P(Xn+1|Zn+1)
X[i] = self.generate_x(Z_pre)
Z[i] = Z_pre
return X, Z
# Evaluation
def X_prob(self, X, Z_seq=np.array([])):
X_length = len(X)
if Z_seq.any():
Z = np.zeros((X_length, self.n_state))
for i in range(X_length):
Z[i][int(Z_seq[i])] = 1
else:
Z = np.ones((X_length, self.n_state))
_, c = self.forward(X, Z) # P(x,z)
prob_X = np.sum(np.log(c)) # P(X)
return prob_X
# def predict_next_prob(self, X, x_next, Z_seq=np.array([]), istrain=True):
# if self.trained == False or istrain == False:
# self.train(X)
# X_length = len(X)
# if Z_seq.any():
# Z = np.zeros((X_length, self.n_state))
# for i in range(X_length):
# Z[i][int(Z_seq[i])] = 1
# else:
# Z = np.ones((X_length, self.n_state))
# alpha, _ = self.forward(X, Z) # P(x,z)
# prob_x_next = self.emit_prob(np.array([x_next])) * np.dot(
# alpha[X_length - 1], self.transmat_prob
# )
# return prob_x_next
def decode(self, X, istrain=True):
"""
Viterbi
:param X: observations
:param istrain:
:return: hidden states
"""
if self.trained == False or istrain == False:
self.train(X)
X_length = len(X)
state = np.zeros(X_length)
pre_state = np.zeros((X_length, self.n_state))
max_pro_state = np.zeros((X_length, self.n_state))
_, c = self.forward(X, np.ones((X_length, self.n_state)))
max_pro_state[0] = self.emit_prob(X[0]) * self.start_prob * (1 / c[0])
# forward
for i in range(X_length):
if i == 0:
continue
for k in range(self.n_state):
prob_state = (
self.emit_prob(X[i])[k]
* self.transmat_prob[:, k]
* max_pro_state[i - 1]
)
max_pro_state[i][k] = np.max(prob_state) * (1 / c[i])
pre_state[i][k] = np.argmax(prob_state)
# backward
state[X_length - 1] = np.argmax(max_pro_state[X_length - 1, :])
for i in reversed(range(X_length)):
if i == X_length - 1:
continue
state[i] = pre_state[i + 1][int(state[i + 1])]
return state
#
# def train_batch(self, X, Z_seq=list()):
#
# self.trained = True
# X_num = len(X)
# self._init(self.expand_list(X))
# if Z_seq == list():
# Z = []
# for n in range(X_num):
# Z.append(list(np.ones((len(X[n]), self.n_state))))
# else:
# Z = []
# for n in range(X_num):
# Z.append(np.zeros((len(X[n]), self.n_state)))
# for i in range(len(Z[n])):
# Z[n][i][int(Z_seq[n][i])] = 1
# for e in range(self.n_iter): # EM
# # E
# print("iter: ", e)
# b_post_state = []
# b_post_adj_state = np.zeros(
# (self.n_state, self.n_state)
# )
# b_start_prob = np.zeros(self.n_state)
# for n in range(X_num):
# X_length = len(X[n])
# alpha, c = self.forward(X[n], Z[n]) # P(x,z)
# beta = self.backward(X[n], Z[n], c) # P(x|z)
# post_state = alpha * beta / np.sum(alpha * beta) # Normalize
# b_post_state.append(post_state)
# post_adj_state = np.zeros((self.n_state, self.n_state))
# for i in range(X_length):
# if i == 0:
# continue
# if c[i] == 0:
# continue
# post_adj_state += (
# (1 / c[i])
# * np.outer(alpha[i - 1], beta[i] * self.emit_prob(X[n][i]))
# * self.transmat_prob
# )
# if np.sum(post_adj_state) != 0:
# post_adj_state = post_adj_state / np.sum(post_adj_state) # Normalize
# b_post_adj_state += post_adj_state
# b_start_prob += b_post_state[n][0]
# # M
# b_start_prob += 0.001 * np.ones(self.n_state)
# self.start_prob = b_start_prob / np.sum(b_start_prob)
# b_post_adj_state += 0.001
# for k in range(self.n_state):
# if np.sum(b_post_adj_state[k]) == 0:
# continue
# self.transmat_prob[k] = b_post_adj_state[k] / np.sum(
# b_post_adj_state[k]
# )
# self.emit_prob_updated(self.expand_list(X), self.expand_list(b_post_state))
def expand_list(self, X):
C = []
for i in range(len(X)):
C += list(X[i])
return np.array(C)
# 针对于单个长序列的训练
def train(self, X, Z_seq=np.array([])):
self.trained = True
X_length = len(X)
self._init(X)
if Z_seq.any():
Z = np.zeros((X_length, self.n_state))
for i in range(X_length):
Z[i][int(Z_seq[i])] = 1
else:
Z = np.ones((X_length, self.n_state))
for e in range(self.n_iter): # EM
print(e, " iter")
# E
alpha, c = self.forward(X, Z) # P(x,z)
beta = self.backward(X, Z, c) # P(x|z)
post_state = alpha * beta
post_adj_state = np.zeros((self.n_state, self.n_state))
for i in range(X_length):
if i == 0:
continue
if c[i] == 0:
continue
post_adj_state += (
(1 / c[i])
* np.outer(alpha[i - 1], beta[i] * self.emit_prob(X[i]))
* self.transmat_prob
)
# M
self.start_prob = post_state[0] / np.sum(post_state[0])
for k in range(self.n_state):
self.transmat_prob[k] = post_adj_state[k] / np.sum(post_adj_state[k])
self.emit_prob_updated(X, post_state)
def forward(self, X, Z):
X_length = len(X)
alpha = np.zeros((X_length, self.n_state)) # P(x,z)
alpha[0] = self.emit_prob(X[0]) * self.start_prob * Z[0]
c = np.zeros(X_length)
c[0] = np.sum(alpha[0])
alpha[0] = alpha[0] / c[0]
for i in range(X_length):
if i == 0:
continue
alpha[i] = (
self.emit_prob(X[i]) * np.dot(alpha[i - 1], self.transmat_prob) * Z[i]
)
c[i] = np.sum(alpha[i])
if c[i] == 0:
continue
alpha[i] = alpha[i] / c[i]
return alpha, c
def backward(self, X, Z, c):
X_length = len(X)
beta = np.zeros((X_length, self.n_state)) # P(x|z)
beta[X_length - 1] = np.ones((self.n_state))
for i in reversed(range(X_length)):
if i == X_length - 1:
continue
beta[i] = (
np.dot(beta[i + 1] * self.emit_prob(X[i + 1]), self.transmat_prob.T)
* Z[i]
)
if c[i + 1] == 0:
continue
beta[i] = beta[i] / c[i + 1]
return beta