如何在 Pytorch 中相互交换 3 个维度? [英] How I can swap 3 dimensions with each other in Pytorch?

查看:26
本文介绍了如何在 Pytorch 中相互交换 3 个维度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 a= torch.randn(28, 28, 8) 并且我想交换张量的维度并将第三个维度移到第一位,第一个移到第二个名次和第二名到第三名.我使用了 b = a.transpose(2, 0, 1) ,但我收到了这个错误:

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?

谢谢.

推荐答案

可以使用Pytorch的permute()函数一次性全部交换,

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])

这篇关于如何在 Pytorch 中相互交换 3 个维度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆