Elementwise sparse matrix multiplication within custom likelihood

Hi,

I am running into problems with elementwise multiplication of sparse matrices within a custom likelihood function.
I am creating a sparse matrix betaM by means of the class DistanceKernel. I pass this on into a custom likelihood function (MyClass) and want to elementwise multiply it with the matrix E which I calculate in the likelihood.

class DistanceKernel():

def __init__(self, c):
    self.c = c

def k(self, kappa, D):
    D.data = np.exp(-D.data*kappa)
    return D

def __call__(self, kappa, D):
    return self.k(kappa, D)



class MyCustom(Continuous):

def __init__(self, R, ni, DistMat, dkernel, *args, **kwargs):
    super(MyCustom, self).__init__(*args, **kwargs)
    self.ni = ni
    self.DistMat = DistMat
    self.dkernel = dkernel
    self.R = R

def logp(self, I):
    E = T.smallest(self.R[None, :self.ni], I[:, None]) - T.smallest(I[None, :self.ni], I[:, None])
    betaM = self.dkernel(self.kappa, self.DistMat)          # BetaM is sparse!!
    test = t.sparse.basic.mul(betaM, E)
    minexpression = T.sum(T.dot(self.R[None, :], test))

 coords # two dimensional numpy array
 R # numpy array
 ni # scalar   

distance = dist.pdist(coords, metric='euclidean')
Dist_gp = dist.squareform(distance)
DistMat = Dist_gp[:, :ni]  # needed for the distance kernel

dsize = np.size(DistMat,0)
for i in range(dsize):
     for j in range(dsize):
          if DistMat[i,j]>20:
               DistMat[i, j] = 0

DistMat_sparse = scipy.sparse.csr_matrix(DistMat)

with Model() as model:
     I = MyCustom('I', R, ni=ni, DistMat=DistMat_sparse, dkernel=DistanceKernel(0.), testval=I0, shape=len(R))

The error I get is with the variable test. Any help on how to fix this is much appreciated! I also tried BetaM * E , resulting in the same error message:

line 221, in logp
test = t.sparse.basic.mul(betaM, E) # sparse matrix multiplication elementwise
File “/usr/local/lib/python3.5/dist-packages/theano/sparse/basic.py”, line 2434, in mul
x = as_sparse_or_tensor_variable(x)
File “/usr/local/lib/python3.5/dist-packages/theano/sparse/basic.py”, line 156, in as_sparse_or_tensor_variable
return theano.tensor.as_tensor_variable(x, name)
File “/usr/local/lib/python3.5/dist-packages/theano/tensor/basic.py”, line 194, in as_tensor_variable
return constant(x, name=name, ndim=ndim)
File “/usr/local/lib/python3.5/dist-packages/theano/tensor/basic.py”, line 266, in constant
raise TypeError(“Could not convert %s to TensorType” % x, type(x))
File “/home/.local/lib/python3.5/site-packages/scipy/sparse/base.py”, line 216, in str
A = self.tocoo()
File “/home/.local/lib/python3.5/site-packages/scipy/sparse/compressed.py”, line 958, in tocoo
dtype=self.dtype)
File “/home/.local/lib/python3.5/site-packages/scipy/sparse/coo.py”, line 182, in init
self.data = self.data.astype(dtype, copy=False)
ValueError: setting an array element with a sequence.