def test_logsumexp(): x = np.abs(np.random.normal(1e-42, size=100000)) logx = np.log(x) assert np.allclose(np.exp(logsumexp(logx)), x.sum()) X = np.vstack([x, x]) logX = np.vstack([logx, logx]) assert np.allclose(np.exp(logsumexp(logX, axis=0)), X.sum(axis=0)) assert np.allclose(np.exp(logsumexp(logX, axis=1)), X.sum(axis=1))
def test_decode(self): _logprob, state_sequence = self.hmm.decode(self.framelogprob) n_samples, n_components = self.framelogprob.shape norm = logsumexp(self.framelogprob, axis=1)[:, np.newaxis] gmmposteriors = np.exp(self.framelogprob - np.tile(norm, (1, n_components))) gmmstate_sequence = gmmposteriors.argmax(axis=1) assert np.allclose(state_sequence, gmmstate_sequence)
def test_score_samples(self): logprob, hmmposteriors = self.hmm.score_samples(self.framelogprob) n_samples, n_components = self.framelogprob.shape assert np.allclose(hmmposteriors.sum(axis=1), np.ones(n_samples)) norm = logsumexp(self.framelogprob, axis=1)[:, np.newaxis] gmmposteriors = np.exp(self.framelogprob - np.tile(norm, (1, n_components))) assert np.allclose(hmmposteriors, gmmposteriors)
def test_hmm_decode_consistent_with_gmm(self): n_components = 8 nobs = 10 h = StubHMM(n_components) # Add dummy observations to stub. framelogprob = np.log(self.prng.rand(nobs, n_components)) h.framelogprob = framelogprob # If startprob and transmat are uniform across all states (the # default), the transitions are uninformative - the model # reduces to a GMM with uniform mixing weights (in terms of # posteriors, not likelihoods). h.startprob_ = np.ones(n_components) / n_components h.transmat_ = np.ones((n_components, n_components)) / n_components viterbi_ll, state_sequence = h.decode(framelogprob) norm = logsumexp(framelogprob, axis=1)[:, np.newaxis] gmmposteriors = np.exp(framelogprob - np.tile(norm, (1, n_components))) gmmstate_sequence = gmmposteriors.argmax(axis=1) assert_array_equal(state_sequence, gmmstate_sequence)
def test_hmm_decode_consistent_with_gmm(self): n_components = 8 nobs = 10 h = StubHMM(n_components) # Add dummy observations to stub. framelogprob = np.log(self.prng.rand(nobs, n_components)) h.framelogprob = framelogprob # If startprob and transmat are uniform across all states (the # default), the transitions are uninformative - the model # reduces to a GMM with uniform mixing weights (in terms of # posteriors, not likelihoods). h.startprob_ = np.ones(n_components) / n_components h.transmat_ = np.ones((n_components, n_components)) / n_components viterbi_ll, state_sequence = h.decode(framelogprob) norm = logsumexp(framelogprob, axis=1)[:, np.newaxis] gmmposteriors = np.exp(framelogprob - np.tile(norm, (1, n_components))) gmmstate_sequence = gmmposteriors.argmax(axis=1) assert np.allclose(state_sequence, gmmstate_sequence)
def test_hmm_score_samples_consistent_with_gmm(self): n_components = 8 nobs = 10 h = StubHMM(n_components) # Add dummy observations to stub. framelogprob = np.log(self.prng.rand(nobs, n_components)) h.framelogprob = framelogprob # If startprob and transmat are uniform across all states (the # default), the transitions are uninformative - the model # reduces to a GMM with uniform mixing weights (in terms of # posteriors, not likelihoods). h.startprob_ = np.ones(n_components) / n_components h.transmat_ = np.ones((n_components, n_components)) / n_components logprob, hmmposteriors = h.score_samples(framelogprob) assert_array_almost_equal(hmmposteriors.sum(axis=1), np.ones(nobs)) norm = logsumexp(framelogprob, axis=1)[:, np.newaxis] gmmposteriors = np.exp(framelogprob - np.tile(norm, (1, n_components))) assert_array_almost_equal(hmmposteriors, gmmposteriors)