Theano: TypeError: ufunc not supported for the input types

Created on 19 Mar 2016  Â·  8Comments  Â·  Source: Theano/Theano

Hi,

I have implemented a VonMises distribution (the idea is to added to PyMC3) the relevant line of code for my problem is:

kappa * T.cos(mu - value) - T.log(2 * np.pi * special.i0(kappa))

Since the modified Bessel function is not implemented in Theano I am using that function from SciPy `special.i0(). The codes runs on my computer, but when I do a pull request to PyMC3 master, Travis complains with the error:

TypeError: ufunc 'i0' not supported for the input types, 
and the inputs could not be safely coerced to any 
supported types according to the casting rule ''safe''

I have two question that I hope you could answer.

  1. Why the code runs on my computer, and do not past the Travis test? Is this related to the theano/scipy/numpy versions?
  2. It would be safer/better to implement the modified Bessel function on Theano?

Thanks.

Most helpful comment

What is the type of 'kappa' in your initial code?

All 8 comments

I think the error is caused by different numpy version. The error you gave
seem to come from numpy. Can you compare the version used by pymc3 travis
and the version you have?

On Sat, Mar 19, 2016 at 10:03 AM, Osvaldo Martin [email protected]
wrote:

Hi,

I have implemented a VonMises distribution (the idea is to added to PyMC3)
the relevant line of code for my problem is:

kappa * T.cos(mu - value) - T.log(2 * np.pi * special.i0(kappa))

Since the modified Bessel function is not implemented in Theano I am using
that function from SciPy `special.i0(). The codes runs on my computer,
but when I do a pull request to PyMC3 master, Travis complains with the
error:

TypeError: ufunc 'i0' not supported for the input types, and the inputs could not be safely coerced to any
supported types according to the casting rule ''safe''

I have two question that I hope you could answer.

  1. Why the code runs on my computer, and do not past the Travis test? Is
    this related to the theano/scipy/numpy versions?
  2. It would be safer/better to implement the modified Bessel function on
    Theano?

Thanks.

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub
https://github.com/Theano/Theano/issues/4267

It seems both versions are the same NumPy 1.10.4. and SciPy 0.17.0.

Btw in as a workaround I have implemented the modified Bessel functions as a series (instead of using the functions from SciPy), using that approximation the Travis test were successful. Nevertheless I am still curious about this problem.

Can you add this theano flag with the code that cause the problem:
exception_verbosity=high and give the full error message.

On Mon, Mar 21, 2016 at 3:04 PM, Osvaldo Martin [email protected]
wrote:

It seems both versions are the same NumPy 1.10.4. and SciPy 0.17.0.

Btw in as a workaround I have implemented the modified Bessel functions as
a series (instead of using the functions from SciPy), using that
approximation the Travis test were successful. Nevertheless I am still
curious about this problem.

—
You are receiving this because you commented.
Reply to this email directly or view it on GitHub
https://github.com/Theano/Theano/issues/4267#issuecomment-199427728

To do that I should send a new pull request, right? I would prefer not to do that because I have already send the workaround version to PyMC3. I tried to replicate the problem in a couple of computers and I do not see the error. I could try installing different NumPy and Theano versions

The error comes from numpy and is from the fact that you are mixing symbolic (Theano) and numeric (scipy) code. This will not work.

If you want to use a scipy function in theano you have to wrap it up as an op (maybe with @as_op http://deeplearning.net/software/theano/library/compile/ops.html#theano.compile.ops.as_op).

@abergeron I know it should not work. but it does. I found this by mistake I just put special.i0 as a placeholder until I found a solution like the one you describe and to my surprise (or the one I finally implemented) and the code runs and I also got the correct results (I use the von mises distribution implemented in SciPy to compare).

Now if I write

import theano.tensor as T
from theano import function
import numpy as np
from scipy import special

mu = T.dscalar('mu')
value = T.dscalar('value')
kappa = T.dscalar('kappa')
f = function([mu, value, kappa], kappa * T.cos(mu - value) - T.log(2 * np.pi * special.i0(kappa)))

I will get, as expected, the error

TypeError: ufunc 'i0' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

So, for some weird reason the code works inside PyMC3, when it should not work at all.

What is the type of 'kappa' in your initial code?

I see now! My original code works only for the special case when kappa is a Python variable like a float or int, but not when kappa is a Theano tensor. Thanks for the help and sorry about the confusion.

Was this page helpful?
0 / 5 - 0 ratings