TensorFlow深度學(xué)習(xí)之實現(xiàn)合并與分割的示例代碼
寫在前面
本文主要介紹了如下四個函數(shù)的參數(shù)意義及其函數(shù)用法,如有不恰當(dāng)?shù)牡胤?,還請不吝指正?。?!
- tf.concat( )
- tf.stack( )
- tf.unstack( )
- tf.split( )
一、tf.concat( )函數(shù)
( 1 )簡單介紹
tf.concat() 函數(shù)是 TensorFlow 中用于連接張量的函數(shù)。它可以將多個張量沿指定的軸連接在一起,形成一個新的張量。
tf.concat(values, axis)
- values 是一個張量列表[tensor1, tensor2, tensor3, ...],表示需要連接的張量。
- axis 是一個整數(shù),可正可負(fù),表示連接軸的方向。
( 2 )學(xué)會使用
分別定義兩個二維,形狀 shape = (2, 3) 的張量,使它們在第一個維度上面拼接,
將第一個維度的中括號打開:
[ [1, 2, 3], [4, 5, 6] ] ——> [1, 2, 3], [4, 5, 6]
[ [7, 8, 9], [10, 11, 12] ] ——> [7, 8, 9], [10, 11, 12]
合并后中括號還原:
[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12] ——> [ [1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12] ]
import tensorflow as tf tensor1 = tf.constant([[1, 2, 3], [4, 5, 6]]) tensor2 = tf.constant([[7, 8, 9], [10, 11, 12]]) tensor = tf.concat([tensor1, tensor2], 0) print(tensor.shape) # (4, 3)
如果將上面的兩個張量在第2個維度上面拼接,又該怎樣做呢?
在第二個維度的拼接,不干擾第一個維度的張量,
將第二個維度的中括號打開:
[ [1, 2, 3], [4, 5, 6] ] ——> [ 1, 2, 3, 4, 5, 6]
[ [7, 8, 9], [10, 11, 12] ] ——> [ 7, 8, 9, 10, 11, 12]
將1, 2, 3與7, 8, 9對應(yīng),將4, 5, 6與10, 11, 12對應(yīng)拼接,合并后中括號還原
[ 1, 2, 3, 7, 8, 9], [ 4, 5, 6, 10, 11, 12] ——> [ [ 1, 2, 3, 7, 8, 9], [ 4, 5, 6, 10, 11, 12] ]
再舉一個例子,還是這個三維的張量理解成【班級,學(xué)生,科目】,axis 分別對應(yīng)0, 1, 2,或-3, -2, -1
import tensorflow as tf # 收集4個班級35名學(xué)生的8個科目的成績 tensor1 = tf.ones([4, 35, 8]) # 收集2個班級35名學(xué)生的8個科目的成績 tensor2 = tf.ones([2, 35, 8]) # 在班級維度上拼接張量, tensor = tf.concat([tensor1, tensor2], axis=0) print(tensor.shape) # 輸出:(6, 35, 8)
理解方式和上面的方法相同,如果單看結(jié)果還是很簡單的
此函數(shù)操作后不產(chǎn)生新的維度,下面的stack()函數(shù)就會產(chǎn)生新的維度
二、tf.stack( )函數(shù)
( 1 )簡單介紹
tf.stack() 函數(shù)用于沿新的維度堆疊張量。這個新維度將用于堆疊其他張量。
tf.stack(values, axis=0,)
- values: 是一個張量列表[tensor1, tensor2, tensor3, ...],表示需要連接的張量,它們將沿著新的維度堆疊在一起。這些張量應(yīng)該有相同的形狀。
- axis: 一個整數(shù),可正可負(fù),表示沿哪個維度堆疊張量。
( 2 )代碼示例
import tensorflow as tf # 創(chuàng)建兩個形狀都為 [2, 3] 的張量 tensor1 = tf.constant([[1, 2, 3], [4, 5, 6]]) tensor2 = tf.constant([[7, 8, 9], [10, 11, 12]]) # 使用 tf.stack() 函數(shù)沿 axis = 0 將兩個張量堆疊在一起 stacked_tensor = tf.stack([tensor1, tensor2], axis=0) print(stacked_tensor) # 輸出 tf.Tensor( [[[ 1 2 3] [ 4 5 6]] [[ 7 8 9] [10 11 12]]], shape=(2, 2, 3), dtype=int32)
三、tf.unstack( )函數(shù)
tf.unstack() 用于將輸入的 Tensor 張量沿著指定的軸(axis)進(jìn)行分解。將指定維度全部拆分,該維度的shape是多少就拆分多少個。
tf.unstack(value, axis)
- value: 一個多維 Tensor 張量。
- axis: 一個整數(shù),表示要將輸入 Tensor 張量沿哪個軸(axis)進(jìn)行分解。如果 axis 的值為負(fù)數(shù),則從反向軸(倒數(shù)軸)開始分解。如果同時提供了 axis 和 num 參數(shù),則優(yōu)先使用 axis 參數(shù)。
( 2 )重點理解
tf.unstack 的作用是將輸入的 Tensor 張量沿著指定的軸(axis)分解成多個 Tensor 張量。例如,如果輸入的 Tensor 張量形狀為 [2, 4, 35, 8],沿軸 8(第四維)進(jìn)行分解,則會得到8個形狀為 [2, 4, 35] 的 Tensor 張量。
import tensorflow as tf tensor1 = tf.ones([4, 35, 8]) tensor = tf.unstack(tensor1, axis=2) print(tensor[0].shape, tensor[3].shape, tensor[7].shape) # 輸出:(4, 35) (4, 35) (4, 35)
四、tf.split( )函數(shù)
tf.split() 用于將一個張量(tensor)沿著指定的軸(axis)拆分為多個子張量。對比于tf.unstack()函數(shù)將某個維度一個個拆分,tf.split()就能實現(xiàn)根據(jù)自己的需求拆分。
tf.split(value, num_or_size_splits, axis)
- value: 一個多維張量。
- num_or_size_splits: 要拆分的張量數(shù)量或每個拆分的大小??梢允且粋€整數(shù)(表示拆分的數(shù)量)或一個一維張量(表示每個拆分的大小)。
- axis: 一個整數(shù),表示沿著哪個軸進(jìn)行拆分。
1.num_or_size_splits為數(shù)字的情況
import tensorflow as tf tensor1 = tf.ones([2, 4, 35, 8]) # 拆分為2個形狀為[2, 4, 35, 4]的張量 tensor = tf.split(tensor1, axis=3, num_or_size_splits=2) print(tensor[0].shape, tensor[1].shape) # 輸出:[2, 4, 35, 4] [2, 4, 35, 4]
2.num_or_size_splits為一維向量的情況
import tensorflow as tf tensor1 = tf.ones([2, 4, 35, 8]) # 在第三個軸上,按照2 / 2 / 4 形狀拆分的張量 tensor = tf.split(tensor1, axis=3, num_or_size_splits=[2, 2, 4]) print(tensor[0].shape, tensor[1].shape, tensor[2].shape) # 輸出:(2, 4, 35, 2) (2, 4, 35, 2) (2, 4, 35, 4)
到此這篇關(guān)于TensorFlow深度學(xué)習(xí)之實現(xiàn)合并與分割的示例代碼的文章就介紹到這了,更多相關(guān)TensorFlow合并 分割內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Python實現(xiàn)將PDF轉(zhuǎn)為圖片
這篇文章主要為大家詳細(xì)介紹了python如何借用第三方庫Spire.PDF for Python,從而實現(xiàn)將PDF轉(zhuǎn)為圖片的功能,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-10-10tensorflow轉(zhuǎn)onnx的實現(xiàn)方法
本文主要介紹了tensorflow轉(zhuǎn)onnx的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03使用虛擬環(huán)境打包python為exe 文件的方法
這篇文章主要介紹了關(guān)于使用虛擬環(huán)境打包python為exe 文件的方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-08-08python使用MQTT給硬件傳輸圖片的實現(xiàn)方法
最近因需要用python寫一個微服務(wù)來用MQTT給硬件傳輸圖片,其中python用的是flask框架。這篇文章主要介紹了python使用MQTT給硬件傳輸圖片,需要的朋友可以參考下2019-05-05