1.torch.reshape()、torch.view()可以調(diào)整Tensor的shape
2.torch.unsqueeze(index)可以為Tensor增加一個維度
3.squeeze()可以刪減維度
4.expand()擴展維度
5.repeat()維度重復(fù),不常用
6.transpose(dim1, dim2)交換dim1與dim2,進行維度變換。
注意這種交換使得存儲不再連續(xù),再執(zhí)行一些reshape的操作會報錯,所以要調(diào)用一下contiguous()使其變成連續(xù)的維度。
參考:https://blog.csdn.net/weicao1990/article/details/93618136文章來源:http://www.zghlxwxcb.cn/news/detail-622087.html
7.Broadcasting(廣播)也就是自動實現(xiàn)了若干unsqueeze和expand操作,以使兩個Tensor的shape一致,從而完成某些操作(往往是加法)。
比如得到的feature maps的維度是[4,32,14,14],對這些特征圖加上偏置,bias[32],先對bias進行unsqueeze操作增加維度:[1,32,1,1],再用expand操作進行擴充[4,32,14,14]。然后兩者就可以相加了。文章來源地址http://www.zghlxwxcb.cn/news/detail-622087.html
到了這里,關(guān)于Pytorch Tensor維度變換方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!