Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Normal distribution throws errors when using shared variables #3139

Closed
benyi1981 opened this issue Aug 7, 2018 · 2 comments · Fixed by #4445
Closed

Bug: Normal distribution throws errors when using shared variables #3139

benyi1981 opened this issue Aug 7, 2018 · 2 comments · Fixed by #4445

Comments

@benyi1981
Copy link

I am having trouble implementing Normal distribution with mu that is a shared variable. See below example code:

Please provide a minimal, self-contained, and reproducible example.

product_mu_shared = shared(0.0)
product_sd = 0.1
total_quantity_shared = shared(np.array([0.0, 0.1, 0.2]))

with pm.Model() as quantity_model:    
    
    # Priors
    a = pm.Lognormal('a',mu=product_mu_shared, sd=product_sd)
    b = pm.Normal('b',mu=0.0, sd=product_sd)
    d = pm.Normal('d', mu=product_mu_shared, sd=product_sd)


    # Data likelihood
    vol = pm.Normal('vol',mu= a+b+d, observed=total_quantity_shared)
        
    # Inference
    trace = pm.sample(10,tune=10)

Please provide the full traceback.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-6131815c61f7> in <module>()
      4     a = pm.Lognormal('a',mu=product_mu_shared, sd=product_sd)
      5     b = pm.Normal('b',mu=0.0, sd=product_sd)
----> 6     d = pm.Normal('d', mu=product_mu_shared, sd=product_sd)
      7 
      8 

C:\ProgramData\Anaconda3\lib\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))

C:\ProgramData\Anaconda3\lib\site-packages\pymc3\model.py in Var(self, name, dist, data, total_size)
    806                 with self:
    807                     var = FreeRV(name=name, distribution=dist,
--> 808                                  total_size=total_size, model=self)
    809                 self.free_RVs.append(var)
    810             else:

C:\ProgramData\Anaconda3\lib\site-packages\pymc3\model.py in __init__(self, type, owner, index, name, distribution, total_size, model)
   1205             self.distribution = distribution
   1206             self.tag.test_value = np.ones(
-> 1207                 distribution.shape, distribution.dtype) * distribution.default()
   1208             self.logp_elemwiset = distribution.logp(self)
   1209             # The logp might need scaling in minibatches.

C:\ProgramData\Anaconda3\lib\site-packages\pymc3\distributions\distribution.py in default(self)
     65 
     66     def default(self):
---> 67         return np.asarray(self.get_test_val(self.testval, self.defaults), self.dtype)
     68 
     69     def get_test_val(self, val, defaults):

C:\ProgramData\Anaconda3\lib\site-packages\pymc3\distributions\distribution.py in get_test_val(self, val, defaults)
     70         if val is None:
     71             for v in defaults:
---> 72                 if hasattr(self, v) and np.all(np.isfinite(self.getattr_value(v))):
     73                     return self.getattr_value(v)
     74         else:

TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''```

**Please provide any additional information below.**


## Versions and main components

* PyMC3 Version: 3.5
* Theano Version: 1.0.2
* Python Version: 3.6
* Operating system: Windows 10
* How did you install PyMC3: (conda/pip) conda-forge
@junpenglao
Copy link
Member

Background discussion see discourse post

The error is that PyMC3 is trying to evaluate whether the default value is finite (for Gaussian is the mean), however np.isfinite cannot evaluate theano.shared type.
Current workaround is assigning a test value to Normal:

d = pm.Normal('d', mu=product_mu_shared, sd=product_sd, testval=0.)

@junpenglao junpenglao added the bug label Aug 7, 2018
@rpgoldman
Copy link
Contributor

this is really a question, and I'm inclined to close it, but should we do something to make this error easier to recognize? Like maybe trap the TypeError and add a suggestion of adding a test value?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants