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

Unable to plot sample trace or get a summary of posterior samples using Arviz with Pymc3 V4 #4957

Closed
sreedat opened this issue Aug 21, 2021 · 3 comments
Labels

Comments

@sreedat
Copy link

sreedat commented Aug 21, 2021

pm.sample() runs correctly but the trace cannot be used to plot or summarize the posterior

I get the following error: AttributeError: 'TensorVariable' object has no attribute 'logp_elemwise'

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

import arviz as az
import matplotlib.pyplot as plt
import numpy as np

%config InlineBackend.figure_format = 'retina'
# Initialize random number generator
RANDOM_SEED = 8927
np.random.seed(RANDOM_SEED)
az.style.use("arviz-darkgrid")

# Case study for a simple linear regression
# True parameter values
alpha, sigma = 1, 1
beta = [1, 2.5]

# Size of dataset
size = 100

# Predictor variable
X1 = np.random.randn(size)
X2 = np.random.randn(size) * 0.2

# Simulate outcome variable
Y = alpha + beta[0] * X1 + beta[1] * X2 + np.random.randn(size) * sigma
import pymc3 as pm
print(f"Running on PyMC3 v{pm.__version__}")

basic_model = pm.Model()

with basic_model:

    # Priors for unknown model parameters
    alpha = pm.Normal("alpha", mu=0, sigma=10)
    beta = pm.Normal("beta", mu=0, sigma=10, shape=2)
    sigma = pm.HalfNormal("sigma", sigma=1)

    # Expected value of outcome
    mu = alpha + beta[0] * X1 + beta[1] * X2

    # Likelihood (sampling distribution) of observations
    Y_obs = pm.Normal("Y_obs", mu=mu, sigma=sigma, observed=Y)

with basic_model:
    # draw 500 posterior samples
    trace = pm.sample(500, cores=1, chains=2, return_inferencedata=False)

with basic_model:
    az.plot_trace(trace);

Please provide the full traceback.

Complete error traceback
AttributeError                            Traceback (most recent call last)
C:\Users\SREEDATTA\AppData\Local\Temp/ipykernel_12636/3824430439.py in <module>
      1 with basic_model:
----> 2     az.plot_trace(trace);

C:\ProgramData\Anaconda3\envs\pm3env\lib\site-packages\arviz\plots\traceplot.py in plot_trace(data, var_names, filter_vars, transform, coords, divergences, kind, figsize, rug, lines, circ_var_names, circ_var_units, compact, compact_prop, combined, chain_prop, legend, plot_kwargs, fill_kwargs, rug_kwargs, hist_kwargs, trace_kwargs, rank_kwargs, axes, backend, backend_config, backend_kwargs, show)
    177         divergence_data = False
    178 
--> 179     coords_data = get_coords(convert_to_dataset(data, group="posterior"), coords)
    180 
    181     if transform is not None:

C:\ProgramData\Anaconda3\envs\pm3env\lib\site-packages\arviz\data\converters.py in convert_to_dataset(obj, group, coords, dims)
    177     xarray.Dataset
    178     """
--> 179     inference_data = convert_to_inference_data(obj, group=group, coords=coords, dims=dims)
    180     dataset = getattr(inference_data, group, None)
    181     if dataset is None:

C:\ProgramData\Anaconda3\envs\pm3env\lib\site-packages\arviz\data\converters.py in convert_to_inference_data(obj, group, coords, dims, **kwargs)
     89             return from_pystan(**kwargs)
     90     elif obj.__class__.__name__ == "MultiTrace":  # ugly, but doesn't make PyMC3 a requirement
---> 91         return from_pymc3(trace=kwargs.pop(group), **kwargs)
     92     elif obj.__class__.__name__ == "EnsembleSampler":  # ugly, but doesn't make emcee a requirement
     93         return from_emcee(sampler=kwargs.pop(group), **kwargs)

C:\ProgramData\Anaconda3\envs\pm3env\lib\site-packages\arviz\data\io_pymc3.py in from_pymc3(trace, prior, posterior_predictive, log_likelihood, coords, dims, model, save_warmup, density_dist_obs)
    561     InferenceData
    562     """
--> 563     return PyMC3Converter(
    564         trace=trace,
    565         prior=prior,

C:\ProgramData\Anaconda3\envs\pm3env\lib\site-packages\arviz\data\io_pymc3.py in to_inference_data(self)
    497             "posterior": self.posterior_to_xarray(),
    498             "sample_stats": self.sample_stats_to_xarray(),
--> 499             "log_likelihood": self.log_likelihood_to_xarray(),
    500             "posterior_predictive": self.posterior_predictive_to_xarray(),
    501             "predictions": self.predictions_to_xarray(),

C:\ProgramData\Anaconda3\envs\pm3env\lib\site-packages\arviz\data\base.py in wrapped(cls, *args, **kwargs)
     44                 if all([getattr(cls, prop_i) is None for prop_i in prop]):
     45                     return None
---> 46             return func(cls, *args, **kwargs)
     47 
     48         return wrapped

C:\ProgramData\Anaconda3\envs\pm3env\lib\site-packages\arviz\data\base.py in wrapped(cls, *args, **kwargs)
     44                 if all([getattr(cls, prop_i) is None for prop_i in prop]):
     45                     return None
---> 46             return func(cls, *args, **kwargs)
     47 
     48         return wrapped

C:\ProgramData\Anaconda3\envs\pm3env\lib\site-packages\arviz\data\io_pymc3.py in log_likelihood_to_xarray(self)
    325         if self.posterior_trace:
    326             try:
--> 327                 data = self._extract_log_likelihood(self.posterior_trace)
    328             except TypeError:
    329                 warnings.warn(warn_msg)

C:\ProgramData\Anaconda3\envs\pm3env\lib\site-packages\arviz\data\base.py in wrapped(cls, *args, **kwargs)
     44                 if all([getattr(cls, prop_i) is None for prop_i in prop]):
     45                     return None
---> 46             return func(cls, *args, **kwargs)
     47 
     48         return wrapped

C:\ProgramData\Anaconda3\envs\pm3env\lib\site-packages\arviz\data\base.py in wrapped(cls, *args, **kwargs)
     44                 if all([getattr(cls, prop_i) is None for prop_i in prop]):
     45                     return None
---> 46             return func(cls, *args, **kwargs)
     47 
     48         return wrapped

C:\ProgramData\Anaconda3\envs\pm3env\lib\site-packages\arviz\data\io_pymc3.py in _extract_log_likelihood(self, trace)
    222         # support extracting a log likelihood.
    223         if self.log_likelihood is True:
--> 224             cached = [(var, var.logp_elemwise) for var in self.model.observed_RVs]
    225         else:
    226             cached = [

C:\ProgramData\Anaconda3\envs\pm3env\lib\site-packages\arviz\data\io_pymc3.py in <listcomp>(.0)
    222         # support extracting a log likelihood.
    223         if self.log_likelihood is True:
--> 224             cached = [(var, var.logp_elemwise) for var in self.model.observed_RVs]
    225         else:
    226             cached = [

AttributeError: 'TensorVariable' object has no attribute 'logp_elemwise'

Please provide any additional information below.

Versions and main components

  • PyMC3 Version: V4.0
  • Aesara/Theano Version: 2.2.0
  • Python Version: 3.8.10
  • Operating system: Windows 10-1904
  • How did you install PyMC3: (conda/pip) pip install git+https://github.com/pymc-devs/pymc3
@ricardoV94 ricardoV94 added the bug label Aug 22, 2021
@OriolAbril
Copy link
Member

Which ArviZ version are you using?

v4 is still unreleased and very experimental, but it a lot of our CI checks depend on it so with the latest or the development version this should definitely work. It could also be related to Aesara 2.2.0 and to #4961 in which case I'd recommend downgrading Aesara until we have been able to update the pymc3 codebase to be compatible with Aesara 2.2.0.

Extra note: even with the right versions this may error out with a different message, hopefully more graceful one because you are calling plot_trace on a MultiTrace object, which relies on ArviZ converting from MultiTrace to InferenceData automatically under the hoods. With v4 we are moving to InferenceData as the default backend for pymc3 directly, and we started that by moving all the conversion functionality from arviz to pymc3 codebase. Therefore, you may need to set return_inferencedata=True or convert explicitly before using plot_trace with pm.to_inference_data

@sreedat
Copy link
Author

sreedat commented Aug 23, 2021

@OriolAbril thanks for getting back. I have been downloading and installing the pymc3 build from GitHub and testing in new environments everyday. The errors I ran into yesterday did go away when I changed return_inferencedata=True in my pm.sample() command. Thank you for explaining the why behind the change. I was working with aesara=2.2.0 before and today as well. The change of setting worked well.

Are there any examples where the two methods you have mentioned are illustrated or is this wip?

@OriolAbril
Copy link
Member

I missed the notification, we have now released a beta for v4 and are working on examples in several notebooks, for example #5268. I will close the issue, but don't hesitate to ask on discourse in after a few weeks there are still not good examples on the documentation

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

No branches or pull requests

3 participants