python中if else语句用法_科学网—python中的ifelse以及switch - 于博的博文

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的稀疏矩阵

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值