版權聲明:本文為博主原創文章,未經博主允許不得轉載。
目錄(?)[+]
本文來自于官方文檔
Theano有多種方式進行聲明變量。變量也可以進行命名,從而便于debug,而且每一種聲明方式都能夠接受name
的參數。 下面這三種聲明方式,聲明的是0維的整型變量,它的名字是myvar
返回一個0維的numpy.ndarray
。
返回一個1維的numpy.ndarray
。
返回一個2維的numpy.ndarray
,但是行數保證是1。
返回一個2維的numpy.ndarray
,但是列數保證是1。
返回一個2維的numpy.ndarray
。
返回一個3維的numpy.ndarray
。
返回一個4維的numpy.ndarray
。
上文說的dtype
在Theano中都有定義。
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) |
用法參考如下
from theano.tensor import *x, y, z = dmatrices(3) # creates three matrix Variables with no namesx, y, z = dmatrices('x', 'y', 'z') # creates three matrix Variables named 'x', 'y' and 'z'12341234如果你想要創建一個非標準的類型,那么就只能創造一個你自己定義的TensorType
。你需要將dtype
和broadcasting pattern
傳入聲明函數中。 下面的例子是,自創一個五維向量。
你也可以重構一個已存在的類型
my_dmatrix = TensorType('float64', (False,)*2)x = my_dmatrix() # allocate a matrix variablePRint my_dmatrix == dmatrix # output is 'True'123123他們會很好地結合起來。
使用的是shared()
函數。
這個函數似乎有一些細節,雖然可以這么轉化,但是缺少上面聲明的一些功能。
返回一個lvector用于表示x的shape
theano.tensor.reshape(x, newshape, ndim=None)11Parameters:
x (某種TensorVariable (或者是可兼容的類型)) – 要進行reshape的變量newshape (lvector (或者是可兼容的類型)) – x的新形狀ndim – 可選的- the length that newshape‘s value will have. If this is None, then reshape() will infer it from newshape.Return type:
variable with x’s dtype, but ndim dimensions新聞熱點
疑難解答