Pystan,运行时错误 - 初始化失败

Pystan, Runtime error - Initialization failed

我正在尝试使用 Pystan 开发贝叶斯模型。我能够成功编译模型。但是当我对数据进行采样时,我遇到了 运行 时间错误。参考以下代码:

my_code = '''
data {
  int N;
  int K1; 
  int K2; 
  real max_intercept;
  matrix[N, K1] X1;
  matrix[N, K2] X2;
  vector[N] y; 
}
parameters {
  vector<lower=0>[K1] beta1;  
  vector[K2] beta2; 
  real<lower=0, upper=max_intercept> alpha; 
  real<lower=0> noise_var; 
}
model {
  beta1 ~ normal(0, 1); 
  beta2 ~ normal(0, 1); 
  noise_var ~ inv_gamma(0.05, 0.05 * 0.01);
  y ~ normal(X1*beta1 + X2*beta2 + alpha, sqrt(noise_var));
}
'''

fit1 = sm1.sampling(data=input_data, iter=2000, chains=4, init=0.5,n_jobs=-1) #Getting an error here

我已经检查了所有数据点(没有丢失数据或没有相同编号的列)及其数据类型(都是 float 64)。我还使用 MinMaxScaler

缩放了数据
input_data = {
    'N': len(data_scaled), #836
    'K1': len(pos_var), #17 
    'K2': len(pos_neg_var),#29 
    'X1': X1, #(836,17)
    'X2': X2, #(836,17)
    'y': data['orders'].values,
    'max_intercept': min(data['orders']) #0
}

以下是我遇到的错误。

RemoteTraceback                           Traceback (most recent call last)
RemoteTraceback: 
"""
Traceback (most recent call last):
  File "C:\Users\abc\.conda\envs\stan_env\lib\multiprocessing\pool.py", line 121, in worker
    result = (True, func(*args, **kwds))
  File "C:\Users\abc\.conda\envs\stan_env\lib\multiprocessing\pool.py", line 44, in mapstar
    return list(map(*args))
  File "stanfit4anon_model_a396b59aabedfaa132f3a814776a219f_7619586994410633893.pyx", line 371, in stanfit4anon_model_a396b59aabedfaa132f3a814776a219f_7619586994410633893._call_sampler_star
  File "stanfit4anon_model_a396b59aabedfaa132f3a814776a219f_7619586994410633893.pyx", line 404, in stanfit4anon_model_a396b59aabedfaa132f3a814776a219f_7619586994410633893._call_sampler
RuntimeError: Initialization failed.
"""

The above exception was the direct cause of the following exception:

RuntimeError                              Traceback (most recent call last)
<timed exec> in <module>

~\.conda\envs\stan_env\lib\site-packages\pystan\model.py in sampling(self, data, pars, chains, iter, warmup, thin, seed, init, sample_file, diagnostic_file, verbose, algorithm, control, n_jobs, **kwargs)
    776         call_sampler_args = izip(itertools.repeat(data), args_list, itertools.repeat(pars))
    777         call_sampler_star = self.module._call_sampler_star
--> 778         ret_and_samples = _map_parallel(call_sampler_star, call_sampler_args, n_jobs)
    779         samples = [smpl for _, smpl in ret_and_samples]
    780 

~\.conda\envs\stan_env\lib\site-packages\pystan\model.py in _map_parallel(function, args, n_jobs)
     83         try:
     84             pool = multiprocessing.Pool(processes=n_jobs)
---> 85             map_result = pool.map(function, args)
     86         finally:
     87             pool.close()

~\.conda\envs\stan_env\lib\multiprocessing\pool.py in map(self, func, iterable, chunksize)
    266         in a list that is returned.
    267         '''
--> 268         return self._map_async(func, iterable, mapstar, chunksize).get()
    269 
    270     def starmap(self, func, iterable, chunksize=None):

~\.conda\envs\stan_env\lib\multiprocessing\pool.py in get(self, timeout)
    655             return self._value
    656         else:
--> 657             raise self._value
    658 
    659     def _set(self, i, obj):

RuntimeError: Initialization failed.

我对 Pystan 比较陌生。我很感激在这里得到的任何指导。

我解决了这个问题!当数据不满足模型中定义的约束时,通常会出现运行时错误。

  1. 例如,当约束为模型中定义的 X>0 时,X 值具有一些 -ve 个数字。
  2. 也是最常见的错误,需要确保 Y 值没有关闭。在我的数据中,很少有 Y 值为 0,这些值通过了缺失值和 pos 值检查。在用 Y 的平均值估算值后,问题就解决了。

学习愉快!