pytorch中unsqueeze()、squeeze()、expand()、repeat()、view()、和cat()函數的總結

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

1、unsqueeze()和squeeze()

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

unsqueeze()的作用是用來增加給定tensor的維度的,unsqueeze(dim)就是在維度序號爲dim的地方給tensor增加一維。例如:維度爲torch.Size([768])的tensor要怎樣才能變爲torch.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經過unsqueeze()處理之後,總數據量不變;維度的擴展類似於list不變直接在外面加幾層[]括號。

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

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

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

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])

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

注意的是: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()

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

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變爲2,可以看到就直接把原來的tensor在該維度上覆制了一下。

3、repeat()

repeat(*sizes)

沿着指定的維度,對原來的tensor進行數據複製。這個函數和expand()還是有點區別的。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:

b

c

4、view()

tensor.view()這個函數有點類似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以後,就會自動得到其他維度維。

需要特別注意的是:view(-1,-1)這樣的用法就會出錯。也就是說view()函數中只能出現單個-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),其中list中的元素爲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])

 

參考文章:1、https://blog.csdn.net/york1996/article/details/81949843

參考文章:2、https://blog.csdn.net/xrinosvip/article/details/81164697

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章