Discussion:
[theano-users] Why is Theano.ifelse.ifelse executing both tensor functions?
Ines Ayed
2017-12-14 19:21:37 UTC
Permalink
I looked up a theano variant for keras.backend.switch, beause I did not
want both operations to be executed and I found this :
https://github.com/Theano/Theano/blob/master/theano/ifelse.py

Here it says that (lazy) ifelse executes only the branch corresponding to
the condition and not both like switch. I tested it like this:

import theano

def function1():
print("function 1 is executed")
return a

def function2():
print("function 2 is executed")
return b
a = 2
b = 10
result = theano.ifelse.ifelse(T.gt(b,a), function1(), function2())

But when I run this both messages are printed which means that both
branches are executed. This is confusing since the description of ifelse
says that it should not. Am I missing something here?
--
---
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
2018-01-10 21:19:39 UTC
Permalink
Hi,

Theano is a functional programming system. To see only one branch executed,
it must be in the graph. Here is an example with the Print op that have a
printing as a side effect:

import theano.ifelse
import theano.tensor as T
a=theano.shared(2)
b=theano.shared(10)
theano.ifelse.ifelse(T.gt(b,a), theano.printing.Print("T")(a),
theano.printing.Print("F")(b)).eval()

Two thing to remember:
- this op have more overhead then switch. So only try to use it to save
significant computation
- We do not guaranty that the minimal number of node will be executed.
There is interaction with some optimization and if you want to get more
node not executed, you should disable some optimization like using this
flag: "optimizer_exclusing=inplace". Sadly, this disable more then the
minimum of optimization needed. There is no way without significant work to
disable the minimum of optimization.

Fred
Post by Ines Ayed
I looked up a theano variant for keras.backend.switch, beause I did not
https://github.com/Theano/Theano/blob/master/theano/ifelse.py
Here it says that (lazy) ifelse executes only the branch corresponding to
import theano
print("function 1 is executed")
return a
print("function 2 is executed")
return b
a = 2
b = 10
result = theano.ifelse.ifelse(T.gt(b,a), function1(), function2())
But when I run this both messages are printed which means that both
branches are executed. This is confusing since the description of ifelse
says that it should not. Am I missing something here?
--
---
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.
Loading...