Example #1
0
    def test_fit(self, params='stmwc', niter=5, **kwargs):
        h = hmm.GMMHMM(self.nstates, self.ndim)
        h.startprob = self.startprob
        h.transmat = hmm.normalize(self.transmat
                                   + np.diag(np.random.rand(self.nstates)), 1)
        h.gmms = self.gmms

        # Create a training and testing set by sampling from the same
        # distribution.
        train_obs = [h.rvs(n=10) for x in xrange(50)]
        test_obs = [h.rvs(n=10) for x in xrange(5)]

        # Mess up the parameters and see if we can re-learn them.
        h.fit(train_obs, niter=0, minit='points')
        h.transmat = hmm.normalize(np.random.rand(self.nstates, self.nstates),
                                   axis=1)
        h.startprob = hmm.normalize(np.random.rand(self.nstates))
        init_testll = [h.lpdf(x) for x in test_obs]

        trainll = h.fit(train_obs, niter=niter, params=params, **kwargs)
        if not np.all(np.diff(trainll) > 0):
            print
            print 'Test train: (%s)\n  %s\n  %s' % (params, trainll,
                                                    np.diff(trainll))
        self.assertTrue(np.all(np.diff(trainll) > -0.5))

        post_testll = [h.lpdf(x) for x in test_obs]
        if not (np.sum(post_testll) > np.sum(init_testll)):
            print
            print 'Test train: (%s)\n  %s\n  %s' % (params, init_testll,
                                                    post_testll)
        self.assertTrue(np.sum(post_testll) > np.sum(init_testll))
Example #2
0
    def test_fit(self, params='ste', niter=5, **kwargs):
        h = hmm.MultinomialHMM(self.nstates, self.nsymbols,
                               startprob=self.startprob, transmat=self.transmat,
                               emissionprob=self.emissionprob)

        # Create a training and testing set by sampling from the same
        # distribution.
        train_obs = [h.rvs(n=10) for x in xrange(50)]
        test_obs = [h.rvs(n=10) for x in xrange(5)]

        # Mess up the parameters and see if we can re-learn them.
        h.startprob = hmm.normalize(np.random.rand(self.nstates))
        h.transmat = hmm.normalize(np.random.rand(self.nstates, self.nstates),
                                   axis=1)
        h.emissionprob = hmm.normalize(
            np.random.rand(self.nstates, self.nsymbols), axis=1)

        init_testll = [h.lpdf(x) for x in test_obs]

        trainll = h.fit(train_obs, niter=niter, params=params, **kwargs)
        if not np.all(np.diff(trainll) > 0):
            print
            print 'Test train: (%s)\n  %s\n  %s' % (params, trainll,
                                                    np.diff(trainll))
        self.assertTrue(np.all(np.diff(trainll) > 0))

        post_testll = [h.lpdf(x) for x in test_obs]
        if not (np.sum(post_testll) > np.sum(init_testll)):
            print
            print 'Test train: (%s)\n  %s\n  %s' % (params, init_testll,
                                                    post_testll)
        self.assertTrue(np.sum(post_testll) > np.sum(init_testll))
Example #3
0
    def test_fit_with_priors(self, params="stmc", n_iter=10, verbose=False):
        startprob_prior = 10 * self.startprob + 2.0
        transmat_prior = 10 * self.transmat + 2.0
        means_prior = self.means
        means_weight = 2.0
        covars_weight = 2.0
        if self.cvtype in ("full", "tied"):
            covars_weight += self.n_features
        covars_prior = self.covars[self.cvtype]

        h = hmm.GaussianHMM(self.n_components, self.cvtype)
        h.startprob = self.startprob
        h.startprob_prior = startprob_prior
        h.transmat = hmm.normalize(self.transmat + np.diag(self.prng.rand(self.n_components)), 1)
        h.transmat_prior = transmat_prior
        h.means = 20 * self.means
        h.means_prior = means_prior
        h.means_weight = means_weight
        h.covars = self.covars[self.cvtype]
        h.covars_prior = covars_prior
        h.covars_weight = covars_weight

        # Create training data by sampling from the HMM.
        train_obs = [h.rvs(n=10) for x in xrange(10)]

        # Mess up the parameters and see if we can re-learn them.
        h.fit(train_obs[:1], n_iter=0)

        trainll = train_hmm_and_keep_track_of_log_likelihood(h, train_obs, n_iter=n_iter, params=params)[1:]
        if not np.all(np.diff(trainll) > 0) and verbose:
            print
            print ("Test MAP train: %s (%s)\n  %s\n  %s" % (self.cvtype, params, trainll, np.diff(trainll)))
        self.assertTrue(np.all(np.diff(trainll) > -0.5))
Example #4
0
    def test_fit(self, params="ste", n_iter=15, verbose=False, **kwargs):
        np.random.seed(0)
        h = hmm.MultinomialHMM(self.n_components, startprob=self.startprob, transmat=self.transmat)
        h.emissionprob = self.emissionprob

        # Create training data by sampling from the HMM.
        train_obs = [h.rvs(n=10) for x in xrange(10)]

        # Mess up the parameters and see if we can re-learn them.
        h.startprob = hmm.normalize(self.prng.rand(self.n_components))
        h.transmat = hmm.normalize(self.prng.rand(self.n_components, self.n_components), axis=1)
        h.emissionprob = hmm.normalize(self.prng.rand(self.n_components, self.n_symbols), axis=1)

        trainll = train_hmm_and_keep_track_of_log_likelihood(h, train_obs, n_iter=n_iter, params=params, **kwargs)[1:]
        if not np.all(np.diff(trainll) > 0) and verbose:
            print
            print "Test train: (%s)\n  %s\n  %s" % (params, trainll, np.diff(trainll))
        self.assertTrue(np.all(np.diff(trainll) > 0))
Example #5
0
    def test_fit(self, params="stmwc", n_iter=5, verbose=False, **kwargs):
        h = hmm.GMMHMM(self.n_components)
        h.startprob = self.startprob
        h.transmat = hmm.normalize(self.transmat + np.diag(self.prng.rand(self.n_components)), 1)
        h.gmms = self.gmms

        # Create training data by sampling from the HMM.
        train_obs = [h.rvs(n=10, random_state=self.prng) for x in xrange(10)]

        # Mess up the parameters and see if we can re-learn them.
        h.fit(train_obs, n_iter=0)
        h.transmat = hmm.normalize(self.prng.rand(self.n_components, self.n_components), axis=1)
        h.startprob = hmm.normalize(self.prng.rand(self.n_components))

        trainll = train_hmm_and_keep_track_of_log_likelihood(
            h, train_obs, n_iter=n_iter, params=params, covars_prior=1.0, **kwargs
        )[1:]
        if not np.all(np.diff(trainll) > 0) and verbose:
            print
            print "Test train: (%s)\n  %s\n  %s" % (params, trainll, np.diff(trainll))
        self.assertTrue(np.all(np.diff(trainll) > -0.5))
Example #6
0
    def create_random_gmm(nmix, ndim, cvtype):
        from scikits.learn import gmm

        means = np.random.randint(-20, 20, (nmix, ndim))
        mincv = 2
        covars = {'spherical': (1.0 + mincv * np.random.rand(nmix))**2,
                  'tied': _generate_random_spd_matrix(ndim)
                          + mincv * np.eye(ndim),
                  'diag': (1.0 + mincv * np.random.rand(nmix, ndim))**2,
                  'full': np.array([_generate_random_spd_matrix(ndim)
                                    + mincv * np.eye(ndim)
                                    for x in xrange(nmix)])}[cvtype]
        weights = hmm.normalize(np.random.rand(nmix))
        return gmm.GMM(nmix, ndim, cvtype=cvtype, weights=weights, means=means,
                       covars=covars)
Example #7
0
def create_random_gmm(n_mix, n_features, cvtype, prng=prng):
    from scikits.learn import mixture

    g = mixture.GMM(n_mix, cvtype=cvtype)
    g.means = prng.randint(-20, 20, (n_mix, n_features))
    mincv = 0.1
    g.covars = {
        "spherical": (mincv + mincv * prng.rand(n_mix)) ** 2,
        "tied": (make_spd_matrix(n_features, random_state=prng) + mincv * np.eye(n_features)),
        "diag": (mincv + mincv * prng.rand(n_mix, n_features)) ** 2,
        "full": np.array(
            [make_spd_matrix(n_features, random_state=prng) + mincv * np.eye(n_features) for x in xrange(n_mix)]
        ),
    }[cvtype]
    g.weights = hmm.normalize(prng.rand(n_mix))
    return g
Example #8
0
    def test_fit(self, params="stmc", n_iter=25, verbose=False, **kwargs):
        np.random.seed(0)
        h = hmm.GaussianHMM(self.n_components, self.cvtype)
        h.startprob = self.startprob
        h.transmat = hmm.normalize(self.transmat + np.diag(self.prng.rand(self.n_components)), 1)
        h.means = 20 * self.means
        h.covars = self.covars[self.cvtype]

        # Create training data by sampling from the HMM.
        train_obs = [h.rvs(n=10) for x in xrange(10)]

        # Mess up the parameters and see if we can re-learn them.
        h.fit(train_obs, n_iter=0)

        trainll = train_hmm_and_keep_track_of_log_likelihood(h, train_obs, n_iter=n_iter, params=params, **kwargs)[1:]
        if not np.all(np.diff(trainll) > 0) and verbose:
            print
            print ("Test train: %s (%s)\n  %s\n  %s" % (self.cvtype, params, trainll, np.diff(trainll)))
        delta_min = np.diff(trainll).min()
        self.assertTrue(
            delta_min > -0.8,
            "The min nll increase is %f which is lower than the admissible"
            " threshold of %f, for model %s. The likelihoods are %s." % (delta_min, -0.8, self.cvtype, trainll),
        )