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路上前行的菜鸟
有任何问题可以加我微信 ,进行进一步交流
在这里插入图片描述

  • 17
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值