How I can swap 3 dimensions with each other in Pytorch?

只谈情不闲聊 提交于 2021-02-11 13:57:34

问题


I have a a= torch.randn(28, 28, 8) and I want to swap the dimensions of the tensor and move the third dimension to the first place, first one to the second place and the second one to the third place. I used b = a.transpose(2, 0, 1) , but I received this error:

TypeError: transpose() received an invalid combination of arguments - got (int, int, int), but expected one of:
 * (name dim0, name dim1)
 * (int dim0, int dim1)

Should I use transpose several times, each time only to swap two dimensions? Is there any way that I can swap all at once?

Thanks.


回答1:


You can use Pytorch's permute() function to swap all at once,

>>>a = torch.randn(28, 28, 8)
>>>b = a.permute(2, 0, 1)
>>>b.shape
torch.Size([8, 28, 28])



回答2:


Use permute:

b = a.permute(2, 0, 1)


来源:https://stackoverflow.com/questions/61104138/how-i-can-swap-3-dimensions-with-each-other-in-pytorch

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!