Reputation: 13
What I am doing right now is this:
In [1]: torch.Tensor([[[] for _ in range(3)] for _ in range(5)])
Out[1]: tensor([], size=(5, 3, 0))
This works fine for me, but is there maybe a torch function that does this that I am missing?
Thanks in advance!
Edit: My use case is this: I use this to aggregate Tensors with all dimensions the same and that dont have the empty dimension. I am using torch.cat:
# results start with shape (a,b,0)
results = torch.Tensor([[[] for _ in range(b)] for _ in range(a)])
for t in range(time):
# r has shape (a,b)
r = model(...)
# results now has shape (a,b,t)
results = torch.cat([results,r.unsqueeze(2)],dim=-1)
Simply appending to a list is impractical for me as I have to do reshaping operations on results
on every step (Im doing beam search).
One solution would also be to not initialize results
until I have the first returned Tensor, but this feels unpythonic/wrong.
Upvotes: 1
Views: 15071
Reputation: 3573
You have the torch.empty function :
torch.empty(5,3,0)
>>> tensor([], size=(5, 3, 0))
is a tensor without any entry
Upvotes: 1
Reputation: 953
This can be another way depending on your usecase.
alpha = torch.tensor([])
In[5]: alpha[:,None,None,None]
Out[5]: tensor([], size=(0, 1, 1, 1))
Otherways:
torch.tensor([[[[]]]]) #tensor([], size=(1, 1, 1, 0))
torch.tensor([[[[],[]]]]) #tensor([], size=(1, 1, 2, 0))
Upvotes: 1