这篇文章,讲矩阵变形以及连续的问题(view reshape permute continuous)
https://blog.csdn.net/u014386899/article/details/107613032
这篇文章讲transpose与permute转置
区别:transpose只能一次转置两维,permute可以多维。且这两个函数转置后都是不连续的
https://www.cnblogs.com/tingtin/p/13547653.html
torch.permute()和np.transpose()效果一样,只是一个用于pytorch,一个用于numpy
https://blog.csdn.net/qq_34806812/article/details/89385831
pytorch的reshape相当于.contiguous().view(),也就是自动先把tensor变连续再变形,忘了在哪儿看的了
permute()
增加一个代码中用到的permute用法
import torch
a = torch.randint(0, 10, (3, 2, 2, 5))
b = a.permute(0, 3, 1, 2).reshape(15, 4)
# 相当于b = a.permute(0, 3, 1, 2).contiguous().reshape(15, 4),说明reshape自动进行了.contiguous()连续操作
print(a)
print(b)
输出
tensor([[[[9, 2, 5, 9, 2],
[7, 0, 2, 5, 8]],
[[5, 9, 7, 9, 7],
[9, 4, 4, 7, 5]]],
[[[4, 1, 5, 8, 9],
[0, 4, 0, 4, 9]],
[[6, 5, 1, 6, 4],
[7, 9, 1, 0, 3]]],
[[[4, 4, 7, 9, 4],
[1, 0, 0, 0, 2]],
[[2, 8, 2, 4, 1],
[9, 5, 0, 6, 9]]]])
tensor([[9, 7, 5, 9],
[2, 0, 9, 4],
[5, 2, 7, 4],
[9, 5, 9, 7],
[2, 8, 7, 5],
[4, 0, 6, 7],
[1, 4, 5, 9],
[5, 0, 1, 1],
[8, 4, 6, 0],
[9, 9, 4, 3],
[4, 1, 2, 9],
[4, 0, 8, 5],
[7, 0, 2, 0],
[9, 0, 4, 6],
[4, 2, 1, 9]])
不知道是否有函数可以使a直接变形为b,以后知道了再补上