How to stack tensors?

i have a tensor x = [1,2,3]. I have another tensor y = [4,5,6]. I want to stack x on top of y such that the new tensor z = [[1,2,3], [4,5,6]]. How to do this?

torch.stack should work:

x = torch.tensor([1,2,3])
y = torch.tensor([4,5,6])
z = torch.stack((x, y), dim=0)
print(z)
# tensor([[1, 2, 3],
#         [4, 5, 6]])
1 Like