2015-08-28 85 views
1

我是Pymc3的新手,我試圖創建https://en.wikipedia.org/wiki/Mixture_model#Categorical_mixture_model中顯示的分類混合模型。我無法連接'x'變量。我認爲這是因爲我必須讓z變量爲Deterministic,但是我在分配'x'的行收到錯誤消息:「ValueError:我們期望3個輸入但得到2」。它看起來像p函數只接受2個輸入,所以我卡住了。我嘗試了一堆不同的東西,但還沒有能夠得到這個工作。Pymc3中的分類混合模型

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

對於初學者來說,在上面的例子中,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))]