我的可观察数据非常大,它的似然函数非常复杂,所以我将所有可观察数据加载到 GPU 中,然后使用 theano 函数根据我试图估计的参数来获得它们的似然性。
def create_function(data_values):
X = theano.shared(np.asarray(data_values,
dtype=theano.config.floatX),
borrow=borrow)
def logp(B,sigma_r,sigma_u):
P = complexTheanoFunction(X)
return T.sum(T.log(P))
return logp
with mc.Model() as model:
data_values = load_data()
logp = create_function(data_values)
sigma_r = mc.Uniform('sigma_r',0,1,testval=0.5,dtype='float32')
sigma_u = mc.Uniform('sigma_u',0,1,testval=0.5,dtype='float32')
B = mc.Uniform('B',0,1,testval=np.array([0.5,0.5,.5,.5],dtype=np.float32),shape=4,dtype='float32')
get_logp = theano.function(
inputs = [B,sigma_r,sigma_u],
outputs = logp,
)
obs = mc.DensityDist('observations',get_logp)
start = mc.find_MAP()
step = mc.NUTS(state=start)
trace = mc.sample(5000,step,start=start)
mc.traceplot(trace, )
我不知道这是否是正确的方法。我收到错误消息:
obs = mc.DensityDist('observations',get_logp)
File "/usr/local/lib/python2.7/dist-packages/pymc3/distributions/distribution.py", line 19, in __new__
return model.Var(name, dist, data)
File "/usr/local/lib/python2.7/dist-packages/pymc3/model.py", line 143, in Var
var = FreeRV(name=name, distribution=dist, model=self)
File "/usr/local/lib/python2.7/dist-packages/pymc3/model.py", line 322, in __init__
self.logp_elemwiset = distribution.logp(self)
File "/usr/local/lib/python2.7/dist-packages/theano/compile/function_module.py", line 517, in __call__
allow_downcast=s.allow_downcast)
File "/usr/local/lib/python2.7/dist-packages/theano/tensor/type.py", line 77, in filter
'Expected an array-like object, but found a Variable: '
TypeError: ('Bad input argument to theano function with name "/home/joao/Dropbox/physological/Juggler/SaliencyExp/mc3tryExp.py:64" at index 0(0-based)', 'Expected an array-like object, but found a Variable: maybe you are trying to call a function on a (possibly shared) variable instead of a numeric array?')
----------//----------- 我试着用John Salvatier给出的例子做一个简单的例子:PS这个模型没有任何意义..我我只是想弄清楚 Theano 如何与 pymc3 一起工作
with mc.Model() as model:
data_values = np.random.rand(200,3)
X = theano.shared(np.asarray(data_values,
dtype=theano.config.floatX),
borrow=True)
B = mc.Uniform('B',0,1,testval=np.array([.5,.5,.5],dtype=np.float32),shape=3,dtype='float32')
def logp(x,B):
P = x.dot(B)
P = P/T.sum(P)
return T.sum(T.log(P))
obs = mc.DensityDist('observations',logp, observed=X)
start = mc.find_MAP()
step = mc.NUTS(state=start)
我得到这个错误:
Traceback (most recent call last):
File "/home/joao/test.py.py", line 27, in <module>
obs = mc.DensityDist('observations',logp, observed=X)
File "/usr/local/lib/python2.7/dist-packages/pymc3/distributions/distribution.py", line 19, in __new__
return model.Var(name, dist, data)
File "/usr/local/lib/python2.7/dist-packages/pymc3/model.py", line 146, in Var
var = ObservedRV(name=name, data=data, distribution=dist, model=self)
File "/usr/local/lib/python2.7/dist-packages/pymc3/model.py", line 348, in __init__
args = [t.constant(args[0], name=name)]
File "/usr/local/lib/python2.7/dist-packages/theano/tensor/basic.py", line 416, in constant
dtype=dtype)
File "/usr/local/lib/python2.7/dist-packages/theano/tensor/basic.py", line 411, in constant_or_value
raise TypeError("Could not convert %s to TensorType" % x, type(x))
TypeError: ('Could not convert <CudaNdarrayType(float32, matrix)> to TensorType', <class 'theano.sandbox.cuda.var.CudaNdarraySharedVariable'>)