How to flatten vertically

I have this tensor:

torch.tensor([[1, 1, 1],
           [1, 1, 1],
           [1, 1, 1],
           [2, 2, 2],
           [2, 2, 2],
           [2, 2, 2],
           [3, 3, 3],
           [3, 3, 3],
           [3, 3, 3]])

I want to flatten it vertically like this:
a = torch.tensor([[1, 1, 1, 2, 2, 2, 3, 3, 3,1, 1, 1, 2, 2, 2, 3, 3, 3,1, 1, 1, 2, 2, 2, 3, 3, 3]])

I have also another problem, when I reshape it by a.reshape(3,-1), the output:

tensor([[1, 1, 1, 2, 2, 2, 3, 3, 3],
        [1, 1, 1, 2, 2, 2, 3, 3, 3],
        [1, 1, 1, 2, 2, 2, 3, 3, 3]])

But I thought it would be:

tensor([[1, 2, 3,1, 2, 3, 1, 2, 3],
        [1, 2, 3,1, 2, 3, 1, 2, 3],
        [1, 2, 3, 1, 2, 3, 1, 2, 3]])

Why, and how I can get the last output?

Thank you for your help.

When you reshape a tensor, you are basically changing how the view of the tensor is done. The values actually remain on the same positions and are still contiguous. You might want to look at how the view works.

This should work for both of the views you requested.

Hope it helps :blush:

a = torch.flatten(t.T).unsqueeze(0)
print(a)

b = torch.stack(torch.split(t, 3), dim=2).reshape(3, -1)
print(b)