Conditions
- Both ops build a condition over symbolic variables.
Switch
takes a tensor as condition and two variables as inputs.switch
is an elementwise operation and is thus more general thanifelse
.- Whereas
switch
evaluates both output variables, is lazy and onlyevaluates one variable with respect to the condition.
In this example, the IfElse
op spends less time (about half as much) than Switch
since it computes only one variable out of the two.
- time spent evaluating both values 0.6700 sec
- time spent evaluating one value 0.3500 sec
There is no automatic optimization replacing a switch
with abroadcasted scalar to an ifelse
, as this is not always faster. Seethis ticket.
If you use , then all branches ofthe IfElse will be computed. This is normal, as using test_valuemeans everything will be computed when we build it, due to Python’sgreedy evaluation and the semantic of test value. As we build bothbranches, they will be executed for test values. This doesn’t causeany changes during the execution of the compiled Theano function.