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

Attributes of trace.report are None when using sample_smc—Converting to arviz fails #3928

Closed
astoeriko opened this issue May 19, 2020 · 3 comments · Fixed by #3931
Closed

Comments

@astoeriko
Copy link

When I generate samples with the sample_smc method, the attributes n_draws, n_tune and t_sampling of the trace's report are all None.
This leads to an error when I try to convert the trace to an arviz inference data object (I don't know if it causes problems elsewhere).
This does not happen when sampling with NUTS.

import numpy as np
import pymc3 as pm
import arviz as az

x_obs = np.random.rand(3)
with pm.Model() as model:
    x = pm.Normal('x')
    pm.Normal('x_obs', mu=x, observed=x_obs)
    trace_smc = pm.sample_smc(draws=100)

trace_smc.report.n_draws is None
trace_smc.report.n_tune is None
trace_smc.report.t_sampling is None

# This fails
data = az.from_pymc3(trace_smc)

The error actually occurs in the arviz code but is due to trace.report.n_draws being None.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-41-042acf6338ba> in <module>
     14 trace_smc.report.t_sampling is None
     15 
---> 16 data = az.from_pymc3(trace_smc)

~/miniconda3/envs/sunode/lib/python3.7/site-packages/arviz/data/io_pymc3.py in from_pymc3(trace, prior, posterior_predictive, log_likelihood, coords, dims, model, save_warmup)
    480         dims=dims,
    481         model=model,
--> 482         save_warmup=save_warmup,
    483     ).to_inference_data()
    484 

~/miniconda3/envs/sunode/lib/python3.7/site-packages/arviz/data/io_pymc3.py in to_inference_data(self)
    413         """
    414         id_dict = {
--> 415             "posterior": self.posterior_to_xarray(),
    416             "sample_stats": self.sample_stats_to_xarray(),
    417             "log_likelihood": self.log_likelihood_to_xarray(),

~/miniconda3/envs/sunode/lib/python3.7/site-packages/arviz/data/base.py in wrapped(cls, *args, **kwargs)
     35                 if all([getattr(cls, prop_i) is None for prop_i in prop]):
     36                     return None
---> 37             return func(cls, *args, **kwargs)
     38 
     39         return wrapped

~/miniconda3/envs/sunode/lib/python3.7/site-packages/arviz/data/io_pymc3.py in posterior_to_xarray(self)
    207                 )
    208             data[var_name] = np.array(
--> 209                 self.trace[-self.ndraws :].get_values(var_name, combine=False, squeeze=False)
    210             )
    211         return (

TypeError: bad operand type for unary -: 'NoneType'

Versions and main components

  • PyMC3 Version: 3.8
  • Theano Version: 1.0.4
  • Python Version: 3.7.6
  • Operating system: Archlinux
  • How did you install PyMC3: conda
  • ArviZ Version: 0.7.0
@kennysong
Copy link

kennysong commented May 23, 2020

I'm seeing the exact same error while following the very basic intro to PyMc3 in Bayesian Methods for Hackers, Chapter 1.

The code is:

with model:
    step = pm.Metropolis()
    trace = pm.sample(10000, tune=5000, step=step)

Stack trace:

Multiprocess sampling (2 chains in 2 jobs)
CompoundStep
>Metropolis: [tau]
>Metropolis: [lambda_2]
>Metropolis: [lambda_1]
Sampling 2 chains, 0 divergences: 100%|██████████| 30000/30000 [00:15<00:00, 1981.30draws/s]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-93c5c02567d8> in <module>
      1 with model:
----> 2     trace = pm.sample(10000, tune=5000, step=step)

~/GDrive/Projects/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers/env/lib/python3.7/site-packages/pymc3/sampling.py in sample(draws, step, init, n_init, start, trace, chain_idx, chains, cores, tune, progressbar, model, random_seed, discard_tuned_samples, compute_convergence_checks, **kwargs)
    496             warnings.warn("The number of samples is too small to check convergence reliably.")
    497         else:
--> 498             trace.report._run_convergence_checks(trace, model)
    499 
    500     trace.report._log_summary()

~/GDrive/Projects/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers/env/lib/python3.7/site-packages/pymc3/backends/report.py in _run_convergence_checks(self, trace, model)
     82                 varnames.append(rv_name)
     83 
---> 84         self._ess = ess = ess(trace, var_names=varnames)
     85         self._rhat = rhat = rhat(trace, var_names=varnames)
     86 

~/GDrive/Projects/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers/env/lib/python3.7/site-packages/pymc3/stats/__init__.py in wrapped(*args, **kwargs)
     22                 )
     23                 kwargs[new] = kwargs.pop(old)
---> 24             return func(*args, **kwargs)
     25 
     26     return wrapped

~/GDrive/Projects/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers/env/lib/python3.7/site-packages/arviz/stats/diagnostics.py in ess(data, var_names, method, relative, prob)
    189             raise TypeError(msg)
    190 
--> 191     dataset = convert_to_dataset(data, group="posterior")
    192     var_names = _var_names(var_names, dataset)
    193 

~/GDrive/Projects/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers/env/lib/python3.7/site-packages/arviz/data/converters.py in convert_to_dataset(obj, group, coords, dims)
    175     xarray.Dataset
    176     """
--> 177     inference_data = convert_to_inference_data(obj, group=group, coords=coords, dims=dims)
    178     dataset = getattr(inference_data, group, None)
    179     if dataset is None:

~/GDrive/Projects/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers/env/lib/python3.7/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)

~/GDrive/Projects/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers/env/lib/python3.7/site-packages/arviz/data/io_pymc3.py in from_pymc3(trace, prior, posterior_predictive, log_likelihood, coords, dims, model, save_warmup)
    483         dims=dims,
    484         model=model,
--> 485         save_warmup=save_warmup,
    486     ).to_inference_data()
    487 

~/GDrive/Projects/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers/env/lib/python3.7/site-packages/arviz/data/io_pymc3.py in to_inference_data(self)
    413         """
    414         id_dict = {
--> 415             "posterior": self.posterior_to_xarray(),
    416             "sample_stats": self.sample_stats_to_xarray(),
    417             "log_likelihood": self.log_likelihood_to_xarray(),

~/GDrive/Projects/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers/env/lib/python3.7/site-packages/arviz/data/base.py in wrapped(cls, *args, **kwargs)
     35                 if all([getattr(cls, prop_i) is None for prop_i in prop]):
     36                     return None
---> 37             return func(cls, *args, **kwargs)
     38 
     39         return wrapped

~/GDrive/Projects/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers/env/lib/python3.7/site-packages/arviz/data/io_pymc3.py in posterior_to_xarray(self)
    207                 )
    208             data[var_name] = np.array(
--> 209                 self.trace[-self.ndraws :].get_values(var_name, combine=False, squeeze=False)
    210             )
    211         return (

TypeError: bad operand type for unary -: 'NoneType'

@aloctavodia
Copy link
Member

aloctavodia commented May 23, 2020

Thanks @astoeriko #3931 should fix the problem. @kennysong The problem you are seeing seems to be fixed in PyMC3 master. Thanks you both for taking the time to report this bug.

@kennysong
Copy link

@aloctavodia Yep! Confirming that the problem is gone in PyMC3 master. Thank for the quick response!

(reinstalled with pip install git+https://github.com/pymc-devs/pymc3)

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.

3 participants