gpt4 book ai didi

python-3.x - 错误的初始能量 : inf. pymc3 中的基本线性回归模型可能错误指定了模型

转载 作者:行者123 更新时间:2023-12-03 17:38:13 25 4
gpt4 key购买 nike

我正在尝试在 pymc3 中构建贝叶斯推理模型,但出现以下错误:

data = [[24, 38.7], [25, 38.6], [26, 38.9], [27, 41.4], [28, 39.7], [29, 41.1], [30, 38.7], [31, 37.6],
[32, 36.3], [33, 36.9], [34, 35.7], [35, 33.8], [36, 33.2], [37, 30.1], [38, 27.8], [39, 22.8],
[40, 21.4], [41, 15.4], [42, 11.2], [43, 9.2], [44, 5.4], [45, 3.0], [46, 1.6]]


data = np.array(data)

x = data[:, 0]
y = data[:, 1]
plt.scatter(x, y, color="red")

with pm.Model() as change_point_model:

switchpoint = pm.DiscreteUniform('switchpoint', lower=x.min(), upper=x.max())

beta0 = pm.Normal('beta0', mu=40, sd=10)
beta1 = pm.Normal('beta1', mu=90, sd=10)

gamma0 = pm.Normal('gamma0', mu=0, sd=5)
gamma1 = pm.Normal('gamma1', mu=0, sd=5)

epsilon = pm.Normal('epsilon', mu=0, sd=1)

intercept = pm.math.switch(switchpoint <= x, beta0, gamma0)
x_coeff = pm.math.switch(switchpoint <= x, beta1, gamma1)

y_pred = pm.Normal('y_pred', mu=intercept + x_coeff * x, sd=epsilon, observed=y)

step1 = pm.NUTS([beta0, beta1, gamma0, gamma1])
step2 = pm.Metropolis([switchpoint])

# In this example we are deliberativelly choosing the metropolis sampler
trace = pm.sample(2000, step=[step1, step2], progressbar=True)

pm.traceplot(trace[100:])

我得到的错误如下:

ValueError: Bad initial energy: inf. The model might be misspecified.

因此,在做了一些阅读之后,我发现 model.logp(model.test_point) 正在返回一个 -inf。因此,我该如何解决这个错误。非常感谢任何帮助!!

最佳答案

您正在使用正态对正态分布的标准差建模。测试点为 0.0,发生概率为 0。

如果您将 epsilon 更改为 Gamma('epsilon', alpha=2.0, beta=0.5) 或类似的,您应该没问题。

关于python-3.x - 错误的初始能量 : inf. pymc3 中的基本线性回归模型可能错误指定了模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49867919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com