PyMC3:对分类变量进行采样时出现 PositiveDefiniteError
PyMC3: PositiveDefiniteError when sampling a Categorical variable
我正在尝试使用狄利克雷先验对分类分布的简单模型进行采样。这是我的代码:
import numpy as np
from scipy import optimize
from pymc3 import *
k = 6
alpha = 0.1 * np.ones(k)
with Model() as model:
p = Dirichlet('p', a=alpha, shape=k)
categ = Categorical('categ', p=p, shape=1)
tr = sample(10000)
我收到这个错误:
PositiveDefiniteError: Scaling is not positive definite. Simple check failed. Diagonal contains negatives. Check indexes [0 1 2 3 4]
问题是 NUTS 无法正确初始化。一种解决方案是像这样使用另一个采样器:
with pm.Model() as model:
p = pm.Dirichlet('p', a=alpha)
categ = pm.Categorical('categ', p=p)
step = pm.Metropolis(vars=p)
tr = pm.sample(1000, step=step)
这里我手动分配 p
给 Metropolis,让 PyMC3 分配 categ
给合适的采样器。
我正在尝试使用狄利克雷先验对分类分布的简单模型进行采样。这是我的代码:
import numpy as np
from scipy import optimize
from pymc3 import *
k = 6
alpha = 0.1 * np.ones(k)
with Model() as model:
p = Dirichlet('p', a=alpha, shape=k)
categ = Categorical('categ', p=p, shape=1)
tr = sample(10000)
我收到这个错误:
PositiveDefiniteError: Scaling is not positive definite. Simple check failed. Diagonal contains negatives. Check indexes [0 1 2 3 4]
问题是 NUTS 无法正确初始化。一种解决方案是像这样使用另一个采样器:
with pm.Model() as model:
p = pm.Dirichlet('p', a=alpha)
categ = pm.Categorical('categ', p=p)
step = pm.Metropolis(vars=p)
tr = pm.sample(1000, step=step)
这里我手动分配 p
给 Metropolis,让 PyMC3 分配 categ
给合适的采样器。