【theano-windows】学习笔记四——theano中的条件语句
来源:互联网 发布:c语言打印long 编辑:程序博客网 时间:2024/05/16 05:04
前言
按照官网教程,学习条件语句Switch
和ifelse
的使用
参考地址: conditions
性能对比
ifElse
是将布尔变量和两个变量当做输入
Switch
是将张量和两个变量当做输入. 因为Switch
是元素级操作,所以比ifElse
更具一般性
Switch
需要对所有输出变量进行评估,ifElse
只需要计算条件中一个变量的结果
用法
需要实现的表达式
使用
Switch
语句#使用Switcha,b=T.dscalars('a','b')x,y=T.dmatrices('a','b')z_switch=T.switch(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'))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))#time spent evaluating both values 0.232891 sec
使用
ifelse
语句#使用ifElsefrom theano.ifelse import ifelse#一定要用引入的,不能用theano.ifelse, 会出问题a,b=T.dscalars('a','b')x,y=T.dmatrices('a','b')z_lazy = ifelse(T.lt(a, b), T.mean(x), T.mean(y))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_lazyifelse(val1,val2,big_mat1,big_mat2)print('time spent evaluating both values %f sec' % (time.clock() - tic))#time spent evaluating both values 0.122027 sec
可以发现由于开头说的
ifelse
比较懒,只执行一条, 所以时间短
【PS】怎么感觉没C++
的switch
那么强大, 还是两个选择, 以后见到其他用法再补充, 一步步学
这篇博客划划水, 就学学这两个条件语句的使用, 目前除了性能差别, 在功能上并未发现区别, 都是根据第一个表达式去决定选择第二个参数或是第三个参数, 如果有复杂的功能, 后面肯定会接触到, 暂时就这样.
然而比较好玩的是看到了两个函数T.mean()
,T.lt()
, 感觉theano.tensor
应该有很多功能, 下一篇博客就摘取一些感觉比较重要的函数贴出来.
本篇博客code: 链接: https://pan.baidu.com/s/1o8Sd9VG 密码: 8t8f
阅读全文
0 0
- 【theano-windows】学习笔记四——theano中的条件语句
- 【theano-windows】学习笔记一——theano中的变量
- 【theano-windows】学习笔记三——theano中的导数
- 【theano-windows】学习笔记二——theano中的函数和共享参数
- 【theano-windows】学习笔记六——theano中的循环函数scan
- 【theano-windows】学习笔记十七——梯度中的consider_constant
- 【theano-windows】学习笔记五——theano中张量部分函数
- 【theano-windows】学习笔记十一——theano中与神经网络相关函数
- Theano学习笔记(四)——导数
- Theano学习笔记(四)——导数
- Theano学习笔记(四)——导数
- Theano学习四----Theano shared
- 【theano-windows】学习笔记七——logistic回归
- 【theano-windows】学习笔记八——预备知识
- 【theano-windows】学习笔记十二——卷积神经网络
- 【theano-windows】学习笔记十五——受限玻尔兹曼机
- theano学习笔记(1)—代数
- Theano学习笔记(六)——载入与保存、条件
- 数据结构-链表,队列,栈
- 蓝牙4.0BLE抓包(一)
- COGS 2580. [HZOI 2015]偏序 II (CDQ分治+BIT)
- 基于Kinect体感遥控多功能小车
- solr 相似匹配
- 【theano-windows】学习笔记四——theano中的条件语句
- linux下 多线程的使用
- 30 个很棒的 PHP 开源 CMS 内容管理系统
- Maven缺少JDK 编译插件报错 --- DescriptionResourcePathLocation Type
- oracle中一个表update慢的优化
- 安全发布和安全初始化
- javaseday36(dom基本概念 名称空间 通过document 获取节点多种方式)
- MIPS架构的传奇演化
- 收获,不止SQL优化——抓住SQL的本质--第八章