Dataloader使用时iter.next()迭代器出现错误怎么解决以及_MultiProcessingDataLoaderIter报错解决指南

出现一下错误 怎么办?


AttributeError: '_MultiProcessingDataLoaderIter' object has no attribute 'next'

以下附上出问题的源码 这是 我在学习如何dataloader 使用迭代器时候报的错误

import torch
import torchvision
from torch.utils.data import Dataset,DataLoader
import numpy as np
import math

class WineDataset(Dataset):
    def __init__(self):
        #data loading
        xy = np.loadtxt('./wine.csv',delimiter=",", dtype=np.float32, skiprows=1)#delimiter  分隔符  skiprows跳过第一行
        self.x = torch.from_numpy(xy[:, 1:])
        self.y = torch.from_numpy(xy[:,[0]])
        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

dataset = WineDataset()
# first_data = dataset[0]
# features, labels = first_data  #test
# print(features,labels)
dataloader = DataLoader(dataset=dataset, batch_size=4,shuffle=True, num_workers=2) #--------------注意点 
dataiter = iter(dataloader)
data = dataiter.next()  #--------------注意点
features, labels = data
print(features, labels)

下方是报错的信息 由于跟着之前的课程学习,由于年代久远一点,时不时会出现一些小的问题 ,库的变动 (装最新的版本 ,老版本的不需要担心)

RuntimeError: 
        An attempt has been made to start a new process before the
        current process has finished its bootstrapping phase.

        This probably means that you are not using fork to start your
        child processes and you have forgotten to use the proper idiom
        in the main module:

            if __name__ == '__main__':
                freeze_support()
                ...

        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.
Traceback (most recent call last):
  File "C:\Users\92301\miniconda3\envs\hyspytorch\lib\site-packages\torch\utils\data\dataloader.py", line 1120, in _try_get_data
    data = self._data_queue.get(timeout=timeout)
  File "C:\Users\92301\miniconda3\envs\hyspytorch\lib\multiprocessing\queues.py", line 108, in get
    raise Empty
_queue.Empty

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "D:\codepython\pythonSpectralProcessing\torch\09Dataset and Dataloader.py", line 27, in <module>
    data = next(dataiter)  #--------------注意点
  File "C:\Users\92301\miniconda3\envs\hyspytorch\lib\site-packages\torch\utils\data\dataloader.py", line 628, in __next__
    data = self._next_data()
  File "C:\Users\92301\miniconda3\envs\hyspytorch\lib\site-packages\torch\utils\data\dataloader.py", line 1316, in _next_data
    idx, data = self._get_data()
  File "C:\Users\92301\miniconda3\envs\hyspytorch\lib\site-packages\torch\utils\data\dataloader.py", line 1282, in _get_data
    success, data = self._try_get_data()
  File "C:\Users\92301\miniconda3\envs\hyspytorch\lib\site-packages\torch\utils\data\dataloader.py", line 1133, in _try_get_data
    raise RuntimeError('DataLoader worker (pid(s) {}) exited unexpectedly'.format(pids_str)) from e
RuntimeError: DataLoader worker (pid(s) 46244, 40520) exited unexpectedly

Process finished with exit code 1

出现上述错误的 可以看看我附上源代码的 注释 #--------------注意点 这个部分
第一步 你需要改 num_workers=0
第二步 next(dataiter)
然后运行一下就可以
希望我的文章对你有帮助
我是炼丹的小道士,一个在ML路上前行的菜鸟
有任何问题可以加我微信 ,进行进一步交流
Dataloader使用时iter.next()迭代器出现错误怎么解决以及_MultiProcessingDataLoaderIter报错解决指南_第1张图片

你可能感兴趣的:(pytorch学习指南,python,深度学习,numpy)