Hi Nikiguo (and gphilip)!



 gphilip:

See torch.stack().


For the specific use case you detail, you will prefer torch.cat().
(torch.stack() will create a new dimension along which to stack.)
>>> import torch
>>> torch.__version__
'1.9.0'
>>> y_0 = torch.arange (5).unsqueeze (0)
>>> y_1 = torch.arange (5).unsqueeze (0) + 10
>>> y_2 = torch.arange (5).unsqueeze (0) + 20
>>> y_0.shape
torch.Size([1, 5])
>>> y_list = [y_0, y_1, y_2]
>>> torch.cat (y_list).shape
torch.Size([3, 5])
>>> torch.cat (y_list)
tensor([[ 0,  1,  2,  3,  4],
        [10, 11, 12, 13, 14],
        [20, 21, 22, 23, 24]])
>>> torch.stack (y_list).shape
torch.Size([3, 1, 5])
>>> torch.stack (y_list)
tensor([[[ 0,  1,  2,  3,  4]],

        [[10, 11, 12, 13, 14]],

        [[20, 21, 22, 23, 24]]])

(In your case, you could use stack() followed by squeeze() to
eliminate one of the singleton dimensions.)
Best.
K. Frank