1.导入包
1 import numpy as np 2 import theano.tensor as T 3 from theano import function
2.定义
1 x = T.dscalarr('x') #名字为x的常量 2 y = T.dscalarr('y') 3 z = x+y #常量相加 4 5 #把上面的结构(小部件)放在function中,在调用时再放入数据 6 f = function([x,y],z) #输入量、输出量
3.执行
1 print(f(2,3)) #调用function
4.结果:

#to pretty-print the function
#function代表的东西
1 from theano import pp #pretty-print 2 print(pp(z))
结果:

#how about matrix(矩阵)
1 x = T.dmatrix('x') #dmatrix-float64 fmatrix-float32 2 x = T.dmatrix('x') 3 z = x+y #T.dot()矩阵乘法 4 f = function([x,y],z)
执行:
1 print(f(np.arange(12).reshape((3,4)), 2 10*np.ones((3,4)))) 3 #3行4列 4 #np.ones()全为1
结果:

浙公网安备 33010602011771号