Pytorch CNN Model: Dimension out of range Error
I keep getting this error &quot;IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)&quot; when I use cross entropy loss my CNN model.
My layers look like this:
    x= self.conv1(x)
    x= F.relu(x)
    x= self.pool1(x)
    x= F.relu(x)
    x= self.conv3(x)
    x= F.relu(x)
    x= self.conv4(x)
    x= F.relu(x)
    
    x= self.pool2(x)

    x = torch.flatten(x, 1)
    
    x= self.lin1(x)
    x= F.relu(x)
    x= self.lin2(x)
    x= F.relu(x)
    x= self.lin3(x)
    x= F.relu(x)
    x= self.sftmax(x)
    

My code for training is pretty much the one on the [Pytorch website].
where x_train is of shape (300, 1000) and y_train has 300 labels. I want to feed in one array in x_train at a time, then optimize based on its corresponding label in y_train.
When I am training, the outputs= net(Input) works without error. For the first epoch, I get an output like tensor([[[0.1072, 0.2725, 0.2963, 0.2395, 0.3821]]], dtype=torch.float64, grad_fn=&lt;UnsqueezeBackward0&gt;). Then I pass in the Label, which is tensor([5.], dtype=torch.float64).
After this first epoch though, I get an error &quot;IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)&quot;. Is this related to the shape of the input and outputs? I'm not sure what is wrong.
Any advice would be appreciated!

I believe the issue might be due to your usage of the squeeze(0) right before you pass in outputs to the loss function.
Note in the docs below it says that the output tensor and input sensor will share the same memory space.
https://pytorch.org/docs/stable/generated/torch.squeeze.html
Edit
After testing with the collab attached to the example. It seems that is not the case. Although I noticed that your labels tensor is of size one. Whereas the example has label arrays that match the size of the output space.
I believe you might be extracting a single value out of the training set as opposed to the expected tensor size? Essentially the output should match the training set output in size so it can calculate the loss between the two.
