forked from hmmlearn/hmmlearn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_hmmc.pyx
142 lines (105 loc) · 4.46 KB
/
_hmmc.pyx
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
# cython: boundscheck=False, wraparound=False
from cython cimport view
from numpy.math cimport expl, logl, isinf, INFINITY
import numpy as np
ctypedef double dtype_t
cdef inline int _argmax(dtype_t[:] X) nogil:
cdef dtype_t X_max = -INFINITY
cdef int pos = 0
cdef int i
for i in range(X.shape[0]):
if X[i] > X_max:
X_max = X[i]
pos = i
return pos
cdef inline dtype_t _max(dtype_t[:] X) nogil:
return X[_argmax(X)]
cdef inline dtype_t _logsumexp(dtype_t[:] X) nogil:
cdef dtype_t X_max = _max(X)
if isinf(X_max):
return -INFINITY
cdef dtype_t acc = 0
for i in range(X.shape[0]):
acc += expl(X[i] - X_max)
return logl(acc) + X_max
def _forward(int n_samples, int n_components,
dtype_t[:] log_startprob,
dtype_t[:, :] log_transmat,
dtype_t[:, :] framelogprob,
dtype_t[:, :] fwdlattice):
cdef int t, i, j
cdef dtype_t[::view.contiguous] work_buffer = np.zeros(n_components)
with nogil:
for i in range(n_components):
fwdlattice[0, i] = log_startprob[i] + framelogprob[0, i]
for t in range(1, n_samples):
for j in range(n_components):
for i in range(n_components):
work_buffer[i] = fwdlattice[t - 1, i] + log_transmat[i, j]
fwdlattice[t, j] = _logsumexp(work_buffer) + framelogprob[t, j]
def _backward(int n_samples, int n_components,
dtype_t[:] log_startprob,
dtype_t[:, :] log_transmat,
dtype_t[:, :] framelogprob,
dtype_t[:, :] bwdlattice):
cdef int t, i, j
cdef dtype_t logprob
cdef dtype_t[::view.contiguous] work_buffer = np.zeros(n_components)
with nogil:
for i in range(n_components):
bwdlattice[n_samples - 1, i] = 0.0
for t in range(n_samples - 2, -1, -1):
for i in range(n_components):
for j in range(n_components):
work_buffer[j] = (log_transmat[i, j]
+ framelogprob[t + 1, j]
+ bwdlattice[t + 1, j])
bwdlattice[t, i] = _logsumexp(work_buffer)
def _compute_lneta(int n_samples, int n_components,
dtype_t[:, :] fwdlattice,
dtype_t[:, :] log_transmat,
dtype_t[:, :] bwdlattice,
dtype_t[:, :] framelogprob,
dtype_t[:, :, :] lneta):
cdef dtype_t logprob = _logsumexp(fwdlattice[n_samples - 1])
cdef int t, i, j
with nogil:
for t in range(n_samples - 1):
for i in range(n_components):
for j in range(n_components):
lneta[t, i, j] = (fwdlattice[t, i]
+ log_transmat[i, j]
+ framelogprob[t + 1, j]
+ bwdlattice[t + 1, j]
- logprob)
def _viterbi(int n_samples, int n_components,
dtype_t[:] log_startprob,
dtype_t[:, :] log_transmat,
dtype_t[:, :] framelogprob):
cdef int i, j, t, where_from
cdef dtype_t logprob
cdef int[::view.contiguous] state_sequence = \
np.empty(n_samples, dtype=np.int32)
cdef dtype_t[:, ::view.contiguous] viterbi_lattice = \
np.zeros((n_samples, n_components))
cdef dtype_t[::view.contiguous] work_buffer = np.empty(n_components)
with nogil:
for i in range(n_components):
viterbi_lattice[0, i] = log_startprob[i] + framelogprob[0, i]
# Induction
for t in range(1, n_samples):
for i in range(n_components):
for j in range(n_components):
work_buffer[j] = (log_transmat[j, i]
+ viterbi_lattice[t - 1, j])
viterbi_lattice[t, i] = _max(work_buffer) + framelogprob[t, i]
# Observation traceback
state_sequence[n_samples - 1] = where_from = \
_argmax(viterbi_lattice[n_samples - 1])
logprob = viterbi_lattice[n_samples - 1, where_from]
for t in range(n_samples - 2, -1, -1):
for i in range(n_components):
work_buffer[i] = (viterbi_lattice[t, i]
+ log_transmat[i, where_from])
state_sequence[t] = where_from = _argmax(work_buffer)
return np.asarray(state_sequence), logprob