pymc-devs / pymc

Bayesian Modeling and Probabilistic Programming in Python
https://docs.pymc.io/
Other
8.72k stars 2.02k forks source link

Exponential mixture models gives theano index error #1882

Closed whitead closed 7 years ago

whitead commented 7 years ago

I'm not sure if it's reasonable to use an exponential mixture model, but I'm trying to cluster arrival time processes and thought it might be a good model. If I try a simple exponential model like this:

import pymc3 as pm
import numpy as np

time = np.array([4,5,6,7,4])
with pm.Model() as pprocess:

    #clusters
    cluster_number = 2
    w = pm.Dirichlet('w', np.ones(cluster_number))

    #arrival time model
    t = pm.Lognormal('t', 100, 50, shape=cluster_number)
    t_obs = pm.Mixture('t_obs', w, pm.Exponential.dist(t), observed=time)

I get the following error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-2-1724aa75761e> in <module>()
     11     #arrival time model
     12     t = pm.Lognormal('t', 100, 50, shape=cluster_number)
---> 13     t_obs = pm.Mixture('t_obs', w, pm.Exponential.dist(t), observed=time)
     14 

/opt/conda/lib/python3.5/site-packages/pymc3/distributions/distribution.py in __new__(cls, name, *args, **kwargs)
     28         if isinstance(name, string_types):
     29             data = kwargs.pop('observed', None)
---> 30             dist = cls.dist(*args, **kwargs)
     31             return model.Var(name, dist, data)
     32         else:

/opt/conda/lib/python3.5/site-packages/pymc3/distributions/distribution.py in dist(cls, *args, **kwargs)
     39     def dist(cls, *args, **kwargs):
     40         dist = object.__new__(cls)
---> 41         dist.__init__(*args, **kwargs)
     42         return dist
     43 

/opt/conda/lib/python3.5/site-packages/pymc3/distributions/mixture.py in __init__(self, w, comp_dists, *args, **kwargs)
     63             comp_modes = self._comp_modes()
     64             comp_mode_logps = self.logp(comp_modes)
---> 65             self.mode = comp_modes[tt.argmax(w * comp_mode_logps, axis=-1)]
     66 
     67             if 'mode' not in defaults:

/opt/conda/lib/python3.5/site-packages/theano/tensor/var.py in __getitem__(self, args)
    530                     self, *theano.tensor.subtensor.Subtensor.collapse(
    531                         args,
--> 532                         lambda entry: isinstance(entry, Variable)))
    533 
    534     def take(self, indices, axis=None, mode='raise'):

/opt/conda/lib/python3.5/site-packages/theano/gof/op.py in __call__(self, *inputs, **kwargs)
    609         """
    610         return_list = kwargs.pop('return_list', False)
--> 611         node = self.make_node(*inputs, **kwargs)
    612 
    613         if config.compute_test_value != 'off':

/opt/conda/lib/python3.5/site-packages/theano/tensor/subtensor.py in make_node(self, x, *inputs)
    482                 len(idx_list), x.type.ndim))
    483             exception.subtensor_invalid = True
--> 484             raise exception
    485 
    486         input_types = Subtensor.collapse(idx_list,

ValueError: The index list is longer (size 1) than the number of dimensions of the tensor(namely 0). You are asking for a dimension of the tensor that does not exist! You might need to use dimshuffle to add extra dimension to your tensor.

Any ideas on what could be wrong? If I use a Poisson or Normal, it works. Are exponentials not intended to be used in mixture models? I'm using latest master (34fdef3) of pymc3 and theano (ee9eae).

AustinRochford commented 7 years ago

I have confirmed the error and will look into it; there's no particular reason I can think of that exponentials shouldn't work.

AustinRochford commented 7 years ago

No matter the shape of the parameters, Exponential was defining its mode to be a scalar. The fix is simple; will merge once tests pass.

whitead commented 7 years ago

Cool, thanks for helping me with the issue so quickly!