我是Pymc3的新手,我试图创建https://en.wikipedia.org/wiki/Mixture_model#Categorical_mixture_model中显示的分类混合模型。我很难挂钩' x'变量。我认为这是因为我必须使z变量确定性,但我在' x'的行中收到错误消息。分配:" ValueError:我们预计有3个输入,但得到2个。"。看起来p函数只接受2个输入,所以我被卡住了。我尝试了很多不同的东西,但还没有能够让它工作。
import numpy as np
from pymc3 import *
import theano.tensor as t
K = 3 #NUMBER OF TOPICS
V = 20 #NUMBER OF WORDS
N = 15 #NUMBER OF DOCUMENTS
#GENERAETE RANDOM CATEGORICAL MIXTURES
data = np.ones([N,V])
@theano.compile.ops.as_op(itypes=[t.lscalar, t.dscalar, t.dscalar],otypes=[t.dvector])
def p(z=z, phi=phi):
return [phi[z[i,j]] for i in range(D) for j in range(W)]
model = Model()
with model:
alpha = np.ones(V)
beta = np.ones(K)
theta = [Dirichlet('theta_%i' % i, alpha, shape=V) for i in range(K)]
phi = Dirichlet('phi', beta, shape=K)
z = [Categorical('z_%i' % i, p = phi, shape=V) for i in range(N)]
x = [Categorical('x_%i_%i' % (i,j), p=p(z[i][j],phi), observed=data[i,j]) for i in range(N) for j in range(V)]
#x = [Categorical('x_%i_%i' % (i,j), p=theta[z[i][j]], observed=data[i,j]) for i in range(N) for j in range(V)]
print "Created model. Now begin sampling"
step = Slice()
trace = sample(n, step)
trace.get_values('phi')
答案 0 :(得分:0)
对于初学者,在上面的例子中,z和phi没有允许它们用作默认值的值。我们也没有D和W的值。
至于参数的数量,你定义的函数有2但你上面的theano装饰器有3个。我建议
@theano.compile.ops.as_op(itypes=[t.lscalar, t.dvector],otypes=[t.dvector])
def p(z, phi):
return [phi[z[i,j]] for i,j in zip(range(D),range(W))]