Beispiel #1
0
    def _messages_forwards_log(trans_matrix, init_state_distn,
                               log_likelihoods):
        from pyhsmm.internals.hmm_messages_interface import messages_forwards_log

        return messages_forwards_log(
            trans_matrix,
            log_likelihoods,
            init_state_distn,
            np.empty_like(log_likelihoods),
        )
Beispiel #2
0
def test_hmm_mp_perf(T=10000, K=100, D=20):
    # Make parameters
    pi0 = np.ones(K) / K
    Ps = npr.rand(T-1, K, K)
    Ps /= Ps.sum(axis=2, keepdims=True)
    ll = npr.randn(T, K)
    out1 = np.zeros((T, K))
    out2 = np.zeros((T, K))

    # Run the PyHSMM message passing code
    from pyhsmm.internals.hmm_messages_interface import messages_forwards_log, messages_backwards_log
    tic = time()
    messages_forwards_log(Ps, ll, pi0, out1)
    pyhsmm_dt = time() - tic
    print("PyHSMM Fwd: ", pyhsmm_dt, "sec")

    # Run the SSM message passing code
    from ssm.messages import forward_pass, backward_pass
    forward_pass(pi0, Ps, ll, out2) # Call once to compile, then time it
    tic = time()
    forward_pass(pi0, Ps, ll, out2)
    smm_dt = time() - tic
    print("SMM Fwd: ", smm_dt, "sec")
    assert np.allclose(out1, out2)

    # Backward pass
    tic = time()
    messages_backwards_log(Ps, ll, out1)
    pyhsmm_dt = time() - tic
    print("PyHSMM Bwd: ", pyhsmm_dt, "sec")

    backward_pass(Ps, ll, out2) # Call once to compile, then time it
    tic = time()
    backward_pass(Ps, ll, out2)
    smm_dt = time() - tic
    print("SMM (Numba) Bwd: ", smm_dt, "sec")
    assert np.allclose(out1, out2)
Beispiel #3
0
def hmm_estep(natparam):
    C = lambda x: np.require(x, np.double, 'C')
    init_params, pair_params, node_params = list(map(C, natparam))

    # compute messages
    alphal = messages_forwards_log(np.exp(pair_params), node_params,
                                   np.exp(init_params),
                                   np.zeros_like(node_params))
    betal = messages_backwards_log(np.exp(pair_params), node_params,
                                   np.zeros_like(node_params))

    # compute expected statistics from messages
    expected_states, expected_transcounts, log_normalizer = \
        expected_statistics_log(
            pair_params, node_params, alphal, betal,
            np.zeros_like(node_params), np.zeros_like(pair_params))

    expected_stats = expected_states[0], expected_transcounts, expected_states

    return log_normalizer, expected_stats
Beispiel #4
0
 def _messages_forwards_log(trans_matrix,init_state_distn,log_likelihoods):
     from pyhsmm.internals.hmm_messages_interface import messages_forwards_log
     return messages_forwards_log(trans_matrix,log_likelihoods,
             init_state_distn,np.empty_like(log_likelihoods))