pythonpytorchtensor

How to make an empty tensor in Pytorch?


In python, we can make an empty list easily by doing a = []. I want to do a similar thing but with Pytorch tensors.

If you want to know why I need that, I want to get all of the data inside a given dataloader (to create another customer dataloader). Having an empty tensor can help me gather all of the data inside a tensor using a for-loop. This is a sudo code for it.

all_data_tensor = # An empty tensor

for data in dataloader:
  all_data_tensor  = torch.cat((all_data_tensor, data), 0)

Is there any way to do this?


Solution

  • We can do this using torch.empty. But notice torch.empty needs dimensions and you have to set the first dimension as zero to have an empty tensor.

    The code would be like this:

    # suppose the data generated by the dataloader has the size of (batch, 25)
    all_data_tensor = torch.empty((0, 25), dtype=torch.float32)
    # first dimension should be zero.
    
    for data in dataloader:
      all_data_tensor = torch.cat((all_data_tensor, data), 0)