【TensorFlow】筆記:基礎(chǔ)知識-張量操作(三)
TensorFlow 使用?張量?(Tensor)作為數(shù)據(jù)的基本單位。
今天學(xué)習(xí)張量操作的形狀、DTypes、廣播、conver_to_tensor
01
操作形狀
首先,我們創(chuàng)建一個張量
var_x = tf.Variable(tf.constant([[1], [2], [3]]))print(var_x.shape)# output(3, 1)
我們也可以將該對象轉(zhuǎn)換為Python列表
print(var_x.shape.as_list())# output[3, 1]
通過重構(gòu)可以改變張量的形狀。重構(gòu)的速度很快,資源消耗很低,因為不需要復(fù)制底層數(shù)據(jù)。
reshaped = tf.reshape(var_x, [1, 3])print(var_x.shape)print(reshaped.shape)# output(3, 1)(1, 3)
數(shù)據(jù)在內(nèi)存中的布局保持不變,同時使用請求的形狀創(chuàng)建一個指向同一數(shù)據(jù)的新張量。TensorFlow 采用 C 樣式的“行優(yōu)先”內(nèi)存訪問順序,即最右側(cè)的索引值遞增對應(yīng)于內(nèi)存中的單步位移。
tensor = tf.constant([[],[]],[],[]],[],[]],])print(tensor)????tf.Tensor([][]][][]][][]]], shape=(3, 2, 5), dtype=int32)
如果展平張量,則可以看到它在內(nèi)存中的排列順序。
print(tf.reshape(tensor, [-1]))#?outputtf.Tensor([ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29], shape=(30,), dtype=int32)
一般來說,tf.reshape?唯一合理的用途是用于合并或拆分相鄰軸(或添加/移除?1)。
對于 3x2x5 張量,重構(gòu)為 (3x2)x5 或 3x(2x5) 都合理,因為切片不會混淆:
print(tf.reshape(tensor, [3*2, 5]), "\n")print(tf.reshape(tensor, [3, -1]))# outputtf.Tensor([[ 0 1 2 3 4][ 5 6 7 8 9][10 11 12 13 14][15 16 17 18 19][20 21 22 23 24][25 26 27 28 29]], shape=(6, 5), dtype=int32)tf.Tensor([[ 0 1 2 3 4 5 6 7 8 9][10 11 12 13 14 15 16 17 18 19][20 21 22 23 24 25 26 27 28 29]], shape=(3, 10), dtype=int32)

重構(gòu)可以處理總元素個數(shù)相同的任何新形狀,但是如果不遵從軸的順序,則不會發(fā)揮任何作用。
02
DTypes詳解
使用?Tensor.dtype?屬性可以檢查?tf.Tensor?的數(shù)據(jù)類型。
從 Python 對象創(chuàng)建?tf.Tensor?時,您可以選擇指定數(shù)據(jù)類型。
如果不指定,TensorFlow 會選擇一個可以表示您的數(shù)據(jù)的數(shù)據(jù)類型。TensorFlow 將 Python 整數(shù)轉(zhuǎn)換為?tf.int32,將 Python 浮點數(shù)轉(zhuǎn)換為?tf.float32。另外,當(dāng)轉(zhuǎn)換為數(shù)組時,TensorFlow 會采用與 NumPy 相同的規(guī)則。
數(shù)據(jù)類型可以相互轉(zhuǎn)換。
the_f64_tensor = tf.constant([2.2, 3.3, 4.4], dtype=tf.float64)the_f16_tensor = tf.cast(the_f64_tensor, dtype=tf.float16)# 現(xiàn)在,讓我們轉(zhuǎn)換為uint8并失去小數(shù)精度the_u8_tensor = tf.cast(the_f16_tensor, dtype=tf.uint8)print(the_u8_tensor)# outputtf.Tensor([2 3 4], shape=(3,), dtype=uint8)
03
廣播
廣播是從 NumPy 中的等效功能借用的一個概念。簡而言之,在一定條件下,對一組張量執(zhí)行組合運算時,為了適應(yīng)大張量,會對小張量進行“擴展”。
最簡單和最常見的例子是嘗試將張量與標(biāo)量相乘或相加。在這種情況下會對標(biāo)量進行廣播,使其變成與其他參數(shù)相同的形狀。
x?=?tf.constant([1,?2,?3])y = tf.constant(2)z = tf.constant([2, 2, 2])#?這些都是相同的運算print(tf.multiply(x, 2))print(x * y)print(x * z)# outputtf.Tensor([2 4 6], shape=(3,), dtype=int32)tf.Tensor([2 4 6], shape=(3,), dtype=int32)tf.Tensor([2 4 6], shape=(3,), dtype=int32)
同樣,可以擴展大小為 1 的維度,使其符合其他參數(shù)。在同一個計算中可以同時擴展兩個參數(shù)。
在本例中,一個 3x1 的矩陣與一個 1x4 進行元素級乘法運算,從而產(chǎn)生一個 3x4 的矩陣。注意前導(dǎo) 1 是可選的:y 的形狀是?[4]。
x = tf.reshape(x,[3,1])y = tf.range(1, 5)print(x, "\n")print(y, "\n")print(tf.multiply(x, y))# outputtf.Tensor([[1][2][3]], shape=(3, 1), dtype=int32)tf.Tensor([1 2 3 4], shape=(4,), dtype=int32)tf.Tensor([[ 1 2 3 4][ 2 4 6 8][ 3 6 9 12]], shape=(3, 4), dtype=int32)
廣播相加:[3, 1] 乘以?[1, 4]的結(jié)果是[3, 4]

04
tf.convert_to_tensor
大部分運算(如?tf.matmul?和?tf.reshape)會使用?tf.Tensor?類的參數(shù)。不過,在上面的示例中,您會發(fā)現(xiàn)我們經(jīng)常傳遞形狀類似于張量的 Python 對象。
大部分(但并非全部)運算會在非張量參數(shù)上調(diào)用?convert_to_tensor。我們提供了一個轉(zhuǎn)換注冊表,大多數(shù)對象類(如 NumPy 的?ndarray、TensorShape、Python 列表和?tf.Variable)都可以自動轉(zhuǎn)換。
參考文獻:文檔主要參考TensorFlow官網(wǎng)
點擊上方“藍字”關(guān)注本公眾號
點擊上方“藍字”關(guān)注本公眾號
?END
掃碼關(guān)注
微信號|sdxx_rmbj
