Theano 学习笔记 Basics:Baby Steps - Algebra
2016-02-19 20:16
393 查看
教程地址:http://deeplearning.net/software/theano/tutorial/adding.html
先定义了两个 symbols (Variables);函数 f 的输出为 a
dimensions。
当执行 f = function([x, y], z) 这句时,明显停滞了,这个时候 f 正被编译成 C 代码。
这样就创建了 f 函数,使用它:
Step 1
Type。
dscalar 不是一个类。 因此,x,y 都不是 dscalar 的实例。 它们是
当 T.dscalar 带字符串参数时,字符串是这个变量的名字;名字不是必须的,只是方便调试程序。
Step 2
可以使用
pp function to pretty-print 出:
Step 3
最后一步是创建 x,y 为输入,z 为输出的函数:
list ,第二个参数为单个变量或者变量的 list,为函数的输出。
Note:我们可以使用变量的
import function()了。
eval() 接收的 {x : 16.3, y : 12.1} 为一个字典,返回表达式的数值结果。
eval()第一次对变量使用时会比较慢,因为它会调用 function() 来编译表达式。后续对相同变量调用 eval() 就会变快了,因为变量缓存了编译好的函数。
以2D arrays为输入使用函数:
以 NumPy array 为输入:
也可以进行 scalars to matrices, vectors to matrices, scalars to vectors 的相加,详见
broadcasting ,类似 matlab 中 repmat 后相加。
如下变量类型可用:
byte:
16-bit integers:
32-bit integers:
64-bit integers:
float:
double:
complex:
Note:需要根据电脑情况人工选择 32- or 64-bit integers (
Modify and execute this code to compute this expression: a ** 2 + b ** 2 + 2 * a * b.
答案:
Adding two Scalars
>>> import numpy >>> import theano.tensor as T >>> from theano import function >>> x = T.dscalar('x') >>> y = T.dscalar('y') >>> z = x + y >>> f = function([x, y], z)
先定义了两个 symbols (Variables);函数 f 的输出为 a
numpy.ndarraywith zero
dimensions。
当执行 f = function([x, y], z) 这句时,明显停滞了,这个时候 f 正被编译成 C 代码。
这样就创建了 f 函数,使用它:
>>> f(2, 3) array(5.0) >>> numpy.allclose(f(16.3, 12.1), 28.4) True
Step 1
>>> x = T.dscalar('x') >>> y = T.dscalar('y')
T.dscalar的变量类类型是 “0-dimensional arrays (scalar) of doubles (d)”. It is a Theano
Type。
dscalar 不是一个类。 因此,x,y 都不是 dscalar 的实例。 它们是
TensorVariable的实例。但是把 theano Type
dscalar赋给了x,y 的
type属性:
>>> type(x) <class 'theano.tensor.var.TensorVariable'> >>> x.type TensorType(float64, scalar) >>> T.dscalar TensorType(float64, scalar) >>> x.type is T.dscalar True
当 T.dscalar 带字符串参数时,字符串是这个变量的名字;名字不是必须的,只是方便调试程序。
Step 2
>>> z = x + y
可以使用
pp function to pretty-print 出:
>>> from theano import pp >>> print(pp(z)) (x + y)
Step 3
最后一步是创建 x,y 为输入,z 为输出的函数:
>>> f = function([x, y], z)
function的第一个参数为输入变量组成的
list ,第二个参数为单个变量或者变量的 list,为函数的输出。
Note:我们可以使用变量的
eval方法。它虽然不想
function()一样灵活,但是已经足够应付教程中所有需求。这样就不需要
import function()了。
>>> import numpy
>>> import theano.tensor as T
>>> x = T.dscalar('x') >>> y = T.dscalar('y')>>> z = x + y>>> numpy.allclose(z.eval({x : 16.3, y : 12.1}), 28.4)
True
eval() 接收的 {x : 16.3, y : 12.1} 为一个字典,返回表达式的数值结果。
eval()第一次对变量使用时会比较慢,因为它会调用 function() 来编译表达式。后续对相同变量调用 eval() 就会变快了,因为变量缓存了编译好的函数。
Adding two Matrices
唯一的改变是实例化 x,y 时使用 matrix Types:>>> x = T.dmatrix('x')
>>> y = T.dmatrix('y')
>>> z = x + y>>> f = function([x, y], z)
以2D arrays为输入使用函数:
>>> f([[1, 2], [3, 4]], [[10, 20], [30, 40]]) array([[ 11., 22.], [ 33., 44.]])
以 NumPy array 为输入:
>>> import numpy >>> f(numpy.array([[1, 2], [3, 4]]), numpy.array([[10, 20], [30, 40]])) array([[ 11., 22.], [ 33., 44.]])
也可以进行 scalars to matrices, vectors to matrices, scalars to vectors 的相加,详见
broadcasting ,类似 matlab 中 repmat 后相加。
如下变量类型可用:
byte:
bscalar, bvector, bmatrix, brow, bcol, btensor3, btensor4
16-bit integers:
wscalar, wvector, wmatrix, wrow, wcol, wtensor3, wtensor4
32-bit integers:
iscalar, ivector, imatrix, irow, icol, itensor3, itensor4
64-bit integers:
lscalar, lvector, lmatrix, lrow, lcol, ltensor3, ltensor4
float:
fscalar, fvector, fmatrix, frow, fcol, ftensor3, ftensor4
double:
dscalar, dvector, dmatrix, drow, dcol, dtensor3, dtensor4
complex:
cscalar, cvector, cmatrix, crow, ccol, ctensor3, ctensor4
a guide to all types compatible with NumPy arrays may be found here: tensor creation
Note:需要根据电脑情况人工选择 32- or 64-bit integers (
iprefix vs. the
lprefix) and floats (
fprefix vs. the
dprefix).
Exercise
import theano a = theano.tensor.vector() # declare variable out = a + a ** 10 # build symbolic expression f = theano.function([a], out) # compile function print(f([0, 1, 2]))
[ 0. 2. 1026.]
Modify and execute this code to compute this expression: a ** 2 + b ** 2 + 2 * a * b.
答案:
import theano a = theano.tensor.vector() # declare variable b = theano.tensor.vector() # declare variable out = a ** 2 + b ** 2 + 2 * a * b # build symbolic expression f = theano.function([a, b], out) # compile function print(f([1, 2], [4, 5])) # prints [ 25. 49.]
相关文章推荐
- ZOJ 2849 Attack of Panda Virus
- Calculator
- leetcode:Rotate List
- Linux 源码安装Python
- 新手必备的常用代码片段整理(一)
- [Locked] Number of Connected Components in an Undirected Graph
- jsp 界面注册、登录按钮的实现
- IOS 文件系统
- 安卓 自动弹出键盘
- hrbust 1176 小陈老师、雪人【map+优先队列】
- 部署Apache网站服务
- 安卓 单例设计模式 查看方法
- HDU 4602 Partition
- CTeX终于要更新了
- BadUSB的防范研究
- php5.6 源码安装过程
- python 编码问题
- 金额转换,阿拉伯数字转换成中国传统形式。例如:101000001010 转换为 壹仟零壹拾亿零壹仟零壹拾圆整
- 有100个人围成一个圈,从1开始报数,报到14的这个人就要退出, 然后其他人重新开始,从1报数,到14退出。问:最后剩下的是100人中的第几个人?
- markdown学习2:Markdown编辑器之比较