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