theano程序(一)
来源:互联网 发布:linux免密码登录失败 编辑:程序博客网 时间:2024/05/22 14:24
下面内容是此文章的练习,加了一些注释和个人理解,不当之处请赐教:
#导入头文件import numpy as npimport matplotlib.pyplot as pltimport theanoimport theano.tensor as Tfrom theano import pp#symbolic variables 符号变量 编写theano代码更像写数学公式foo = T.scalar('foo') #定义一个标量bar = foo**2 #另一个变量bar2 = foo**3print type(bar) #class 'theano.tensor.var.TensorVariable'print bar.type #TensorType(float64, scalar)print pp(bar) # pretty print funtion ,bar的内容: (foo ** TensorConstant{3})#为了进行计算,需要定义符号函数#funtion函数的第一个参数表示输入,输入部分必须用中括号[ ]括起,如果输入参数为空,则用[]表示#第2个参数为函数表达式,如果有多个表达式则也必须用中括号[ ]括起,若只有一个则可括可不括#这里定义了两个函数f1 = theano.function([foo],[bar]) f2 = theano.function([foo],bar2)print f1(3)#替代函数的另一个方式:用符号变量的eval方法#eval的参数是字典形式,用“键-值对”表示print bar.eval({foo:3})#python定义的函数也可以给theano的变量赋值def square(x): return x**2bar = square(foo)print 'hi:'print bar.eval({foo:3})print square(2)#theano中有向量、矩阵等类型,在这些类型上可能施加各种操作A = T.matrix('A')x = T.vector('x')b = T.vector('b')y = T.dot(A,x) + b #与其他矩阵语言不同,NumPy中的乘法运算符*按元素逐个计算,矩阵乘法可以使用dot函数z = T.sum(A**2) #这里先求A各元素平方,然后各列相加。# theano.function函数可以同时做多个计算任务,如下所示同时计算y和z两个表示式。下面第3个参数b设置了默认值linear_mix = theano.function([A,x,theano.Param(b,default=np.array([0,0]))],[y,z])#下面的参数分别对应A,x,bprint linear_mix(np.array([[1,2,3],[4,5,6]]),np.array([1,2,3]),np.array([4,5])) #下面只有两个实参:A和x. 则第3个形参b使用默认的值 print linear_mix(np.array([[1,2,3],[4,5,6]]),np.array([1,2,3]))
0 0
- theano程序(一)
- theano 程序(二)
- theano程序(三)
- theano程序(四)
- theano程序(五)
- Theano基础(一)
- Theano 初探(一)
- Theano编程细节(一)
- theano tutorial -- 用theano实现逻辑回归LR(一)theano基础知识
- theano tutorial学习笔记(一)
- Theano深度学习(一). 概述
- Theano Tutorial文档翻译(一):词汇
- Python--Theano学习笔记(一)
- 深度学习(一)theano环境搭建
- Theano学习一----安装
- Theano 学习笔记(一)
- theano tutorial(一)
- theano学习初步(一)
- Android手机上获取其他应用的包名及版本号
- 大概的发挥地方好地方
- 数据库原理
- 程序员求职之道(《程序员面试笔试宝典》)之求职有用网站及QQ群一览表
- 程序员求职之道(《程序员面试笔试宝典》)之如何回答系统设计题?
- theano程序(一)
- [tarjan] poj 2553 The Bottom of a Graph
- 工作笔记2.软件开发常用工具
- How to Solve Magento 500 Internal Server Errors
- 程序员求职之道(《程序员面试笔试宝典》)之如何回答算法设计问题?
- QuerySet那些常用技巧
- STAIR Vision Library 安装配置心得(Windows平台下)
- ZOJ 3229 Shoot the Bullet
- 浅谈Android应用性能之内存