pymc-devs / pymc-examples

Examples of PyMC models, including a library of Jupyter notebooks.
https://www.pymc.io/projects/examples/en/latest/
MIT License
289 stars 249 forks source link

nb: GLM Poisson Regression (error) #512

Open reshamas opened 1 year ago

reshamas commented 1 year ago

Notebook title: GLM: Poisson Regression Notebook url: https://www.pymc.io/projects/examples/en/latest/generalized_linear_models/GLM-poisson-regression.html

Issue description

# code, cell 18
inf_fish_alt = model.fit()

short Error:

TypeError: You are calling an Aesara function with PyTensor variables.
Starting with PyMC 5.0, Aesara was replaced by PyTensor (see https://www.pymc.io/blog/pytensor_announcement.html).
Replace your import of aesara.tensor with pytensor.tensor.

long Error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[18], line 1
----> 1 inf_fish_alt = model.fit()

File ~/miniforge3/envs/pymc-ex/lib/python3.10/site-packages/bambi/models.py:267, in Model.fit(self, draws, tune, discard_tuned_samples, omit_offsets, include_mean, inference_method, init, n_init, chains, cores, random_seed, **kwargs)
    264         inference_method = method
    266 if not self.built:
--> 267     self.build()
    269 # Tell user which event is being modeled
    270 if isinstance(self.family, univariate.Bernoulli):

File ~/miniforge3/envs/pymc-ex/lib/python3.10/site-packages/bambi/models.py:298, in Model.build(self)
    293 """Set up the model for sampling/fitting.
    294 
    295 Creates an instance of the underlying PyMC model and adds all the necessary terms to it.
    296 """
    297 self.backend = PyMCModel()
--> 298 self.backend.build(self)
    299 self.built = True

File ~/miniforge3/envs/pymc-ex/lib/python3.10/site-packages/bambi/backend/pymc.py:69, in PyMCModel.build(self, spec)
     67 self._build_intercept(spec)
     68 self._build_offsets(spec)
---> 69 self._build_common_terms(spec)
     70 self._build_group_specific_terms(spec)
     71 self._build_response(spec)

File ~/miniforge3/envs/pymc-ex/lib/python3.10/site-packages/bambi/backend/pymc.py:160, in PyMCModel._build_common_terms(self, spec)
    157     columns.append(data)
    159 # Column vector of coefficients and design matrix
--> 160 coefs = at.concatenate(coefs)
    161 data = np.hstack(columns)
    163 # If there's an intercept, center the data
    164 # Also store the design matrix without the intercept to uncenter the intercept later

File ~/miniforge3/envs/pymc-ex/lib/python3.10/site-packages/aesara/tensor/basic.py:2820, in concatenate(tensor_list, axis)
   2813 if not isinstance(tensor_list, (tuple, list)):
   2814     raise TypeError(
   2815         "The 'tensors' argument must be either a tuple "
   2816         "or a list, make sure you did not forget () or [] around "
   2817         "arguments of concatenate.",
   2818         tensor_list,
   2819     )
-> 2820 return join(axis, *tensor_list)

File ~/miniforge3/envs/pymc-ex/lib/python3.10/site-packages/aesara/tensor/basic.py:2645, in join(axis, *tensors_list)
   2643     return tensors_list[0]
   2644 else:
-> 2645     return join_(axis, *tensors_list)

File ~/miniforge3/envs/pymc-ex/lib/python3.10/site-packages/aesara/graph/op.py:297, in Op.__call__(self, *inputs, **kwargs)
    255 r"""Construct an `Apply` node using :meth:`Op.make_node` and return its outputs.
    256 
    257 This method is just a wrapper around :meth:`Op.make_node`.
   (...)
    294 
    295 """
    296 return_list = kwargs.pop("return_list", False)
--> 297 node = self.make_node(*inputs, **kwargs)
    299 if config.compute_test_value != "off":
    300     compute_test_value(node)

File ~/miniforge3/envs/pymc-ex/lib/python3.10/site-packages/aesara/tensor/basic.py:2369, in Join.make_node(self, axis, *tensors)
   2366 if not tensors:
   2367     raise ValueError("Cannot join an empty list of tensors")
-> 2369 tensors = [as_tensor_variable(x) for x in tensors]
   2370 out_dtype = aes.upcast(*[x.type.dtype for x in tensors])
   2372 if not builtins.all(targs.type.ndim for targs in tensors):

File ~/miniforge3/envs/pymc-ex/lib/python3.10/site-packages/aesara/tensor/basic.py:2369, in <listcomp>(.0)
   2366 if not tensors:
   2367     raise ValueError("Cannot join an empty list of tensors")
-> 2369 tensors = [as_tensor_variable(x) for x in tensors]
   2370 out_dtype = aes.upcast(*[x.type.dtype for x in tensors])
   2372 if not builtins.all(targs.type.ndim for targs in tensors):

File ~/miniforge3/envs/pymc-ex/lib/python3.10/site-packages/aesara/tensor/__init__.py:42, in as_tensor_variable(x, name, ndim, **kwargs)
     10 def as_tensor_variable(
     11     x: Any, name: Optional[str] = None, ndim: Optional[int] = None, **kwargs
     12 ) -> "TensorVariable":
     13     """Convert `x` into an equivalent `TensorVariable`.
     14 
     15     This function can be used to turn ndarrays, numbers, `ScalarType` instances,
   (...)
     40 
     41     """
---> 42     return _as_tensor_variable(x, name, ndim, **kwargs)

File ~/miniforge3/envs/pymc-ex/lib/python3.10/functools.py:889, in singledispatch.<locals>.wrapper(*args, **kw)
    885 if not args:
    886     raise TypeError(f'{funcname} requires at least '
    887                     '1 positional argument')
--> 889 return dispatch(args[0].__class__)(*args, **kw)

File ~/miniforge3/envs/pymc-ex/lib/python3.10/site-packages/pymc/pytensorf.py:99, in raise_informative_error(*args, **kwargs)
     97 @_as_tensor_variable_aesara.register(TensorVariable)
     98 def raise_informative_error(*args, **kwargs):
---> 99     raise TypeError(
    100         "You are calling an Aesara function with PyTensor variables.\n"
    101         "Starting with PyMC 5.0, Aesara was replaced by PyTensor (see https://www.pymc.io/blog/pytensor_announcement.html).\n"
    102         "Replace your import of aesara.tensor with pytensor.tensor.",
    103     )

TypeError: You are calling an Aesara function with PyTensor variables.
Starting with PyMC 5.0, Aesara was replaced by PyTensor (see https://www.pymc.io/blog/pytensor_announcement.html).
Replace your import of aesara.tensor with pytensor.tensor.

Expected output

If applicable, describe what should happen instead.

Proposed solution

If applicable, explain possible solutions and workarounds.

OriolAbril commented 1 year ago

My first guess is you have pymc v5 and pytensor installed but at the same time don't have the latest bambi version so bambi attempts to combine pymc v5 and aesara. Make sure you have bambi>=0.9.3