pythoncondapymc3

Problems with theano when trying to use pymc3


I have been trying to use the pymc3 package but have constantly been receiving errors. First off, when I import the pymc3 package, here is what happens:

import pymc3 as pm

WARNING (theano.configdefaults): g++ not detected ! Theano 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 Theano flags cxx to an empty string. WARNING (theano.tensor.blas): Using NumPy C-API based implementation for BLAS functions.

Afterwards, here is my code:

x = np.linspace(-5,5, 50)  # Wavelength data. Here we have fifty points between -5 and 5
sigma = 1
mu = 0
A = 20
B = 100
# define underlying model -- Gaussian
y = A * np.exp( - (x - mu)**2 / (2 * sigma**2)) + B
y_noise = np.random.normal(0, 1, 50)  # Let's add some noise
data = y+y_noise
# Set model
basic_model = pm.Model()
with basic_model:
    # Priors for unknown model parameters
    A = pm.Uniform("A", lower=0, upper=50)
    B = pm.Uniform("B", lower=0, upper=200)
    sigma = 1
    # Expected value of outcome
    y_m = A*np.exp(-(x)**2/2)+B
# Likelihood  of observations
    Y_obs = pm.Normal("Y_obs", mu=mu, sigma=np.sqrt(data), observed=data)
# Now sample
with basic_model:
    # draw posterior samples
    trace = pm.sample_smc(100, parallel=True)

And here is the error output:

RemoteTraceback Traceback (most recent call last) RemoteTraceback: """ Traceback (most recent call last): File "/home/osgrinds/anaconda3/envs/pymc3Env/lib/python3.10/multiprocessing/pool.py", line 125, in worker result = (True, func(*args, **kwds)) File "/home/osgrinds/anaconda3/envs/pymc3Env/lib/python3.10/multiprocessing/pool.py", line 51, in starmapstar return list(itertools.starmap(args[0], args[1])) File "/home/osgrinds/anaconda3/envs/pymc3Env/lib/python3.10/site-packages/pymc3/smc/sample_smc.py", line 267, in sample_smc_int smc.setup_kernel() File "/home/osgrinds/anaconda3/envs/pymc3Env/lib/python3.10/site-packages/pymc3/smc/smc.py", line 135, in setup_kernel self.likelihood_logp_func = logp_forw([self.model.datalogpt], self.variables, shared) File "/home/osgrinds/anaconda3/envs/pymc3Env/lib/python3.10/site-packages/pymc3/smc/smc.py", line 288, in logp_forw f = theano_function([inarray0], out_list[0]) File "/home/osgrinds/anaconda3/envs/pymc3Env/lib/python3.10/site-packages/theano/compile/function/init.py", line 337, in function fn = pfunc( File "/home/osgrinds/anaconda3/envs/pymc3Env/lib/python3.10/site-packages/theano/compile/function/pfunc.py", line 524, in pfunc return orig_function( File "/home/osgrinds/anaconda3/envs/pymc3Env/lib/python3.10/site-packages/theano/compile/function/types.py", line 1970, in orig_function m = Maker( File "/home/osgrinds/anaconda3/envs/pymc3Env/lib/python3.10/site-packages/theano/compile/function/types.py", line 1573, in init self._check_unused_inputs(inputs, outputs, on_unused_input) File "/home/osgrinds/anaconda3/envs/pymc3Env/lib/python3.10/site-packages/theano/compile/function/types.py", line 1745, in _check_unused_inputs raise UnusedInputError(msg % (inputs.index(i), i.variable, err_msg)) theano.compile.function.types.UnusedInputError: theano.function was asked to create a function computing outputs given certain inputs, but the provided input variable at index 0 is not part of the computational graph needed to compute the outputs: inarray. To make this error into a warning, you can pass the parameter on_unused_input='warn' to theano.function. To disable it completely, use on_unused_input='ignore'. """

The above exception was the direct cause of the following exception:

UnusedInputError Traceback (most recent call last) Input In [7], in <cell line: 2>() 1 # Now sample 2 with basic_model: 3 # draw posterior samples ----> 4 trace = pm.sample_smc(100, parallel=True)

File ~/anaconda3/envs/pymc3Env/lib/python3.10/site-packages/pymc3/smc/sample_smc.py:196, in sample_smc(draws, kernel, n_steps, start, tune_steps, p_acc_rate, threshold, save_sim_data, save_log_pseudolikelihood, model, random_seed, parallel, chains, cores) 194 loggers = [_log] + [None] * (chains - 1) 195 pool = mp.Pool(cores) --> 196 results = pool.starmap( 197 sample_smc_int, [(*params, random_seed[i], i, loggers[i]) for i in range(chains)] 198 ) 200 pool.close() 201 pool.join()

File ~/anaconda3/envs/pymc3Env/lib/python3.10/multiprocessing/pool.py:372, in Pool.starmap(self, func, iterable, chunksize) 366 def starmap(self, func, iterable, chunksize=None): 367 ''' 368 Like map() method but the elements of the iterable are expected to 369 be iterables as well and will be unpacked as arguments. Hence 370 func and (a, b) becomes func(a, b). 371 ''' --> 372 return self._map_async(func, iterable, starmapstar, chunksize).get()

File ~/anaconda3/envs/pymc3Env/lib/python3.10/multiprocessing/pool.py:771, in ApplyResult.get(self, timeout) 769 return self._value 770 else: --> 771 raise self._value

UnusedInputError: theano.function was asked to create a function computing outputs given certain inputs, but the provided input variable at index 0 is not part of the computational graph needed to compute the outputs: inarray. To make this error into a warning, you can pass the parameter on_unused_input='warn' to theano.function. To disable it completely, use on_unused_input='ignore'.

I am simply following a tutorial on medium, so I don't think there is a problem with the code. I have a strong feeling that the problems arises from the way I installed the packages. I installed pymc3 in a conda environment by using these 3 commands:

conda install numpy scipy mkl
conda install theano pygpu
conda install pymc3

I have also tried installing pymc3 by following the guide from the developpers on github:

conda create -c conda-forge -n pymc3_env pymc3 theano-pymc mkl mkl-service
conda activate pymc3_env

Solution

  • I was able to replicate the issue in PyMC3. This appears to be something wrong with the SMC sampler when combined with multiprocessing. Changing to parallel=False will get the SMC sampler working. Changing to NUTS or Metropolis sampling will also work fine.

    Please file a bug report on the PyMC repository. However, you may also want to try upgrading to PyMC v4 first.