问题
I am not able to understand anything about them, I read it from http://deeplearning.net/software/theano/tutorial/conditions.html. I suppose they function like our ifelse but not able to understand how to use them. Please some guide on this. Thanks
回答1:
I would think of it as just another operator that acts on three symbolic variables, if the first is true, return the second, else return the third.
But for many operators (like -
and +
) theano has overloaded them for symbolic variables, so probably you don't feel the difference.
For example, if a
and b
are numbers, then c=a+b
creates a variable c
with the value of a+b
. If a
and b
are symbolic variables, then c=a+b
creates another symbolic variable c
, that will apply (element-wise) addition to a
and b
when the corresponded function gets called/evaluated.
Here's an introduction on theano operators and graphs. http://deeplearning.net/software/theano/extending/graphstructures.html
The ternary operator is python's native equivalent to theano's ifelse
or switch
for symbolic variables in some sense.
回答2:
The code is copied from the link to be explained easier: http://deeplearning.net/software/theano/extending/graphstructures.html
from theano import tensor as T
from theano.ifelse import ifelse
import theano, time, numpy
a,b = T.scalars('a','b')
x,y = T.matrices('x','y')
z_switch = T.switch(T.lt(a,b), T.mean(x), T.mean(y))
z_lazy = ifelse(T.lt(a,b), T.mean(x), T.mean(y))
f_switch = theano.function([a,b,x,y], z_switch,
mode=theano.Mode(linker='vm'))
f_lazyifelse = theano.function([a,b,x,y], z_lazy,
mode=theano.Mode(linker='vm'))
val1 = 0.
val2 = 1.
big_mat1 = numpy.ones((10000,1000))
big_mat2 = numpy.ones((10000,1000))
n_times = 10
tic = time.clock()
for i in range(n_times):
f_switch(val1, val2, big_mat1, big_mat2)
print('time spent evaluating both values %f sec' % (time.clock()-tic))
tic = time.clock()
for i in range(n_times):
f_lazyifelse(val1, val2, big_mat1, big_mat2)
print('time spent evaluating one value %f sec' % (time.clock()-tic))
In the example, both f_switch(val1, val2, big_mat1, big_mat2) and f_lazyifelse(val1, val2, big_mat1, big_mat2) are doing same thing. If val1 is "Less Than" val2, it returns big_mat1 otherwise big_mat2.
However there is a difference between "switch" and "ifelse" in performance. In "ifelse" depending to the condition, only one of the possible outputs will be evaluated. In this example, in f_lazyifelse(val1, val2, big_mat1, big_mat2) , if val1 is less than val2 then only big_mat1 will be evaluated which is performing mean function on all elements of a big matrix, and big_mat2 will not be evaluated.
On the other hand, in case of "switch" regardless of the condition both the possible outputs will be evaluated, and in the example, in f_switch(val1, val2, big_mat1, big_mat2), regardless of val1 and val2 both big_mat1 and big_mat2 will be evaluated (here, performing mean function on all elements of a big matrix). Therefore "switch" is almost takes two times of "ifelse" to perform.
来源:https://stackoverflow.com/questions/35823165/what-is-purpose-of-ifelse-and-switch-in-theano