Discussion:
[theano-users] And operator doesn't work with theano logical operators
Sym
2017-06-28 14:26:38 UTC
Permalink
I want to build a piecewise function with theano, for instance a function
that is nonzero only in the interval [2,3].

Here is the minimal code reproducing the error :


import theano
import theano.tensor as T
import numpy as np
import matplotlib.pyplot as plt

r = T.scalar()
gate = T.switch( T.ge(r,2.) and T.le(r,3.) , 1., 0.)
f = theano.function([r],gate)
x = np.arange(0.,4.,0.05,dtype='float32')
y = [f(i) for i in x]
plt.plot(x,y)



The result is the following : Loading Image...

Which is clearly not correct : only one condition is satisfied here.


If I replace T.switch by theano.ifelse.ifelse the result is the same...

Is it a known bug, or am I missing something here?


Thanks a lot !
--
---
You received this message because you are subscribed to the Google Groups "theano-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to theano-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Frédéric Bastien
2017-06-29 01:15:14 UTC
Permalink
Don't use the Python "and" operation. Use theano.tensor.and(a,b) instead. I
think it will fix your problem.
Post by Sym
I want to build a piecewise function with theano, for instance a function
that is nonzero only in the interval [2,3].
import theano
import theano.tensor as T
import numpy as np
import matplotlib.pyplot as plt
r = T.scalar()
gate = T.switch( T.ge(r,2.) and T.le(r,3.) , 1., 0.)
f = theano.function([r],gate)
x = np.arange(0.,4.,0.05,dtype='float32')
y = [f(i) for i in x]
plt.plot(x,y)
The result is the following : https://i.stack.imgur.com/XMQme.png
Which is clearly not correct : only one condition is satisfied here.
If I replace T.switch by theano.ifelse.ifelse the result is the same...
Is it a known bug, or am I missing something here?
Thanks a lot !
--
---
You received this message because you are subscribed to the Google Groups
"theano-users" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
---
You received this message because you are subscribed to the Google Groups "theano-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to theano-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Sym
2017-06-29 14:17:26 UTC
Permalink
I am using theano 0.9.0 and this function does not exist.. I can't find it
in the documentation either !

And in the theano docs it says that :
__{abs,neg,lt,le,gt,ge,invert,and,or,add,sub,mul,div,truediv,floordiv}__
Those elemwise operation are supported via Python syntax.
Post by Frédéric Bastien
Don't use the Python "and" operation. Use theano.tensor.and(a,b) instead.
I think it will fix your problem.
Post by Sym
I want to build a piecewise function with theano, for instance a function
that is nonzero only in the interval [2,3].
import theano
import theano.tensor as T
import numpy as np
import matplotlib.pyplot as plt
r = T.scalar()
gate = T.switch( T.ge(r,2.) and T.le(r,3.) , 1., 0.)
f = theano.function([r],gate)
x = np.arange(0.,4.,0.05,dtype='float32')
y = [f(i) for i in x]
plt.plot(x,y)
The result is the following : https://i.stack.imgur.com/XMQme.png
Which is clearly not correct : only one condition is satisfied here.
If I replace T.switch by theano.ifelse.ifelse the result is the same...
Is it a known bug, or am I missing something here?
Thanks a lot !
--
---
You received this message because you are subscribed to the Google Groups
"theano-users" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
---
You received this message because you are subscribed to the Google Groups "theano-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to theano-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Sym
2017-06-29 16:35:50 UTC
Permalink
I found a solution :

The and operator should be implemented with * and the or operator with +,
as the python operators do no work properly with theano (as opposed to what
they say in the docs).

For further readers, the corrected code is :

import theano
import theano.tensor as T
import numpy as np
import matplotlib.pyplot as plt

r = T.scalar()
gate = T.switch( T.ge(r,2.) * T.le(r,3.) , 1., 0.)
f = theano.function([r],gate)
x = np.arange(0.,4.,0.05,dtype='float32')
y = [f(i) for i in x]
plt.plot(x,y)

Which indeed gives the correct output.
--
---
You received this message because you are subscribed to the Google Groups "theano-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to theano-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Pascal Lamblin
2017-07-01 00:11:09 UTC
Permalink
Post by Sym
I am using theano 0.9.0 and this function does not exist.. I can't find it
in the documentation either !
That would be theano.tensor.and_ with a trailing underscore.
Post by Sym
__{abs,neg,lt,le,gt,ge,invert,and,or,add,sub,mul,div,truediv,floordiv}__
Those elemwise operation are supported via Python syntax.
This is true, but the Python syntax that will call `__and__` is actually `a
&& b`, not `a and b`.

So you can use `a && b`, `and_(a, b)`, or `a * b` if you prefer.
Post by Sym
Post by Frédéric Bastien
Don't use the Python "and" operation. Use theano.tensor.and(a,b) instead.
I think it will fix your problem.
Post by Sym
I want to build a piecewise function with theano, for instance a
function that is nonzero only in the interval [2,3].
import theano
import theano.tensor as T
import numpy as np
import matplotlib.pyplot as plt
r = T.scalar()
gate = T.switch( T.ge(r,2.) and T.le(r,3.) , 1., 0.)
f = theano.function([r],gate)
x = np.arange(0.,4.,0.05,dtype='float32')
y = [f(i) for i in x]
plt.plot(x,y)
The result is the following : https://i.stack.imgur.com/XMQme.png
Which is clearly not correct : only one condition is satisfied here.
If I replace T.switch by theano.ifelse.ifelse the result is the same...
Is it a known bug, or am I missing something here?
Thanks a lot !
--
---
You received this message because you are subscribed to the Google
Groups "theano-users" group.
To unsubscribe from this group and stop receiving emails from it, send
For more options, visit https://groups.google.com/d/optout.
--
---
You received this message because you are subscribed to the Google Groups "theano-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to theano-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Sym
2017-07-04 17:00:52 UTC
Permalink
I clearly haven't searched enough for theano.tensor.and_ !

Thank you for the correction, everything is clear now :)
--
---
You received this message because you are subscribed to the Google Groups "theano-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to theano-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...