pymc-devs / pymc-resources

PyMC educational resources
MIT License
1.95k stars 743 forks source link

Trouble importing pymc3 #151

Closed JaimeArboleda closed 3 years ago

JaimeArboleda commented 3 years ago

First of all, thanks for this wonderful package and for the effort of adapting the R code in those bayesian books to Python.

I am trying to use the Rethinking_2 code examples. I have cloned the full repo and then I have followed the instructions:

conda env create -f environment.yml
source activate stat-rethink2-pymc3

But then, when opening the jupyter notebook of the second chapter, the first cell fails when trying to to the imports:

import arviz as az
import matplotlib.pyplot as plt
import numpy as np
import pymc3 as pm
import scipy.stats as stats

I have the following error:

WARNING (aesara.configdefaults): g++ not available, if using conda: `conda install m2w64-toolchain`
WARNING (aesara.configdefaults): g++ not detected ! Aesara will be unable to execute optimized C-implementations (for both CPU and GPU) and will default to Python implementations. Performance will be severely degraded. To remove this warning, set Aesara flags cxx to an empty string.
WARNING (aesara.tensor.blas): Using NumPy C-API based implementation for BLAS functions.
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-75554ce6e5eb> in <module>
      2 import matplotlib.pyplot as plt
      3 import numpy as np
----> 4 import pymc3 as pm
      5 import scipy.stats as stats

D:\Python\envs\stat-rethink2-pymc3\lib\site-packages\pymc3\__init__.py in <module>
     39 __set_compiler_flags()
     40 
---> 41 from pymc3 import gp, ode, sampling
     42 from pymc3.aesaraf import *
     43 from pymc3.backends import load_trace, save_trace

D:\Python\envs\stat-rethink2-pymc3\lib\site-packages\pymc3\gp\__init__.py in <module>
     14 
     15 from pymc3.gp import cov, mean, util
---> 16 from pymc3.gp.gp import TP, Latent, LatentKron, Marginal, MarginalKron, MarginalSparse

D:\Python\envs\stat-rethink2-pymc3\lib\site-packages\pymc3\gp\gp.py in <module>
     23 import pymc3 as pm
     24 
---> 25 from pymc3.distributions import draw_values
     26 from pymc3.gp.cov import Constant, Covariance
     27 from pymc3.gp.mean import Zero

D:\Python\envs\stat-rethink2-pymc3\lib\site-packages\pymc3\distributions\__init__.py in <module>
     13 #   limitations under the License.
     14 
---> 15 from pymc3.distributions import shape_utils, timeseries, transforms
     16 from pymc3.distributions.bart import BART
     17 from pymc3.distributions.bound import Bound

D:\Python\envs\stat-rethink2-pymc3\lib\site-packages\pymc3\distributions\timeseries.py in <module>
     19 from scipy import stats
     20 
---> 21 from pymc3.distributions import distribution, multivariate
     22 from pymc3.distributions.continuous import Flat, Normal, get_tau_sigma
     23 from pymc3.distributions.shape_utils import to_tuple

D:\Python\envs\stat-rethink2-pymc3\lib\site-packages\pymc3\distributions\distribution.py in <module>
     45     to_tuple,
     46 )
---> 47 from pymc3.model import (
     48     ContextMeta,
     49     FreeRV,

D:\Python\envs\stat-rethink2-pymc3\lib\site-packages\pymc3\model.py in <module>
     41 from pymc3.blocking import ArrayOrdering, DictToArrayBijection
     42 from pymc3.exceptions import ImputationWarning
---> 43 from pymc3.math import flatten_list
     44 from pymc3.util import WithMemoization, get_transformed_name, get_var_name, hash_key
     45 from pymc3.vartypes import continuous_types, discrete_types, isgenerator, typefilter

D:\Python\envs\stat-rethink2-pymc3\lib\site-packages\pymc3\math.py in <module>
     79 
     80 from aesara.tensor.nlinalg import det, matrix_dot, matrix_inverse, trace
---> 81 from aesara.tensor.nnet import sigmoid
     82 from scipy.linalg import block_diag as scipy_block_diag
     83 

ImportError: cannot import name 'sigmoid' from 'aesara.tensor.nnet' (D:\Python\envs\stat-rethink2-pymc3\lib\site-packages\aesara\tensor\nnet\__init__.py)

I don't know what should I do. Thanks in advance.

harcel commented 3 years ago

Hi @JaimeArboleda!

I can reproduce that behavior. It is because the pymc3 version referenced (pip installed from github) wants to use aesara, rather than theano-pymc (which is probably what you should want to use for another while). Installing the current (3.11.2) version from conda-forge would be the quickest way to get a working environment for now.

If it helps, I copy-paste a parameter file below that works for me, that you can use instead of the existing environment.yml, which would probably work. If desired, I can make a pull request with this environment file, but it switches conda channel too and removes stuff from pip to conda, which I am not sure maintainers want... Hope this helps either way!

Note that you need to remove your environment first (conda env remove -n stat-rethink2-pymc3) and then create a new one the same way as before.

name: stat-rethink2-pymc3
channels:
- conda-forge
dependencies:
  - jupyter
  - seaborn
  - mkl-service
  - watermark
  - pymc3
  - arviz
  - pip
  - pip:
     - causalgraphicalmodels
     - daft
JaimeArboleda commented 3 years ago

Thank you so much harcel!!!!!! It worked perfectly! :)