Arctan2 in Theano

Hi,

Anybody has any idea about the AttributeError: ‘TensorVariable’ object has no attribute ‘arctan2’?

I am using customized function in Pymc3. Looks like Theano recognize the numpy functions arccos, sqrt but not the arctan2 function. I tested arctan and it works. But that is not what I want. I looked into the Theano document, there seems to have a arctan2 function built in the basics.

I wonder why this happens?

Thanks!

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-114-53c8728a77ad> in <module>
 20                  tpw_pole_angle = None,
 21                  tpw_rate_scale = None,
---> 22                  sample_size = 2000)

<ipython-input-113-cfc6a15ee3bc> in create_model(name, paleomagnetic_pole_list, A95_list, age_list, age_uncertainty_list, age_uncertainty_type_list, n_euler_poles, include_tpw, site_lon_lat, k, kw, euler_rate, tpw_pole_angle, tpw_rate_scale, sample_size)
 81                 lon_lat = pole_position(start, start_age, this_age, euler_list, rate_list, changepoint_list, tpw_pole_angle, tpw_rate)
 82             else:
---> 83                 lon_lat = pole_position(start, start_age, this_age, euler_list, rate_list, changepoint_list)
 84 
 85 #             print(lon_lat)

<ipython-input-112-74444e1e42c3> in pole_position(start, start_age, this_age, euler_list, rate_list, changepoint_list, tpw_pole_angle, tpw_rate)
 41                     break
 42 
---> 43         lon, lat, _ = cartesian_to_spherical(this_pole)
 44         return T.stacklists([lon[0], lat[0]])

<ipython-input-111-1affa050701b> in cartesian_to_spherical(vecs)
 66     norm = np.sqrt(v[0, :] * v[0, :] + v[1, :] * v[1, :] + v[2, :] * v[2, :])
 67     latitude = 90. - np.arccos(v[2, :] / norm) * r2d
---> 68     longitude = np.arctan2(v[1, :],v[0, :]) * r2d
 69 
 70     return longitude, latitude, norm

AttributeError: 'TensorVariable' object has no attribute 'arctan2'

Hi,
having the same problem… have you found a suitable solution in the meanwhile?

regards Leon

Hey Leon,

This is a weird problem… I think it has to do with theano… There is an arctan2 function in theano basic.py but it is somehow not recognized sometimes. I don’t fully understand…

I think you can try to declare theano variable type for your functions. One of my examples is this:
I tell explicitly say that my input variable start should be treated as a vector, input euler_1 should be a vector, input rate_1 should be a scaler… etc and output value that is being returned should be a vector.

@as_op(itypes=[T.dvector, T.dvector, T.dscalar, T.dscalar, T.dscalar], otypes=[T.dvector])
def pole_position_1e( start, euler_1, rate_1, start_age, age ):

I’m happy to discuss more if you want to show your code.

Hey Duserzym,
thanks for your answer! Interesting approach, I’ ll try it. But I guess it will be difficult because the function has scalars and TheanoTensor Variables as input.
Sadly I can’ t post my code here since it’s not solely written by me… But I work with a customized function which would return complex values. But since pymc3 does not support complex inference I need to split every value into real and imaginary part via complex roots and build the argument.
Using tt.arctan2(x,y) I get my model to run but sampling is still not possible. I am not sure yet if this because of the arctan2 or if I have another problem somewhere. But one problem about using a tensor function is that the inference now only works within the context of the pymc3 model.
I also tried to bypass the arctan2 mathematicaly by using the sign function. This sadly does not work because python regards it as ‘not stable’. Difficult problem…

Looking forward to your opinion!

You shouldn’t try to use the same code for theano and numpy operations. Theano can sometimes recognize and override a numpy function call as you observed but this is not always done consistently and shouldn’t really be relied upon.

You can always make two versions, one which uses theano operators and another that uses numpy ones. You can also compile a theano expression if you want to use it to evaluate numbers as you would with a numpy function.

1 Like

Oh that’ s good to know, thanks!