theano and sympy

40 views
Skip to first unread message

draz...@gmail.com

unread,
May 19, 2015, 6:41:06 PM5/19/15
to sy...@googlegroups.com
Hi,

I was experimenting with the following code:

import sympy
from sympy.abc import x
expr = sympy.exp(-x ** 2)
from sympy.printing.theanocode import theano_function
fn_theano  = theano_function([x], [expr], dims={x: 1}, dtypes={x: 'float64'})
print fn_theano([29.]),sympy.exp(-29.**2)

I got 

[ 0.] 5.73245586032578e-366

My question is why fn_theano does not  give the same value?
Thank you,
Costas


Jason Moore

unread,
May 19, 2015, 6:47:53 PM5/19/15
to sy...@googlegroups.com
Theano is doing floating point computations whereas SymPy does the computation using arbitrary precision.

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/4662668c-68b0-482e-b467-92a6d2206d2e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

draz...@gmail.com

unread,
May 20, 2015, 9:48:33 AM5/20/15
to sy...@googlegroups.com
Thank you for your answer.
So, in what sense theano cooperates with sympy (or the inverse)?
Since, we can not have the advantage of sympy functions in theano.
Costas

Frédéric Bastien

unread,
May 20, 2015, 9:57:18 AM5/20/15
to sympy
The execution by Theano should be faster when you work big not too small inputs size. If you work only on scalar, Theano is probably not faster then sympy.

But if you work on vector/tensor big enough (this size depend of the function...) then Theano get faster.

Fred

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.

draz...@gmail.com

unread,
May 20, 2015, 3:21:42 PM5/20/15
to sy...@googlegroups.com
ok, I oversimplified my example. In fact my computations are in vectors. The problem seems to be that I cannot pass the results of 
[sympy.exp(x[i]) for i in range(dim)] to Theano (for x[i] small enough). Theano evaluate them as zero but here I need the real values of exp([x[i]]), where only
sympy.exp can give me.
Costas  

Frédéric Bastien

unread,
May 20, 2015, 3:39:29 PM5/20/15
to sympy
Theano use real number (float32 or float64), not bigfloat. If you need bigfloat precission, there isn't easy way to do it in Theano. We have stability optimization that help in some cases, but it isn't equivalent to bigfloat.

If you have just one op that need bigfloat, you can build a quick Theano op to wrap other python code that do the execution:

http://deeplearning.net/software/theano/tutorial/extending_theano.html#as-op

But your code:


[sympy.exp(x[i]) for i in range(dim)]

do not seem vectorised. In theano x would be a vector and you would just do theano.tensor.exp(x).

Fred

Reply all
Reply to author
Forward
0 new messages