question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

from_pymc3 fails in certain circumstances

See original GitHub issue

Describe the bug A clear and concise description of what the bug is.

To Reproduce

import arviz as az
import pymc3 as pm
print(az.__version__)
print(pm.__version__)

with pm.Model() as planetmodel:
    p_water = pm.Uniform("p", 0 ,1)
    w = pm.Binomial("w", p=p_water, n=2, observed=1)
    trace = pm.sample(5000, chains=2)

water_data = az.from_pymc3(trace=trace)

0.4.0 3.7 Auto-assigning NUTS sampler… Initializing NUTS using jitter+adapt_diag… Multiprocess sampling (2 chains in 2 jobs) NUTS: [p] Sampling 2 chains: 100%|███████████████████████████████████████████████████████████████| 11000/11000 [00:03<00:00, 2882.36draws/s] /Users/canyon/.miniconda3/envs/bayestutorial/lib/python3.6/site-packages/arviz/data/io_pymc3.py:56: FutureWarning: arrays to stack must be passed as a “sequence” type such as list or tuple. Support for non-sequence iterables such as generators is deprecated as of NumPy 1.16 and will raise an error in the future. chain_likelihoods.append(np.stack(log_like)) Traceback (most recent call last): File “trace_error.py”, line 11, in <module> water_data = az.from_pymc3(trace=trace) File “/Users/canyon/.miniconda3/envs/bayestutorial/lib/python3.6/site-packages/arviz/data/io_pymc3.py”, line 152, in from_pymc3 dims=dims, File “/Users/canyon/.miniconda3/envs/bayestutorial/lib/python3.6/site-packages/arviz/data/io_pymc3.py”, line 137, in to_inference_data “sample_stats”: self.sample_stats_to_xarray(), File “/Users/canyon/.miniconda3/envs/bayestutorial/lib/python3.6/site-packages/arviz/data/base.py”, line 25, in wrapped return func(cls, *args, **kwargs) File “/Users/canyon/.miniconda3/envs/bayestutorial/lib/python3.6/site-packages/arviz/data/io_pymc3.py”, line 78, in sample_stats_to_xarray log_likelihood, dims = self._extract_log_likelihood() File “/Users/canyon/.miniconda3/envs/bayestutorial/lib/python3.6/site-packages/arviz/data/base.py”, line 25, in wrapped return func(cls, *args, **kwargs) File “/Users/canyon/.miniconda3/envs/bayestutorial/lib/python3.6/site-packages/arviz/data/io_pymc3.py”, line 56, in _extract_log_likelihood chain_likelihoods.append(np.stack(log_like)) File “/Users/canyon/.miniconda3/envs/bayestutorial/lib/python3.6/site-packages/numpy/core/shape_base.py”, line 410, in stack arrays = [asanyarray(arr) for arr in arrays] File “/Users/canyon/.miniconda3/envs/bayestutorial/lib/python3.6/site-packages/numpy/core/shape_base.py”, line 410, in <listcomp> arrays = [asanyarray(arr) for arr in arrays] File “/Users/canyon/.miniconda3/envs/bayestutorial/lib/python3.6/site-packages/arviz/data/io_pymc3.py”, line 55, in <genexpr> log_like = (log_likelihood_vals_point(point) for point in self.trace.points([chain])) File “/Users/canyon/.miniconda3/envs/bayestutorial/lib/python3.6/site-packages/arviz/data/io_pymc3.py”, line 51, in log_likelihood_vals_point return np.concatenate(log_like_vals) ValueError: zero-dimensional arrays cannot be concatenated

Expected behavior An Inferencedata object should be created Additional context

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
canyon289commented, Jun 5, 2019

Tutorial has pinned version? No matter, I’ll just fix and move on with life. Thanks @ahartikainen

1reaction
ahartikainencommented, Jun 5, 2019

In this func

    def log_likelihood_vals_point(point):
            """Compute log likelihood for each observed point."""
            log_like_vals = []
            for var, log_like in cached:
                log_like_val = log_like(point)
                if var.missing_values:
                    log_like_val = log_like_val[~var.observations.mask]
                log_like_vals.append(log_like_val)
            return np.concatenate(log_like_vals)

log_like(point) returns a 0-dim array (np.array(num)), which can’t be concatenated.

One option is to wrap that to np.atleast_1d(log_like(point))

Read more comments on GitHub >

github_iconTop Results From Across the Web

Multiprocessing fails when sampling multiple chains using ...
Since PR #3011 I have been having troubles sampling multiple chains with multiple cores. In Jupyter notebook I get random kernel shutdowns ...
Read more >
Getting started with PyMC3 - Learn PyMC & Bayesian modeling
The lack of a domain specific language allows for great flexibility and direct interaction with the model. This paper is a tutorial-style introduction...
Read more >
Probabilistic Programming in Python using PyMC - arXiv
In [3]: from pymc3 import Model, Normal, HalfNormal ... of values that, given the satisfaction of certain conditions, are indistinguishable.
Read more >
Harvard CS109B | Lecture 13 - Hierarchical Models (Lab)
Multiple chains initialized from different initial conditions should give similar results. If all chains converge to the same equilibrium, ˆR will be 1....
Read more >
PyMC3/Arviz: CDF value from trace - python xarray
I have a sample from PyMC3 and I'm trying to get a cumulative probability from ... Is there a better way to do...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found