permute、transpose、view、reshape、unsequeeze与flatten
生活随笔
收集整理的這篇文章主要介紹了
permute、transpose、view、reshape、unsequeeze与flatten
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Pytorch中view, transpose, permute等方法的區(qū)別
關(guān)于張量的Flatten、Reshape和Squeeze的解釋
transpose 只能交換兩個維度,參數(shù)是(dim1,dim2)。與原數(shù)組共內(nèi)存空間 (np.transpose(dims)接受的參數(shù)是維度列表)
permute 也是交換維度,參數(shù)是維度列表(dim1,dim2,dim3,…)。不與元素組公用內(nèi)存空間
view:
reshape(self,dim):變形
unsequeeze(self,dim):解壓縮 增加一個長度為1的維數(shù)。
sequeeze(self):壓縮數(shù)組,去掉維度為1的維度
flatten(self):將數(shù)據(jù)處理為一維向量
transpose和permute操作后,數(shù)組是不連續(xù)的
a1 = a.permute((2,1,0)) a2= a.transpose(0,2) print(a1==a2) print(a1.is_contiguous()) print(a2.is_contiguous())
不連續(xù)的數(shù)組,不能使用view().推薦使用reshape()
將不連續(xù)數(shù)組變成連續(xù)數(shù)組,用contiguous()方法
創(chuàng)建數(shù)組的方式,第一種生成的是浮點型的,后面的是整型
總結(jié)
以上是生活随笔為你收集整理的permute、transpose、view、reshape、unsequeeze与flatten的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pytorch中数组维度的理解
- 下一篇: numpy中的broadcasting