TypeError: Unknown parameter type: <class 'theano.tensor.var.TensorVariable'>

I’m new using pymc3 and wanted to learn how to use it with a simple example [GLM: Linear regression — PyMC3 3.10.0 documentation]. When building the model and running it I get a problem related to theano. Here’s the code:

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

%config InlineBackend.figure_format = 'retina'
az.style.use("arviz-darkgrid")

size = 200
true_intercept = 1
true_slope = 2

x = np.linspace(0,1, size)
true_regression_line = true_intercept + true_slope * x
y = true_regression_line + np.random.normal(scale=0.5, size=size)
data = dict(x=x, y=y)

fig = plt.figure(figsize=((7,7)))
ax = fig.add_subplot(111, xlabel="x", ylabel="y", title="Generated data and underlying model")
ax.plot(x,y, "x", label="sampled data")
ax.plot(x, true_regression_line, label="true regression line", lw=2.0)
plt.legend(loc=0);

with pm.Model() as model:
    sigma = pm.HalfCauchy("sigma", beta=10, testval=1.0)
    intercept = pm.Normal("Intercept", 0, sigma=20)
    x_coeff = pm.Normal("x", 0, sigma=20)
    likelihood = pm.Normal("y", mu=intercept + x_coeff * x, sigma=sigma, observed=y)
    trace = pm.sample(3000, cores=2)
    
az.plot_trace(trace)

which yields:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-1-807f8acca5be> in <module>
     27     x_coeff = pm.Normal("x", 0, sigma=20)
     28     likelihood = pm.Normal("y", mu=intercept + x_coeff * x, sigma=sigma, observed=y)
---> 29     trace = pm.sample(3000, cores=2)
     30 
     31 az.plot_trace(trace)

/opt/anaconda3/lib/python3.8/site-packages/pymc3/sampling.py in sample(draws, step, init, n_init, start, trace, chain_idx, chains, cores, tune, progressbar, model, random_seed, discard_tuned_samples, compute_convergence_checks, callback, return_inferencedata, idata_kwargs, mp_ctx, pickle_backend, **kwargs)
    479             # By default, try to use NUTS
    480             _log.info("Auto-assigning NUTS sampler...")
--> 481             start_, step = init_nuts(
    482                 init=init,
    483                 chains=chains,

/opt/anaconda3/lib/python3.8/site-packages/pymc3/sampling.py in init_nuts(init, chains, n_init, model, random_seed, progressbar, **kwargs)
   2168         raise ValueError("Unknown initializer: {}.".format(init))
   2169 
-> 2170     step = pm.NUTS(potential=potential, model=model, **kwargs)
   2171 
   2172     return start, step

/opt/anaconda3/lib/python3.8/site-packages/pymc3/step_methods/hmc/nuts.py in __init__(self, vars, max_treedepth, early_max_treedepth, **kwargs)
    166         `pm.sample` to the desired number of tuning steps.
    167         """
--> 168         super().__init__(vars, **kwargs)
    169 
    170         self.max_treedepth = max_treedepth

/opt/anaconda3/lib/python3.8/site-packages/pymc3/step_methods/hmc/base_hmc.py in __init__(self, vars, scaling, step_scale, is_cov, model, blocked, potential, dtype, Emax, target_accept, gamma, k, t0, adapt_step_size, step_rand, **theano_kwargs)
     91         vars = inputvars(vars)
     92 
---> 93         super().__init__(vars, blocked=blocked, model=model, dtype=dtype, **theano_kwargs)
     94 
     95         self.adapt_step_size = adapt_step_size

/opt/anaconda3/lib/python3.8/site-packages/pymc3/step_methods/arraystep.py in __init__(self, vars, model, blocked, dtype, **theano_kwargs)
    241         self.blocked = blocked
    242 
--> 243         func = model.logp_dlogp_function(
    244             vars, dtype=dtype, **theano_kwargs)
    245 

/opt/anaconda3/lib/python3.8/site-packages/pymc3/model.py in logp_dlogp_function(self, grad_vars, **kwargs)
    933         varnames = [var.name for var in grad_vars]
    934         extra_vars = [var for var in self.free_RVs if var.name not in varnames]
--> 935         return ValueGradFunction(self.logpt, grad_vars, extra_vars, **kwargs)
    936 
    937     @property

/opt/anaconda3/lib/python3.8/site-packages/pymc3/model.py in __init__(self, cost, grad_vars, extra_vars, dtype, casting, **kwargs)
    652         inputs = [self._vars_joined]
    653 
--> 654         self._theano_function = theano.function(
    655             inputs, [self._cost_joined, grad], givens=givens, **kwargs
    656         )

/opt/anaconda3/lib/python3.8/site-packages/theano/compile/function/__init__.py in function(inputs, outputs, mode, updates, givens, no_default_updates, accept_inplace, name, rebuild_strict, allow_input_downcast, profile, on_unused_input)
    335         # note: pfunc will also call orig_function -- orig_function is
    336         #      a choke point that all compilation must pass through
--> 337         fn = pfunc(
    338             params=inputs,
    339             outputs=outputs,

/opt/anaconda3/lib/python3.8/site-packages/theano/compile/function/pfunc.py in pfunc(params, outputs, mode, updates, givens, no_default_updates, accept_inplace, name, rebuild_strict, allow_input_downcast, profile, on_unused_input, output_keys)
    424 
    425     # transform params into theano.compile.In objects.
--> 426     inputs = [
    427         _pfunc_param_to_in(p, allow_downcast=allow_input_downcast) for p in params
    428     ]

/opt/anaconda3/lib/python3.8/site-packages/theano/compile/function/pfunc.py in <listcomp>(.0)
    425     # transform params into theano.compile.In objects.
    426     inputs = [
--> 427         _pfunc_param_to_in(p, allow_downcast=allow_input_downcast) for p in params
    428     ]
    429 

/opt/anaconda3/lib/python3.8/site-packages/theano/compile/function/pfunc.py in _pfunc_param_to_in(param, strict, allow_downcast)
    541     elif isinstance(param, In):
    542         return param
--> 543     raise TypeError(f"Unknown parameter type: {type(param)}")
    544 
    545 

TypeError: Unknown parameter type: <class 'theano.tensor.var.TensorVariable'>

Before I was able to get the trace and run into problems trying to az.plot_trace(), which I tried to solve. I updated the CLT, uninstalled pymc3 and theano, tried git clone and version 3.11 gave another type of theano error related to gcc, so I uninstalled again and installed it back with conda (giving version 3.9.3). I’m running Big Sur and the problem could be related to my machine, since a colleague of mine with older OS was able to run correctly the complete code. I would appreciate any help on getting it solved.

  • PyMC3 Version: 3.9.3
  • Theano Version: 1.0.5
  • Python Version: 3.8.3 (Clang 10.0.0)
  • Operating system: macOS Big Sur 11.2
  • How did you install PyMC3: (conda/pip) conda install pymc3 / conda install -c conda-forge mkl pymc3

Hi @Erikmeier ,
The current PyMC3 releases have very specific requirements for the Theano backend. Your problem is likely because the installed versions are not compatible with each other.

We recently updated the installation instructions: Installation Guide (MacOS) · pymc-devs/pymc3 Wiki · GitHub

The latest 3.11.0 release prints some warnings that you can ignore. We’ll release 3.11.1 in the next few days to fix this.

Hi @michaelosthege,
Thank you for your reply! I followed the installations instructions. Using the command conda install -c conda-forge mkl pymc3 installs version 3.9.3, which is not the latest release. After your reply I tried conda install -c conda-forge pymc3=3.11.0 and now everything works! I wasn’t aware that I wasn’t getting the latest version.
Again thank you so much for your reply.
Erik

2 Likes