Search code examples
theanopymc3

Use pymc3 variable to set the length of another variable?


Is it possible to use a pymc3 RV to set the shape of another RV?

I'm looking to do something along the lines of:

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

When I run this, I get the error:

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

Solution

  • Unfortunately you can't, roughly because this would imply your model changing dimension while sampling (which kinda sorta changes the model).

    Luckily, you can usually fake a model like this: set the shape of objs to more than the maximum number you expect, and then you can slice objs[:n_obj, :] and use it downstream.

    This is a good writeup from Austin Rochford using this approach for Dirichlet processes.