示例#1
0
    def _accumulate_sufficient_statistics(self, stats, obs, framelogprob,
                                          posteriors, fwdlattice, bwdlattice,
                                          params):
        super(GMMHMM, self)._accumulate_sufficient_statistics(
            stats, obs, framelogprob, posteriors, fwdlattice, bwdlattice,
            params)

        for state, g in enumerate(self.gmms):
            lgmm_posteriors = np.log(g.eval(obs)[1] + np.finfo(np.float).eps)
            lgmm_posteriors += np.log(posteriors[:, state][:, np.newaxis]
                                      + np.finfo(np.float).eps)
            gmm_posteriors = np.exp(lgmm_posteriors)
            tmp_gmm = GMM(g.n_components, cvtype=g.cvtype)
            tmp_gmm.n_features = g.n_features
            tmp_gmm.covars = _distribute_covar_matrix_to_match_cvtype(
                                np.eye(g.n_features), g.cvtype, g.n_components)
            norm = tmp_gmm._do_mstep(obs, gmm_posteriors, params)

            if np.any(np.isnan(tmp_gmm.covars)):
                raise ValueError

            stats['norm'][state] += norm
            if 'm' in params:
                stats['means'][state] += tmp_gmm.means * norm[:, np.newaxis]
            if 'c' in params:
                if tmp_gmm.cvtype == 'tied':
                    stats['covars'][state] += tmp_gmm._covars * norm.sum()
                else:
                    cvnorm = np.copy(norm)
                    shape = np.ones(tmp_gmm._covars.ndim)
                    shape[0] = np.shape(tmp_gmm._covars)[0]
                    cvnorm.shape = shape
                    stats['covars'][state] += tmp_gmm._covars * cvnorm
示例#2
0
    def _init(self, obs, params='stwmc'):
        super(GMMHMM, self)._init(obs, params=params)

        allobs = np.concatenate(obs, 0)
        n_centers = self.n_components * self.n_mix
        cluster_centers = cluster.KMeans(
            k=n_centers).fit(allobs).cluster_centers_
        K = cluster_centers.shape[1]
        inds = random.sample(np.arange(0, n_centers), n_centers)

        for i in xrange(self.n_components):
            self.gmms[i].n_features = K

        if 'm' in params:
            for i in xrange(self.n_components):
                self.gmms[i]._means = cluster_centers[inds[(i * self.n_mix):(
                    (i + 1) * self.n_mix)], :] + np.random.multivariate_normal(
                        np.zeros(K),
                        np.eye(K) * self.var, self.n_mix)

        if 'c' in params:
            cv = np.cov(obs[0].T)
            if not cv.shape:
                cv.shape = (1, 1)
            for i in xrange(self.n_components):
                self.gmms[
                    i]._covars = _distribute_covar_matrix_to_match_cvtype(
                        cv, self.cvtype, self.n_mix)
示例#3
0
    def _init(self, obs, params='stmc', **kwargs):
        super(GaussianHMM, self)._init(obs, params=params)

        if 'm' in params:
            self._means, tmp = sp.cluster.vq.kmeans2(obs[0], self._nstates,
                                                     **kwargs)
        if 'c' in params:
            cv = np.cov(obs[0].T)
            if not cv.shape:
                cv.shape = (1, 1)
            self._covars = _distribute_covar_matrix_to_match_cvtype(
                cv, self._cvtype, self._nstates)
示例#4
0
文件: hmm.py 项目: ronw/gm
    def _init(self, obs, params='stmc', **kwargs):
        super(GaussianHMM, self)._init(obs, params=params)

        if 'm' in params:
            self._means, tmp = sp.cluster.vq.kmeans2(obs[0], self._nstates,
                                                     **kwargs)
        if 'c' in params:
            cv = np.cov(obs[0].T)
            if not cv.shape:
                cv.shape = (1, 1)
            self._covars = _distribute_covar_matrix_to_match_cvtype(
                cv, self._cvtype, self._nstates)
示例#5
0
    def __init__(self,
                 nstates=1,
                 ndim=1,
                 cvtype='diag',
                 startprob=None,
                 transmat=None,
                 labels=None,
                 means=None,
                 covars=None,
                 trainer=hmm_trainers.GaussianHMMBaumWelchTrainer()):
        """Create a hidden Markov model with Gaussian emissions.

        Initializes parameters such that every state has zero mean and
        identity covariance.

        Parameters
        ----------
        ndim : int
            Dimensionality of the states.
        nstates : int
            Number of states.
        cvtype : string (read-only)
            String describing the type of covariance parameters to
            use.  Must be one of 'spherical', 'tied', 'diag', 'full'.
            Defaults to 'diag'.
        """
        super(GaussianHMM, self).__init__(nstates, startprob, transmat, labels,
                                          trainer)

        self._ndim = ndim
        self._cvtype = cvtype

        if means is None:
            means = np.zeros((nstates, ndim))
        self.means = means

        if covars is None:
            covars = _distribute_covar_matrix_to_match_cvtype(
                np.eye(ndim), cvtype, nstates)
        self.covars = covars

        self.trainer = trainer
示例#6
0
    def _init(self, obs, params='stmc'):
        super(GaussianHMM, self)._init(obs, params=params)

        if (hasattr(self, 'n_features')
            and self.n_features != obs[0].shape[1]):
            raise ValueError('Unexpected number of dimensions, got %s but '
                             'expected %s' % (obs[0].shape[1],
                                              self.n_features))

        self.n_features = obs[0].shape[1]

        if 'm' in params:
            self._means = cluster.KMeans(
                k=self.n_components).fit(obs[0]).cluster_centers_
        if 'c' in params:
            cv = np.cov(obs[0].T)
            if not cv.shape:
                cv.shape = (1, 1)
            self._covars = _distribute_covar_matrix_to_match_cvtype(
                cv, self._cvtype, self.n_components)
示例#7
0
文件: gmmhmm.py 项目: davidreber/Labs
    def _init(self, obs, params='stwmc'):
        super(GMMHMM, self)._init(obs, params=params)

        allobs = np.concatenate(obs, 0)
	n_centers = self.n_components*self.n_mix
	cluster_centers = cluster.KMeans(n_clusters=n_centers).fit(allobs).cluster_centers_
	K = cluster_centers.shape[1]
	inds = random.sample(np.arange(0,n_centers),n_centers)

	for i in xrange(self.n_components):
            self.gmms[i].n_features = K

	if 'm' in params:
	    for i in xrange(self.n_components):
                self.gmms[i]._means = cluster_centers[inds[(i*self.n_mix):((i+1)*self.n_mix)],:] + np.random.multivariate_normal(np.zeros(K),np.eye(K)*self.var,self.n_mix)

        if 'c' in params:
            cv = np.cov(obs[0].T)
            if not cv.shape:
                cv.shape = (1, 1)
            for i in xrange(self.n_components):
                self.gmms[i]._covars = _distribute_covar_matrix_to_match_cvtype(
                    cv, self.cvtype, self.n_mix)
示例#8
0
    def __init__(self, nstates, ndim=1, cvtype='diag', startprob=None,
                 transmat=None, labels=None, means=None, covars=None,
                 trainer=hmm_trainers.GaussianHMMBaumWelchTrainer()):
        """Create a hidden Markov model with Gaussian emissions.

        Initializes parameters such that every state has zero mean and
        identity covariance.

        Parameters
        ----------
        nstates : int
            Number of states.
        ndim : int
            Dimensionality of the emissions.
        cvtype : string
            String describing the type of covariance parameters to
            use.  Must be one of 'spherical', 'tied', 'diag', 'full'.
            Defaults to 'diag'.
        """
        super(GaussianHMM, self).__init__(nstates, startprob, transmat, labels)

        self._ndim = ndim
        self._cvtype = cvtype
        if not cvtype in ['spherical', 'tied', 'diag', 'full']:
            raise ValueError('bad cvtype')

        if means is None:
            means = np.zeros((nstates, ndim))
        self.means = means

        if covars is None:
            covars = _distribute_covar_matrix_to_match_cvtype(np.eye(ndim),
                                                              cvtype, nstates)
        self.covars = covars

        self._default_trainer = trainer