pythonpytorchtorchpytorch-dataloader

AttributeError: '_MultiProcessingDataLoaderIter' object has no attribute 'next'


I am trying to load the dataset using Torch Dataset and DataLoader, but I got the following error:

AttributeError: '_MultiProcessingDataLoaderIter' object has no attribute 'next'

the code I use is:

class WineDataset(Dataset):

    def __init__(self):
        # Initialize data, download, etc.
        # read with numpy or pandas
        xy = np.loadtxt('./data/wine.csv', delimiter=',', dtype=np.float32, skiprows=1)
        self.n_samples = xy.shape[0]

        # here the first column is the class label, the rest are the features
        self.x_data = torch.from_numpy(xy[:, 1:]) # size [n_samples, n_features]
        self.y_data = torch.from_numpy(xy[:, [0]]) # size [n_samples, 1]

    # support indexing such that dataset[i] can be used to get i-th sample
    def __getitem__(self, index):
        return self.x_data[index], self.y_data[index]

    # we can call len(dataset) to return the size
    def __len__(self):
        return self.n_samples

    dataset = WineDataset()
        
    train_loader = DataLoader(dataset=dataset,
                              batch_size=4,
                              shuffle=True,
                              num_workers=2)

I tried to make the num_workers=0, still have the same error.

Python version 3.8.9
PyTorch version 1.13.0

Solution

  • I too faced the same issue, when i tried to call the next() method as follows

    dataiter = iter(dataloader)
    data = dataiter.next()
    

    You need to use the following instead and it works perfectly:

    dataiter = iter(dataloader)
    data = next(dataiter)
    

    Finally your code should look like follows:

    class WineDataset(Dataset):
    
        def __init__(self):
            # Initialize data, download, etc.
            # read with numpy or pandas
            xy = np.loadtxt('./data/wine.csv', delimiter=',', dtype=np.float32, skiprows=1)
            self.n_samples = xy.shape[0]
    
            # here the first column is the class label, the rest are the features
            self.x_data = torch.from_numpy(xy[:, 1:]) # size [n_samples, n_features]
            self.y_data = torch.from_numpy(xy[:, [0]]) # size [n_samples, 1]
    
        # support indexing such that dataset[i] can be used to get i-th sample
        def __getitem__(self, index):
            return self.x_data[index], self.y_data[index]
    
        # we can call len(dataset) to return the size
        def __len__(self):
            return self.n_samples
    
    dataset = WineDataset()      
    dataloader = DataLoader(dataset=dataset,
                                  batch_size=4,
                                  shuffle=True,
                                  num_workers=2)
    
    dataiter = iter(dataloader)
    data = next(dataiter)