pymc-devs / pymc

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

Theano type error with shared vars #3649

Closed ghost closed 4 years ago

ghost commented 5 years ago

What seems to be some typing issue with shared observed vars leads to theano error. Changing the Binomial to eg Normal or "unsharing" the observed variables both make the problem go away, as can be seen below. I might just be doing something wrong, not sure. Tried casting in all sorts of ways but it didn't help.

theano.config.optimizer = 'None'

obs = {
    'a': np.array([10., 20., 30.]),
    'b': np.array([1., 2., 3.]),
}

### commenting the below makes the problem go away
for k in obs:
    obs[k] = theano.shared(obs[k])
###

with pm.Model() as m:
    a = pm.Normal('a', 0, 50, observed=obs['a'])

    b = pm.Binomial('b', n=a, p=0.1, testval=1, observed=obs['b']) # BAD
#     b = pm.Normal('b', a/10, sigma=1, testval=1, observed=obs['b']) # GOOD

    bb = pm.Normal('bb', mu=b, sigma=1, testval=1) # Failure to give testval here leads to another error

m.check_test_point()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/anaconda3/envs/research/lib/python3.7/site-packages/theano/compile/function_module.py in __call__(self, *args, **kwargs)
    902             outputs =\
--> 903                 self.fn() if output_subset is None else\
    904                 self.fn(output_subset=output_subset)

TypeError: expected type_num 12 (NPY_FLOAT64) got 7

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-21-b26c5e722741> in <module>
     20     bb = pm.Normal('bb', mu=b, sigma=1., testval=1.)
     21 
---> 22 m.check_test_point()

/anaconda3/envs/research/lib/python3.7/site-packages/pymc3/model.py in check_test_point(self, test_point, round_vals)
   1043             test_point = self.test_point
   1044 
-> 1045         return Series({RV.name:np.round(RV.logp(self.test_point), round_vals) for RV in self.basic_RVs},
   1046             name='Log-probability of test_point')
   1047 

/anaconda3/envs/research/lib/python3.7/site-packages/pymc3/model.py in <dictcomp>(.0)
   1043             test_point = self.test_point
   1044 
-> 1045         return Series({RV.name:np.round(RV.logp(self.test_point), round_vals) for RV in self.basic_RVs},
   1046             name='Log-probability of test_point')
   1047 

/anaconda3/envs/research/lib/python3.7/site-packages/pymc3/model.py in __call__(self, *args, **kwargs)
   1184     def __call__(self, *args, **kwargs):
   1185         point = Point(model=self.model, *args, **kwargs)
-> 1186         return self.f(**point)
   1187 
   1188 compilef = fastfn

/anaconda3/envs/research/lib/python3.7/site-packages/theano/compile/function_module.py in __call__(self, *args, **kwargs)
    915                     node=self.fn.nodes[self.fn.position_of_error],
    916                     thunk=thunk,
--> 917                     storage_map=getattr(self.fn, 'storage_map', None))
    918             else:
    919                 # old-style linkers raise their own exceptions

/anaconda3/envs/research/lib/python3.7/site-packages/theano/gof/link.py in raise_with_op(node, thunk, exc_info, storage_map)
    323         # extra long error message in that case.
    324         pass
--> 325     reraise(exc_type, exc_value, exc_trace)
    326 
    327 

/anaconda3/envs/research/lib/python3.7/site-packages/six.py in reraise(tp, value, tb)
    690                 value = tp()
    691             if value.__traceback__ is not tb:
--> 692                 raise value.with_traceback(tb)
    693             raise value
    694         finally:

/anaconda3/envs/research/lib/python3.7/site-packages/theano/compile/function_module.py in __call__(self, *args, **kwargs)
    901         try:
    902             outputs =\
--> 903                 self.fn() if output_subset is None else\
    904                 self.fn(output_subset=output_subset)
    905         except Exception:

TypeError: expected type_num 12 (NPY_FLOAT64) got 7
Apply node that caused the error: Elemwise{sub,no_inplace}(InplaceDimShuffle{x}.0, b)
Toposort index: 10
Inputs types: [TensorType(float64, (True,)), TensorType(float64, vector)]
Inputs shapes: [(1,), (3,)]
Inputs strides: [(8,), (8,)]
Inputs values: [array([1.]), array([1, 2, 3])]
Outputs clients: [[Elemwise{pow,no_inplace}(Elemwise{sub,no_inplace}.0, InplaceDimShuffle{x}.0)]]

Backtrace when the node is created(use Theano flag traceback.limit=N to make it longer):
  File "/anaconda3/envs/research/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3248, in run_ast_nodes
    if (await self.run_code(code, result,  async_=asy)):
  File "/anaconda3/envs/research/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3325, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-21-b26c5e722741>", line 20, in <module>
    bb = pm.Normal('bb', mu=b, sigma=1., testval=1.)
  File "/anaconda3/envs/research/lib/python3.7/site-packages/pymc3/distributions/distribution.py", line 46, in __new__
    return model.Var(name, dist, data, total_size)
  File "/anaconda3/envs/research/lib/python3.7/site-packages/pymc3/model.py", line 826, in Var
    total_size=total_size, model=self)
  File "/anaconda3/envs/research/lib/python3.7/site-packages/pymc3/model.py", line 1277, in __init__
    self.logp_sum_unscaledt = distribution.logp_sum(self)
  File "/anaconda3/envs/research/lib/python3.7/site-packages/pymc3/distributions/distribution.py", line 123, in logp_sum
    return tt.sum(self.logp(*args, **kwargs))
  File "/anaconda3/envs/research/lib/python3.7/site-packages/pymc3/distributions/continuous.py", line 518, in logp
    return bound((-tau * (value - mu)**2 + tt.log(tau / np.pi / 2.)) / 2.,

HINT: Use the Theano flag 'exception_verbosity=high' for a debugprint and storage map footprint of this apply node.
eigenfoo commented 4 years ago

Hi @uri-datorama, thanks for your question. We try to keep the GitHub issues for development issues, instead of user support for PyMC3 or modelling questions. In general, if you aren't sure that your issue is a bug, please post your issue on our Discourse Forums: https://discourse.pymc.io/.

Closing now, but feel free to continue the discussion on the forums!