Python pymc3使用LKJCholeskyCov的起始值给出了错误的初始能量错误

Python pymc3使用LKJCholeskyCov的起始值给出了错误的初始能量错误,python,bayesian,pymc3,Python,Bayesian,Pymc3,当我试图为LKJCholeskyCov的标准偏差给出初始起始值时,我在pymc3中得到了错误的初始能量 下面,第一段代码运行良好。但是第二个会给你这个错误。我认为我在设置起始值数组时出错了 工作示例: pip install arviz==0.11 pip install pymc3==3.11.1 # Not working example: import pymc3 as pm n_samples = 20 n_tune_samples = 10 mu = np.zeros(3) tr

当我试图为
LKJCholeskyCov
的标准偏差给出初始起始值时,我在
pymc3
中得到了
错误的初始能量

下面,第一段代码运行良好。但是第二个会给你这个错误。我认为我在设置起始值数组时出错了

工作示例:

pip install arviz==0.11
pip install pymc3==3.11.1

# Not working example:
import pymc3 as pm
n_samples = 20
n_tune_samples = 10

mu = np.zeros(3)
true_cov = np.array([[1.0, 0.5, 0.1],
                     [0.5, 2.0, 0.2],
                     [0.1, 0.2, 1.0]])
data = np.random.multivariate_normal(mu, true_cov, 10)
print(data.shape)

with pm.Model() as model2:
    sd_dist = pm.Exponential.dist(1.0, shape=3)
    print(sd_dist.shape)
    
    chol, corr, stds = pm.LKJCholeskyCov('chol_cov', n=3, eta=2,
        sd_dist=sd_dist, compute_corr=True)
    
    vals = pm.MvNormal('vals', mu=mu, chol=chol, observed=data)

with model2:
    chol_init = np.diag([0.56, 0.61, 0.74])[np.tril_indices(3)]
       
    trace2 = pm.sample(draws=n_samples, tune=n_tune_samples,
                      start={'chol_cov':chol_init})
不起作用示例:

pip install arviz==0.11
pip install pymc3==3.11.1

# Not working example:
import pymc3 as pm
n_samples = 20
n_tune_samples = 10

mu = np.zeros(3)
true_cov = np.array([[1.0, 0.5, 0.1],
                     [0.5, 2.0, 0.2],
                     [0.1, 0.2, 1.0]])
data = np.random.multivariate_normal(mu, true_cov, 10)
print(data.shape)

with pm.Model() as model2:
    sd_dist = pm.Exponential.dist(1.0, shape=3)
    print(sd_dist.shape)
    
    chol, corr, stds = pm.LKJCholeskyCov('chol_cov', n=3, eta=2,
        sd_dist=sd_dist, compute_corr=True)
    
    vals = pm.MvNormal('vals', mu=mu, chol=chol, observed=data)

with model2:
    chol_init = np.diag([0.56, 0.61, 0.74])[np.tril_indices(3)]
       
    trace2 = pm.sample(draws=n_samples, tune=n_tune_samples,
                      start={'chol_cov':chol_init})
错误消息:


Only 20 samples in chain.
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
Multiprocess sampling (4 chains in 4 jobs)
NUTS: [chol_cov]

 0.00% [0/120 00:00<00:00 Sampling 4 chains, 0 divergences]
Bad initial energy, check any log probabilities that are inf or -inf, nan or very small:
Series([], )
---------------------------------------------------------------------------
RemoteTraceback                           Traceback (most recent call last)
RemoteTraceback: 
"""
Traceback (most recent call last):
  File "C:\Users\ilyas\Anaconda3\envs\pymc\lib\site-packages\pymc3\parallel_sampling.py", line 191, in _start_loop
    point, stats = self._compute_point()
  File "C:\Users\ilyas\Anaconda3\envs\pymc\lib\site-packages\pymc3\parallel_sampling.py", line 216, in _compute_point
    point, stats = self._step_method.step(self._point)
  File "C:\Users\ilyas\Anaconda3\envs\pymc\lib\site-packages\pymc3\step_methods\arraystep.py", line 276, in step
    apoint, stats = self.astep(array)
  File "C:\Users\ilyas\Anaconda3\envs\pymc\lib\site-packages\pymc3\step_methods\hmc\base_hmc.py", line 159, in astep
    raise SamplingError("Bad initial energy")
pymc3.exceptions.SamplingError: Bad initial energy
"""

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

SamplingError                             Traceback (most recent call last)
SamplingError: Bad initial energy

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

ParallelSamplingError                     Traceback (most recent call last)
<ipython-input-412-6bc0303342ea> in <module>
     21     chol_init = np.diag([0.56, 0.61, 0.74])[np.tril_indices(3)]
     22 
---> 23     trace2 = pm.sample(draws=n_samples, tune=n_tune_samples,
     24                       start={'chol_cov':chol_init})

~\Anaconda3\envs\pymc\lib\site-packages\pymc3\sampling.py in sample(draws, step, init, n_init, start, trace, chain_idx, chains, cores, tune, progressbar, model, random_seed, discard_tuned_samples, compute_convergence_checks, callback, jitter_max_retries, return_inferencedata, idata_kwargs, mp_ctx, pickle_backend, **kwargs)
    556         _print_step_hierarchy(step)
    557         try:
--> 558             trace = _mp_sample(**sample_args, **parallel_args)
    559         except pickle.PickleError:
    560             _log.warning("Could not pickle model, sampling singlethreaded.")

~\Anaconda3\envs\pymc\lib\site-packages\pymc3\sampling.py in _mp_sample(draws, tune, step, chains, cores, chain, random_seed, start, progressbar, trace, model, callback, discard_tuned_samples, mp_ctx, pickle_backend, **kwargs)
   1474         try:
   1475             with sampler:
-> 1476                 for draw in sampler:
   1477                     trace = traces[draw.chain - chain]
   1478                     if trace.supports_sampler_stats and draw.stats is not None:

~\Anaconda3\envs\pymc\lib\site-packages\pymc3\parallel_sampling.py in __iter__(self)
    477 
    478         while self._active:
--> 479             draw = ProcessAdapter.recv_draw(self._active)
    480             proc, is_last, draw, tuning, stats, warns = draw
    481             self._total_draws += 1

~\Anaconda3\envs\pymc\lib\site-packages\pymc3\parallel_sampling.py in recv_draw(processes, timeout)
    357             else:
    358                 error = RuntimeError("Chain %s failed." % proc.chain)
--> 359             raise error from old_error
    360         elif msg[0] == "writing_done":
    361             proc._readable = True

ParallelSamplingError: Bad initial energy


链中只有20个样本。
自动分配螺母采样器。。。
使用抖动+自适应诊断初始化螺母。。。
多进程采样(4个作业中的4个链)
坚果:[chol_cov]
0.00%[0/120 00:00 23 trace2=pm.sample(draws=n_samples,tune=n_tune_samples,
24开始={'chol_cov':chol_init})
样本中的~\Anaconda3\envs\pymc\lib\site packages\pymc3\sampling.py(draws、step、init、n_init、start、trace、chain_idx、chains、core、tune、progressbar、model、random_seed、discard_tuned_samples、compute_convergence_check_check、callback、jitter_max_max_retries、retriends_推断数据、idataèèèè232
556打印步骤层次结构(步骤)
557尝试:
-->558跟踪=\u mp\u样本(**样本参数,**并行参数)
559除pickle.pickle错误外:
560_log.警告(“无法pickle模型,采样单线程。”)
~\Anaconda3\envs\pymc\lib\site packages\pymc3\sampling.py in\u mp\u sample(绘制、调整、步进、链、核心、链、随机种子、开始、进度条、跟踪、模型、回调、丢弃调整的样本、mp\u ctx、pickle\u后端、**kwargs)
1474尝试:
1475带取样器:
->1476对于吸入式取样器:
1477轨迹=轨迹[draw.chain-chain]
1478如果trace.supports_sampler_stats和draw.stats不是None:
~\Anaconda3\envs\pymc\lib\site packages\pymc3\parallel\u sampling.py in\uuuuuu iter\uuuu(self)
477
478自激活时:
-->479 draw=ProcessAdapter.recv\u draw(自激活)
480 proc,最后一次,绘制,调整,统计,警告=绘制
481个自吸总吸+=1
~\Anaconda3\envs\pymc\lib\site packages\pymc3\parallel\u sampling.py in recv\u draw(进程,超时)
357其他:
358错误=运行时错误(“链%s失败”。%proc.Chain)
-->359从旧_错误引发的raise错误
360 elif消息[0]=“写入完成”:
361程序可读=真
并行采样错误:初始能量不好
感谢您的帮助

伊利亚斯