-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhmmbase.py
102 lines (73 loc) · 2.26 KB
/
hmmbase.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
from __future__ import division
import abc
import numpy as np
import forward_backward as fb
import gmm
class HMMBase(object):
"""
"""
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def infer():
"""
Perform inference.
"""
pass
def __init__(self, obs, S, pi, A, L):
"""
Initialize the HMM object.
Parameters
----------
obs : numpy.array
A T x D numpy array of T observations in D dimensions.
S : int
The number of hidden states to use.
pi : numpy.array
A 1 x K array representing the initial distribution.
A : numpy.array
A K x K matrix representing the transition matrix.
L : int
The number of mixtures for the Gaussian Mixture Model.
"""
self.obs = obs
self.pi = pi
self.A = A
# initialize GMM parameters
self.cs
self.mus
self.sigmas
def Baum_Welch(self):
for itr in iters:
# Expectation Step
self._forward_msgs()
self._backward_msgs()
self._log_weights()
lexpected_states, expected_transcounts = self._expected_statistics()
# Maximization Step
expected_states = np.exp(lexpected_states)
expected_states = epxected_states/np.sum(expected_states, axis=1)[:,np.newaxis]
self.pi = expected_states[0]
self.A = expected_transcounts/ \
np.sum(expected_states[:len(expected_states)-1], axis=0)[:,np.newaxis]
gmm.update_parameters(self.cs, self.mus, self.sigmas, self.obs, self.lweights)
def _forward_msgs(self):
"""
"""
self.lalpha = fb.forward_msgs(self.pi, self.A, self.lliks)
def _backward_msgs(self):
"""
"""
self.lbeta = fb.backward_msgs(self.A, self.lliks)
def _log_likelihood(self):
"""
"""
pass
def _log_weights(self):
"""
"""
self.lweights = fb.log_weights(self.lalpha, self.lbeta)
def _expected_statistics(self):
"""
"""
return fb.expected_statistics(self.pi, self.A, self.lliks,
self.lalpha, self.lbeta)