pytorch | transpose、permute、view、contiguous、is_contiguous、reshape
生活随笔
收集整理的這篇文章主要介紹了
pytorch | transpose、permute、view、contiguous、is_contiguous、reshape
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
- transpose、contiguous、view
結(jié)果:
?
其中:is_contiguous函數(shù)是判斷一個(gè)變量是否內(nèi)存連續(xù),連續(xù)返回True,不連續(xù)返回False
- torch.reshape()
結(jié)果:?
- 查看變量的內(nèi)存地址:id()函數(shù)
?結(jié)果:
transpose、permute異同點(diǎn)
transpose:交換維度?
torch.manual_seed(1) a = torch.randn(2, 3) print(a) b = a.transpose(0, 1) print(b) print('=====================') a1 = torch.randn(2, 3, 4) print(a1) b1 = a1.transpose(1, 2) print(b1)結(jié)果:
permute:排列、置換維度,比transpose更靈活,適用于多維度
a1 = torch.from_numpy(np.array([[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[1, 2, 3], [4, 5, 6], [7, 8, 9]]])) print(a1) b1 = a1.permute(1, 0, 2) # 原本是2*3*3,將第1維和第2維交換,結(jié)果變?yōu)?*2*3 print(b1) c1 = a1.permute(1, 2, 0) # 原本是2*3*3,第1維變?yōu)榈?維,第2維變?yōu)榈?維,第3維變?yōu)榈?維,結(jié)果是:3*3*2 print(c1)結(jié)果:
雖然都是維度變化,但transpose只能選擇兩個(gè)維度進(jìn)行交換,permute則可以多維交換。
看函數(shù)原型也能看出:
def transpose(self, dim0: _int, dim1: _int) -> Tensor: ... def permute(self, dims: _size) -> Tensor: ...?
總結(jié)
以上是生活随笔為你收集整理的pytorch | transpose、permute、view、contiguous、is_contiguous、reshape的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 理解 | 理解a: float=10
- 下一篇: 问题 | list(set(list))