Error during run sampling method

Dear experts
I am practicing with pymc3. Now I am carrying out sampling method in pymc3 with the code following.

import pymc3 as pm

model = pm.Model()
with model:
mu1 = pm.Normal (“mu1”,mu=0,sd=1,shape=10)
with model:
trace =pm.sample(2000)

When I run the this code, it has appeared the error :slight_smile:

Auto-assigning NUTS sampler…
Initializing NUTS using jitter+adapt_diag…
Multiprocess sampling (4 chains in 4 jobs)
NUTS: [mu1]
Traceback (most recent call last):
File “”, line 1, in
File “C:\Users\hoaithanh\Anaconda3\lib\multiprocessing\spawn.py”, line 105, in spawn_main
exitcode = _main(fd)
File “C:\Users\hoaithanh\Anaconda3\lib\multiprocessing\spawn.py”, line 114, in _main
Traceback (most recent call last):
File “C:/Users/hoaithanh/Desktop/hocpython/thuproject.py”, line 12, in
prepare(preparation_data)
File “C:\Users\hoaithanh\Anaconda3\lib\multiprocessing\spawn.py”, line 225, in prepare
trace = pm.sample(2000)
File “C:\Users\hoaithanh\Anaconda3\lib\site-packages\pymc3\sampling.py”, line 449, in sample
_fixup_main_from_path(data[‘init_main_from_path’])
File “C:\Users\hoaithanh\Anaconda3\lib\multiprocessing\spawn.py”, line 277, in _fixup_main_from_path
trace = _mp_sample(**sample_args)
File “C:\Users\hoaithanh\Anaconda3\lib\site-packages\pymc3\sampling.py”, line 996, in _mp_sample
run_name=“mp_main”)
File “C:\Users\hoaithanh\Anaconda3\lib\runpy.py”, line 263, in run_path
chain, progressbar)
File “C:\Users\hoaithanh\Anaconda3\lib\site-packages\pymc3\parallel_sampling.py”, line 275, in init
pkg_name=pkg_name, script_name=fname)
File “C:\Users\hoaithanh\Anaconda3\lib\runpy.py”, line 96, in _run_module_code
for chain, seed, start in zip(range(chains), seeds, start_points)
File “C:\Users\hoaithanh\Anaconda3\lib\site-packages\pymc3\parallel_sampling.py”, line 275, in
mod_name, mod_spec, pkg_name, script_name)
File “C:\Users\hoaithanh\Anaconda3\lib\runpy.py”, line 85, in _run_code
for chain, seed, start in zip(range(chains), seeds, start_points)
File “C:\Users\hoaithanh\Anaconda3\lib\site-packages\pymc3\parallel_sampling.py”, line 182, in init
exec(code, run_globals)
File “C:\Users\hoaithanh\Desktop\hocpython\thuproject.py”, line 12, in
self._process.start()
File “C:\Users\hoaithanh\Anaconda3\lib\multiprocessing\process.py”, line 105, in start
trace = pm.sample(2000)
self._popen = self._Popen(self) File “C:\Users\hoaithanh\Anaconda3\lib\site-packages\pymc3\sampling.py”, line 449, in sample

File “C:\Users\hoaithanh\Anaconda3\lib\multiprocessing\context.py”, line 223, in _Popen
return _default_context.get_context().Process._Popen(process_obj)
File “C:\Users\hoaithanh\Anaconda3\lib\multiprocessing\context.py”, line 322, in _Popen
return Popen(process_obj)
File “C:\Users\hoaithanh\Anaconda3\lib\multiprocessing\popen_spawn_win32.py”, line 65, in init
trace = _mp_sample(**sample_args)
File “C:\Users\hoaithanh\Anaconda3\lib\site-packages\pymc3\sampling.py”, line 996, in _mp_sample
reduction.dump(process_obj, to_child)
File “C:\Users\hoaithanh\Anaconda3\lib\multiprocessing\reduction.py”, line 60, in dump
ForkingPickler(file, protocol).dump(obj)
BrokenPipeError: [Errno 32] Broken pipe
chain, progressbar)
File “C:\Users\hoaithanh\Anaconda3\lib\site-packages\pymc3\parallel_sampling.py”, line 275, in init
for chain, seed, start in zip(range(chains), seeds, start_points)
File “C:\Users\hoaithanh\Anaconda3\lib\site-packages\pymc3\parallel_sampling.py”, line 275, in
for chain, seed, start in zip(range(chains), seeds, start_points)
File “C:\Users\hoaithanh\Anaconda3\lib\site-packages\pymc3\parallel_sampling.py”, line 182, in init
self._process.start()
File “C:\Users\hoaithanh\Anaconda3\lib\multiprocessing\process.py”, line 105, in start
self._popen = self._Popen(self)
File “C:\Users\hoaithanh\Anaconda3\lib\multiprocessing\context.py”, line 223, in _Popen
return _default_context.get_context().Process._Popen(process_obj)
File “C:\Users\hoaithanh\Anaconda3\lib\multiprocessing\context.py”, line 322, in _Popen
return Popen(process_obj)
File “C:\Users\hoaithanh\Anaconda3\lib\multiprocessing\popen_spawn_win32.py”, line 33, in init
prep_data = spawn.get_preparation_data(process_obj._name)
File “C:\Users\hoaithanh\Anaconda3\lib\multiprocessing\spawn.py”, line 143, in get_preparation_data
_check_not_importing_main()
File “C:\Users\hoaithanh\Anaconda3\lib\multiprocessing\spawn.py”, line 136, in _check_not_importing_main
is not going to be frozen to produce an executable.’’’)
RuntimeError:
An attempt has been made to start a new process before the
current process has finished its bootstrapping phase.

    This probably means that you are not using fork to start your
    child processes and you have forgotten to use the proper idiom
    in the main module:

        if __name__ == '__main__':
            freeze_support()
            ...

    The "freeze_support()" line can be omitted if the program
    is not going to be frozen to produce an executable.

Process finished with exit code 1

I want to konw what is this error? and how to fix it ? I also try to run the code in the getting started guide but it too.
please help me !!!
Thanks you

There is some problem with multi-cores sampling under windows, we are currently working on it in https://github.com/pymc-devs/pymc3/issues/3140

For now, you will have to set cores=1 to make sampling works: trace = pm.sample(2000, cores=1)

thanks you replied for me.

I have just run again with the other guide in the tutorial code which it is available in the help of pymc3. but it is also same as error

import pymc3 as pm
n=100
h=61
alpha=2
beta=2
with pm.Model() as model:

thanks you replied for me.

I have just run again with the other guide in the tutorial code which it is available in the help of pymc3. but it is also same as error

import pymc3 as pm
n=100
h=61
alpha=2
beta=2
with pm.Model() as model:
p=pm.Beta(‘p’,alpha=alpha,beta=beta)
y= pm.Binomial(‘y’,n=n.p=p,observed=h)
trace = pm.sample (2000,tune=1000, cores=4)

it appears similar error with above the code.

I have try to run with cores=1. it worked. But how to run it with multiple process?

To sample with more than one core you have to put, at least the sample call inside an if __name__ == "__main__": statement. Like this:

import pymc3 as pm
n=100
h=61
alpha=2
beta=2
with pm.Model() as model:
    p=pm.Beta(‘p’,alpha=alpha,beta=beta)
    y= pm.Binomial(‘y’,n=n.p=p,observed=h)

if __name__ == "__main__":
    with model:
        trace = pm.sample (2000,tune=1000, cores=4)

The problem happens because of how python does the multiprocessing in windows. It basically creates a new process and attempts to run the __main__ module almost from scratch. This multiprocessing method is called spawning. The problem is that the __main__ module needs to have a statement that can only be executed by the process that tries to spawn all the rest, and the spawned children should never be able to run said part of the __main__ module. That is why the if __name__ == "__main__": statement is required on windows when you run the code as a batch script (running it on a jupyter notebook does work).

2 Likes