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 3 Answers
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) 2In 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)) 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