How to interleave two tensors along certain dimension?

I’m sure there is a better way to achieve this, but as of now, you could do the following:

a = torch.Tensor([[1,1], [1,1]])
b = torch.Tensor([[2,2], [2,2]])

torch.stack((a, b), dim=0).view(2, 4).t().contiguous().view(2, 4)
>> 1 2 1 2
>> 1 2 1 2
>> [torch.FloatTensor of size 2x4]

torch.stack((a, b), dim=0).view(4, 2).t().contiguous().view(4, 2)
>> 1 1
>> 2 2
>> 1 1 
>> 2 2
>> [torch.FloatTensor of size 4x2]
7 Likes