1

I want to combine two 3D tensors (Pytorch) alternatively

import torch
x = torch.ones(2, 2, 2, requires_grad=False, dtype=torch.float64)
y = torch.zeros(2, 2, 2, requires_grad=False, dtype=torch.float64)

#The results I want are
#Slicing[:,:,0]
#1010
#1010
#Slicing[:,:,1]
#1010
#1010

I found this one from the previous question (Interweaving two numpy arrays), but I think it only works for 1d array (tensor)

import torch
x = torch.ones(2, 2, 2, requires_grad=False, dtype=torch.float64)
y = torch.zeros(2, 2, 2, requires_grad=False, dtype=torch.float64)
result=torch.ravel(torch.column_stack((x, y)))
print('Check\n',result)

Miraboreasu
  • 83
  • 1
  • 1
  • 7
  • You are right, can I change to```torch.dstack((x,y)).reshape(size, -1, size)``` if x and y are```(size, size, size, requires_grad=False, dtype=torch.float64)``` – Miraboreasu Jul 03 '22 at 18:52
  • try this "output = torch.cat([x, y], dim=-1) order = [0, 2, 1, 3] output = output[:, :, order].permute(0, 2, 1) print(output[:, :, 0])" – Mohammad Sartaj Jul 03 '22 at 20:40

0 Answers0