#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ù)的總結
學習Bert模型的時候,需要使用到pytorch來進行tensor的操作,由于對pytorch和tensor不熟悉,就把pytorch中常用的、有關tensor操作的unsqueeze()、squeeze()、expand()、view()、cat()和repeat()等函數(shù)做一個總結,加深記憶。
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)為,去掉一層[]括號。
同時,輸出的張量與原張量共享內存,如果改變其中的一個,另一個也會改變。
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])
![](/d/20211017/ab5b75d0f6d10fe6f69177e6ef67d5fa.gif)
圖片中的維度信息就不一樣,紅框中的括號層數(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的維度是一樣的
![](/d/20211017/089119fe15d0d6eaac921614d72a4b6a.gif)
第0維由1變?yōu)?,可以看到就直接把原來的tensor在該維度上復制了一下。
3、repeat()
repeat(*sizes)
沿著指定的維度,對原來的tensor進行數(shù)據(jù)復制。這個函數(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的對應維度進行乘以1,乘以2,乘以1的操作,所以b:torch.Size([2, 1, 768])
c表示對a的對應維度進行乘以3,乘以3,乘以3的操作,所以c:torch.Size([6, 3, 2304])
a:
![](/d/20211017/ba31f8fc3d4f6b15f23e10f9b5061c54.gif)
b
![](/d/20211017/49044db4ca6fdb7069863bb7a5ad00c7.gif)
c
![](/d/20211017/23eca01f9c72f8b6c63cd545eadcf6fb.gif)
4、view()
tensor.view()這個函數(shù)有點類似reshape的功能,簡單的理解就是:先把一個tensor轉換成一個一維的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的維度的時候,知道關鍵的維度,有不想手動的去計算其他的維度值,就可以使用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)
結果如下:使用-1以后,就會自動得到其他維度維。
![](/d/20211017/ff4aa6826601e8810de3f0a4296491d7.gif)
需要特別注意的是: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)
結果:
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ù)使用