Data to vector not transposed

When I transpose 2D array it works fine. but when I want to put transposed data to vector, then it’s the same as vector before transpose function.

CODE:

torch::Tensor data = torch::rand({5,10}).to(torch::kFloat32);

std::cout << data << std::endl;
std::vector vectorData(data.data_ptr(), data.data_ptr() + data.numel());
std::cout << vectorData << std::endl;

data = data.transpose(0, -1);

std::cout << data << std::endl;
std::vector vectorData1(data.data_ptr(), data.data_ptr() + data.numel());
std::cout << vectorData1 << std::endl;

data.transpose changes the metadata of the tensor and doesn’t trigger a copy so you might want to call .coniguous() on the tensor:

x = torch.randn(3, 4)
print(x.shape)
# torch.Size([3, 4])
print(x.stride())
# (4, 1)
print(x.data_ptr())

y = x.transpose(1, 0)
print(y.shape)
# torch.Size([4, 3])
print(y.stride())
# (1, 4)

z = y.contiguous()
print(z.shape)
# torch.Size([4, 3])
print(z.stride())
# (3, 1)

Thank you for the explanation.