Iter() get stuck on CIFAR10

jryhu2gh: <p>Hi there,<br>
I’m following the tutorials on this link: <a href="https://pytorch.org/tutorials/beginner/introyt/introyt1_tutorial.html" class="inline-onebox" rel="noopener nofollow ugc">Introduction to PyTorch — PyTorch Tutorials 2.2.2+cu121 documentation</a><br>
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:</p>
<p>==================================Code below===============================</p>
<p>import torch<br>
import torch.nn as nn<br>
import torch.nn.functional as F<br>
import torch.optim as optim</p>
<p>import torchvision<br>
import torchvision.transforms as transforms</p>
<p>import matplotlib<br>
import matplotlib.pyplot as plt<br>
import numpy as np</p>
<p>transform = transforms.Compose(<br>
[transforms.ToTensor(),<br>
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])</p>
<p>trainset = torchvision.datasets.CIFAR10(root=‘./data’, train=True,<br>
download=True, transform=transform)<br>
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4,<br>
shuffle=True, num_workers=2)</p>
<p>testset = torchvision.datasets.CIFAR10(root=‘./data’, train=False,<br>
download=True, transform=transform)<br>
testloader = torch.utils.data.DataLoader(testset, batch_size=4,<br>
shuffle=False, num_workers=2)</p>
<p>classes = (‘plane’, ‘car’, ‘bird’, ‘cat’,<br>
‘deer’, ‘dog’, ‘frog’, ‘horse’, ‘ship’, ‘truck’)</p>
<p>import matplotlib.pyplot as plt<br>
import numpy as np</p>
<p>def imshow(img):<br>
img = img / 2 + 0.5     # unnormalize<br>
npimg = img.numpy()<br>
plt.imshow(np.transpose(npimg, (1, 2, 0)))</p>
<p>dataiter = iter(trainloader)<br>
images, labels = next(dataiter)</p>
<p>imshow(torchvision.utils.make_grid(images))</p>
<p>print(’ ‘.join(’%5s’ % classes[labels[j]] for j in range(4)))</p>
<p>class Net(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(Net,self).<strong>init</strong>()<br>
self.conv1 = nn.Conv2d(3, 6, 5)<br>
self.conv2 = nn.Conv2d(6, 16, 5)<br>
self.pool = nn.MaxPool2d(2, 2)<br>
self.fc1 = nn.Linear(16<em>5</em>5, 120)<br>
self.fc2 = nn.Linear(120, 84)<br>
self.fc2 = nn.Linear(84, 10)</p>
<pre><code>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
</code></pre>
<p>print(f"Using {device} device")</p>
<p>net = Net()<br>
criterion = nn.CrossEntropyLoss()<br>
optimizer = optim.SGD(net.parameters(),lr = 0.001, momentum = 0.9)</p>
<p>for epoch in range(2):<br>
running_loss = 0.0<br>
for i, data in enumerate(trainloader, 0):<br>
inputs, labels = data</p>
<pre><code>    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
</code></pre>
<p>print(“Finished Training”)</p>

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

jryhu2gh: <p>Hi UMAR, Thank you so much for your reply! I found where the problem might be <img src="https://discuss.pytorch.org/images/emoji/apple/rofl.png?v=12" title=":rofl:" class="emoji" alt=":rofl:" loading="lazy" width="20" height="20"> I used Spyder IDE. It gets stuck there. I tried again with ipython and this issue is gone… Thanks a lot!!</p>