我越来越熟悉 PyStan。我已经运行了几个模型,包括贝叶斯线性回归模型,没有问题。但是,当我尝试运行以下代码时,会出现 seg fault 错误:
Segmentation fault (core dumped)
有趣的是,这只发生在老化迭代完成之后。我的模型代码在这里。我目前正在编造一些数据并试图推断参数beta_tru_1
和alpha
. 型号代码来自Stan User's Guide。
import pystan
import numpy as np
from scipy.stats import norm
# the params
beta_tru_1 = 3.7
alpha = 2.3
# make some data
n = 1000
np.random.seed(1)
x1 = norm(0, 1).rvs(n)
z = alpha + x1 * beta_tru_1
y = [1 if i > 0.7 else 0 for i in norm.cdf(z)]
# train test split
y_train, y_test = y[:750], y[750:]
x_train, x_test = x1[:750], x1[750:]
# stan code
probit_code = """
data {
int<lower=0> n; // number of data vectors
real x[n]; // data matrix
int<lower=0,upper=1> y[n]; // response vector
}
parameters {
real beta; // regression coefs
real alpha;
}
model {
for (i in 1:n)
y[i] ~ bernoulli(Phi(alpha + beta * x[i]));
}
"""
# compile the model
probit_model = pystan.StanModel(model_code=probit_code)
# the data
probit_dat = {
"n": len(y_train),
"y": y_train,
"x": x_train
}
# fit the model (small number of iterations for debug)
# this is where the error is
probit_fit = probit_model.sampling(data=probit_dat, iter=500, warmup=500, chains=4, init="0")
我在 Linux Pop OS 20.10 上使用 PyStan v. 2.19.1.1 和 python 3.7.6。我已经在多台机器上运行了这段代码,包括一个没有运气的 Ubuntu 容器。任何帮助表示赞赏。