The Transpose Method Arguments

What does it mean in the transpose method:
“dim0 - the first dimension to be transposed”
“dim1 - the second dimension to be transposed”
which values do they get?

a = torch.tensor(np.arange(1,7).reshape(2,3))
print(a)
b = torch.transpose(c,0,1)
print(b)

and

a = torch.tensor(np.arange(1,7).reshape(2,3))
print(a)
b = torch.transpose(c,1,0)
print(b)

are the same and is the known transpose row to column and vice-versa (A_ij=A_ji)
but

a = torch.tensor(np.arange(1,7).reshape(2,3))
print(a)
b = torch.transpose(c,1,1)
print(b)

and

a = torch.tensor(np.arange(1,7).reshape(2,3))
print(a)
b = torch.transpose(c,0,0)
print(b)

do nothing.
Thanks

By definition the transpose permutes two dimensions of a vector.
For example if I have a tensor t1 of dimensions (0, 1, ..., i, ..., j, ..., n-1) ~ (dim_0, dim_1, ..., dim_i, ..., dim_j, ..., dim_n-1), transposing the dimensions i and j is the same as transforming t1 into a tensor t2 of dimension (0, 1, ..., i, ..., j, ..., n-1) ~ (dim_0, dim_1, ..., dim_j, ..., dim_i, ..., dim_n-1)

This is done with pytorch as follows: t2 = torch.transpose(t1,i,j) # or t1.transpose(i,j)

1 Like
a = torch.tensor(np.arange(1,7))
b = torch.transpose(a,0,2)
print(b)

returns Dimension out of range (expected to be in range of [-1, 0], but got 2)

I think your problem is more mathematical than pytorch.

The only possible transpose of a vector is itself, since it is of dimension in R (I say of dimension, the vector can itself be an element of R^n, n >= 1).
For example a = torch.arange(1, 7) just creates a vector of R^6, but remains of dimension in R, a scalar (a.shape = torch.Size([6]) ~ 6 € IR), so the only possible transpose is a.transpose(0,0) and is a: we have only one dimension, the dimension 0 (1-1)

But a = torch.rand((2, 4, 3)) is an element of R^2 x R^4 x R^3, so of dimension in R^3 (a.shape = torch.Size([2, 3, 7])), so we can transpose the dimension :

  • 0 with 1 (a.transpose(0, 1) = a.transpose(1, 0), of shape = torch.Size([4, 2, 3])) or 2 (a.transpose(0,2) = a.transpose(2,0))
  • 1 with 2 (a.transpose(1, 2) = a.transpose(2, 1), of shape = torch.Size([2, 3, 4])`)
  • we can’t go beyond 2 = 3-1, because the created tensor is of dimension in R^3 (I say of dimension) : take some linear algebra course if necessary :grinning: