Python 3.x 使用PyMC3时无错误:Theano.gof.fg.MissingInputError
我正在生成一些(有噪声的)数据点(y),其中包含一些表示直线方程的已知参数(m,c)。使用基于抽样的贝叶斯方法,我现在想从数据中知道参数(m,c)的真实值。因此,我使用DE Metropolis(PyMC3)来估计真实参数 我得到的是无错误Python 3.x 使用PyMC3时无错误:Theano.gof.fg.MissingInputError,python-3.x,theano,pymc3,Python 3.x,Theano,Pymc3,我正在生成一些(有噪声的)数据点(y),其中包含一些表示直线方程的已知参数(m,c)。使用基于抽样的贝叶斯方法,我现在想从数据中知道参数(m,c)的真实值。因此,我使用DE Metropolis(PyMC3)来估计真实参数 我得到的是无错误theano.gof.fg.missingingputeror:未提供用于计算sigmoid(c_区间)的图形输入0(索引从0开始),也未给出值。 Theano版本:1.0.4 PyMC3版本:3.9.1 import matplotlib.pyplot as
theano.gof.fg.missingingputeror:未提供用于计算sigmoid(c_区间)的图形输入0(索引从0开始),也未给出值。
Theano版本:1.0.4
PyMC3版本:3.9.1
import matplotlib.pyplot as plt
import numpy as np
import arviz as az
import pymc3
import theano.tensor as tt
from theano.compile.ops import as_op
plt.style.use("ggplot")
# define a theano Op for our likelihood function
class LogLike(tt.Op):
itypes = [tt.dvector] # expects a vector of parameter values when called
otypes = [tt.dscalar] # outputs a single scalar value (the log likelihood)
def __init__(self, loglike, data, x, sigma):
# add inputs as class attributes
self.likelihood = loglike
self.data = data
self.x = x
self.sigma = sigma
def perform(self, node, inputs, outputs):
# the method that is used when calling the Op
theta, = inputs # this will contain my variables
# call the log-likelihood function
logl = self.likelihood(theta, self.x, self.data, self.sigma)
outputs[0][0] = np.array(logl) # output the log-likelihood
def my_model(theta, x):
y = theta[0]*x + theta[1]
return y
def my_loglike(theta, x, data, sigma):
model = my_model(theta, x)
ll = -(0.5/sigma**2)*np.sum((data - model)**2)
return ll
# set up our data
N = 10 # number of data points
sigma = 1. # standard deviation of noise
x = np.linspace(0., 9., N)
mtrue = 0.4 # true gradient
ctrue = 3. # true y-intercept
truemodel = my_model([mtrue, ctrue], x)
# make data
np.random.seed(716742) # set random seed, so the data is reproducible each time
data = sigma*np.random.randn(N) + truemodel
print(data)
ndraws = 3000 # number of draws from the distribution
# create our Op
logl = LogLike(my_loglike, data, x, sigma)
# use PyMC3 to sampler from log-likelihood
with pymc3.Model():
# uniform priors on m and c
m = pymc3.Uniform('m', lower=-10., upper=10.)
c = pymc3.Uniform('c', lower=-10., upper=10.)
# convert m and c to a tensor vector
theta = tt.as_tensor_variable([m, c])
# use a DensityDist (use a lamdba function to "call" the Op)
pymc3.DensityDist('likelihood', lambda v: logl(v), observed={'v': theta})
step = pymc3.DEMetropolis()
trace = pymc3.sample(ndraws, step)
# plot the traces
axes = az.plot_trace(trace)
fig = axes.ravel()[0].figure
fig.savefig('./trace_plots.png')
在此处找到完整跟踪:
Population sampling (4 chains)
DEMetropolis: [c, m]
Attempting to parallelize chains to all cores. You can turn this off with `pm.sample(cores=1)`.
Population parallelization failed. Falling back to sequential stepping of chains.---------------------| 0.00% [0/4 00:00<00:00]
Sampling 4 chains for 0 tune and 4_000 draw iterations (0 + 16_000 draws total) took 5 seconds.███████| 100.00% [4000/4000 00:04<00:00]
Traceback (most recent call last):
File "test.py", line 75, in <module>
trace = pymc3.sample(ndraws, step)
File "/home/csl_user/.local/lib/python3.7/site-packages/pymc3/sampling.py", line 599, in sample
idata = arviz.from_pymc3(trace, **ikwargs)
File "/home/csl_user/.local/lib/python3.7/site-packages/arviz/data/io_pymc3.py", line 531, in from_pymc3
save_warmup=save_warmup,
File "/home/csl_user/.local/lib/python3.7/site-packages/arviz/data/io_pymc3.py", line 159, in __init__
self.observations, self.multi_observations = self.find_observations()
File "/home/csl_user/.local/lib/python3.7/site-packages/arviz/data/io_pymc3.py", line 172, in find_observations
multi_observations[key] = val.eval() if hasattr(val, "eval") else val
File "/home/csl_user/.local/lib/python3.7/site-packages/theano/gof/graph.py", line 522, in eval
self._fn_cache[inputs] = theano.function(inputs, self)
File "/home/csl_user/.local/lib/python3.7/site-packages/theano/compile/function.py", line 317, in function
output_keys=output_keys)
File "/home/csl_user/.local/lib/python3.7/site-packages/theano/compile/pfunc.py", line 486, in pfunc
output_keys=output_keys)
File "/home/csl_user/.local/lib/python3.7/site-packages/theano/compile/function_module.py", line 1839, in orig_function
name=name)
File "/home/csl_user/.local/lib/python3.7/site-packages/theano/compile/function_module.py", line 1487, in __init__
accept_inplace)
File "/home/csl_user/.local/lib/python3.7/site-packages/theano/compile/function_module.py", line 181, in std_fgraph
update_mapping=update_mapping)
File "/home/csl_user/.local/lib/python3.7/site-packages/theano/gof/fg.py", line 175, in __init__
self.__import_r__(output, reason="init")
File "/home/csl_user/.local/lib/python3.7/site-packages/theano/gof/fg.py", line 346, in __import_r__
self.__import__(variable.owner, reason=reason)
File "/home/csl_user/.local/lib/python3.7/site-packages/theano/gof/fg.py", line 391, in __import__
raise MissingInputError(error_msg, variable=r)
theano.gof.fg.MissingInputError: Input 0 of the graph (indices start from 0), used to compute sigmoid(c_interval__), was not provided and not given a value. Use the Theano flag exception_verbosity='high', for more information on this error.
总体抽样(4链)
德米特罗利斯:[c,m]
正在尝试将链并行化到所有核心。您可以使用“pm.sample(cores=1)”关闭此选项。
人口并行化失败。返回到链的顺序步进。------------------0.00%[0/4 00:00我在下面的示例中遇到了相同的问题,即如何从此处发现的黑盒可能性中采样:
这似乎是一个版本问题。我在Manjaro Linux上,还使用python 3.8运行了theano 1.0.4和pymc3 3.9。我可以通过降级到python 3.7和pymc3 3.8来解决这个问题并使代码正常工作。这似乎是python 3.8的问题,因为简单地降级pymc3并不能解决我的问题。我远非pymc3方面的专家,所以我没有解决方案如何使用最新版本修复此问题,但目前降级使我的模拟运行
希望这有帮助
编辑:开发者似乎意识到了这一点,他们的github页面上有一个悬而未决的问题
我在以下示例中遇到了相同的问题,即如何从此处找到的黑盒可能性中采样:
这似乎是一个版本问题。我在Manjaro Linux上,还使用python 3.8运行了theano 1.0.4和pymc3 3.9。我可以通过降级到python 3.7和pymc3 3.8来解决这个问题并使代码正常工作。这似乎是python 3.8的问题,因为简单地降级pymc3并不能解决我的问题。我远非pymc3方面的专家,所以我没有解决方案如何使用最新版本修复此问题,但目前降级使我的模拟运行
希望这有帮助
编辑:开发者似乎意识到了这一点,他们的github页面上有一个悬而未决的问题
谢谢@SarahS,我正在使用Python3.7。我将降级PyMC3版本并检查。谢谢@SarahS,我正在使用Python3.7。我将降级PyMC3版本并检查。