Problems with a hidden Markov model in PyMC3

吃可爱长大的小学妹 提交于 2020-02-01 19:59:30

问题


To learn PyMC, I'm trying to do a simple Hidden Markov Model as shown below:

with pymc3.Model() as hmm:
    # Transition "matrix"
    a_t = np.ones(num_states)
    T = [pymc3.Dirichlet('T{0}'.format(i), a = a_t,  shape = num_states) for i in xrange(num_states)]
    # Emission "matrix"
    a_e = np.ones(num_emissions)
    E = [pymc3.Dirichlet('E{0}'.format(i), a = a_e,  shape = num_emissions) for i in xrange(num_states)]
    # State models
    p0 = np.ones(num_states) / num_states
    # No shape, so each state is a scalar tensor
    states = [pymc3.Categorical('s0', p = p0)]
    emissions = [pymc3.Categorical('z0',
                               p = ifelse(eq(states[0], 0), E[0], ifelse(eq(states[0], 1), E[1], E[2])),
                               observed = example_observation[0])]
    for i in xrange(1, num_times):
        states.append(pymc3.Categorical('s{0}'.format(i),
                                    p = ifelse(eq(states[i-1], 0), T[0], ifelse(eq(states[i-1], 1), T[1], T[2]))))
        emissions.append(pymc3.Categorical('z{0}'.format(i),
                                    p = ifelse(eq(states[i], 0), E[0], ifelse(eq(states[i], 1), E[1], E[2])),
                                    observed = example_observation[i]))

I think this model should be correct, but when I try sampling from this model, I get a really strange InvalidValueError:

InvalidValueError: InvalidValueError
        type(variable) = TensorType(float32, scalar)
        variable       = TensorConstant{-inf}
        type(value)    = <type 'numpy.ndarray'>
        dtype(value)   = float32
        shape(value)   = ()
        value          = -inf
        min(value)     = -inf
        max(value)     = -inf
        isfinite       = False
        client_node    = None
        hint           = Graph Input 'TensorConstant{-inf}' has invalid value -inf
        specific_hint  = none
        context        = ...
  TensorConstant{-inf} [@A]

I've uplaoded the ipython notebook with the complete code. Any hints on what am I doing wrong?

来源:https://stackoverflow.com/questions/31622780/problems-with-a-hidden-markov-model-in-pymc3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!