TensorFlow程序使用一個tensor數據結構來代表所有數據。你可以認爲一個TensorFlow的tensor是一個n維數組或者列表。一個tensor有一個靜態類型和動態維度。只有tensor可以在計算圖節點之間被傳遞。
Rank
在TensorFlow系統中,tensor可用被稱爲“秩”(rank)的維度單元描述。Tensor的秩與矩陣的秩不一樣。Tensor的秩(有時也叫“序”(order)或者“度”(degree)又或者“n-維度”)是tensor維度的數量。例如,下面的tensor(定義爲一個Python列表)的秩爲2:
t =[[1,2,3],[4,5,6],[7,8,9]]
我們通常會把一個秩爲2的tensor看作爲一個矩陣,秩爲1的是一個向量。對於秩爲2的tensor,你可以用t[i,j]這樣的語法來獲取任意元素。而對於一個秩爲3的tensor,你需要使用t[i,j,k]來獲取一個元素。
Rank |
Math entity |
Python example |
---|
0 |
Scalar (magnitude only) |
s = 483 |
1 |
Vector (magnitude and direction) |
v = [1.1, 2.2, 3.3] |
2 |
Matrix (table of numbers) |
m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] |
3 |
3-Tensor (cube of numbers) |
t = [[[2], [4], [6]], [[8], [10], [12]], [[14], [16], [18]]] |
n |
n-Tensor (you get the idea) |
.... |
形狀
Tensorflow文檔使用三種慣用標記來描述tensor的維度:秩、形狀和維數。下表這些標記相互之間的對應關係:
Rank |
Shape |
Dimension number |
Example |
---|
0 |
[] |
0-D |
A 0-D tensor. A scalar. |
1 |
[D0] |
1-D |
A 1-D tensor with shape [5]. |
2 |
[D0, D1] |
2-D |
A 2-D tensor with shape [3, 4]. |
3 |
[D0, D1, D2] |
3-D |
A 3-D tensor with shape [1, 4, 3]. |
n |
[D0, D1, ... Dn-1] |
n-D |
A tensor with shape [D0, D1, ... Dn-1]. |
形狀可以通過Python的整型列表、元組來表示,或者通過
tf.TensorShape
。
數據類型
除了維度外,tensor還有數據類型。你可以指定tensor屬於下表中任何一種數據類型:
Data type |
Python type |
Description |
---|
DT_FLOAT |
tf.float32 |
32 bits floating point. |
DT_DOUBLE |
tf.float64 |
64 bits floating point. |
DT_INT8 |
tf.int8 |
8 bits signed integer. |
DT_INT16 |
tf.int16 |
16 bits signed integer. |
DT_INT32 |
tf.int32 |
32 bits signed integer. |
DT_INT64 |
tf.int64 |
64 bits signed integer. |
DT_UINT8 |
tf.uint8 |
8 bits unsigned integer. |
DT_UINT16 |
tf.uint16 |
16 bits unsigned integer. |
DT_STRING |
tf.string |
Variable length byte arrays. Each element of a Tensor is a byte array. |
DT_BOOL |
tf.bool |
Boolean. |
DT_COMPLEX64 |
tf.complex64 |
Complex number made of two 32 bits floating points: real and imaginary parts. |
DT_COMPLEX128 |
tf.complex128 |
Complex number made of two 64 bits floating points: real and imaginary parts. |
DT_QINT8 |
tf.qint8 |
8 bits signed integer used in quantized Ops. |
DT_QINT32 |
tf.qint32 |
32 bits signed integer used in quantized Ops. |
DT_QUINT8 |
tf.quint8 |
8 bits unsigned integer used in quantized Ops. |