Reshape a tensor

Hi I want to make the following tensor
tensor([[1., 1., 1., 1., 1.],
[2., 2., 2., 2., 2.],
[3., 3., 3., 3., 3.],
[4., 4., 4., 4., 4.],
[5., 5., 5., 5., 5.],
[6., 6., 6., 6., 6.]])

to

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

I tried to do with view, but failed to do so. I want to do it so that I can compute backward() on the tensor.

Can anyone help?

x.view(2, 3, 5).permute(1, 0, 2).contiguous().view(3, 10) should work.

Thanks a lot!:grinning::grinning::grinning:

One more question.
what about if I want to add the first half and the second half?
That is,

[5,5,5,5,5]
[7,7,7,7,7]
[9,9,9,9,9]

Thanks a lot!

This should work:

x.view(2, 3, 5).sum(0)

thanks a lot!!:grinning::grinning:

Sorry for coming back with a similar question.

How do I make

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

to

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

This should work:

x.view(3, 2, 5).permute(1, 0, 2).reshape(6, 5)