您的位置:首页 > 其它

Theano 学习笔记 Basics:Baby Steps - Algebra

2016-02-19 20:16 393 查看
教程地址:http://deeplearning.net/software/theano/tutorial/adding.html

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.ndarray
with 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 (
i
prefix vs. the
l
prefix) and floats (
f
prefix vs. the
d
prefix).

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.]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: