简体   繁体   English

pymc3多维变量方程

[英]Pymc3 multi-dimensional variable equation

I am trying to use multi-dimensional variables in pymc3 to describe a system of 4 variables that share a common factor. 我试图在pymc3中使用多维变量来描述一个共享一个共同因素的4个变量的系统。 Perhaps I am thinking about this wrong, but when I setup the model I continually get errors from pymc3 about input dimension mismatch. 也许我在想这个错误,但是当我建立模型时,我不断从pymc3中得到关于输入尺寸不匹配的错误。 I have tried several variations to no avail. 我尝试了几种变体但无济于事。 How can I use pymc3's shape argument to describe a system of 4 observed variables with a structure that shares a common observed variable? 如何使用pymc3的shape参数来描述一个由4个观察变量组成的系统,该变量的结构共享一个共同的观察变量?

import pymc3
import numpy

# Simulate some data
shared_obs = numpy.random.normal(0., 0.25, 1000)
indiv_obs = numpy.random.normal(0.1, 0.5, 1000)

# Build a model
with pymc3.Model():
    shared_mu = pymc3.Uniform('shared_mu', -0.5, 0.5)
    shared_sigma = pymc3.Lognormal('shared_sigma', 0.1, 1.)
    shared = pymc3.Normal('shared', mu=shared_mu, sd=shared_sigma, observed=shared_obs)
    a = pymc3.Uniform('a', -1., 1., shape=4)
    B = pymc3.Uniform('B', -1., 1., shape=4)
    sigma = pymc3.Lognormal('sigma', 0.1, 1., shape=4)
    indiv = pymc3.Normal('indiv', mu=a + B*shared, sd = sigma, observed=indiv_obs, shape=4)

ValueError Traceback (most recent call last) in () 6 B = pymc3.Uniform('B', -1., 1., shape=4) 7 sigma = pymc3.Lognormal('sigma', 0.1, 1., shape=4) ----> 8 indiv = pymc3.Normal('indiv', mu=a + B*shared, sd = sigma, observed=indiv_obs, shape=4) 在()中的ValueError Traceback(最近一次调用为最后一次)6 B = pymc3.Uniform('B',-1。,1.,shape = 4)7 sigma = pymc3.Lognormal('sigma',0.1,1.,shape = 4)----> 8个indiv = pymc3.Normal('indiv',mu = a + B * shared,sd = sigma,观察= indiv_obs,shape = 4)

C:\\Program Files\\Anaconda3\\lib\\site-packages\\theano\\tensor\\var.py in mul (self, other) 160 # and the return value in that case 161 try: --> 162 return theano.tensor.mul(self, other) 163 except (NotImplementedError, AsTensorError): 164 return NotImplemented C:\\ Program Files \\ Anaconda3 \\ lib \\ site-packages \\ theano \\ tensor \\ var.py in mul (self,other)160#在这种情况下的返回值161尝试:-> 162 return theano.tensor.mul (自身)163(NotImplementedError,AsTensorError)除外:164 return NotImplemented

C:\\Program Files\\Anaconda3\\lib\\site-packages\\theano\\gof\\op.py in call (self, *inputs, **kwargs) 666 thunk.outputs = [storage_map[v] for v in node.outputs] 667 --> 668 required = thunk() 669 assert not required # We provided all inputs 670 C:\\ Program Files \\ Anaconda3 \\ lib \\ site-packages \\ theano \\ gof \\ op.py处于通话中 (自身,*输入,**扭曲)666 thunk.outputs = [node.outputs中v的存储映射[v]] 667-> 668必需= thunk()669不需要断言#我们提供了所有输入670

C:\\Program Files\\Anaconda3\\lib\\site-packages\\theano\\gof\\op.py in rval() 881 882 def rval(): --> 883 fill_storage() 884 for o in node.outputs: 885 compute_map[o][0] = True C:\\ Program Files \\ Anaconda3 \\ lib \\ site-packages \\ theano \\ gof \\ op.py in rval()881882 def rval():-> 883 fill_storage()884 for node中的o。输出:885 compute_map [ o] [0] =真

C:\\Program Files\\Anaconda3\\lib\\site-packages\\theano\\gof\\cc.py in call (self) 1705 print(self.error_storage, file=sys.stderr) 1706 raise -> 1707 reraise(exc_type, exc_value, exc_trace) 1708 1709 C:\\ Program Files \\ Anaconda3 \\ lib \\ site-packages \\ theano \\ gof \\ cc.py在调用中 (self)1705 print(self.error_storage,file = sys.stderr)1706提高-> 1707 reraise(exc_type,exc_value, (ex_trace)1708 1709

C:\\Program Files\\Anaconda3\\lib\\site-packages\\six.py in reraise(tp, value, tb) 684 if value. C:\\ Program Files \\ Anaconda3 \\ lib \\ site-packages \\ six.py in reiseise(tp,value,tb)684 if value。 traceback is not tb: 685 raise value.with_traceback(tb) --> 686 raise value 687 688 else: traceback不是tb:685提高值with_traceback(tb)-> 686提高值687688否则:

ValueError: Input dimension mis-match. ValueError:输入尺寸不匹配。 (input[0].shape[0] = 4, input[1].shape[0] = 1000) (输入[0] .shape [0] = 4,输入[1] .shape [0] = 1000)

* is element-wise multiplication. *是逐元素乘法。 If you want the outer product you can use 如果您需要外部产品,则可以使用

B[None, :] * shared[:, None]

I got something similar to work by using theano.shared.stack() . 通过使用theano.shared.stack()我得到了一些类似于工作的东西。 Basically, I stacked a bunch of copies of my data (making sure to set the axis right) and then multiplied by the parameters I needed. 基本上,我堆叠了一堆数据(确保将轴设置为正确),然后乘以所需的参数。 My command was 我的命令是

mu_alphas = pm.Normal('mu_alphas', 0, 5, shape=3)
mu_betas = pm.Normal('mu_betas', 0, 5, shape=3)
mu = pm.Deterministic('mu', mu_alphas + mu_betas * tt.stack([df.x, df.x, df.x], axis=1))

where df.x is (1000,) . df.x is (1000,) Hope something similar can work for you. 希望类似的东西可以为您服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM