博客摘自:Deep learning 第二篇 婴儿学步 Theano如何做算数?
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)
print f(2,3)
输出:
5.0
两个矩阵相加:
x=T.dmatrix('x') #注意这里使用矩阵类型进行格式化
y=T.dmatrix('y')
z=x+y
f2=function([x,y],z)
print f2([1,2],[3,4],[10,20],[30,40])
到这里,手册里面出现了3个专业名词:
scalars ----标量:只有大小,没有方向。比如欧几里几何中的两点的距离。
matrix ----矩阵:一种数学标记方法,通常由行和列组成.
vectors ---向量:也常称为矢量,即有方向的量,与标量相对。
可以用来做加法的变量:
The following types are available:
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
import numpy
import theano.tensor as T
from theano import function
#from numpy import asarray
#from PIL import Image
#im=Image.open("/Users/chaimwu/Desktop/test.png")
#im.rotate(45).show()
print "OK!"
#print random.rand(4,4)
print numpy.asarray([1,2],[3,4],[5,6]).shape
print numpy.asarray([1,2],[3,4],[5,6])[2,0]
a = numpy.asarray([1.0, 2.0, 3.0])
b=2.0
print a*b
print b*a
x=T.dscalar('x')
y=T.dscalar('y')
z=x+y
f1=function([x,y],z)
print f1(1,3)
x=T.dmatrix('x')
y=T.dmatrix('y')
z=x+y
f2=function([x,y],z)
print f2([1,2],[3,4],[10,20],[30,40])
x=T.dmatrix()
y=T.dmatrix()
z=x+2**y
f3=function([x,y],z)
print f3([1,2],[3,4],[10,20],[30,40])
x=T.dvector()
y=T.dvector()
z=2*x+2**y
f4=function([x,y],z)
print f4([2,3],[4,5])