Theano - 資料型別



現在,您已經瞭解了 Theano 的基礎知識,讓我們開始學習建立表示式時可用的不同資料型別。下表列出了 Theano 中定義的部分資料型別。

資料型別 Theano 型別
位元組

bscalar, bvector, bmatrix, brow, bcol, btensor3, btensor4, btensor5, btensor6, btensor7

16 位整數

wscalar, wvector, wmatrix, wrow, wcol, wtensor3, wtensor4, wtensor5, wtensor6, wtensor7

32 位整數

iscalar, ivector, imatrix, irow, icol, itensor3, itensor4, itensor5, itensor6, itensor7

64 位整數

lscalar, lvector, lmatrix, lrow, lcol, ltensor3, ltensor4, ltensor5, ltensor6, ltensor7

浮點數

fscalar, fvector, fmatrix, frow, fcol, ftensor3, ftensor4, ftensor5, ftensor6, ftensor7

雙精度浮點數

dscalar, dvector, dmatrix, drow, dcol, dtensor3, dtensor4, dtensor5, dtensor6, dtensor7

複數

cscalar, cvector, cmatrix, crow, ccol, ctensor3, ctensor4, ctensor5, ctensor6, ctensor7

以上列表並不完整,讀者可以參考張量建立文件以獲取完整列表。

我現在將給出一些如何在 Theano 中建立各種資料型別變數的示例。

標量

要構造一個標量變數,可以使用以下語法:

語法

x = theano.tensor.scalar ('x')
x = 5.0
print (x)

輸出

5.0

一維陣列

要建立一個一維陣列,請使用以下宣告:

示例

f = theano.tensor.vector
f = (2.0, 5.0, 3.0)
print (f)f = theano.tensor.vector
f = (2.0, 5.0, 3.0)
print (f)
print (f[0])
print (f[2])

輸出

(2.0, 5.0, 3.0)
2.0
3.0

如果您執行 f[3],它將生成一個索引超出範圍的錯誤,如下所示:

print f([3])

輸出

IndexError                          Traceback (most recent call last)
<ipython-input-13-2a9c2a643c3a> in <module>
   4 print (f[0])
   5 print (f[2])
----> 6 print (f[3])
IndexError: tuple index out of range

二維陣列

要宣告一個二維陣列,可以使用以下程式碼片段:

示例

m = theano.tensor.matrix
m = ([2,3], [4,5], [2,4])
print (m[0])
print (m[1][0])

輸出

[2, 3]
4

5 維陣列

要宣告一個 5 維陣列,請使用以下語法:

示例

m5 = theano.tensor.tensor5
m5 = ([0,1,2,3,4], [5,6,7,8,9], [10,11,12,13,14])
print (m5[1])
print (m5[2][3])

輸出

[5, 6, 7, 8, 9]
13

您可以使用資料型別 tensor3 代替 tensor5 來宣告一個 3 維陣列,使用資料型別 tensor4 來宣告一個 4 維陣列,依此類推,直到 tensor7

複數構造器

有時,您可能希望在一個宣告中建立相同型別的多個變數。您可以使用以下語法:

語法

from theano.tensor import * x, y, z = dmatrices('x', 'y', 'z') 
x = ([1,2],[3,4],[5,6]) 
y = ([7,8],[9,10],[11,12]) 
z = ([13,14],[15,16],[17,18]) 
print (x[2]) 
print (y[1]) 
print (z[0])

輸出

[5, 6] 
[9, 10] 
[13, 14]
廣告