In running quickstart example code, got "IndexError: list index out of range"

oat: <p>I got the following error after loading the saved model for prediction, using the exactly the same example code shown in <a href="https://pytorch.org/tutorials/beginner/basics/quickstart_tutorial.html" rel="noopener nofollow ugc">QuickStart</a>.</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "/Users/minioat/Downloads/git_repo/oat_pytorch_tutorial/quickstart.py", line 163, 
in &lt;module&gt;
    predicted, actual = classes[pred[0].argmax(0)], classes[y]
IndexError: list index out of range
</code></pre>
<p>May I ask what I missed here? The code is shown in this <a href="https://gist.github.com/hopezh/1bf785a2e257e7f22256586b34218d64" rel="noopener nofollow ugc">gist</a>.</p>
<p>Complete running and error messages:</p>
<pre data-code-wrap="Shell"><code class="lang-plaintext">&gt; python quickstart.py

PyTorch version: 2.0.1
Shape of X [N, C, H, W]: torch.Size([64, 1, 28, 28])
Shape of y: torch.Size([64]) torch.int64
Using mps device
Model:
NeuralNetwork(
  (flatten): Flatten(start_dim=1, end_dim=-1)
  (linear_relu_stack): Sequential(
    (0): Linear(in_features=784, out_features=512, bias=True)
    (1): ReLU()
    (2): Linear(in_features=512, out_features=512, bias=True)
    (3): ReLU()
    (4): Linear(in_features=512, out_features=10, bias=True)
  )
)
Epoch 1
-------------------------------
loss: 2.305242 [   64/60000]
loss: 2.289008 [ 6464/60000]
loss: 2.267145 [12864/60000]
loss: 2.264364 [19264/60000]
loss: 2.235085 [25664/60000]
loss: 2.225608 [32064/60000]
loss: 2.227139 [38464/60000]
loss: 2.197326 [44864/60000]
loss: 2.195373 [51264/60000]
loss: 2.160924 [57664/60000]
Test error: 
 Accuracy: 0.0%, Avg loss: 2.150579

Epoch 2
-------------------------------
loss: 2.158707 [   64/60000]
loss: 2.150648 [ 6464/60000]
loss: 2.091588 [12864/60000]
loss: 2.111565 [19264/60000]
loss: 2.047482 [25664/60000]
loss: 2.005746 [32064/60000]
loss: 2.025058 [38464/60000]
loss: 1.950950 [44864/60000]
loss: 1.954253 [51264/60000]
loss: 1.881486 [57664/60000]
Test error: 
 Accuracy: 0.0%, Avg loss: 1.876239

Epoch 3
-------------------------------
loss: 1.903327 [   64/60000]
loss: 1.879490 [ 6464/60000]
loss: 1.756992 [12864/60000]
loss: 1.798573 [19264/60000]
loss: 1.678978 [25664/60000]
loss: 1.649856 [32064/60000]
loss: 1.656017 [38464/60000]
loss: 1.567788 [44864/60000]
loss: 1.587256 [51264/60000]
loss: 1.482197 [57664/60000]
Test error: 
 Accuracy: 0.0%, Avg loss: 1.499892

Epoch 4
-------------------------------
loss: 1.561598 [   64/60000]
loss: 1.538423 [ 6464/60000]
loss: 1.381259 [12864/60000]
loss: 1.449662 [19264/60000]
loss: 1.327623 [25664/60000]
loss: 1.339831 [32064/60000]
loss: 1.338461 [38464/60000]
loss: 1.277467 [44864/60000]
loss: 1.306573 [51264/60000]
loss: 1.209029 [57664/60000]
Test error: 
 Accuracy: 0.0%, Avg loss: 1.232618

Epoch 5
-------------------------------
loss: 1.305687 [   64/60000]
loss: 1.300576 [ 6464/60000]
loss: 1.125573 [12864/60000]
loss: 1.228220 [19264/60000]
loss: 1.100806 [25664/60000]
loss: 1.140190 [32064/60000]
loss: 1.148437 [38464/60000]
loss: 1.099727 [44864/60000]
loss: 1.133196 [51264/60000]
loss: 1.053014 [57664/60000]
Test error: 
 Accuracy: 0.0%, Avg loss: 1.069904

Done!
Saved PyTorch Model State to model.pth
Traceback (most recent call last):
  File "/Users/minioat/Downloads/git_repo/oat_pytorch_tutorial/quickstart.py", line 163, 
in &lt;module&gt;
    predicted, actual = classes[pred[0].argmax(0)], classes[y]
IndexError: list index out of range
</code></pre>

ptrblck: <p>Check the values and shapes of all used tensors in:</p>
<pre><code class="lang-python">classes[pred[0].argmax(0)], classes[y]
</code></pre>
<p>to narrow down which operation fails.<br>
Based on the error message I would assume <code>classes[y]</code> fails.<br>
The code creates <code>y</code> via <code>y = test_data[0][1]</code> and classes as a <code>list</code> of all 10 classes, so it’s unclear why this operation would fail.</p>