要学习PyMC,我正在尝试做一个简单的隐马尔可夫模型,如下所示:
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]))
我认为这个模型应该是正确的,但是当我尝试从这个模型中采样时,我得到一个非常奇怪的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]
我上传了ipython notebook with the complete code。关于我做错了什么的暗示?