Beginner Problem: Error iterating through the MNIST dataset

Iterating through the MNIST Dataset from torch vision throws error

--> for i, data in enumerate(trainloader):

This line causes the following error:

TypeError: zip argument #2 must support iteration

How can I deal with this?

1 Like

Could you post your code please?
This small example works for me:

dataset = datasets.MNIST(root='./data',
                         download=False,
                         transform=transforms.ToTensor())
loader = DataLoader(dataset)

for i, data in enumerate(loader):
    print(i)

I used this:

transform = transforms.Compose([transforms.ToTensor(),
                                        transforms.Normalize((0.5), (0.5))])
                               
                        
trainset = torchvision.datasets.MNIST(root='./data', train=True,
                                        download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=128,
                                          shuffle=True)

for i, data in enumerate (trainloader):

``

Thanks for the code.
The error is thrown, because Normalize internally iterates the tensor channels, mean and std.
Since you just have one value, you should pass it as:

transforms.Normalize((0.5,), (0.5,))

Also, have a look at these mean and std estimates. They might work a bit better than 0.5, since they were calculated on the MNIST training data.

2 Likes