出現(xiàn)一下錯誤 怎么辦?
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)
下方是報錯的信息 由于跟著之前的課程學習,由于年代久遠一點,時不時會出現(xiàn)一些小的問題 ,庫的變動 (裝最新的版本 ,老版本的不需要擔心)文章來源:http://www.zghlxwxcb.cn/news/detail-559145.html
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
出現(xiàn)上述錯誤的 可以看看我附上源代碼的 注釋 #--------------注意點 這個部分
第一步 你需要改 num_workers=0
第二步 next(dataiter)
然后運行一下就可以
希望我的文章對你有幫助
我是煉丹的小道士,一個在ML路上前行的菜鳥
有任何問題可以加我微信 ,進行進一步交流
文章來源地址http://www.zghlxwxcb.cn/news/detail-559145.html
到了這里,關(guān)于Dataloader使用時iter.next()迭代器出現(xiàn)錯誤怎么解決以及_MultiProcessingDataLoaderIter報錯解決指南的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!