Pytorch 擴(kuò)展Tensor維度、壓縮Tensor維度的方法
1. 擴(kuò)展Tensor維度
相信剛接觸Pytorch的寶寶們,會(huì)遇到這樣一個(gè)問(wèn)題,輸入的數(shù)據(jù)維度和實(shí)驗(yàn)需要維度不一致,輸入的可能是2維數(shù)據(jù)或3維數(shù)據(jù),實(shí)驗(yàn)需要用到3維或4維數(shù)據(jù),那么我們需要擴(kuò)展這個(gè)維度。其實(shí)特別簡(jiǎn)單,只要對(duì)數(shù)據(jù)加一個(gè)擴(kuò)展維度方法就可以了。
1.1torch.unsqueeze(self: Tensor, dim: _int)
torch.unsqueeze(self: Tensor, dim: _int)
參數(shù)說(shuō)明:self:輸入的tensor數(shù)據(jù),dim:要對(duì)哪個(gè)維度擴(kuò)展就輸入那個(gè)維度的整數(shù),可以輸入0,1,2……
1.2Code
第一種方式,輸入數(shù)據(jù)后直接加unsqueeze()
擴(kuò)展第一維和第二維為1
import torch
def reset_unsqueeze1():
data = torch.rand([3, 3])
data1 = data.unsqueeze(dim=0).unsqueeze(dim=1)
print("data_size: ", data.shape)
print("data: ", data)
print("data1_size: ", data1.shape)
print("data1: ", data1)
結(jié)果顯示

第二種方式,用torch.unsqueeze()
import torch
def reset_unsqueeze2():
data = torch.rand([3, 3])
data1 = torch.unsqueeze(data, dim=0)
print("data_size: ", data.shape)
print("data: ", data)
print("data1_size: ", data1.shape)
print("data1: ", data1)
結(jié)果顯示

2. 壓縮Tensor維度
2.1torch.squeeze(self: Tensor, dim: _int)
這個(gè)方法剛好和torch.unsqueeze()方法效果相反,壓縮Tensor維度。
2.2 Code
第一種方式,輸入數(shù)據(jù)后直接加squeeze()
import torch
def reset_squeeze1():
data = torch.rand([1, 1, 3, 3])
data1 = data.squeeze(dim=0).squeeze(dim=1)
print("data_size: ", data.shape)
print("data: ", data)
print("data1_size: ", data1.shape)
print("data1: ", data1)
結(jié)果顯示

第二種方式,用torch.squeeze()
import torch
def reset_squeeze2():
data = torch.rand([1, 1, 3, 3])
data1 = torch.squeeze(data, dim=0)
print("data_size: ", data.shape)
print("data: ", data)
print("data1_size: ", data1.shape)
print("data1: ", data1)
結(jié)果顯示

到此這篇關(guān)于Pytorch 擴(kuò)展Tensor維度、壓縮Tensor維度的方法的文章就介紹到這了,更多相關(guān)Pytorch 擴(kuò)展Tensor維度、壓縮Tensor維度內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Datawhale練習(xí)之二手車價(jià)格預(yù)測(cè)
此篇文章是關(guān)于Datawhale練習(xí),代碼完整,但由于該數(shù)據(jù)集中數(shù)據(jù)特征較少(39維),以下可作為少量特征情況下的分析。當(dāng)特征數(shù)目過(guò)大(成千上萬(wàn))時(shí),需要繼續(xù)學(xué)習(xí)。需要的朋友可以參考下2021-04-04
5分鐘快速掌握Python定時(shí)任務(wù)框架的實(shí)現(xiàn)
這篇文章主要介紹了5分鐘快速掌握 Python 定時(shí)任務(wù)框架,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
Python3.10.4激活venv環(huán)境失敗解決方法
這篇文章主要介紹了Python3.10.4激活venv環(huán)境失敗解決方法的相關(guān)資料,需要的朋友可以參考下2023-01-01
Python應(yīng)用自動(dòng)化部署工具Fabric原理及使用解析
這篇文章主要介紹了Python應(yīng)用自動(dòng)化部署工具Fabric原理及使用解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
python中的多線程鎖lock=threading.Lock()使用方式
這篇文章主要介紹了python中的多線程鎖lock=threading.Lock()使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
Python 實(shí)現(xiàn)微信自動(dòng)回復(fù)的方法
這篇文章主要介紹了Python 實(shí)現(xiàn)微信自動(dòng)回復(fù)的方法,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-09-09
Python數(shù)據(jù)分析基礎(chǔ)之文件的讀取
這篇文章主要為大家介紹了Python數(shù)據(jù)分析之文件的讀取,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2021-12-12
對(duì)python GUI實(shí)現(xiàn)完美進(jìn)度條的示例詳解
今天小編就為大家分享一篇對(duì)python GUI實(shí)現(xiàn)完美進(jìn)度條的示例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12

