24

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
ChaosPredictor
  • 3,777
  • 1
  • 36
  • 46
Adham Enaya
  • 780
  • 1
  • 11
  • 29

3 Answers3

55

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()
        
    train_loader = DataLoader(dataset=dataset,
                              batch_size=4,
                              shuffle=True,
                              num_workers=2)

dataiter = iter(dataloader)
data = next(dataiter)
  • 1
    If you can, please explain why this works too – Temba Mar 23 '23 at 14:18
  • @Temba, Thanks for the question. Also thanks to ChaosPredictor's answer which explains why the next() method works the other way. In short, it is because of the difference in the pytorch versions. You can verify it by installing pip install torch==1.12.1 in virtual enviroment and pip install torch==1.13.1 in another venv and check. I personally verified it and it is as ChaosPreditor says in his answer below. – Syed Jameel Ahmed Apr 01 '23 at 10:33
9

In pytorch 1.12 the syntax:

iter(trn_loader).next()

work fine as well as:

next(iter(trn_loader))

From pytorch 1.13 the only working syntax is:

next(iter(trn_loader))
ChaosPredictor
  • 3,777
  • 1
  • 36
  • 46
1

Updated April 2023 Instead of changing from iter(trn_loader).next() to next(iter(trn_loader)). I prefer to solve pyTorch version problem because I have no idea how many .next() is present in the code.

conda install pytorch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1 -c pytorch
Khawar Islam
  • 2,556
  • 2
  • 34
  • 56