Reputation: 908
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
Upvotes: 37
Views: 59188
Reputation: 1056
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)
Upvotes: 94
Reputation: 21
I was facing the same issue. I am using torch ==2.3 version. This worked for me.
class WineDataset(Dataset):
def __init__(self):
# data loading
xy = np.loadtxt('path\\wine.csv', delimiter = ",", dtype = np.float32, skiprows =1 )
self.x = torch.from_numpy(xy[:,1:])
self.y = torch.from_numpy(xy[:,[0]]) # n_samples,1
self.n_samples = xy.shape[0]
def __getitem__(self,index):
# dataset[0]
return self.x[index], self.y[index]
def __len__(self):
#len(dataset)
return self.n_samples
if __name__ == '__main__':
dataset = WineDataset()
dataloader = DataLoader(dataset=dataset, batch_size=4, shuffle=True, num_workers=2)
dataiter = iter(dataloader)
data = next(dataiter)
Upvotes: 2
Reputation: 2924
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
Upvotes: 0
Reputation: 4081
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))
Upvotes: 13