我尝试使用
Torch Dataset and DataLoader
加载数据集,但出现以下错误:
AttributeError: '_MultiProcessingDataLoaderIter' object has no attribute 'next'
我使用的代码是:
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)
我尝试让num_workers=0,仍然有同样的错误。
Python version 3.8.9
PyTorch version 1.13.0
当我尝试调用 next() 方法时,我也遇到了同样的问题
dataiter = iter(dataloader)
data = dataiter.next()
您需要使用以下内容,它可以完美运行:
dataiter = iter(dataloader)
data = next(dataiter)
最后你的代码应该如下所示:
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)
在 pytorch 1.12 中语法:
iter(trn_loader).next()
工作正常以及:
next(iter(trn_loader))
从 pytorch 1.13 开始,唯一有效的语法是:
next(iter(trn_loader))
于 2023 年 4 月更新 而不是从
iter(trn_loader).next()
更改
到next(iter(trn_loader))
。我更喜欢解决 pyTorch 版本问题,因为我不知道代码中有多少个.next()
。
conda install pytorch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1 -c pytorch
我也面临着同样的问题。我使用的是torch ==2.3版本。 我收到的错误是 RuntimeError: An attempts has made to start a new process before the current process has finish its bootstrapping stage,这表明 Python 中的多处理存在问题。当使用 PyTorch 的 DataLoader 启用多处理时,经常会发生此错误,尤其是在 Windows 系统上。 这对我有用:
类 WineDataset(数据集):
def __init__(self):
# data loading
xy = np.loadtxt('E:\\Github\\Pytorch_beginner\\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]
# support indexing such that dataset[i] can be used to get i-th sample
def __getitem__(self,index):
# dataset[0]
return self.x[index], self.y[index]
# we can call len(dataset) to return the size
def __len__(self):
#len(dataset)
return self.n_samples
if name == 'main': 数据集 = WineDataset() dataloader = DataLoader(数据集=数据集,batch_size=4,shuffle=True,num_workers=2) dataiter = iter(数据加载器) 数据 = 下一个(dataiter)
希望这有帮助!