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

AttributeError: '_FutureWarningValidatingScratchpad' object has no attribute 'observations' #54

Closed
ferrine opened this issue Aug 24, 2023 · 2 comments · Fixed by #55
Closed

Comments

@ferrine
Copy link
Member

ferrine commented Aug 24, 2023

import pymc as pm
import nutpie
from pymc_experimental.model_transform.conditioning import observe
with pm.Model() as model:
    p = pm.Uniform("p")
    c = pm.Binomial("c", p=p, n=10)
model_obs = observe(model, {c: 4})
compiled_model = nutpie.compile_pymc_model(obs_model)

gives error

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[18], line 1
----> 1 compiled_model = nutpie.compile_pymc_model(obs_model)

File ~/.cache/pypoetry/virtualenvs/creativeab-fyBc7-Ny-py3.10/lib/python3.10/site-packages/nutpie/compile_pymc.py:170, in compile_pymc_model(model, **kwargs)
    146 def compile_pymc_model(model: pm.Model, **kwargs) -> CompiledPyMCModel:
    147     """Compile necessary functions for sampling a pymc model.
    148 
    149     Parameters
   (...)
    158 
    159     """
    161     (
    162         n_dim,
    163         n_expanded,
    164         logp_fn_pt,
    165         logp_fn,
    166         expand_fn_pt,
    167         expand_fn,
    168         shared_expand,
    169         shape_info,
--> 170     ) = _make_functions(model)
    172     shared_data = {val.name: val.get_value().copy() for val in logp_fn_pt.get_shared()}
    173     for val in shared_data.values():

File ~/.cache/pypoetry/virtualenvs/creativeab-fyBc7-Ny-py3.10/lib/python3.10/site-packages/nutpie/compile_pymc.py:248, in _make_functions(model)
    247 def _make_functions(model):
--> 248     shapes = _compute_shapes(model)
    250     # Make logp_dlogp_function
    251     joined = pt.dvector("__joined_variables")

File ~/.cache/pypoetry/virtualenvs/creativeab-fyBc7-Ny-py3.10/lib/python3.10/site-packages/nutpie/compile_pymc.py:234, in _compute_shapes(model)
    222 point = pm.model.make_initial_point_fn(model=model, return_transformed=True)(0)
    224 trace_vars = {
    225     var.name: var
    226     for var in model.value_vars + model.free_RVs + model.deterministics
    227     if var not in model.observed_RVs + model.potentials
    228 }
    230 shape_func = pytensor.compile.function.function(
    231     inputs=[],
    232     outputs=[var.shape for var in trace_vars.values()],
    233     givens=(
--> 234         [(obs, obs.tag.observations) for obs in model.observed_RVs]
    235         + [
    236             (trace_vars[name], point[name])
    237             for name in trace_vars.keys()
    238             if name in point
    239         ]
    240     ),
    241     mode=pytensor.compile.mode.FAST_COMPILE,
    242     on_unused_input="ignore",
    243 )
    244 return {name: shape for name, shape in zip(trace_vars.keys(), shape_func())}

File ~/.cache/pypoetry/virtualenvs/creativeab-fyBc7-Ny-py3.10/lib/python3.10/site-packages/nutpie/compile_pymc.py:234, in <listcomp>(.0)
    222 point = pm.model.make_initial_point_fn(model=model, return_transformed=True)(0)
    224 trace_vars = {
    225     var.name: var
    226     for var in model.value_vars + model.free_RVs + model.deterministics
    227     if var not in model.observed_RVs + model.potentials
    228 }
    230 shape_func = pytensor.compile.function.function(
    231     inputs=[],
    232     outputs=[var.shape for var in trace_vars.values()],
    233     givens=(
--> 234         [(obs, obs.tag.observations) for obs in model.observed_RVs]
    235         + [
    236             (trace_vars[name], point[name])
    237             for name in trace_vars.keys()
    238             if name in point
    239         ]
    240     ),
    241     mode=pytensor.compile.mode.FAST_COMPILE,
    242     on_unused_input="ignore",
    243 )
    244 return {name: shape for name, shape in zip(trace_vars.keys(), shape_func())}

File ~/.cache/pypoetry/virtualenvs/creativeab-fyBc7-Ny-py3.10/lib/python3.10/site-packages/pymc/util.py:505, in _FutureWarningValidatingScratchpad.__getattribute__(self, name)
    500         else:
    501             warnings.warn(
    502                 f"The tag attribute {name} is deprecated. Use {alternative} instead",
    503                 FutureWarning,
    504             )
--> 505 return super().__getattribute__(name)

File ~/.cache/pypoetry/virtualenvs/creativeab-fyBc7-Ny-py3.10/lib/python3.10/site-packages/pytensor/graph/utils.py:285, in Scratchpad.__getattribute__(self, name)
    284 def __getattribute__(self, name):
--> 285     return super().__getattribute__(name)

AttributeError: '_FutureWarningValidatingScratchpad' object has no attribute 'observations'
@ferrine
Copy link
Member Author

ferrine commented Aug 24, 2023

I think there is a quick fix

image

@ricardoV94
Copy link
Member

Yes, we should get rid of all the tag.x uses. Everything is available as a dict in Model

ferrine added a commit that referenced this issue Aug 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants