Reshape to use every second item in tensor

Hello,

my goal is the following:

t = torch.tensor([1, 2, 3, 4])

# Every second element now in own tensor
expected = torch.tensor([[1, 3], [2, 4]])

assert expected == t

What operations do I have to perform to reshape the tensor, such that we use every second element as a new dimension.

With best regards,
Patrick

the following code works for your example but i don’t know if that’s what you want.

import torch
t = torch.tensor([1, 2, 3, 4])

# Every second element now in own tensor
expected = torch.tensor([[1, 3], [2, 4]])
t.view(-1,2).t()

assert (expected == t.view(-1,2).t()).all()
1 Like