參考: http://deeplearning.net/software/theano_versions/dev/tutorial/adding.htmlhtml
歷來沒見過這樣定義函數的方式, 這編程技巧太強大了.python
先定義2個symbol(x,y), 而後直接描述表達式(z=x+y),最後用function定義函數出來.編程
import numpy import theano.tensor as T from theano import function from theano import pp ################################################# x = T.dscalar('x') x.type # TensorType(float64, scalar) y = T.dscalar('y') z = x + y # Elemwise{add,no_inplace}.0 f = function([x, y], z) f(2, 3) # array(5.0) numpy.allclose(f(16.3, 12.1), 28.4) # True pp(z) # (x + y) ################################################# x = T.dvector('x') y = T.dvector('y') z = x + y f = function([x, y], z) f([1, 2], [10, 20]) # [ 11. 22.] ################################################# x = T.dmatrix('x') y = T.dmatrix('y') z = x + y f = function([x, y], z) f([[1, 2], [3, 4]], [[10, 20], [30, 40]]) # [[ 11. 22.] [ 33. 44.]]
Constructor | dtype | ndim | shape | broadcastable |
---|---|---|---|---|
bscalar | int8 | 0 | () | () |
bvector | int8 | 1 | (?,) | (False,) |
brow | int8 | 2 | (1,?) | (True, False) |
bcol | int8 | 2 | (?,1) | (False, True) |
bmatrix | int8 | 2 | (?,?) | (False, False) |
btensor3 | int8 | 3 | (?,?,?) | (False, False, False) |
btensor4 | int8 | 4 | (?,?,?,?) | (False, False, False, False) |
btensor5 | int8 | 5 | (?,?,?,?,?) | (False, False, False, False, False) |
wscalar | int16 | 0 | () | () |
wvector | int16 | 1 | (?,) | (False,) |
wrow | int16 | 2 | (1,?) | (True, False) |
wcol | int16 | 2 | (?,1) | (False, True) |
wmatrix | int16 | 2 | (?,?) | (False, False) |
wtensor3 | int16 | 3 | (?,?,?) | (False, False, False) |
wtensor4 | int16 | 4 | (?,?,?,?) | (False, False, False, False) |
wtensor5 | int16 | 5 | (?,?,?,?,?) | (False, False, False, False, False) |
iscalar | int32 | 0 | () | () |
ivector | int32 | 1 | (?,) | (False,) |
irow | int32 | 2 | (1,?) | (True, False) |
icol | int32 | 2 | (?,1) | (False, True) |
imatrix | int32 | 2 | (?,?) | (False, False) |
itensor3 | int32 | 3 | (?,?,?) | (False, False, False) |
itensor4 | int32 | 4 | (?,?,?,?) | (False, False, False, False) |
itensor5 | int32 | 5 | (?,?,?,?,?) | (False, False, False, False, False) |
lscalar | int64 | 0 | () | () |
lvector | int64 | 1 | (?,) | (False,) |
lrow | int64 | 2 | (1,?) | (True, False) |
lcol | int64 | 2 | (?,1) | (False, True) |
lmatrix | int64 | 2 | (?,?) | (False, False) |
ltensor3 | int64 | 3 | (?,?,?) | (False, False, False) |
ltensor4 | int64 | 4 | (?,?,?,?) | (False, False, False, False) |
ltensor5 | int64 | 5 | (?,?,?,?,?) | (False, False, False, False, False) |
dscalar | float64 | 0 | () | () |
dvector | float64 | 1 | (?,) | (False,) |
drow | float64 | 2 | (1,?) | (True, False) |
dcol | float64 | 2 | (?,1) | (False, True) |
dmatrix | float64 | 2 | (?,?) | (False, False) |
dtensor3 | float64 | 3 | (?,?,?) | (False, False, False) |
dtensor4 | float64 | 4 | (?,?,?,?) | (False, False, False, False) |
dtensor5 | float64 | 5 | (?,?,?,?,?) | (False, False, False, False, False) |
fscalar | float32 | 0 | () | () |
fvector | float32 | 1 | (?,) | (False,) |
frow | float32 | 2 | (1,?) | (True, False) |
fcol | float32 | 2 | (?,1) | (False, True) |
fmatrix | float32 | 2 | (?,?) | (False, False) |
ftensor3 | float32 | 3 | (?,?,?) | (False, False, False) |
ftensor4 | float32 | 4 | (?,?,?,?) | (False, False, False, False) |
ftensor5 | float32 | 5 | (?,?,?,?,?) | (False, False, False, False, False) |
cscalar | complex64 | 0 | () | () |
cvector | complex64 | 1 | (?,) | (False,) |
crow | complex64 | 2 | (1,?) | (True, False) |
ccol | complex64 | 2 | (?,1) | (False, True) |
cmatrix | complex64 | 2 | (?,?) | (False, False) |
ctensor3 | complex64 | 3 | (?,?,?) | (False, False, False) |
ctensor4 | complex64 | 4 | (?,?,?,?) | (False, False, False, False) |
ctensor5 | complex64 | 5 | (?,?,?,?,?) | (False, False, False, False, False) |
zscalar | complex128 | 0 | () | () |
zvector | complex128 | 1 | (?,) | (False,) |
zrow | complex128 | 2 | (1,?) | (True, False) |
zcol | complex128 | 2 | (?,1) | (False, True) |
zmatrix | complex128 | 2 | (?,?) | (False, False) |
ztensor3 | complex128 | 3 | (?,?,?) | (False, False, False) |
ztensor4 | complex128 | 4 | (?,?,?,?) | (False, False, False, False) |
ztensor5 | complex128 | 5 | (?,?,?,?,?) | (False, False, False, False, False) |