Pytorch:IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)

It is discuss on many platforms but I am still not getting full understanding of this code.
Below are the inputs.

Number of classes = 5 (0,1,2,3,4)
Input Size = torch.Size([8, 3, 512, 512]) ## 8 is a batch-size
**Actual Labels size = torch.Size([8]) **
predicted labels = torch.Size([8, 5])

epoch = 1
from torch.autograd import Variable
criteria = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(params = model.parameters() , lr = .001)
for n in range(epoch):
for index,(images,label) in enumerate(train_loader):
images = Variable(
labels = Variable(
y_pred = model(images)
loss = criteria(labels,y_pred)

Please let me know if anyone need more Info.

Try to swap the labels and y_pred tensors when passing them to your criterion.
If that doesn’t help, could you post the complete stack trace?

Also, Variables are deprecated since PyTorch 0.4, so you should use tensors now. :wink:

1 Like

Wow.!! Thanks Bro it’s works.