我试图用切换点的确定性函数来重现煤矿开采示例,而不是使用theano的开关功能。代码:
%matplotlib inline
import matplotlib.pyplot as plt
import pymc3
import numpy as np
import theano.tensor as t
import theano
data = np.hstack((np.random.poisson(15,1000),np.random.poisson(2,100)))
plt.plot(data)
@theano.compile.ops.as_op(itypes=[t.lscalar, t.dscalar,t.dscalar],otypes=[t.dvector])
def rate1(sw,mu1,mu2):
n = len(data)
out = np.empty(n)
out[:sw] = mu1
out[sw:] = mu2
return out
with pymc3.Model() as dis:
switchpoint = pymc3.DiscreteUniform('switchpoint',lower=0, upper=len(data)-1)
mu1 = pymc3.Exponential('mu1', lam=1.)
mu2 = pymc3.Exponential('mu2',lam=1.)
disasters=pymc3.Poisson('disasters', mu=rate1, observed = data)
但是这段代码出现了错误:
----------------------------------------------- ---------------------------- KeyError Traceback(最近一次调用 最后)c:\ program files \ git \ theano \ theano \ tensor \ type.py in dtype_specs(个体经营) 266' complex64' :(复杂,' theano_complex64',' NPY_COMPLEX64') - > 267} [self.dtype] 268除了KeyError:
KeyError:' object'
在处理上述异常期间,发生了另一个异常:
TypeError Traceback(最近一次调用 最后)c:\ program files \ git \ theano \ theano \ tensor \ basic.py in constant_or_value(x,rtype,name,ndim,dtype) 407 rval = rtype( - > 408 TensorType(dtype = x_.dtype,broadcastable = bcastable), 409 x_.copy(),
init 中的c:\ program files \ git \ theano \ theano \ tensor \ type.py(self, dtype,broadcastable,name,sparse_grad) 49 self.broadcastable = tuple(bool(b)for b in broadcastable) ---> 50 self.dtype_specs()#错误检查在那里完成 51 self.name = name
dtype_specs中的c:\ program files \ git \ theano \ theano \ tensor \ type.py(self) 269引发TypeError("%s不支持的dtype:%s" - > 270%(自我。类。名称,self.dtype)) 271
TypeError:TensorType:object
不支持的dtype在处理上述异常期间,发生了另一个异常:
TypeError Traceback(最近一次调用 最后)c:\ program files \ git \ theano \ theano \ tensor \ basic.py in as_tensor_variable(x,name,ndim) 201尝试: - > 202返回常量(x,name = name,ndim = ndim) 203除了TypeError:
c:\ program files \ git \ theano \ theano \ tensor \ basic.py in constant(x, name,ndim,dtype) 421 ret = constant_or_value(x,rtype = TensorConstant,name = name,ndim = ndim, - > 422 dtype = dtype) 423
c:\ program files \ git \ theano \ theano \ tensor \ basic.py in constant_or_value(x,rtype,name,ndim,dtype) 416除了例外: - > 417引发TypeError("无法将%s转换为TensorType"%x,类型(x)) 418
TypeError :('无法将FromFunctionOp {rate1}转换为TensorType', )
在处理上述异常期间,发生了另一个异常:
AsTensorError Traceback(最近一次调用 最后)in() 14 mu2 = pymc3.Exponential(' mu2',lam = 1。) 15#rate1 = pymc3.switch(switchpoint> = np.arange(len(data)),mu1,mu2) ---> 16次灾难= pymc3.Poisson('灾难',mu = rate1,观察=数据)
C:\用户\用户\ Anaconda3 \ lib中\站点包\ pymc3 \分布\ distribution.py 在新(cls,name,* args,** kwargs) 19如果isinstance(name,str): 20 data = kwargs.pop('观察',无) ---> 21 dist = cls.dist(* args,** kwargs) 22返回model.Var(名称,dist,数据) 23 elif名称为None:
C:\用户\用户\ Anaconda3 \ lib中\站点包\ pymc3 \分布\ distribution.py 在dist(cls,* args,** kwargs) 32 def dist(cls,* args,** kwargs): 33 dist = object。 new (cls) ---> 34 dist。 init (* args,** kwargs) 35返回dist 36
C:\用户\用户\ Anaconda3 \ lib中\站点包\ pymc3 \分布\ discrete.py 在 init (self,mu,* args,** kwargs) 185 super(Poisson,self)。 init (* args,** kwargs) 186 self.mu = mu - > 187 self.mode = floor(mu).astype(' int32') 188 189 def random(self,point = None,size = None,repeat = None):
调用中的c:\ program files \ git \ theano \ theano \ gof \ op.py(self, *输入,** kwargs) 598""" 599 return_list = kwargs.pop(' return_list',False) - > 600 node = self.make_node(* inputs,** kwargs) 601 602如果config.compute_test_value!=' off':
c:\ program files \ git \ theano \ theano \ tensor \ elemwise.py in make_node(self,* inputs) 540使用DimShuffle。 541""" - > 542个输入=列表(map(as_tensor_variable,inputs)) 543 shadow = self.scalar_op.make_node( 544 * [get_scalar_type(dtype = i.type.dtype).make_variable()
c:\ program files \ git \ theano \ theano \ tensor \ basic.py in as_tensor_variable(x,name,ndim) 206除了异常: 207 str_x = repr(x) - > 208引发AsTensorError("无法将%s转换为TensorType"%str_x,类型(x)) 209 210#这个名称不同,因为_as_tensor_variable是
AsTensorError :('无法将FromFunctionOp {rate1}转换为TensorType', )
我怎么处理这个?
第二件事 - 当我使用像这样的pymc3.switch函数时:
with pymc3.Model() as dis:
switchpoint = pymc3.DiscreteUniform('switchpoint',lower=0, upper=len(data)-1)
mu1 = pymc3.Exponential('mu1', lam=1.)
mu2 = pymc3.Exponential('mu2',lam=1.)
rate1 = pymc3.switch(switchpoint >= np.arange(len(data)), mu1,mu2)
disasters=pymc3.Poisson('disasters', mu=rate1, observed = data)
然后尝试示例:
with dis:
step1 = pymc3.NUTS([mu1, mu2])
step2 = pymc3.Metropolis([switchpoint])
trace = pymc3.sample(10000, step = [step1,step2])
我收到错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
c:\program files\git\theano\theano\compile\function_module.py in __call__(self, *args, **kwargs)
858 try:
--> 859 outputs = self.fn()
860 except Exception:
TypeError: expected type_num 9 (NPY_INT64) got 7
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
<ipython-input-4-3247d908f897> in <module>()
2 step1 = pymc3.NUTS([mu1, mu2])
3 step2 = pymc3.Metropolis([switchpoint])
----> 4 trace = pymc3.sample(10000, step = [step1,step2])
C:\Users\User\Anaconda3\lib\site-packages\pymc3\sampling.py in sample(draws, step, start, trace, chain, njobs, tune, progressbar, model, random_seed)
153 sample_args = [draws, step, start, trace, chain,
154 tune, progressbar, model, random_seed]
--> 155 return sample_func(*sample_args)
156
157
C:\Users\User\Anaconda3\lib\site-packages\pymc3\sampling.py in _sample(draws, step, start, trace, chain, tune, progressbar, model, random_seed)
162 progress = progress_bar(draws)
163 try:
--> 164 for i, strace in enumerate(sampling):
165 if progressbar:
166 progress.update(i)
C:\Users\User\Anaconda3\lib\site-packages\pymc3\sampling.py in _iter_sample(draws, step, start, trace, chain, tune, model, random_seed)
244 if i == tune:
245 step = stop_tuning(step)
--> 246 point = step.step(point)
247 strace.record(point)
248 yield strace
C:\Users\User\Anaconda3\lib\site-packages\pymc3\step_methods\compound.py in step(self, point)
11 def step(self, point):
12 for method in self.methods:
---> 13 point = method.step(point)
14 return point
C:\Users\User\Anaconda3\lib\site-packages\pymc3\step_methods\arraystep.py in step(self, point)
116 bij = DictToArrayBijection(self.ordering, point)
117
--> 118 apoint = self.astep(bij.map(point))
119 return bij.rmap(apoint)
120
C:\Users\User\Anaconda3\lib\site-packages\pymc3\step_methods\metropolis.py in astep(self, q0)
123
124
--> 125 q_new = metrop_select(self.delta_logp(q,q0), q, q0)
126
127 if q_new is q:
c:\program files\git\theano\theano\compile\function_module.py in __call__(self, *args, **kwargs)
869 node=self.fn.nodes[self.fn.position_of_error],
870 thunk=thunk,
--> 871 storage_map=getattr(self.fn, 'storage_map', None))
872 else:
873 # old-style linkers raise their own exceptions
c:\program files\git\theano\theano\gof\link.py in raise_with_op(node, thunk, exc_info, storage_map)
312 # extra long error message in that case.
313 pass
--> 314 reraise(exc_type, exc_value, exc_trace)
315
316
C:\Users\User\Anaconda3\lib\site-packages\six.py in reraise(tp, value, tb)
656 value = tp()
657 if value.__traceback__ is not tb:
--> 658 raise value.with_traceback(tb)
659 raise value
660
c:\program files\git\theano\theano\compile\function_module.py in __call__(self, *args, **kwargs)
857 t0_fn = time.time()
858 try:
--> 859 outputs = self.fn()
860 except Exception:
861 if hasattr(self.fn, 'position_of_error'):
TypeError: expected type_num 9 (NPY_INT64) got 7
Apply node that caused the error: Elemwise{Composite{Switch(GE(i0, i1), i2, i3)}}(InplaceDimShuffle{x}.0, TensorConstant{[ 0 1..1098 1099]}, InplaceDimShuffle{x}.0, InplaceDimShuffle{x}.0)
Toposort index: 11
Inputs types: [TensorType(int64, (True,)), TensorType(int32, vector), TensorType(float64, (True,)), TensorType(float64, (True,))]
Inputs shapes: [(1,), (1100,), (1,), (1,)]
Inputs strides: [(4,), (4,), (8,), (8,)]
Inputs values: [array([549]), 'not shown', array([ 1.07762995]), array([ 1.01502801])]
Outputs clients: [[Elemwise{eq,no_inplace}(Elemwise{Composite{Switch(GE(i0, i1), i2, i3)}}.0, TensorConstant{(1,) of 0}), Elemwise{Composite{Switch(GE(i0, i1), ((Switch(i2, i3, (i4 * log(i0))) - i5) - i0), i3)}}[(0, 0)](Elemwise{Composite{Switch(GE(i0, i1), i2, i3)}}.0, TensorConstant{(1,) of 0}, InplaceDimShuffle{x}.0, TensorConstant{(1,) of -inf}, TensorConstant{[ 13. 13... 0. 1.]}, TensorConstant{[ 22.55216... ]})]]
HINT: Re-running with most Theano optimization disabled could give you a back-trace of when this node was created. This can be done with by setting the Theano flag 'optimizer=fast_compile'. If that does not work, Theano optimizations can be disabled with 'optimizer=None'.
HINT: Use the Theano flag 'exception_verbosity=high' for a debugprint and storage map footprint of this apply node.
作为简单的分析师,我是否应该学习所有关于theano的东西,以便能够处理我的统计问题?具有渐变功能的新mcmc采样器是否应该促使我从pymc2切换到pymc3?
答案 0 :(得分:1)
对于您的第一个问题,您似乎正在尝试将theano函数作为变量传递。您需要使用其他变量作为参数调用该函数,然后返回一个theano变量。尝试将您的行更改为
disasters=pymc3.Poisson('disasters', mu=rate1(switchpoint, mu1, mu2), observed = data)
我无法在你的第二部分重现这个错误;抽样对我来说效果很好。