pytorch中unsqueeze用法小結(jié)
在指定的位置插入一個(gè)維度,有兩個(gè)參數(shù),input是輸入的tensor,dim是要插到的維度
需要注意的是dim的范圍是[-input.dim()-1, input.dim()+1),是一個(gè)左閉右開的區(qū)間,當(dāng)dim為負(fù)值時(shí),會(huì)自動(dòng)轉(zhuǎn)換為dim = dim+input.dim()+1,類似于使用負(fù)數(shù)對(duì)python列表進(jìn)行切片。
import torch a = torch.randn(2,5) print(a) print("") b = a.unsqueeze(0) print(b.shape) print("") c = a.unsqueeze(a.dim()) print(c.shape) 輸出: tensor([[-0.4734, 0.4115, -0.9415, -1.1280, -0.1065], [ 0.1613, 1.2594, 1.1261, 1.3881, 0.1112]]) torch.Size([1, 2, 5]) torch.Size([2, 5, 1])
以上是二維數(shù)據(jù)情況:
首先生成了一個(gè)二維矩陣,其大小為[2,5]
然后,在0維度上插入一個(gè)維度,可以看到現(xiàn)在新矩陣a的形狀變?yōu)閇1,2,5],第0維度的大小默認(rèn)是1
最后,在最后一個(gè)維度上插入一個(gè)維度,形狀變?yōu)閇2, 5, 1]
a=torch.rand(2,3,2) print("") print("torch.unsqueeze(a,3) size: {}".format(torch.unsqueeze(a,3).size())) print("") print("torch.unsqueeze(a,2) size: {}".format(torch.unsqueeze(a,2).size())) print("") print("torch.unsqueeze(a,1) size: {}".format(torch.unsqueeze(a,1).size())) print("") print("torch.unsqueeze(a,0) size: {}".format(torch.unsqueeze(a,0).size())) print("") print("torch.unsqueeze(a,-1) size: {}".format(torch.unsqueeze(a,-1).size())) print("") print("torch.unsqueeze(a,-2) size: {}".format(torch.unsqueeze(a,-2).size())) print("") print("torch.unsqueeze(a,-3) size: {}".format(torch.unsqueeze(a,-3).size())) print("") print("torch.unsqueeze(a,-4) size: {}".format(torch.unsqueeze(a,-4).size())) 輸出: torch.unsqueeze(a,3) size: torch.Size([2, 3, 2, 1]) torch.unsqueeze(a,2) size: torch.Size([2, 3, 1, 2]) torch.unsqueeze(a,1) size: torch.Size([2, 1, 3, 2]) torch.unsqueeze(a,0) size: torch.Size([1, 2, 3, 2]) torch.unsqueeze(a,-1) size: torch.Size([2, 3, 2, 1]) torch.unsqueeze(a,-2) size: torch.Size([2, 3, 1, 2]) torch.unsqueeze(a,-3) size: torch.Size([2, 1, 3, 2]) torch.unsqueeze(a,-4) size: torch.Size([1, 2, 3, 2])
對(duì)于三維數(shù)據(jù)input.dim() = 3,因此dim的范圍是[-4, 4)
torch.squeeze() 和 torch.unsqueeze()區(qū)別
第一塊:
squeeze(),主要是對(duì)數(shù)據(jù)的維度進(jìn)行壓縮,去掉元素?cái)?shù)為1的那個(gè)維度,使用方式:a.squeeze(N) or torch.squeeze(a,N) ,去掉a的第N維度,以此來實(shí)現(xiàn)數(shù)據(jù)a的維度壓縮;
unsqueeze()與squeeze()函數(shù)功能相反,其功能是對(duì)數(shù)據(jù)維度進(jìn)行擴(kuò)充,使用方式:a.unsqueeze(N) or torch.unsqueeze(a,N),在數(shù)據(jù)a的第N維度上增加一個(gè)維數(shù)為1的維度,以此實(shí)現(xiàn)對(duì)數(shù)據(jù)的擴(kuò)充,方便后續(xù)模型訓(xùn)練喂入模型的數(shù)據(jù)的維度和模型接收數(shù)據(jù)的維度是匹配的。
第二塊:
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") model.to(device) # 選擇第0個(gè)cuda
model.to(device)
以上兩行代碼放在讀取數(shù)據(jù)之前。
mytensor = my_tensor.to(device) #將所有最開始讀取數(shù)據(jù)時(shí)的tensor變量copy一份到device所指定的GPU上,之后運(yùn)算都在指定的GPU上進(jìn)行。這些tensor多是最開始讀取數(shù)據(jù)時(shí)的變量,后面其衍生出的新變量也會(huì)在已指定的GPU上運(yùn)行計(jì)算。
第三塊:
Tensor & Numpy 都是矩陣,區(qū)別在與Tensor可以在GPU上運(yùn)行,Numpy只能在CPU上運(yùn)行。(天吶,我現(xiàn)在才知道!)Tensor與Numpy互相轉(zhuǎn)化很方便,類型也比較兼容,Tensor可以直接通過print顯示數(shù)據(jù)類型,而Numpy不可以。
第四塊:
x.aadd(y) 實(shí)現(xiàn)x與y Tensor的相加,不改變x,返回一個(gè)新的Tensor
x.add_(y) 實(shí)現(xiàn)x與y Tensor的相加,會(huì)修改x的維數(shù)
到此這篇關(guān)于pytorch中unsqueeze用法小結(jié)的文章就介紹到這了,更多相關(guān)pytorch unsqueeze內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python爬蟲程序中使用生產(chǎn)者與消費(fèi)者模式時(shí)進(jìn)程過早退出的問題
本文主要介紹了Python爬蟲程序中使用生產(chǎn)者與消費(fèi)者模式時(shí)進(jìn)程過早退出的問題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01Pytorch生成隨機(jī)數(shù)Tensor的方法匯總
這篇文章主要介紹了Pytorch生成隨機(jī)數(shù)Tensor的方法匯總,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09python實(shí)現(xiàn)代碼審查自動(dòng)回復(fù)消息
這篇文章主要介紹了python實(shí)現(xiàn)代碼審查回復(fù)消息生成的示例,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下2021-02-02python中?OpenCV和Pillow處理圖像操作及時(shí)間對(duì)比
這篇文章主要介紹了python中OpenCV和Pillow處理圖像操作及時(shí)間對(duì)比,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09Python遞歸函數(shù)反轉(zhuǎn)序列的實(shí)現(xiàn)
本文主要介紹了Python遞歸函數(shù)反轉(zhuǎn)序列的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07使用Python在Excel中創(chuàng)建和取消數(shù)據(jù)分組
Excel中的分組是一種通過添加層級(jí)結(jié)構(gòu)將相鄰行或列組織在一起的功能,當(dāng)分組完成后,用戶可以通過折疊或展開數(shù)據(jù)組來簡(jiǎn)化數(shù)據(jù)視圖,這篇博客將介紹如何使用Python在Excel中創(chuàng)建或取消數(shù)據(jù)分組,需要的朋友可以參考下2025-02-02