Sample runtimeerror when cores>1

If this is just a normal python script (and not a notebook), you need to protect your final code using the standard if __name__ == '__main__'. We are using the multiprocessing module from the std lib, and this will reimport (“execute”) your whole script again, but with __name__ set to a different value. If that worker then also executes the pm.sample() function, it would again start worker processes, which would exectue the script…

So your script should look something like this:

import pymc3 as pm

def main():
    # load_data
    radon = pd.read_csv('C:/Users\Craig/source/repos/pymc_test/radon.csv', index_col=0)
    radon_hennepin = radon.query('county=="HENNEPIN"').log_radon
    print(radon_hennepin.head())
    
    # build and sample model
    with Model() as radon_model:
        mu = Normal('mu',mu=0, sd=10)
        sigma = Uniform('sigma', 0, 10)

    with radon_model:
        dist = Normal('dist', mu=mu, sigma=sigma, observed=radon_hennepin)

    with radon_model:
        samples = sample(1000, tune=1000, cores=4, random_seed=42)

if __name__ == '__main__':
    main()

PS: It is best practice to use import pymc3 as pm instead of importing individual names.

3 Likes