Tensor.permute(a,b,c,d, ...):permute函数可以对任意高维矩阵进行转置,但没有 torch.permute() 这个调用方式, 只能 Tensor.permute():

torch.transpose(Tensor, a,b):transpose只能操作2D矩阵的转置

a=torch.randn(2,3,4,5).permute(3,2,0,1).shape
print(a)

b=torch.randn(2,3,4,5).transpose(3,1).transpose(0,2).transpose(0,1).shape
print(b)

torch.Size([5, 4, 2, 3])
torch.Size([5, 4, 2, 3])

连续使用transpose也可实现permute的效果: 

permute相当于可以同时操作于tensor的若干维度,transpose只能同时作用于tensor的两个维度

permute函数与contiguous、view函数之关联

contiguous:view只能作用在contiguous的variable上,如果在view之前调用了transpose、permute等,就需要调用contiguous()来返回一个contiguous copy;

一种可能的解释是:有些tensor并不是占用一整块内存,而是由不同的数据块组成,而tensor的view()操作依赖于内存是整块的,这时只需要执行contiguous()这个函数,把tensor变成在内存中连续分布的形式;

判断ternsor是否为contiguous,可以调用torch.Tensor.is_contiguous()函数

在pytorch的最新版本0.4版本中,增加了torch.reshape(),与 numpy.reshape() 的功能类似,大致相当于 tensor.contiguous().view(),这样就省去了对tensor做view()变换前,调用contiguous()的麻烦;

综合, torch.reshape() 与 numpy.reshape() 较tensor.view 更好