Iter() get stuck on CIFAR10

Hi there,
I’m following the tutorials on this link: Introduction to PyTorch — PyTorch Tutorials 2.2.2+cu121 documentation
I implemented code under " Training Your Pytorch Model ", but found that the code always get stuck on " dataiter = iter(trainloader) ". It also gets stuck during the training on “for i, data in enumerate(trainloader, 0):” Could any one help me with this? Thanks! The original code is pasted below:

==================================Code below===============================

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

import torchvision
import torchvision.transforms as transforms

import matplotlib
import matplotlib.pyplot as plt
import numpy as np

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

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)

testset = torchvision.datasets.CIFAR10(root=‘./data’, train=False,
download=True, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=4,
shuffle=False, num_workers=2)

classes = (‘plane’, ‘car’, ‘bird’, ‘cat’,
‘deer’, ‘dog’, ‘frog’, ‘horse’, ‘ship’, ‘truck’)

import matplotlib.pyplot as plt
import numpy as np

def imshow(img):
img = img / 2 + 0.5 # unnormalize
npimg = img.numpy()
plt.imshow(np.transpose(npimg, (1, 2, 0)))

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

imshow(torchvision.utils.make_grid(images))

print(’ ‘.join(’%5s’ % classes[labels[j]] for j in range(4)))

class Net(nn.Module):
def init(self):
super(Net,self).init()
self.conv1 = nn.Conv2d(3, 6, 5)
self.conv2 = nn.Conv2d(6, 16, 5)
self.pool = nn.MaxPool2d(2, 2)
self.fc1 = nn.Linear(1655, 120)
self.fc2 = nn.Linear(120, 84)
self.fc2 = nn.Linear(84, 10)

def forward(self,x):
    x = self.pool(F.relu(self.conv1(x)))
    x = self.pool(F.relu(self.conv2(x)))
    x = x.view(-1,16*5*5)
    x = F.relu(self.fc1(x))
    x = F.relu(self.fc2(x))
    x = self.fc3(x)
    return x

print(f"Using {device} device")

net = Net()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(),lr = 0.001, momentum = 0.9)

for epoch in range(2):
running_loss = 0.0
for i, data in enumerate(trainloader, 0):
inputs, labels = data

    optimizer.zero_grad()
    
    outputs = net.input(inputs)
    loss = criterion(outputs, labels)
    loss.backward()
    optimizer.step()
    
    running_loss += loss.item()
    
    if i % 2000 == 1999:
        print('[%d, %5d] loss: %3d' % 
              (epoch+1, i+1, running_loss/2000))
        running_loss = 0.0

print(“Finished Training”)

Hi, can you post and describe your error as well? It doesn’t seem to be wrong when I run dataiter = iter(trainloader) part in colab (although there are some errors in code later).

Hi UMAR, Thank you so much for your reply! I found where the problem might be :rofl: I used Spyder IDE. It gets stuck there. I tried again with ipython and this issue is gone… Thanks a lot!!