In [13]:
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 xrange(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 xrange(n_times):
    f_lazyifelse(val1, val2, big_mat1, big_mat2)
print 'time spent evaluating one value %f sec' % (time.clock() - tic)


time spent evaluating both values 0.228487 sec
time spent evaluating one value 0.109198 sec

In [5]:
from theano import tensor as T
from theano.ifelse import ifelse
import theano, time, numpy
a, b = T.scalars('a', 'b')

T.lt(2,4)


Out[5]:
Elemwise{lt,no_inplace}.0

In [ ]: