分類目錄:《深入淺出Pytorch函數(shù)》總目錄
相關(guān)文章:
· 深入淺出Pytorch函數(shù)——torch.squeeze
· 深入淺出Pytorch函數(shù)——torch.unsqueeze
將輸入張量形狀為1的維度去除并返回。比如輸入向量的形狀為
A
×
1
×
B
×
1
×
C
×
1
×
D
A\times1\times B\times1\times C\times1\times D
A×1×B×1×C×1×D,則輸出向量形狀就為
A
×
B
×
C
×
D
A\times B\times C\times D
A×B×C×D。當(dāng)給定參數(shù)dim
時(shí),則操作只在給定維度dim
上。例如,輸入向量的形狀為
A
×
1
×
B
A\times1\times B
A×1×B,使用squeeze(input, 0)
,輸出向量的形狀將會保持張量不變,只有使用 squeeze(input, 1)
,輸出向量的形狀才會變成
A
×
B
A\times B
A×B。需要注意的是,返回張量與輸入張量共享內(nèi)存,所以改變其中一個(gè)的內(nèi)容會改變另一個(gè)。
語法
torch.squeeze(input, dim=None) -> Tensor
參數(shù)
-
input
:[Tensor
] 輸入張量 -
dim
:[可選,int
/tuple
] 擠壓維度的位置索引
實(shí)例
輸入:
x = torch.zeros(2, 1, 2, 1, 2)
x.size()
輸出:
torch.Size([2, 1, 2, 1, 2])
輸入:
y = torch.squeeze(x)
y.size()
輸出:
torch.Size([2, 2,, 2])
輸入:
y = torch.squeeze(x, 0)
y.size()
輸出:
torch.Size([2, 1, 2, 1, 2])
輸入:
y = torch.squeeze(x, 1)
y.size()
輸出:
torch.Size([2, 2, 1, 2])
輸入:文章來源:http://www.zghlxwxcb.cn/news/detail-600147.html
y = torch.squeeze(x, (1, 2, 3))
y.size()
輸出:文章來源地址http://www.zghlxwxcb.cn/news/detail-600147.html
torch.Size([2, 2, 2])
到了這里,關(guān)于深入淺出Pytorch函數(shù)——torch.squeeze的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!