使用pymc3变量设置另一个变量的长度?

Use pymc3 variable to set the length of another variable?

是否可以使用 pymc3 RV 设置另一个 RV 的形状?

我希望按照以下方式做一些事情:

with pm.Model() as model:
        n_obj = pm.Poisson(name='n', mu=5)
        objs = pm.Uniform(lower=0, upper=1, shape=(n_obj, 2))

当我运行这个时,我得到错误:

TypeError: 'TensorVariable' object cannot be interpreted as an integer

不幸的是你不能,大致是因为这意味着你的模型在采样时改变维度(这有点改变模型)。

幸运的是,你通常可以像这样伪造一个模型:将 objs 的形状设置为超过你期望的最大数量,然后你可以切片 objs[:n_obj, :] 并在下游使用它。

This 是 Austin Rochford 对 Dirichlet 过程使用这种方法的一篇很好的文章。