RuntimeError: Expected a ‘cuda‘ device type for generator but found ‘cpu‘

Posted AI浩

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RuntimeError: Expected a ‘cuda‘ device type for generator but found ‘cpu‘相关的知识,希望对你有一定的参考价值。

    batch_iterator = iter(data_loader)
  File "D:\\ProgramData\\Anaconda3\\envs\\mytorch\\lib\\site-packages\\torch\\utils\\data\\dataloader.py", line 368, in __iter__
    return self._get_iterator()
  File "D:\\ProgramData\\Anaconda3\\envs\\mytorch\\lib\\site-packages\\torch\\utils\\data\\dataloader.py", line 314, in _get_iterator
    return _MultiProcessingDataLoaderIter(self)
  File "D:\\ProgramData\\Anaconda3\\envs\\mytorch\\lib\\site-packages\\torch\\utils\\data\\dataloader.py", line 965, in __init__
    self._reset(loader, first_iter=True)
  File "D:\\ProgramData\\Anaconda3\\envs\\mytorch\\lib\\site-packages\\torch\\utils\\data\\dataloader.py", line 996, in _reset
    self._try_put_index()
  File "D:\\ProgramData\\Anaconda3\\envs\\mytorch\\lib\\site-packages\\torch\\utils\\data\\dataloader.py", line 1230, in _try_put_index
    index = self._next_index()
  File "D:\\ProgramData\\Anaconda3\\envs\\mytorch\\lib\\site-packages\\torch\\utils\\data\\dataloader.py", line 521, in _next_index
    return next(self._sampler_iter)  # may raise StopIteration
  File "D:\\ProgramData\\Anaconda3\\envs\\mytorch\\lib\\site-packages\\torch\\utils\\data\\sampler.py", line 226, in __iter__
    for idx in self.sampler:
  File "D:\\ProgramData\\Anaconda3\\envs\\mytorch\\lib\\site-packages\\torch\\utils\\data\\sampler.py", line 122, in __iter__
    yield from torch.randperm(n, generator=generator).tolist()
RuntimeError: Expected a 'cuda' device type for generator but found 'cpu'

    data_loader = data.DataLoader(dataset, args.batch_size,
                                  num_workers=args.num_workers,
                                  shuffle=True, collate_fn=detection_collate,
                                  pin_memory=True)

改为

    data_loader = data.DataLoader(dataset, args.batch_size,
                                  num_workers=args.num_workers,
                                  shuffle=True, collate_fn=detection_collate,
                                  pin_memory=True,generator=torch.Generator(device = 'cuda'))

以上是关于RuntimeError: Expected a ‘cuda‘ device type for generator but found ‘cpu‘的主要内容,如果未能解决你的问题,请参考以下文章

RuntimeError: Expected hidden size (2, 24, 50), got (2, 30, 50)

RuntimeError:Expected to have finished reduction in the prior iteration

RuntimeError: Expected hidden[0] size (1, 1, 512), got (1, 128, 512) for LSTM pytorch

RuntimeError: Expected object of type torch.cuda.FloatTensor but found type torch.FloatTensor for ar

RuntimeError: Expected all tensors to be on the same device, but found at least two devices

RuntimeError: Expected all tensors to be on the same device,但发现至少有两个设备,cpu和cuda:0!使用我的模型进行预测时