AttributeError:“_MultiProcessingDataLoaderIter”对象没有属性“next”

Adh*_*aya 26 python torch pytorch pytorch-dataloader

我尝试使用 加载数据集Torch Dataset and DataLoader,但出现以下错误:

AttributeError: '_MultiProcessingDataLoaderIter' object has no attribute 'next'
Run Code Online (Sandbox Code Playgroud)

我使用的代码是:

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)
Run Code Online (Sandbox Code Playgroud)

我尝试让num_workers=0,仍然有同样的错误。

Python version 3.8.9
PyTorch version 1.13.0
Run Code Online (Sandbox Code Playgroud)

Sye*_*med 71

当我尝试调用 next() 方法时,我也遇到了同样的问题,如下所示

dataiter = iter(dataloader)
data = dataiter.next()
Run Code Online (Sandbox Code Playgroud)

您需要使用以下内容来代替它并且它可以完美工作:

dataiter = iter(dataloader)
data = next(dataiter)
Run Code Online (Sandbox Code Playgroud)

最后你的代码应该如下所示:

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)
Run Code Online (Sandbox Code Playgroud)


Cha*_*tor 9

在 pytorch 1.12 中语法:

iter(trn_loader).next()
Run Code Online (Sandbox Code Playgroud)

工作正常以及:

next(iter(trn_loader))
Run Code Online (Sandbox Code Playgroud)

从 pytorch 1.13 开始,唯一有效的语法是:

next(iter(trn_loader))
Run Code Online (Sandbox Code Playgroud)