Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'iterator' object has no attribute 'next' in python 3.7

I'm trying to iterate through my data set and get the first element

    transform = transforms.Compose([transforms.ToTensor(),transforms.Normalize((0.5),(0.5)),])
    trainloader = datasets.MNIST('~/.pytorch/MNIST_data' , download=True,train=True , transform=transform)
    ds = iter(trainloader)
    img, labels = ds.next()

but it returns this error

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

I also tried this

    img , labels = next(ds)

returned this error

    StopIteration:

Did I miss something ?

like image 867
art Avatar asked Aug 14 '26 03:08

art


2 Answers

Might be this issue: https://github.com/microsoft/DeepSpeedExamples/issues/222

Then change from:

images, labels = dataiter.next()

to:

images, labels = next(dataiter)
like image 188
Al Conrad Avatar answered Aug 16 '26 16:08

Al Conrad


If you follow the tutorial on https://pytorch.org/tutorials/beginner/blitz/cifar10_tutorial.html

trainset = torchvision.datasets.CIFAR10(root='./data', train=True,
                                        download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4,
                                          shuffle=True, num_workers=2)

dataiter = iter(trainloader)
images, labels = dataiter.next()

You are missing the DataLoader() function on your dataset

like image 43
Rajarishi Devarajan Avatar answered Aug 16 '26 15:08

Rajarishi Devarajan