1

I have the following PyMC3 code in Python, which are originated from here. In the last line, I got the error RuntimeError: An attempt has been made to start a new process before the current process has finished its bootstrapping phase.

I believe this error is similar to the error in this post. But I don't really understand how to fix the code. I tried to put from distributed import Client or from dask.distributed import progress at the front of my code, but my code still failed. I would just simply get ModuleNotFoundError: No module named 'distributed' or ModuleNotFoundError: No module named 'dask' if I tried to apply those fixes.

How can I get my code work? Thanks.

import numpy as np
import pymc3 as pm3
from scipy.stats import norm


sigma = 3 # Note this is the std of our data
data = norm(10,sigma).rvs(100)
mu_prior = 8
sigma_prior = 1.5  # Note this is our prior on the std of mu

basic_model = pm3.Model()

with basic_model:
    # Priors for unknown model parameters
    mu = pm3.Normal('Mean of Data',mu_prior,sigma_prior)
    
    # Likelihood (sampling distribution) of observations
    data_in = pm3.Normal('Y_obs', mu=mu, sd=sigma, observed=data)

chain_length = 10000 

with basic_model:
    
    # obtain starting values via MAP
    startvals = pm3.find_MAP(model=basic_model)
    
    print(startvals)
    # instantiate sampler
    step = pm3.Metropolis() 

    # draw 5000 posterior samples
    trace = pm3.sample(chain_length, step=step, start=startvals) 

Updates 1

If I added cores=1 to pm3.sample() at the last line (i.e. suggested by merv in the comment), the error will disappear. And then if I tried to draw the trace plot by pm3.plot_trace(trace,figsize=(20,5)), I will get the warning Got error No model on context stack. trying to find log_likelihood in translation but I will also obtain the trace-plot which looks well. But I have no idea why cores=1 works while cores=2 doesn't.

Besides, if I checked the version:

print(sys.version)
print(pymc3.__version__)
print(theano.__version__)
print(arviz.__version__)

I got

3.9.7 (default, Sep 16 2021, 16:59:28) [MSC v.1916 64 bit (AMD64)]
3.11.4
1.1.2
0.11.4

The Python codes are run in conda environment and my conda version is 4.10.3, installed in my Windows 10 Education version 21H1 (OS build 19043.1348).


Updates 2

Below is what I got after entering my conda environment pymc_env and run conda env export:

channels:
  - conda-forge
  - http://conda.anaconda.org/gurobi
  - defaults
dependencies:
  - alabaster=0.7.12=pyhd3eb1b0_0
  - appdirs=1.4.4=pyhd3eb1b0_0
  - argh=0.26.2=py39haa95532_0
  - arrow=0.13.1=py39haa95532_0
  - astroid=2.6.6=py39haa95532_0
  - async_generator=1.10=pyhd3eb1b0_0
  - atomicwrites=1.4.0=py_0
  - attrs=21.2.0=pyhd3eb1b0_0
  - autopep8=1.5.7=pyhd3eb1b0_0
  - babel=2.9.1=pyhd3eb1b0_0
  - backcall=0.2.0=pyhd3eb1b0_0
  - bcrypt=3.2.0=py39h196d8e1_0
  - binaryornot=0.4.4=pyhd3eb1b0_1
  - black=19.10b0=py_0
  - bleach=4.0.0=pyhd3eb1b0_0
  - brotlipy=0.7.0=py39h2bbff1b_1003
  - ca-certificates=2021.10.26=haa95532_2
  - certifi=2021.10.8=py39haa95532_0
  - cffi=1.14.6=py39h2bbff1b_0
  - chardet=4.0.0=py39haa95532_1003
  - charset-normalizer=2.0.4=pyhd3eb1b0_0
  - click=8.0.3=pyhd3eb1b0_0
  - cloudpickle=2.0.0=pyhd3eb1b0_0
  - colorama=0.4.4=pyhd3eb1b0_0
  - cookiecutter=1.7.2=pyhd3eb1b0_0
  - cryptography=35.0.0=py39h71e12ea_0
  - debugpy=1.4.1=py39hd77b12b_0
  - decorator=5.1.0=pyhd3eb1b0_0
  - defusedxml=0.7.1=pyhd3eb1b0_0
  - diff-match-patch=20200713=pyhd3eb1b0_0
  - docutils=0.17.1=py39haa95532_1
  - entrypoints=0.3=py39haa95532_0
  - expat=2.4.1=h39d44d4_0
  - flake8=3.9.2=pyhd3eb1b0_0
  - font-ttf-dejavu-sans-mono=2.37=hab24e00_0
  - font-ttf-inconsolata=3.000=h77eed37_0
  - font-ttf-source-code-pro=2.038=h77eed37_0
  - font-ttf-ubuntu=0.83=hab24e00_0
  - fontconfig=2.13.1=h1989441_1005
  - fonts-conda-ecosystem=1=0
  - fonts-conda-forge=1=0
  - freetype=2.10.4=h546665d_1
  - fribidi=1.0.10=h8d14728_0
  - getopt-win32=0.1=h8ffe710_0
  - gettext=0.19.8.1=ha2e2712_1008
  - graphite2=1.3.13=1000
  - graphviz=2.38=hfd603c8_2
  - gts=0.7.6=h7c369d9_2
  - icu=58.2=ha925a31_3
  - idna=3.2=pyhd3eb1b0_0
  - imagesize=1.2.0=pyhd3eb1b0_0
  - importlib-metadata=4.8.1=py39haa95532_0
  - importlib_metadata=4.8.1=hd3eb1b0_0
  - inflection=0.5.1=py39haa95532_0
  - intel-openmp=2021.4.0=h57928b3_3556
  - intervaltree=3.1.0=pyhd3eb1b0_0
  - ipykernel=6.4.1=py39haa95532_1
  - ipython=7.29.0=py39hd4e2768_0
  - ipython_genutils=0.2.0=pyhd3eb1b0_1
  - isort=5.9.3=pyhd3eb1b0_0
  - jbig=2.1=h8d14728_2003
  - jedi=0.18.0=py39haa95532_1
  - jinja2=2.11.3=pyhd3eb1b0_0
  - jinja2-time=0.2.0=pyhd3eb1b0_2
  - jpeg=9d=h8ffe710_0
  - jsonschema=3.2.0=pyhd3eb1b0_2
  - jupyter_client=6.1.12=pyhd3eb1b0_0
  - jupyter_core=4.8.1=py39haa95532_0
  - jupyterlab_pygments=0.1.2=py_0
  - keyring=23.1.0=py39haa95532_0
  - lazy-object-proxy=1.6.0=py39h2bbff1b_0
  - lerc=3.0=h0e60522_0
  - libblas=3.9.0=12_win64_mkl
  - libcblas=3.9.0=12_win64_mkl
  - libdeflate=1.8=h8ffe710_0
  - libffi=3.4.2=h0e60522_4
  - libglib=2.70.0=h3be07f2_1
  - libiconv=1.16=he774522_0
  - liblapack=3.9.0=12_win64_mkl
  - libpng=1.6.37=h1d00b33_2
  - libpython=2.1=py39hcbf5309_1
  - libspatialindex=1.9.3=h6c2663c_0
  - libtiff=4.3.0=hd413186_2
  - libwebp=1.2.1=h57928b3_0
  - libwebp-base=1.2.1=h8ffe710_0
  - libxcb=1.13=hcd874cb_1003
  - libxml2=2.9.12=hf5bbc77_1
  - libzlib=1.2.11=h8ffe710_1013
  - llvmlite=0.37.0=py39ha0cd8c8_0
  - lz4-c=1.9.3=h8ffe710_1
  - m2w64-binutils=2.25.1=5
  - m2w64-bzip2=1.0.6=6
  - m2w64-crt-git=5.0.0.4636.2595836=2
  - m2w64-gcc=5.3.0=6
  - m2w64-gcc-ada=5.3.0=6
  - m2w64-gcc-fortran=5.3.0=6
  - m2w64-gcc-libgfortran=5.3.0=6
  - m2w64-gcc-libs=5.3.0=7
  - m2w64-gcc-libs-core=5.3.0=7
  - m2w64-gcc-objc=5.3.0=6
  - m2w64-gmp=6.1.0=2
  - m2w64-headers-git=5.0.0.4636.c0ad18a=2
  - m2w64-isl=0.16.1=2
  - m2w64-libiconv=1.14=6
  - m2w64-libmangle-git=5.0.0.4509.2e5a9a2=2
  - m2w64-libwinpthread-git=5.0.0.4634.697f757=2
  - m2w64-make=4.1.2351.a80a8b8=2
  - m2w64-mpc=1.0.3=3
  - m2w64-mpfr=3.1.4=4
  - m2w64-pkg-config=0.29.1=2
  - m2w64-toolchain=5.3.0=7
  - m2w64-tools-git=5.0.0.4592.90b8472=2
  - m2w64-windows-default-manifest=6.4=3
  - m2w64-winpthreads-git=5.0.0.4634.697f757=2
  - m2w64-zlib=1.2.8=10
  - markupsafe=1.1.1=py39h2bbff1b_0
  - matplotlib-inline=0.1.2=pyhd3eb1b0_2
  - mccabe=0.6.1=py39haa95532_1
  - mistune=0.8.4=py39h2bbff1b_1000
  - mkl=2021.4.0=h0e2418a_729
  - mkl-service=2.4.0=py39hb82d6ee_0
  - msys2-conda-epoch=20160418=1
  - mypy_extensions=0.4.3=py39haa95532_0
  - nbclient=0.5.3=pyhd3eb1b0_0
  - nbconvert=6.1.0=py39haa95532_0
  - nbformat=5.1.3=pyhd3eb1b0_0
  - nest-asyncio=1.5.1=pyhd3eb1b0_0
  - numba=0.54.1=py39hb8cd55e_0
  - numpy=1.20.3=py39h6635163_1
  - numpydoc=1.1.0=pyhd3eb1b0_1
  - openssl=1.1.1l=h2bbff1b_0
  - pandocfilters=1.4.3=py39haa95532_1
  - paramiko=2.7.2=py_0
  - parso=0.8.2=pyhd3eb1b0_0
  - pathspec=0.7.0=py_0
  - pcre=8.45=h0e60522_0
  - pexpect=4.8.0=pyhd3eb1b0_3
  - pickleshare=0.7.5=pyhd3eb1b0_1003
  - pip=21.3.1=pyhd8ed1ab_0
  - pixman=0.40.0=h8ffe710_0
  - pluggy=1.0.0=py39haa95532_0
  - poyo=0.5.0=pyhd3eb1b0_0
  - prompt-toolkit=3.0.20=pyhd3eb1b0_0
  - psutil=5.8.0=py39h2bbff1b_1
  - pthread-stubs=0.4=hcd874cb_1001
  - ptyprocess=0.7.0=pyhd3eb1b0_2
  - pycodestyle=2.7.0=pyhd3eb1b0_0
  - pycparser=2.20=py_2
  - pydocstyle=6.1.1=pyhd3eb1b0_0
  - pyflakes=2.3.1=pyhd3eb1b0_0
  - pygments=2.10.0=pyhd3eb1b0_0
  - pylint=2.9.6=py39haa95532_1
  - pyls-spyder=0.4.0=pyhd3eb1b0_0
  - pynacl=1.4.0=py39hbd8134f_1
  - pyopenssl=21.0.0=pyhd3eb1b0_1
  - pyqt=5.9.2=py39hd77b12b_6
  - pyrsistent=0.18.0=py39h196d8e1_0
  - pysocks=1.7.1=py39haa95532_0
  - python=3.9.7=h6244533_1
  - python-dateutil=2.8.2=pyhd3eb1b0_0
  - python-graphviz=0.16=pyhd3eb1b0_1
  - python-lsp-black=1.0.0=pyhd3eb1b0_0
  - python-lsp-jsonrpc=1.0.0=pyhd3eb1b0_0
  - python-lsp-server=1.2.4=pyhd3eb1b0_0
  - python-slugify=5.0.2=pyhd3eb1b0_0
  - python_abi=3.9=2_cp39
  - pywin32=228=py39hbaba5e8_1
  - pywin32-ctypes=0.2.0=py39haa95532_1000
  - pyyaml=6.0=py39h2bbff1b_1
  - pyzmq=22.2.1=py39hd77b12b_1
  - qdarkstyle=3.0.2=pyhd3eb1b0_0
  - qstylizer=0.1.10=pyhd3eb1b0_0
  - qt=5.9.7=vc14h73c81de_0
  - qtawesome=1.0.2=pyhd3eb1b0_0
  - qtconsole=5.1.1=pyhd3eb1b0_0
  - qtpy=1.10.0=pyhd3eb1b0_0
  - regex=2021.8.3=py39h2bbff1b_0
  - requests=2.26.0=pyhd3eb1b0_0
  - rope=0.19.0=pyhd3eb1b0_0
  - rtree=0.9.7=py39h2eaa2aa_1
  - scipy=1.7.1=py39hc0c34ad_0
  - setuptools=58.5.3=py39hcbf5309_0
  - sip=4.19.13=py39hd77b12b_0
  - six=1.16.0=pyh6c4a22f_0
  - snowballstemmer=2.1.0=pyhd3eb1b0_0
  - sortedcontainers=2.4.0=pyhd3eb1b0_0
  - sphinx=4.2.0=pyhd3eb1b0_0
  - sphinxcontrib-applehelp=1.0.2=pyhd3eb1b0_0
  - sphinxcontrib-devhelp=1.0.2=pyhd3eb1b0_0
  - sphinxcontrib-htmlhelp=2.0.0=pyhd3eb1b0_0
  - sphinxcontrib-jsmath=1.0.1=pyhd3eb1b0_0
  - sphinxcontrib-qthelp=1.0.3=pyhd3eb1b0_0
  - sphinxcontrib-serializinghtml=1.1.5=pyhd3eb1b0_0
  - spyder=5.1.5=py39haa95532_0
  - spyder-kernels=2.1.3=py39haa95532_0
  - sqlite=3.36.0=h8ffe710_2
  - tbb=2021.4.0=h2d74725_1
  - testpath=0.5.0=pyhd3eb1b0_0
  - text-unidecode=1.3=pyhd3eb1b0_0
  - textdistance=4.2.1=pyhd3eb1b0_0
  - three-merge=0.1.1=pyhd3eb1b0_0
  - tinycss=0.4=pyhd3eb1b0_1002
  - toml=0.10.2=pyhd3eb1b0_0
  - tornado=6.1=py39h2bbff1b_0
  - traitlets=5.1.0=pyhd3eb1b0_0
  - typed-ast=1.4.3=py39h2bbff1b_1
  - typing_extensions=3.10.0.2=pyh06a4308_0
  - tzdata=2021e=he74cb21_0
  - ucrt=10.0.20348.0=h57928b3_0
  - ujson=4.0.2=py39hd77b12b_0
  - unidecode=1.2.0=pyhd3eb1b0_0
  - urllib3=1.26.7=pyhd3eb1b0_0
  - vc=14.2=hb210afc_5
  - vs2015_runtime=14.29.30037=h902a5da_5
  - watchdog=2.1.3=py39haa95532_0
  - wcwidth=0.2.5=pyhd3eb1b0_0
  - webencodings=0.5.1=py39haa95532_1
  - wheel=0.37.0=pyhd8ed1ab_1
  - whichcraft=0.6.1=pyhd3eb1b0_0
  - win_inet_pton=1.1.0=py39haa95532_0
  - wrapt=1.12.1=py39h196d8e1_1
  - xorg-kbproto=1.0.7=hcd874cb_1002
  - xorg-libice=1.0.10=hcd874cb_0
  - xorg-libsm=1.2.3=hcd874cb_1000
  - xorg-libx11=1.7.2=hcd874cb_0
  - xorg-libxau=1.0.9=hcd874cb_0
  - xorg-libxdmcp=1.1.3=hcd874cb_0
  - xorg-libxext=1.3.4=hcd874cb_1
  - xorg-libxpm=3.5.13=hcd874cb_0
  - xorg-libxt=1.2.1=hcd874cb_2
  - xorg-xextproto=7.3.0=hcd874cb_1002
  - xorg-xproto=7.0.31=hcd874cb_1007
  - xz=5.2.5=h62dcd97_1
  - yaml=0.2.5=he774522_0
  - yapf=0.31.0=pyhd3eb1b0_0
  - zipp=3.6.0=pyhd3eb1b0_0
  - zlib=1.2.11=h8ffe710_1013
  - zstd=1.5.0=h6255e5f_0
  - pip:
    - arviz==0.11.4
    - cachetools==4.2.4
    - cftime==1.5.1.1
    - cycler==0.11.0
    - dill==0.3.4
    - fastprogress==1.0.0
    - filelock==3.3.2
    - kiwisolver==1.3.2
    - matplotlib==3.4.3
    - netcdf4==1.5.7
    - packaging==21.2
    - pandas==1.3.4
    - patsy==0.5.2
    - pillow==8.4.0
    - pymc3==3.11.4
    - pyparsing==2.4.7
    - pytz==2021.3
    - semver==2.13.0
    - theano-pymc==1.1.2
    - xarray==0.20.1
H42
  • 725
  • 2
  • 9
  • 28
  • Cannot replicate. Please add versions of Python, PyMC3, Theano, and ArViZ to the question. Also include OS. What happens if you set a `cores=1` argument in the `pm3.sample` call? – merv Nov 09 '21 at 21:09
  • @merv Thanks for your reply. `cores=1` solve the problems, though I still get a warning. So what really happened here? I also update my post to include the version information. – H42 Nov 13 '21 at 10:52
  • The `cores=1` is a test, not a solution. It indicates something is wrong with how the subprocesses are launched (PyMC3 defaults to using multiple cores). I don’t know specifically, and don’t run Windows to test myself, but I’d guess something is off with either the PyMC3 or Theano builds you are using. Maybe drop an issue on the PyMC3 repository. BTW, the `dask` stuff is only relevant to the other question - just that it was having a similar error. – merv Nov 13 '21 at 14:38
  • @merv If I found some platform that can run my code without error (e.g. say Google Colab) and I copied the version of libraries from that platform. After that I build another conda environment in my Windows using the exact version of those libraries, do you think it will probably solve my problem? – H42 Nov 13 '21 at 18:25
  • Not necessarily, because you are on Windows and Colab is Linux, and these have completely different mechanisms for creating subprocesses. If you want exact help, add a YAML file of the environment (`conda env export`) to the question. – merv Nov 17 '21 at 02:16
  • @merv Thanks for your reply. I added the information as discussed. – H42 Nov 17 '21 at 11:18
  • Start simpler: recreate with just `conda create -n foo pymc3` and try your code. – merv Nov 17 '21 at 11:36

0 Answers0