PyTorch Equivalent of np.empty()

May I know what’s the equivalent of PyTorch’s np.empty()?

I would like to have an empty array and concatenate arrays in a loop to this original array.

My current usage:

a = np.empty(0)  # This creates an empty array
b = np.concatenate((a, [1]))  # New array of shape (1,) 
c = np.concatenate((b,[1]))  # New array of shape (2,) and so on

The fastest way to do this in PyTorch would be to repeatedly append to a list, then call torch.cat on the whole list once at the end. Assuming you’re on GPU, each call to torch.cat is a kernel invocation while appending to a list doesn’t touch the GPU.

1 Like