佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > pytorch下的unsqueeze和squeeze的用法說明

pytorch下的unsqueeze和squeeze的用法說明

熱門標簽:企業(yè)做大做強 電話運營中心 語音系統(tǒng) 硅谷的囚徒呼叫中心 客戶服務(wù) 呼叫中心市場需求 百度AI接口 Win7旗艦版

#squeeze 函數(shù):從數(shù)組的形狀中刪除單維度條目,即把shape中為1的維度去掉

#unsqueeze() 是squeeze()的反向操作,增加一個維度,該維度維數(shù)為1,可以指定添加的維度。例如unsqueeze(a,1)表示在1這個維度進行添加

import torch 
a=torch.rand(2,3,1)       
print(torch.unsqueeze(a,2).size())#torch.Size([2, 3, 1, 1]) 
print(a.size())         #torch.Size([2, 3, 1])
print(a.squeeze().size())    #torch.Size([2, 3]) 
print(a.squeeze(0).size())   #torch.Size([2, 3, 1])
 
print(a.squeeze(-1).size())   #torch.Size([2, 3])
print(a.size())         #torch.Size([2, 3, 1])
print(a.squeeze(-2).size())   #torch.Size([2, 3, 1])
print(a.squeeze(-3).size())   #torch.Size([2, 3, 1])
print(a.squeeze(1).size())   #torch.Size([2, 3, 1])
print(a.squeeze(2).size())   #torch.Size([2, 3])
print(a.squeeze(3).size())   #RuntimeError: Dimension out of range (expected to be in range of [-3, 2], but got 3)
 
print(a.unsqueeze().size())   #TypeError: unsqueeze() missing 1 required positional arguments: "dim"
print(a.unsqueeze(-3).size())  #torch.Size([2, 1, 3, 1])
print(a.unsqueeze(-2).size())  #torch.Size([2, 3, 1, 1])
print(a.unsqueeze(-1).size())  #torch.Size([2, 3, 1, 1])
print(a.unsqueeze(0).size())  #torch.Size([1, 2, 3, 1])
print(a.unsqueeze(1).size())  #torch.Size([2, 1, 3, 1])
print(a.unsqueeze(2).size())  #torch.Size([2, 3, 1, 1])
print(a.unsqueeze(3).size())  #torch.Size([2, 3, 1, 1])
print(torch.unsqueeze(a,3))
b=torch.rand(2,1,3,1)
print(b.squeeze().size())    #torch.Size([2, 3])

補充:pytorch中unsqueeze()、squeeze()、expand()、repeat()、view()、和cat()函數(shù)的總結(jié)

學(xué)習(xí)Bert模型的時候,需要使用到pytorch來進行tensor的操作,由于對pytorch和tensor不熟悉,就把pytorch中常用的、有關(guān)tensor操作的unsqueeze()、squeeze()、expand()、view()、cat()和repeat()等函數(shù)做一個總結(jié),加深記憶。

1、unsqueeze()和squeeze()

torch.unsqueeze(input, dim,out=None) → Tensor

unsqueeze()的作用是用來增加給定tensor的維度的,unsqueeze(dim)就是在維度序號為dim的地方給tensor增加一維。例如:維度為torch.Size([768])的tensor要怎樣才能變?yōu)閠orch.Size([1, 768, 1])呢?就可以用到unsqueeze(),直接上代碼:

a=torch.randn(768)
print(a.shape) # torch.Size([768])
a=a.unsqueeze(0)
print(a.shape) #torch.Size([1, 768])
a = a.unsqueeze(2)
print(a.shape) #torch.Size([1, 768, 1])

也可以直接使用鏈式編程:

a=torch.randn(768)
print(a.shape) # torch.Size([768])
a=a.unsqueeze(1).unsqueeze(0)
print(a.shape) #torch.Size([1, 768, 1])

tensor經(jīng)過unsqueeze()處理之后,總數(shù)據(jù)量不變;維度的擴展類似于list不變直接在外面加幾層[]括號。

torch.squeeze(input, dim=None, out=None) → Tensor

squeeze()的作用就是壓縮維度,直接把維度為1的維給去掉。形式上表現(xiàn)為,去掉一層[]括號。

同時,輸出的張量與原張量共享內(nèi)存,如果改變其中的一個,另一個也會改變。

a=torch.randn(2,1,768)
print(a)
print(a.shape) #torch.Size([2, 1, 768])
a=a.squeeze()
print(a)
print(a.shape) #torch.Size([2, 768])

圖片中的維度信息就不一樣,紅框中的括號層數(shù)不同。

注意的是:squeeze()只能壓縮維度為1的維;其他大小的維不起作用。

a=torch.randn(2,768)
print(a.shape) #torch.Size([2, 768])
a=a.squeeze()
print(a.shape) #torch.Size([2, 768])

2、expand()

這個函數(shù)的作用就是對指定的維度進行數(shù)值大小的改變。只能改變維大小為1的維,否則就會報錯。不改變的維可以傳入-1或者原來的數(shù)值。

torch.Tensor.expand(*sizes) → Tensor

返回張量的一個新視圖,可以將張量的單個維度擴大為更大的尺寸。

a=torch.randn(1,1,3,768)
print(a) 
print(a.shape) #torch.Size([1, 1, 3, 768])
b=a.expand(2,-1,-1,-1)
print(b)
print(b.shape) #torch.Size([2, 1, 3, 768])
c=a.expand(2,1,3,768)
print(c.shape) #torch.Size([2, 1, 3, 768])

可以看到b和c的維度是一樣的

第0維由1變?yōu)?,可以看到就直接把原來的tensor在該維度上復(fù)制了一下。

3、repeat()

repeat(*sizes)

沿著指定的維度,對原來的tensor進行數(shù)據(jù)復(fù)制。這個函數(shù)和expand()還是有點區(qū)別的。expand()只能對維度為1的維進行擴大,而repeat()對所有的維度可以隨意操作。

a=torch.randn(2,1,768)
print(a)
print(a.shape) #torch.Size([2, 1, 768])
b=a.repeat(1,2,1)
print(b)
print(b.shape) #torch.Size([2, 2, 768])
c=a.repeat(3,3,3)
print(c)
print(c.shape) #torch.Size([6, 3, 2304])

b表示對a的對應(yīng)維度進行乘以1,乘以2,乘以1的操作,所以b:torch.Size([2, 1, 768])

c表示對a的對應(yīng)維度進行乘以3,乘以3,乘以3的操作,所以c:torch.Size([6, 3, 2304])

a:

b

c

4、view()

tensor.view()這個函數(shù)有點類似reshape的功能,簡單的理解就是:先把一個tensor轉(zhuǎn)換成一個一維的tensor,然后再組合成指定維度的tensor。例如:

word_embedding=torch.randn(16,3,768)
print(word_embedding.shape)
new_word_embedding=word_embedding.view(8,6,768)
print(new_word_embedding.shape)

當然這里指定的維度的乘積一定要和原來的tensor的維度乘積相等,不然會報錯的。16*3*768=8*6*768

另外當我們需要改變一個tensor的維度的時候,知道關(guān)鍵的維度,有不想手動的去計算其他的維度值,就可以使用view(-1),pytorch就會自動幫你計算出來。

word_embedding=torch.randn(16,3,768)
print(word_embedding.shape)
new_word_embedding=word_embedding.view(-1)
print(new_word_embedding.shape)
new_word_embedding=word_embedding.view(1,-1)
print(new_word_embedding.shape)
new_word_embedding=word_embedding.view(-1,768)
print(new_word_embedding.shape)

結(jié)果如下:使用-1以后,就會自動得到其他維度維。

需要特別注意的是:view(-1,-1)這樣的用法就會出錯。也就是說view()函數(shù)中只能出現(xiàn)單個-1。

5、cat()

cat(seq,dim,out=None),表示把兩個或者多個tensor拼接起來。

其中 seq表示要連接的兩個序列,以元組的形式給出,例如:seq=(a,b), a,b 為兩個可以連接的序列

dim 表示以哪個維度連接,dim=0, 橫向連接 dim=1,縱向連接

a=torch.randn(4,3)
b=torch.randn(4,3)
 
c=torch.cat((a,b),dim=0)#橫向拼接,增加行 torch.Size([8, 3])
print(c.shape)
d=torch.cat((a,b),dim=1)#縱向拼接,增加列 torch.Size([4, 6])
print(d.shape)

還有一種寫法:cat(list,dim,out=None),其中l(wèi)ist中的元素為tensor。

tensors=[]
for i in range(10):
  tensors.append(torch.randn(4,3))
a=torch.cat(tensors,dim=0) #torch.Size([40, 3])
print(a.shape)
b=torch.cat(tensors,dim=1) #torch.Size([4, 30])
print(b.shape)

結(jié)果:

torch.Size([40, 3])
torch.Size([4, 30])

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。

您可能感興趣的文章:
  • 詳解pytorch中squeeze()和unsqueeze()函數(shù)介紹
  • pytorch中torch.max和Tensor.view函數(shù)用法詳解
  • pytorch中的squeeze函數(shù)、cat函數(shù)使用

標簽:濟南 山西 安康 崇左 長沙 山西 喀什 海南

巨人網(wǎng)絡(luò)通訊聲明:本文標題《pytorch下的unsqueeze和squeeze的用法說明》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    兖州市| 包头市| 信阳市| 广西| 郎溪县| 滨州市| 永安市| 临城县| 达尔| 灌云县| 万盛区| 长兴县| 湘阴县| 孟津县| 高台县| 长白| 左权县| 闻喜县| 赞皇县| 海宁市| 奉化市| 鄂伦春自治旗| 庄浪县| 阳泉市| 琼中| 八宿县| 六盘水市| 富川| 齐齐哈尔市| 闸北区| 中宁县| 周宁县| 香港| 岱山县| 英吉沙县| 自治县| 海城市| 江津市| 泸溪县| 广饶县| 新丰县|