python中的ifelse以及switch
ifelse以及switch在python中的用法,其中switch在python里可以用字典的形式表示
result=‘a’:lambda x:x*2......大括号打不出来,此处省略了~~~
import theano.tensor as T
from theano.ifelse import ifelse
import theano,time,numpy
from theano import function
a,b=T.dscalars('a','b')
x,y=T.dmatrices('x','y')
z_switch=T.switch(T.lt(a,b),T.mean(x),T.mean(y))
z_ifelse=ifelse(T.lt(a,b),T.mean(x),T.mean(y))
f_switch=function([a,b,x,y],z_switch,mode=theano.Mode(linker='vm'))
f_ifelse=function([a,b,x,y],z_ifelse,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_ifelse(val1,val2,big_mat1,big_mat2)
print'time spent evaluating one value %f sec'%(time.clock()-tic)
转载本文请联系原作者获取授权,同时请注明本文来自于博科学网博客。
链接地址:http://blog.sciencenet.cn/blog-571755-707369.html
上一篇:python theano中文件的加载和关闭
下一篇:python theano的稀疏矩阵