Why is Theano.ifelse.ifelse executing both tensor functions?

Skip to first unread message

Ines Ayed

Dec 14, 2017, 10:01:40 AM12/14/17
to theano-dev
I looked up a theano variant for keras.backend.switch, beause I did not want both operations to be executed and I found this :

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?

Brandon T. Willard

May 27, 2018, 2:47:46 PM5/27/18
to theano-dev

What you’re seeing is most likely a property of Python and not Theano. In your example, function1 and function2 are evaluated when the call to theano.ifelse.ifelse is made. See Calls, which says:

All argument expressions are evaluated before the call is attempted.

Reply all
Reply to author
0 new messages