Why does torch transpose return a non-contiguous tensor but the equivalent view operation returns a contiguous one?

Viewed 176

Both torch.Tensor.view() and torch.tensor.transpose() can transpose a 2 dimensional tensor, e.g.

a = torch.arange(8).reshape(2, 4)
a.t().is_contiguous() # False
a.view(4,2).is_contiguous() # True

But exchanging the dimensions with .view() results in a contiguous tensor while using .t() results in a non-contiguous one. It's clearly somehow possible to transpose while retaining contiguity, though I don't really understand how, but my main question is: why is this behavior different for these two functions?

The documentation for view says that the "returned tensor shares the same data" and for transpose that the "resulting out tensor shares its underlying storage with the input tensor" so I naively expect the contiguity status of the outputs to be the same for both functions, but perhaps "sharing the same data" and "sharing underlying storage" are two different things?

Edit: It did not occur to me that there is a way to reshape this tensor into 4,2 that is not tansposing lol. Thanks to the author of the accepted answer for pointing out that a.t() != a.view(dim1, dim0).

1 Answers

With view(4, 2) you are not transposing the tensor, you are simply reshaping it.

Original tensor:

t = torch.tensor([[1, 2],
                  [3, 4],
                  [5, 6],
                  [7, 8]])

t.transpose() returns:

torch.tensor([[1, 3, 5, 7],
              [2, 4, 6, 8]])

t.view(4, 2) returns:

t.tensor([[1, 2, 3, 4],
          [5, 6, 7, 8]])
Related