http://deeplearning.net/software/theano/tutorial/adding.html
常见的数据类型:
- 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
注意:You, the user—not the system architecture—have to choose whether your program will use 32- or 64-bit integers (i
prefix vs. the l
prefix) and floats (f
prefix vs. the d
prefix).
add两个dscalar:
定义两个dscalar变量:
可以使用 pp function to pretty-print out the computation associated to z:
function第一个参数为输入,第二个参数为输出: The first argument to
function
is a list of Variables that will be provided as inputs to the function. The second argument is a single Variable
or
a list of Variables. For either case, the second argument is what we want to see as output when we apply the function.
f
may then be used like a normal Python function.
调用定义的函数,进行加法运算:
回顾整个代码:
theano中tensor下的内容,以张量处理为主,操作对象是符号变量。比如,T.grad处理的是一个代数式,得到的也是一个代数式。
theano目录下的function,scan处理的是数据,如function定义了输入数据,输出数据,运算公式。scan可以看做对做循环计算的function函数。
最后,总结代码风格:由于是符号运算,所以不会出现我们常见的x=2,y=3这样的直接赋值,以及z=2+3这样的直接运算;必须:
【首先】将 想要运算的quantity 定义成对应的符号(symbol,或者称variable),x=T.dscalar('x')
【其次】将 在这些quantity上进行的运算 同样定义成相应的符号,z=x+y
【最后】通过定义function,将 结果变量(符号) 和 需要运算的变量(符号) 关联起来,f=function([x,y], z)
【最最后】调用定义的function,将实际的数值传递进去,进行相应运算,a=f(2,3)
讲到function,再提一点关于【延迟执行】的做法:If you are following along and typing into an interpreter, you may have noticed that there was a slight delay in executing the function
instruction. Behind the scene, f was being compiled into C code.