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

Issue

I created a random function with the line below

x = torch.randn(4, 3)

and used the transpose function as shown here

torch.transpose(x, 0, 1)

I got the error line below. Who can assist with a solution


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)

Solution

You are trying to transpose x = torch.randn(4, 3), which is 2D. torch.transpose(x, 0, 1) works fine because you swap dimensions 0 and 1. But then, you try to swap dimensions 0 and 3 by doing torch.transpose(x, 0, 3) but your x does not have the 3rd dimension

Answered By – Yekaterina

Answer Checked By – Robin (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *