PyTorch函數(shù)torch.cat與torch.stac的區(qū)別小結(jié)
一、torch.cat與torch.stack的區(qū)別
torch.cat
用于在給定的維度上連接多個(gè)張量,它將這些張量沿著指定維度堆疊在一起。
torch.stack
用于在新的維度上堆疊多個(gè)張量,它會(huì)創(chuàng)建一個(gè)新的維度,并將這些張量沿著這個(gè)新維度堆疊在一起。
二、torch.cat
Example1:
import torch tensor1 = torch.tensor([[1, 2], [3, 4]]) tensor2 = torch.tensor([[5, 6], [7, 8]]) result1 = torch.cat((tensor1, tensor2), dim=0) result2 = torch.cat((tensor1, tensor2), dim=1) print(result1.shape) print(result1) print(result2.shape) print(result2)
torch.Size([4, 2]) tensor([[1, 2], [3, 4], [5, 6], [7, 8]]) torch.Size([2, 4]) tensor([[1, 2, 5, 6], [3, 4, 7, 8]])
三、torch.stack
Example1:
import torch tensor1 = torch.tensor([1, 2, 3]) tensor2 = torch.tensor([4, 5, 6]) result1 = torch.stack((tensor1, tensor2), dim=0) result2 = torch.stack((tensor1, tensor2), dim=1) print(result1.shape) print(result1) print(result2.shape) print(result2)
torch.Size([2, 3]) tensor([[1, 2, 3], [4, 5, 6]]) torch.Size([3, 2]) tensor([[1, 4], [2, 5], [3, 6]])
Example2:
import torch tensor1 = torch.tensor([[1, 2], [3, 4], [5, 6]]) tensor2 = torch.tensor([[7, 8], [9, 10], [11, 12]]) tensor3 = torch.tensor([[13, 14], [15, 16], [17, 18]]) result1 = torch.stack((tensor1, tensor2, tensor3), dim=0) result2 = torch.stack((tensor1, tensor2, tensor3), dim=1) print(result1.shape) print(result1) print(result2.shape) print(result2)
torch.Size([3, 3, 2]) tensor([[[ 1, 2], [ 3, 4], [ 5, 6]], [[ 7, 8], [ 9, 10], [11, 12]], [[13, 14], [15, 16], [17, 18]]]) torch.Size([3, 3, 2]) tensor([[[ 1, 2], [ 7, 8], [13, 14]], [[ 3, 4], [ 9, 10], [15, 16]], [[ 5, 6], [11, 12], [17, 18]]])
到此這篇關(guān)于PyTorch函數(shù)torch.cat與torch.stac的區(qū)別小結(jié)的文章就介紹到這了,更多相關(guān)PyTorch torch.cat與torch.stac 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python模擬菜刀反彈shell繞過(guò)限制【推薦】
這篇文章主要介紹了利用python模擬菜刀反彈shell繞過(guò)限制,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06Python中Django框架利用url來(lái)控制登錄的方法
這篇文章主要介紹了Python中Django框架利用url來(lái)控制登錄的方法,實(shí)例分析了Django框架實(shí)現(xiàn)URL登陸的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07Python實(shí)現(xiàn)GUI圖片瀏覽的小程序
這篇文章主要介紹了Python實(shí)現(xiàn)GUI圖片瀏覽程序,程序的實(shí)現(xiàn)需要pillow庫(kù),pillow是 Python 的第三方圖像處理庫(kù),需要安裝才能實(shí)用,文中通過(guò)代碼示例給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12Python中l(wèi)ist列表添加元素的3種方法總結(jié)
這篇文章主要介紹了Python中l(wèi)ist列表添加元素的3種方法總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01