【问题标题】:Iterator doesn't work with DataLoader on GPU迭代器不适用于 GPU 上的 DataLoader
【发布时间】:2020-12-08 18:27:40
【问题描述】:

我在 Google Colab 上使用 PyTorch,使用 GPU 时出现此错误,

TypeError                                 Traceback (most recent call last)
<ipython-input-33-41cdbc758ecd> in <module>()
----> 1 dataiter= iter(trainloader)

TypeError: '_SingleProcessDataLoaderIter' object is not callable

但是使用正常的CPU没有错误。

我的代码:

%matplotlib inline
%config InlineBackend.figure_format = 'retina'

import torch
import numpy as np
from torchvision import datasets, transforms

from collections import  OrderedDict

from torch import nn
from torch import  optim
import torch.nn.functional as F
import helper


transform = transforms.Compose([transforms.ToTensor(),
     transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

trainset= datasets.MNIST("MINIST_data/", download= True, train=True, transform=transform)
trainloader= torch.utils.data.DataLoader(trainset, batch_size= 64, shuffle=True)
dataiter= iter(trainloader)

使用enumerate 而不是iter 适用于GPU,但我不知道为什么,有人可以向我解释错误以及为什么会发生!?

【问题讨论】:

  • 你的代码在 colab 上和我一起工作
  • @Mughees 在 GPU 上运行?!
  • 是的,它适用于 gpu
  • 也许是火炬版本的原因??
  • 您自己安装?因为 colab 的 torch 是相同的 1.6

标签: python pytorch google-colaboratory


【解决方案1】:

您不必使用 iter。 trainloader 已经是可迭代的。循环应该像这样for data in trainloader:for index, data in enumerate(trainloader):

【讨论】:

    猜你喜欢
    • 2020-10-02
    • 2020-10-22
    • 2013-04-12
    • 1970-01-01
    • 1970-01-01
    • 2022-10-14
    • 2013-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多