Error while using theano functions

I am trying to use theano functions with pymc3, but I always get errors when running the below function.

from pymc3 import (
    NUTS,
    Deterministic,
    HalfCauchy,
    Model,
    MvNormal,
    find_MAP,
    sample,
    summary,
    traceplot,
)
import theano.tensor as T

np.random.seed(20090425)
n = 30
p = 10
p_dash = 2
q= 3
X = np.random.rand(n,p)
y = np.random.rand(n,p_dash)
Z = np.random.rand(n,q)
true_scale = 3
from theano.tensor import _shared

def kernel(x_1, x_2, k):
    """Kernel function."""
    z = np.dot(x_1.T, k)
    
    return np.dot(z, x_2)
squared_distance = lambda x, y: np.array(
    [[np.mean(x[i,:] - y[j,:]) ** 2 for i in range(len(x))] for j in range(len(y))]
)
with pm.Model() as model:

    sigma_p = pm.HalfCauchy("sigma_p", 5)
    non_linear = np.random.rand(n,p)
    # trying to add gaussian process to the model to acccount for non-linear relation between X and y
    mu = np.zeros(n)

    eta_sq = HalfCauchy("η_sq", 5)
    rho_sq = HalfCauchy("ρ_sq", 5)
    sigma_sq = HalfCauchy("σ_sq", 5)

    D = (squared_distance(X, X))
    # Squared exponential
    #K_star = T.fill_diagonal(eta_sq * T.exp(-rho_sq * D), eta_sq + sigma_sq)
    non_linear = pm.MvNormal('non_linear',mu=np.zeros(n), cov=D,shape=(p_dash, n))
    #print(np.tile(non_linear, (p, 1)).T)

    beta = pm.MvNormal("beta", mu=np.zeros(p_dash), cov=sigma_p*np.eye(p_dash), shape=(p,p_dash))

    u = pm.MvNormal("u", mu=np.zeros(p_dash), cov=sigma_p*np.eye(p_dash), shape=(q,p_dash))
    # Model error
    eps = pm.HalfCauchy("eps", 5)
    print(pm.math.dot(X,beta))
    print(Z.shape)
    # model
    radon_est = pm.math.dot(Z,u) + pm.math.dot(X,beta) + non_linear.T
    
        # Setup right cholesky matrix
    sd_dist = pm.HalfCauchy.dist(beta=2.5, shape=p_dash)
    colchol_packed = pm.LKJCholeskyCov('colcholpacked', n=p_dash, eta=2,
                                       sd_dist=sd_dist)
    colchol = pm.expand_packed_triangular(p_dash, colchol_packed)

    # Setup left covariance matrix
    scale = pm.Lognormal('scale', mu=np.log(true_scale), sigma=0.5)
    rowcov = T.nlinalg.diag([scale**(2*i) for i in range(n)])
    
    y_obs = pm.MatrixNormal("y_obs", mu=radon_est, colchol=colchol, rowcov=rowcov,observed=y)
    
with model:
    trace = pm.sample(2000)

The error is-

KeyError                                  Traceback (most recent call last)
~/.local/lib/python3.8/site-packages/theano/tensor/type.py in dtype_specs(self)
    267                 'complex128': (complex, 'theano_complex128', 'NPY_COMPLEX128'),
--> 268                 'complex64': (complex, 'theano_complex64', 'NPY_COMPLEX64')
    269             }[self.dtype]

KeyError: 'object'

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-38-53fab479b7c6> in <module>
     67     # Setup left covariance matrix
     68     scale = pm.Lognormal('scale', mu=np.log(true_scale), sigma=0.5)
---> 69     rowcov = tt.nlinalg.diag([scale**(2*i) for i in range(n)])
     70 
     71     y_obs = pm.MatrixNormal("y_obs", mu=radon_est, colchol=colchol, rowcov=rowcov,observed=y)

~/.local/lib/python3.8/site-packages/theano/tensor/nlinalg.py in diag(x)
    213         return extract_diag(xx)
    214     else:
--> 215         raise TypeError('diag requires vector or matrix argument', x)
    216 
    217 

~/.local/lib/python3.8/site-packages/theano/tensor/basic.py in as_tensor_variable(x, name, ndim)
    203 # function which ops use to upcast their arguments... this
    204 # internal-use function is a good place to put debugging stuff, better
--> 205 # than the global astensor.
    206 _as_tensor_variable = as_tensor_variable
    207 

~/.local/lib/python3.8/site-packages/theano/tensor/basic.py in constant(x, name, ndim, dtype)
    251         if sig in constant_cache:
    252             return constant_cache[sig]
--> 253 
    254         ret = TensorConstant(ttype, x_, name=name)
    255         if (x_.size == 1 and

~/.local/lib/python3.8/site-packages/theano/tensor/type.py in __init__(self, dtype, broadcastable, name, sparse_grad)
     50         self.broadcastable = tuple(bool(b) for b in broadcastable)
     51         self.dtype_specs()  # error checking is done there
---> 52         self.name = name
     53         self.numpy_dtype = np.dtype(self.dtype)
     54         self.sparse_grad = sparse_grad

~/.local/lib/python3.8/site-packages/theano/tensor/type.py in dtype_specs(self)
    283             and other.broadcastable == self.broadcastable
    284 
--> 285     def convert_variable(self, var):
    286         if (type(self) == type(var.type) and  # noqa
    287             self.dtype == var.type.dtype and

TypeError: Unsupported dtype for TensorType: object

I am not able to perform any functions with theano.If I try to use theano, I get the above error. I am using python 3.8 and pymc 3.11. Probably this might be some naive mistake from my side. I even tried running example code given in the documentation but it gave me same error-

# Setup data
true_colcov = np.array([[1.0, 0.5, 0.1],
                        [0.5, 1.0, 0.2],
                        [0.1, 0.2, 1.0]])
m = 3
n = true_colcov.shape[0]
true_scale = 3
true_rowcov = np.diag([true_scale**(2*i) for i in range(m)])
mu = np.zeros((m, n))
true_kron = np.kron(true_rowcov, true_colcov)
data = np.random.multivariate_normal(mu.flatten(), true_kron)
data = data.reshape(m, n)

with pm.Model() as model:
    # Setup right cholesky matrix
    sd_dist = pm.HalfCauchy.dist(beta=2.5, shape=3)
    colchol_packed = pm.LKJCholeskyCov('colcholpacked', n=3, eta=2,
                                       sd_dist=sd_dist)
    colchol = pm.expand_packed_triangular(3, colchol_packed)

    # Setup left covariance matrix
    scale = pm.Lognormal('scale', mu=np.log(true_scale), sigma=0.5)
    rowcov = tt.nlinalg.diag([scale**(2*i) for i in range(m)])

    vals = pm.MatrixNormal('vals', mu=mu, colchol=colchol, rowcov=rowcov,
                           observed=data, shape=(m, n))

I ran your code with the latest version of PyMC3 and the error you described doesn’t occur, but there is an issue with your covariance matrix D. I think it isn’t positive semidefinite - you should make sure it has all positive eigenvalues. You can see my modifications to your code here: gp-radon-model-broken · GitHub

Thanks for the suggestion. I downgraded pymc from 3.11 to 3.10, and everything worked. I think there is incompatibility with different versions of pymc and theano, is there a page that describes which versions are compatible?

I sometimes look at the release history for more information regarding which versions use Theano, Theano-PyMC, or aesara. I’m not sure what other resources are available.

Thanks