【问题标题】:AttributeError: '_MultiProcessingDataLoaderIter' object has no attribute 'next'AttributeError: \'_MultiProcessingDataLoaderIter\' 对象没有属性 \'next\'
【发布时间】:2022-11-06 03:50:12
【问题描述】:

我正在尝试使用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

【问题讨论】:

    标签: pytorch torch pytorch-dataloader


    【解决方案1】:

    当我尝试如下调用 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-05
      • 1970-01-01
      • 2012-12-01
      • 2021-04-19
      • 2021-11-22
      • 1970-01-01
      • 1970-01-01
      • 2018-08-28
      相关资源
      最近更新 更多