How does the transpose function work

So im using torch.transpose, but it sounds like i cannot not quite understand how it works.
lets say i have:

M = torch.Tensor([[[1, 2, 3], [4, 5, 6]],[[10, 20, 30], [40, 50, 60]]])
print(M)
(0 ,.,.) = 
   1   2   3
  10  20  30
(1 ,.,.) = 
   4   5   6
  40  50  60
[torch.FloatTensor of size 2x2x3]

Now I do:
M2 = torch.transpose(M, 0, 1)
which is swiping the the dimension 1 with 0 and gives me:

(0 ,.,.) = 
   1   2   3
  10  20  30
(1 ,.,.) = 
   4   5   6
  40  50  60
[torch.FloatTensor of size 2x2x3]

so far so good. but why when i do M2 = torch.transpose(M, 1, 0) is still gives me the same results and NOT:

(0 ,.,.) = 
  10  20  30
  1   2   3
(1 ,.,.) = 
  40  50  60
   4   5   6
[torch.FloatTensor of size 2x2x3]

???

After all, can anyone please tell me how I can get the latest representation?

1 Like

hmm did you by chance swap M and M2 in the paste here? when i do M[0,:,:], and M[1,:,:] i get:

M[0,:,:] = tensor([[1., 2., 3.],
[4., 5., 6.]])

M[1,:,:] = tensor([[10., 20., 30.],
[40., 50., 60.]])

and the transpose is:

tensor([[[ 1., 2., 3.],
[10., 20., 30.]],

    [[ 4.,  5.,  6.],
     [40., 50., 60.]]])

transposing just swaps axes, so transpose(M,0,1) is same as transpose(M,1,0)

You need M3 = torch.transpose(M.flip(0),0,1)