DataLoader Error: cannot unsqueeze empty tensor

I have the following code. I don’t know why it’s giving this error.

# Generate dummy data
input_data = torch.rand((100, 30, 40))
output_data = torch.rand((100, 20, 10))

# Data loader
class Loader(Dataset):
    
    def __len__(self):
        return inpu_data.size()[0]
    
    def __getitem__(self, idx): 
        input_frame = input_data[idx*batch_size:(idx+1)*batch_size]
        output_words = output_data[idx*batch_size:(idx+1)*batch_size]
        return input_frame, output_words

# dataloader 
loader = Loader()
dataloader = DataLoader(loader, batch_size=20,shuffle=True)

for idx, sample in enumerate(dataloader):
    print(idx)

RuntimeError: cannot unsqueeze empty tensor

It works for me.

import torch
from torch.utils.data.dataset import Dataset
from torch.utils.data import DataLoader

# Generate dummy data
input_data = torch.rand((100, 30, 40))
output_data = torch.rand((100, 20, 10))

# Data loader
class Loader(Dataset):
    def __len__(self):
        return input_data.size()[0]
    
    def __getitem__(self, idx): 
        input_frame = input_data[idx]
        output_words = output_data[idx]
        return input_frame, output_words

# dataloader 
loader = Loader()
dataloader = DataLoader(loader, batch_size=20, shuffle=True)

for idx, sample in enumerate(dataloader):
    print(idx, sample)

There are 2 things wrong with the code here. Firstly, the example you have provided does not run. Secondly, I think that you may have misunderstood how the data loader and dataset works.

The dataset class is just a class that provides access to your data. As such it needs 2 things:
a) how much data you have in the dataset, which it gets from the len method
b) how to access a data point from the dataset, which it gets from the getitem method.

Once the dataloader has the dataset and the the batch size that you need it will assemble the batch for you. I think the point that you misunderstood is that the getitem method does not need to provide the entire batch but just one element. The entire batch is assembled by the data loader automatically.

The example by fangyh is the proper way to do it

1 Like