Appending tensors

I want to append a new row to a tensor in each step of a loop, but neither torch.cat nor torch.stack is working for me, because they return an error. It says the tensors must have the same shape.
I can append the tensors to a list, and then stack them, but I wanted to do it in GPU, could you please suggest a way?

e.g. I have a 1x10 tensor, I want to add another 1x10 tensor so I could have 2x10 tensor. It works, but for appending another 1x10 to the 2x10 tensor, so I would have 3x10 tensor.

1 Like

torch.cat works for me:

a = tc.empty(1, 10)
for i in range(6):
    b = tc.empty(1, 10)
    a = tc.cat((a, b))

I suggest appending them to a list and stack them. You can test which way is faster on your GPU.

1 Like