Пример #1
0
def test_logsum_3D():
    """
    Test also on a 3D matrix
    """
    A = np.random.rand(2, 2, 2) + 1.0
    for axis in range(3):
        Asum = gmm.logsum(A, axis)
        assert_array_almost_equal(np.exp(Asum), np.sum(np.exp(A), axis))
Пример #2
0
    def _accumulate_sufficient_statistics(self, stats, obs, framelogprob,
                                          posteriors, fwdlattice, bwdlattice,
                                          params):
        """
        Slight modification of the fit function, just to give us
        a reference to the posteriors, so we can modify the observations
        (impute them) in the M step.
        """
        # keep posteriors
        self.curr_posteriors = posteriors
        # regulat fit, except M step modified (see below)
        #super(GMMHMM_impute,self)._accumulate_sufficient_statistics(stats,
        #                                  obs, framelogprob,
        #                                  posteriors, fwdlattice, bwdlattice,
        #                                  params)

        # from _BaseHMM
        stats['nobs'] += 1
        if 's' in params:
            stats['start'] += posteriors[0]
        if 't' in params:
            for t in xrange(len(framelogprob)):
                zeta = (fwdlattice[t - 1][:, np.newaxis] + self._log_transmat +
                        framelogprob[t] + bwdlattice[t])
                stats['trans'] += np.exp(zeta - logsum(zeta))

        # from GMMHMM
        for state, g in enumerate(self.gmms):
            gmm_logprob, gmm_posteriors = g.eval(obs)
            # don't learn from missing data!
            gmm_posteriors[
                self.impute_p1:self.impute_p2, :] = 1. / self.n_states
            #posteriors2 = posteriors.copy()
            #posteriors2[self.impute_p1:self.impute_p2,:] = 1. / self.n_states
            gmm_posteriors *= posteriors[:, state][:, np.newaxis]
            subops = np.concatenate(
                [obs[:self.impute_p1, :], obs[self.impute_p2:, :]], axis=0)
            subposts = np.concatenate([
                gmm_posteriors[:self.impute_p1, :],
                gmm_posteriors[self.impute_p2:, :]
            ],
                                      axis=0)
            tmpgmm = GMM(g.n_states, g.n_dim, cvtype=g.cvtype)
            norm = tmpgmm._do_mstep(subops, subposts, params)
            # ******************************
            stats['norm'][state] += norm
            if 'm' in params:
                stats['means'][state] += tmpgmm.means * norm[:, np.newaxis]
            if 'c' in params:
                if tmpgmm.cvtype == 'tied':
                    stats['covars'][state] += tmpgmm._covars * norm.sum()
                else:
                    cvnorm = np.copy(norm)
                    shape = np.ones(tmpgmm._covars.ndim)
                    shape[0] = np.shape(tmpgmm._covars)[0]
                    cvnorm.shape = shape
                    stats['covars'][state] += tmpgmm._covars * cvnorm
Пример #3
0
    def _accumulate_sufficient_statistics(self, stats, obs, framelogprob,
                                          posteriors, fwdlattice, bwdlattice,
                                          params):
        """
        Slight modification of the fit function, just to give us
        a reference to the posteriors, so we can modify the observations
        (impute them) in the M step.
        """
        # keep posteriors
        self.curr_posteriors = posteriors
        # regulat fit, except M step modified (see below)
        #super(GMMHMM_impute,self)._accumulate_sufficient_statistics(stats,
        #                                  obs, framelogprob,
        #                                  posteriors, fwdlattice, bwdlattice,
        #                                  params)

        # from _BaseHMM
        stats['nobs'] += 1
        if 's' in params:
            stats['start'] += posteriors[0]
        if 't' in params:
            for t in xrange(len(framelogprob)):
                zeta = (fwdlattice[t-1][:,np.newaxis] + self._log_transmat
                        + framelogprob[t] + bwdlattice[t])
                stats['trans'] += np.exp(zeta - logsum(zeta))

        # from GMMHMM
        for state,g in enumerate(self.gmms):
            gmm_logprob, gmm_posteriors = g.eval(obs)
            # don't learn from missing data!
            gmm_posteriors[self.impute_p1:self.impute_p2,:] = 1. / self.n_states
            #posteriors2 = posteriors.copy()
            #posteriors2[self.impute_p1:self.impute_p2,:] = 1. / self.n_states
            gmm_posteriors *= posteriors[:,state][:,np.newaxis]
            subops = np.concatenate([obs[:self.impute_p1,:],
                                     obs[self.impute_p2:,:]],axis=0)
            subposts = np.concatenate([gmm_posteriors[:self.impute_p1,:],
                                       gmm_posteriors[self.impute_p2:,:]],axis=0)
            tmpgmm = GMM(g.n_states, g.n_dim, cvtype=g.cvtype)
            norm = tmpgmm._do_mstep(subops,subposts,params)
            # ******************************
            stats['norm'][state] += norm
            if 'm' in params:
                stats['means'][state] += tmpgmm.means * norm[:,np.newaxis]
            if 'c' in params:
                if tmpgmm.cvtype == 'tied':
                    stats['covars'][state] += tmpgmm._covars * norm.sum()
                else:
                    cvnorm = np.copy(norm)
                    shape = np.ones(tmpgmm._covars.ndim)
                    shape[0] = np.shape(tmpgmm._covars)[0]
                    cvnorm.shape = shape
                    stats['covars'][state] += tmpgmm._covars * cvnorm
Пример #4
0
def test_logsum_1D():
    A = np.random.rand(2) + 1.0
    for axis in range(1):
        Asum = gmm.logsum(A, axis)
        assert_array_almost_equal(np.exp(Asum), np.sum(np.exp(A), axis))
Пример #5
0
 def test_logsum_with_axis_3D(self):
     A = np.random.rand(10, 4, 5) + 1.0
     for axis in range(3):
         Asum = gmm.logsum(A, axis)
         assert_array_almost_equal(np.exp(Asum), np.sum(np.exp(A), axis))
Пример #6
0
 def test_logsum_2D(self):
     A = np.random.rand(10, 4) + 1.0
     Asum = gmm.logsum(A)
     self.assertAlmostEqual(np.exp(Asum), np.sum(np.exp(A)))