IndexError: Dimension out of range (expected to be in range of [-2, 1], but got 3)

IndexError: Dimension out of range (expected to be in range of [-2, 1], but got 3)

我用下面的代码创建了一个随机函数

x = torch.randn(4, 3)

并使用此处所示的转置函数

torch.transpose(x, 0, 1)

我收到以下错误行。谁能协助解决


IndexError                                Traceback (most recent call last)
<ipython-input-19-28494ba2cedc> in <module>()
----> 1 torch.transpose(x, 0, 3)

IndexError: Dimension out of range (expected to be in range of [-2, 1], but got 3)

您正在尝试转置 x = torch.randn(4, 3),这是二维的。 torch.transpose(x, 0, 1) 工作正常,因为你交换维度 0 和 1。但是,你尝试通过 torch.transpose(x, 0, 3) 交换维度 0 和 3,但是你的 x没有第三维