Пример #1
0
 def _do_backward_pass(self, framelogprob):
     n_observations = framelogprob.shape[0]
     state_combinations = [tuple(x) for x in list(itertools.product(np.arange(self.n_states), repeat=self.n_chains))]
     bwdlattice = np.zeros((n_observations, self.n_states ** self.n_chains))
     fhmmc._backward(n_observations, self.n_chains, self.n_states, state_combinations, self.log_startprob,
                     self.log_transmat, framelogprob, bwdlattice)
     return bwdlattice
Пример #2
0
 def _do_backward_pass(self, framelogprob):
     n_observations = framelogprob.shape[0]
     state_combinations = [
         tuple(x) for x in list(
             itertools.product(np.arange(self.n_states),
                               repeat=self.n_chains))
     ]
     bwdlattice = np.zeros((n_observations, self.n_states**self.n_chains))
     fhmmc._backward(n_observations, self.n_chains, self.n_states,
                     state_combinations, self.log_startprob,
                     self.log_transmat, framelogprob, bwdlattice)
     return bwdlattice
Пример #3
0
    def test_backward_with_hmmlearn(self):
        r = np.random.randn
        obs = [np.array([[-600 + r(), 100 + r()], [-300 + r(), 200 + r()], [0 + r(), 300 + r()]]) for _ in xrange(10)]
        hmm = GaussianHMM(n_components=3)
        hmm.fit(obs)

        # Calculcate bwdlattice using hmmlearn algorithm
        framelogprob = hmm._compute_log_likelihood(obs[0])
        start = timeit.default_timer()
        bwdlattice1 = hmm._do_backward_pass(framelogprob)
        print('hmmlearn took %fs' % (timeit.default_timer() - start))

        # Calculate bwdlattice using fhmm algorithm with #chains = 1. This should yield the exact same results
        start = timeit.default_timer()
        bwdlattice2 = np.zeros(bwdlattice1.shape)
        fhmmc._backward(obs[0].shape[0], 1, hmm.n_components, [(x,) for x in xrange(hmm.n_components)],
                        hmm._log_startprob.reshape(1, 3), hmm._log_transmat.reshape(1, 3, 3), framelogprob, bwdlattice2)
        print('fhmm took %fs' % (timeit.default_timer() - start))
        self.assertTrue(np.allclose(bwdlattice1, bwdlattice2))