Issue with categorical.dist

Hi,
All I am trying to do is sample an integer from [0-4] with uniform probability, in three different ways.

CASE 1

p=np.ones(5)/5
with pm.Model() as model1:
    x = pm.Categorical('x',p,shape=1)

CASE 2

with pm.Model() as model2:
    dist2 = pm.DiscreteUniform.dist(0,4)
    y= pm.DensityDist('y',dist2.logp)

CASE 3

with pm.Model() as model3:
    comp_dists = pm.Categorical.dist(p)
    z= pm.DensityDist('z',comp_dists.logp)

Case1, Case 2 both works and I understand them

BUT WHY CASE 3 does not work and shows me some strange expect integer error.

TypeError                                 Traceback (most recent call last)
<ipython-input-30-32aa58e76bbe> in <module>
      1 with pm.Model() as model3:
      2     comp_dists = pm.Categorical.dist(p)
----> 3     z= pm.DensityDist('z',comp_dists.logp)

~/anaconda3/lib/python3.6/site-packages/pymc3/distributions/distribution.py in __new__(cls, name, *args, **kwargs)
     40             total_size = kwargs.pop('total_size', None)
     41             dist = cls.dist(*args, **kwargs)
---> 42             return model.Var(name, dist, data, total_size)
     43         else:
     44             raise TypeError("Name needs to be a string but got: {}".format(name))

~/anaconda3/lib/python3.6/site-packages/pymc3/model.py in Var(self, name, dist, data, total_size)
    807                 with self:
    808                     var = FreeRV(name=name, distribution=dist,
--> 809                                  total_size=total_size, model=self)
    810                 self.free_RVs.append(var)
    811             else:

~/anaconda3/lib/python3.6/site-packages/pymc3/model.py in __init__(self, type, owner, index, name, distribution, total_size, model)
   1207             self.tag.test_value = np.ones(
   1208                 distribution.shape, distribution.dtype) * distribution.default()
-> 1209             self.logp_elemwiset = distribution.logp(self)
   1210             # The logp might need scaling in minibatches.
   1211             # This is done in `Factor`.

~/anaconda3/lib/python3.6/site-packages/pymc3/distributions/discrete.py in logp(self, value)
    738             # pattern = (p.ndim - 1,) + tuple(range(p.ndim - 1))
    739             # a = tt.log(p.dimshuffle(pattern)[value_clip])
--> 740             a = tt.log(p[value_clip])
    741 
    742         return bound(a, value >= 0, value <= (k - 1), sumto1)

~/anaconda3/lib/python3.6/site-packages/theano/tensor/var.py in __getitem__(self, args)
    542             try:
    543                 if arg is not np.newaxis:
--> 544                     theano.tensor.subtensor.Subtensor.convert(arg)
    545             except theano.tensor.subtensor.AdvancedIndexingError:
    546                 if advanced:

~/anaconda3/lib/python3.6/site-packages/theano/tensor/subtensor.py in convert(entry, slice_ok)
    352             (entry.type in invalid_scal_types or
    353              entry.type in invalid_tensor_types)):
--> 354             raise TypeError("Expected an integer")
    355 
    356         if isinstance(entry, gof.Variable) and entry.type in scal_types:

TypeError: Expected an integer
1 Like