I want to stack tensors as I collect them in a loop and start with an empty tensor. Usually, when collecting elements in python we start with an empty list and then append. But when I try to stack with an “empty tensor” (with no dimensions) I get an error. Check the error and code:
import torch

empty = torch.tensor([])
x = torch.randn(3, 5, 7)
print(torch.stack([empty, x], dim=0).size())

error
import torch
empty = torch.tensor([])
x = torch.randn(3, 5, 7)
print(torch.stack([empty, x], dim=0).size())
Traceback (most recent call last):
  File "/Users/brando/anaconda3/envs/automl-meta-learning/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3343, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-9-59f445556977>", line 5, in <module>
    print(torch.stack([empty, x], dim=0).size())
RuntimeError: stack expects each tensor to be equal size, but got [0] at entry 0 and [3, 5, 7] at entry 1

how do we do torch.stack such that: torch.stack([emtpty,x]) = x? where x is a torch tensor.

Update
hmmm perhaps what I need is torch.cat actually
import torch

empty = torch.tensor([])
x = torch.randn(3, 5, 7)

print(torch.cat([empty, x], dim=0).size())

since this adds new values at the front of the “list” but I guess I don’t really need to distinguish each append in my special case…probably still useful to have an identity for stack though.
# https://discuss.pytorch.org/t/identity-element-for-stack-operator-torch-stack-emtpty-x-x-empty-tensor-exists/111459
import torch
empty = torch.tensor([])
x = torch.randn(3, 5, 7)
print(torch.cat([empty, x], dim=0).size())
# print(torch.stack([empty, x], dim=0).size())
torch.Size([3, 5, 7])