Sorry if this is a simple question, but I don’t seem to be able to figure it out.
I provide a minimal working example of a code, which creates a multivariate normal distribution with a covariance that depends on random variables.
import pymc3 as pm
import numpy as npwith pm.Model() as model:
n1 = pm.Gamma(‘n1’, alpha=2, beta=1 / 1.3, transform=None)
n2 = pm.Gamma(‘n2’, alpha=2, beta=1 / 1.3, transform=None)
D2 = pm.Gamma(‘D2’, alpha=2, beta=1 / 1.3, transform=None)mu = [0,0] cov = np.array([[n1, n2], [n2, D2]]) pm.MvNormal('test', mu=mu, cov=cov, observed = [1, 10])
However, the last line fails with the following error.
WARNING (theano.tensor.blas): Using NumPy C-API based implementation for BLAS functions.
Traceback (most recent call last):
File “C:\Python37\lib\site-packages\theano\tensor\type.py”, line 269, in dtype_specs
}[self.dtype]
KeyError: ‘object’During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “C:\Python37\lib\site-packages\theano\tensor\basic.py”, line 246, in constant
ttype = TensorType(dtype=x_.dtype, broadcastable=bcastable)
File “C:\Python37\lib\site-packages\theano\tensor\type.py”, line 51, in init
self.dtype_specs() # error checking is done there
File “C:\Python37\lib\site-packages\theano\tensor\type.py”, line 272, in dtype_specs
% (self.class.name, self.dtype))
TypeError: Unsupported dtype for TensorType: objectDuring handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “C:\Python37\lib\site-packages\theano\tensor\basic.py”, line 194, in as_tensor_variable
return constant(x, name=name, ndim=ndim)
File “C:\Python37\lib\site-packages\theano\tensor\basic.py”, line 266, in constant
raise TypeError(“Could not convert %s to TensorType” % x, type(x))
TypeError: (‘Could not convert [[n1 n2]\n [D1 D2]] to TensorType’, <class ‘numpy.ndarray’>)During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2019.1.2\helpers\pydev\pydevd.py”, line 1758, in
main()
File “C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2019.1.2\helpers\pydev\pydevd.py”, line 1752, in main
globals = debugger.run(setup[‘file’], None, None, is_module)
File “C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2019.1.2\helpers\pydev\pydevd.py”, line 1147, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File “C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2019.1.2\helpers\pydev_pydev_imps_pydev_execfile.py”, line 18, in execfile
exec(compile(contents+“\n”, file, ‘exec’), glob, loc)
File “D:/temp.py”, line 43, in
pm.MvNormal(‘test’, mu=mu, cov=cov, observed = [1, 10])
File “C:\Python37\lib\site-packages\pymc3\distributions\distribution.py”, line 46, in new
dist = cls.dist(*args, **kwargs)
File “C:\Python37\lib\site-packages\pymc3\distributions\distribution.py”, line 57, in dist
dist.init(*args, **kwargs)
File “C:\Python37\lib\site-packages\pymc3\distributions\multivariate.py”, line 222, in init
super().init(mu=mu, cov=cov, tau=tau, chol=chol, lower=lower, *args, **kwargs)
File “C:\Python37\lib\site-packages\pymc3\distributions\multivariate.py”, line 59, in init
cov = tt.as_tensor_variable(cov)
File “C:\Python37\lib\site-packages\theano\tensor\basic.py”, line 200, in as_tensor_variable
raise AsTensorError(“Cannot convert %s to TensorType” % str_x, type(x))
theano.tensor.var.AsTensorError: (‘Cannot convert [[n1 n2]\n [D1 D2]] to TensorType’, <class ‘numpy.ndarray’>)
I was following an example given here
https://docs.pymc.io/api/distributions/multivariate.html#pymc3.distributions.multivariate.MvNormal
but I must be doing something wrong. Can someone advise?
It seems like theano is unable to convert cov
from a numpy array to a tensor, but in it seems to be the case in the example above as well.