Error in basic model due to trigonometric functions using math library

Dear all,
I am very new on python and just start using PYMC3. I am getting an error while using trigonometric function in the model of PYMC3. Can You please help me why this occuring and how can I avoid this in future.
Please note that I have already calculated the data (Y) separately.
Could you please help me in this regard. Thank you!

import pymc3 as pm
basic_model = pm.Model()
with basic_model:
    A_1 = pm.Normal('A_1', mu=0, sd=1)
    A_2 = pm.Normal('A_2', mu=0, sd=1)
    Y_obs = pm.Normal('Y_obs', mu=mu, sd=sigma, observed=y)


  File "<ipython-input-16-916a619013f6>", line 22, in <module>

TypeError: must be real number, not FreeRV

You cannot use functions from the math library (or numpy/scipy for that matter), as in most case the variables under the pymc3 with context are theano tensors. You should subtuitue it with either a theano.tensor function or a function from pymc3.math

Thank You very much Junpenglao!

One more question in this regard: My forward model convert the output from matrix to float as following:
Output1=float(output1). When I calculate my data, I get no error of course but when I call same function in the PYMC3 model, I get following error:

TypeError: float() argument must be a string or a number, not ‘TensorVariable’

Can you please help also in this.

You dont need to do that for TensorVariable, they are ndarray backend.

In the same work: Another error. Can you please also suggest what wrong I am doing here. Following is my code:

#%% Forward code
import matplotlib.pyplot as plt
import numpy as np
import math
          -math.sin(math.radians(Azim)), 0],\
          0], [0, 0, 1]])

new_x= xrot1*(np.matrix([[x0],[y0],[z0]]))

h  = new_x[2]
U=h + (10.0*0.9999)

#%% PYMC model

import pymc3 as pm
basic_model = pm.Model()

with basic_model:
    Azim=pm.Normal('Azim', mu=0, sd=1)    
    sigma = pm.HalfNormal('sigma', sd=1)
          -pm.math.sin(Azim), 0],\
          0], [0, 0, 1]])
    new_x= xrot*(np.matrix([[x0],[y0],[z0]]))
    h  = np.array(new_x[2])
    mu=h + (10.0*0.9999)
    Y_obs = pm.Normal('Y_obs', mu=mu, sd=sigma, observed=U)

-----------------------------output error--------------------------

  File "<ipython-input-34-d6ad12081d4d>", line 20, in <module>
    Y_obs = pm.Normal('Y_obs', mu=mu, sd=sigma, observed=U)

  File "C:\Users\singh.CT\AppData\Local\Continuum\miniconda3\lib\site-packages\pymc3\distributions\", line 41, in __new__
    dist = cls.dist(*args, **kwargs)

  File "C:\Users\singh.CT\AppData\Local\Continuum\miniconda3\lib\site-packages\pymc3\distributions\", line 52, in dist
    dist.__init__(*args, **kwargs)

  File "C:\Users\singh.CT\AppData\Local\Continuum\miniconda3\lib\site-packages\pymc3\distributions\", line 431, in __init__
    self.mean = self.median = self.mode = = mu = tt.as_tensor_variable(mu)

  File "C:\Users\singh.CT\AppData\Local\Continuum\miniconda3\lib\site-packages\theano\tensor\", line 200, in as_tensor_variable
    raise AsTensorError("Cannot convert %s to TensorType" % str_x, type(x))

AsTensorError: ('Cannot convert [[21.999000000000002]] to TensorType', <class 'numpy.ndarray'>)

Actually I dont get any error running your code, so make sure upgrade to the newest release of PyMC3 and theano. However, you can still make your model block more theano/pymc3:

import pymc3 as pm
import theano.tensor as tt

with pm.Model() as basic_model:
    Azim = pm.Normal('Azim', mu=0, sd=1)    
    sigma = pm.HalfNormal('sigma', sd=1)
    xrot = tt.stacklists([[tt.cos(Azim), -tt.sin(Azim), 0],\
                          [tt.sin(Azim), tt.cos(Azim), 0], 
                          [0, 0, 1]])
    new_x= xrot * (np.asarray([[x0],[y0],[z0]]))
    mu = new_x[2] + 10.0*0.9999
    Y_obs = pm.Normal('Y_obs', mu=mu, sd=sigma, observed=U)

Thank You very much for the reply. I tried with your suggested code and this leads to another error as below (using pymc3 version 3.6, theano version 1.0.3, numpy version 1.16.2):

File “”, line 5, in
Azim = pm.Normal(‘Azim’, mu=0, sd=1)

File “C:\Users\singh.CT\AppData\Local\Continuum\miniconda3\lib\site-packages\pymc3\distributions\”, line 41, in new
dist = cls.dist(*args, **kwargs)

File “C:\Users\singh.CT\AppData\Local\Continuum\miniconda3\lib\site-packages\pymc3\distributions\”, line 52, in dist
dist.init(*args, **kwargs)

File “C:\Users\singh.CT\AppData\Local\Continuum\miniconda3\lib\site-packages\pymc3\distributions\”, line 432, in init
self.variance = 1. / self.tau

File “C:\Users\singh.CT\AppData\Local\Continuum\miniconda3\lib\site-packages\theano\tensor\”, line 203, in rtruediv
return theano.tensor.basic.true_div(other, self)

File “C:\Users\singh.CT\AppData\Local\Continuum\miniconda3\lib\site-packages\theano\gof\”, line 670, in call

File “C:\Users\singh.CT\AppData\Local\Continuum\miniconda3\lib\site-packages\theano\gof\”, line 955, in make_thunk

File “C:\Users\singh.CT\AppData\Local\Continuum\miniconda3\lib\site-packages\theano\gof\”, line 858, in make_c_thunk

File “C:\Users\singh.CT\AppData\Local\Continuum\miniconda3\lib\site-packages\theano\gof\”, line 1217, in make_thunk

File “C:\Users\singh.CT\AppData\Local\Continuum\miniconda3\lib\site-packages\theano\gof\”, line 1157, in compile

File “C:\Users\singh.CT\AppData\Local\Continuum\miniconda3\lib\site-packages\theano\gof\”, line 1609, in cthunk_factory
key = self.cmodule_key()

File “C:\Users\singh.CT\AppData\Local\Continuum\miniconda3\lib\site-packages\theano\gof\”, line 1300, in cmodule_key

File “C:\Users\singh.CT\AppData\Local\Continuum\miniconda3\lib\site-packages\theano\gof\”, line 1379, in cmodule_key_

AttributeError: module ‘numpy.core.multiarray’ has no attribute ‘_get_ndarray_c_version’

most likely your set up environment is not correct - are you using conda?

Yes, I install python and all related stuff using conda. I write and run the script in spyder. If you could suggest me better option to code and as well to use PyMc3, please let me know. I am in very initial stage of learning PyMc3 and also pyhton. Thank You [Sorry for late reply because it was night here].

I think I come across questions re problem under spyder sometimes as well - I recommend using either jupyter notebook or jupyterlab.

I had the same problem when I updated numpy. Part of numpy is compiled c code and another is pure python, the mismatch happened because the update just change the pure python part and did not recompile the c part. I think that I solved it uninstalling and then reinstalling numpy

1 Like