Ejemplo n.º 1
0
    def test_fit(self, params='stmwc', n_iter=5, verbose=False, **kwargs):
        h = hmm.GMMHMM(self.n_components, covars_prior=1.0)
        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.sample(n=10, random_state=self.prng)[0]
                     for x in range(10)]

        # Mess up the parameters and see if we can re-learn them.
        h.n_iter = 0
        h.fit(train_obs)
        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)[1:]

        if not np.all(np.diff(trainll) > 0) and verbose:
            print('Test train: (%s)\n  %s\n  %s' % (params, trainll,
                                                    np.diff(trainll)))

        # XXX: this test appears to check that training log likelihood should
        # never be decreasing (up to a tolerance of 0.5, why?) but this is not
        # the case when the seed changes.
        raise SkipTest("Unstable test: trainll is not always increasing "
                       "depending on seed")

        self.assertTrue(np.all(np.diff(trainll) > -0.5))
Ejemplo n.º 2
0
    def test_fit(self, params='ste', n_iter=5, verbose=False, **kwargs):
        h = self.h

        # Create training data by sampling from the HMM.
        train_obs = [h.sample(n=10)[0] for x in range(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:]

        # Check that the loglik is always increasing during training
        if not np.all(np.diff(trainll) > 0) and verbose:
            print('Test train: (%s)\n  %s\n  %s' %
                  (params, trainll, np.diff(trainll)))
        self.assertTrue(np.all(np.diff(trainll) > -1.e-3))
Ejemplo n.º 3
0
    def test_fit(self, params='stmc', n_iter=5, verbose=False, **kwargs):
        h = hmm.GaussianHMM(self.n_components, self.covariance_type)
        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.covariance_type]

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

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

        trainll = train_hmm_and_keep_track_of_log_likelihood(h,
                                                             train_obs,
                                                             n_iter=n_iter,
                                                             params=params,
                                                             **kwargs)[1:]

        # Check that the loglik is always increasing during training
        if not np.all(np.diff(trainll) > 0) and verbose:
            print('Test train: %s (%s)\n  %s\n  %s' %
                  (self.covariance_type, 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.covariance_type, trainll))
Ejemplo n.º 4
0
    def test_fit(self, params='stmc', n_iter=5, verbose=False, **kwargs):
        h = hmm.GaussianHMM(self.n_components, self.covariance_type)
        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.covariance_type]

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

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

        trainll = train_hmm_and_keep_track_of_log_likelihood(
            h, train_obs, n_iter=n_iter, params=params, **kwargs)[1:]

        # Check that the loglik is always increasing during training
        if not np.all(np.diff(trainll) > 0) and verbose:
            print('Test train: %s (%s)\n  %s\n  %s'
                  % (self.covariance_type, 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.covariance_type, trainll))
Ejemplo n.º 5
0
    def test_fit(self, params='ste', n_iter=5, verbose=False, **kwargs):
        h = self.h

        # Create training data by sampling from the HMM.
        train_obs = [h.sample(n=10)[0] for x in range(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:]

        # Check that the loglik is always increasing during training
        if not np.all(np.diff(trainll) > 0) and verbose:
            print('Test train: (%s)\n  %s\n  %s' % (params, trainll,
                                                    np.diff(trainll)))
        self.assertTrue(np.all(np.diff(trainll) > -1.e-3))
Ejemplo n.º 6
0
    def initHMM(self):

        if os.path.exists('hmm-model.pkl'):
            with open("hmm-model.pkl", "rb") as file:
                self.dhmm = pickle.load(file)
        else:
            Pi = np.array([1, 0, 0, 0])
            A = np.array([[0.5, 0.5, 0, 0], [0, 0.5, 0.5, 0], [0, 0, 0.5, 0.5],
                          [0.5, 0, 0, 0.5]])
            #B = np.array([
            #    [0.001      ,0.001      ,0.001      ,0.5-0.001    ,0.25-0.001   ,0.25-0.002   ,0.001  ],
            #    [0.001      ,0.25-0.001   ,0.25-0.001   ,0.5-0.002    ,0.001      ,0.001      ,0.001  ],
            #    [2/3-0.003    ,1/3-0.002    ,0.001      ,0.001      ,0.001      ,0.001      ,0.001  ],
            #    [0.001      ,0.001      ,0.001      ,0.001      ,0.001      ,1/3-0.002    ,2/3-0.003]
            #])
            B = np.array([[0, 0, 0, 0.5, 0.25, 0.25, 0],
                          [0, 0.25, 0.25, 0.5, 0, 0, 0],
                          [2 / 3, 1 / 3, 0, 0, 0, 0, 0],
                          [0, 0, 0, 0, 0, 1 / 3, 2 / 3]])
            n_states = len(self.states)

            self.dhmm = hmm.MultinomialHMM(n_components=n_states,
                                           n_iter=100,
                                           verbose=True,
                                           init_params="s",
                                           params='e')
            #self.dhmm = hmm.MultinomialHMM(n_components=n_states, startprob_prior=Pi, transmat_prior=A, init_params="",  n_iter=100)
            self.dhmm.n_features = 7
            #self.dhmm.n_features_ = 7
            hmm.normalize(B, axis=1)
            self.dhmm.transmat_ = A
            self.dhmm.transmat_prior = A
            self.dhmm.emissionprob_ = B
            self.dhmm.emissionprob_prior = B
            self.dhmm.startprob_ = Pi
            self.dhmm.monitor_.verbose = True
            self.dhmm.monitor_.tol = 0.0001
Ejemplo n.º 7
0
    def test_fit(self, params='stmwc', n_iter=5, verbose=False, **kwargs):
        h = hmm.GMMHMM(self.n_components, covars_prior=1.0)
        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.sample(n=10, random_state=self.prng)[0] for x in range(10)
        ]

        # Mess up the parameters and see if we can re-learn them.
        h.n_iter = 0
        h.fit(train_obs)
        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)[1:]

        if not np.all(np.diff(trainll) > 0) and verbose:
            print('Test train: (%s)\n  %s\n  %s' %
                  (params, trainll, np.diff(trainll)))

        # XXX: this test appears to check that training log likelihood should
        # never be decreasing (up to a tolerance of 0.5, why?) but this is not
        # the case when the seed changes.
        raise SkipTest("Unstable test: trainll is not always increasing "
                       "depending on seed")

        self.assertTrue(np.all(np.diff(trainll) > -0.5))
Ejemplo n.º 8
0
def create_random_gmm(n_mix, n_features, covariance_type, prng=0):
    prng = check_random_state(prng)
    g = mixture.GMM(n_mix, covariance_type=covariance_type)
    g.means_ = prng.randint(-20, 20, (n_mix, n_features))
    mincv = 0.1
    g.covars_ = {
        'spherical': (mincv + mincv * np.dot(prng.rand(n_mix, 1),
                                             np.ones((1, n_features)))) ** 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 range(n_mix)])
    }[covariance_type]
    g.weights_ = hmm.normalize(prng.rand(n_mix))
    return g
Ejemplo n.º 9
0
def create_random_gmm(n_mix, n_features, covariance_type, prng=0):
    prng = check_random_state(prng)
    g = mixture.GMM(n_mix, covariance_type=covariance_type)
    g.means_ = prng.randint(-20, 20, (n_mix, n_features))
    mincv = 0.1
    g.covars_ = {
        'spherical':
        (mincv + mincv * np.dot(prng.rand(n_mix, 1), np.ones(
            (1, n_features))))**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 range(n_mix)
        ])
    }[covariance_type]
    g.weights_ = hmm.normalize(prng.rand(n_mix))
    return g
Ejemplo n.º 10
0
def train_pose():
    global is_collecting_new_pose
    is_collecting_new_pose = True
    new_poses_ranges = []
    countdown('do regular stuff and not your pose.')
    for _ in xrange(1):
        time.sleep(2)
        countdown('do your pose.')
        new_pose_index_start = len(new_pose_data) - 1
        time.sleep(1)
        new_pose_index_end = len(new_pose_data)
        new_poses_ranges.append((new_pose_index_start, new_pose_index_end))
        print("Do regular stuff again.")
    print("Done training.")
    is_collecting_new_pose = False
    logging.debug('new_pose_data:\n%s', new_pose_data)
    X = np.array(new_pose_data)
    logging.debug('X:\n%s', X)
    print('number of Observations:', len(X))
    global model
    startprob = np.zeros(n_states)
    startprob[0] = 1
    transmat = hmm.normalize(np.random.rand(n_states, n_states), axis=1)
    model = hmm.GaussianHMM(n_components=n_states,
                            covariance_type="full",
                            startprob=startprob,
                            transmat=transmat)
    model.means_ = np.zeros((n_states,NUM_FEATURES))
    model.covars_ = np.tile(np.identity(NUM_FEATURES), (n_states, 1, 1))
    print(model.fit([X]))
    predictions = model.predict(X)
    logging.info("Predictions:\n%s", list(predictions))
    predictions_set = set(predictions)
    logging.info("Set of predictions:\n%s", predictions_set)
    logging.info("Num distinct predictions:\n%s", len(predictions_set))

    # TODO keep track of regular states distribution

    global new_pose_states_dist
    new_pose_states_dist = get_distribution(predictions, new_poses_ranges)
    logging.info('new_pose_states_dist: %s', new_pose_states_dist)
Ejemplo n.º 11
0
    def test_fit_with_priors(self, params='stmc', n_iter=5, 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.covariance_type in ('full', 'tied'):
            covars_weight += self.n_features
        covars_prior = self.covars[self.covariance_type]

        h = hmm.GaussianHMM(self.n_components, self.covariance_type)
        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.covariance_type]
        h.covars_prior = covars_prior
        h.covars_weight = covars_weight

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

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

        trainll = train_hmm_and_keep_track_of_log_likelihood(h,
                                                             train_obs,
                                                             n_iter=n_iter,
                                                             params=params)[1:]

        # Check that the loglik is always increasing during training
        if not np.all(np.diff(trainll) > 0) and verbose:
            print('Test MAP train: %s (%s)\n  %s\n  %s' %
                  (self.covariance_type, params, trainll, np.diff(trainll)))
        # XXX: Why such a large tolerance?
        self.assertTrue(np.all(np.diff(trainll) > -0.5))
Ejemplo n.º 12
0
    def test_fit_with_priors(self, params='stmc', n_iter=5, 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.covariance_type in ('full', 'tied'):
            covars_weight += self.n_features
        covars_prior = self.covars[self.covariance_type]

        h = hmm.GaussianHMM(self.n_components, self.covariance_type)
        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.covariance_type]
        h.covars_prior = covars_prior
        h.covars_weight = covars_weight

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

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

        trainll = train_hmm_and_keep_track_of_log_likelihood(
            h, train_obs, n_iter=n_iter, params=params)[1:]

        # Check that the loglik is always increasing during training
        if not np.all(np.diff(trainll) > 0) and verbose:
            print('Test MAP train: %s (%s)\n  %s\n  %s'
                  % (self.covariance_type, params, trainll, np.diff(trainll)))
        # XXX: Why such a large tolerance?
        self.assertTrue(np.all(np.diff(trainll) > -0.5))
Ejemplo n.º 13
0
def test_normalize_3D():
    A = rng.rand(2, 2, 2) + 1.0
    for axis in range(3):
        Anorm = hmm.normalize(A, axis)
        assert np.all(np.allclose(Anorm.sum(axis), 1.0))
Ejemplo n.º 14
0
def test_normalize_3D():
    A = rng.rand(2, 2, 2) + 1.0
    for axis in range(3):
        Anorm = hmm.normalize(A, axis)
        assert np.all(np.allclose(Anorm.sum(axis), 1.0))