IndexError: index 3 is out of bounds for dimension 0 with size 3

i am stuck shows accuracy but this having error.

when i set batch size =40 this error remove and not shows correct accuracy but when i set batch size=10 or less then 40 this error comes or shows correct accuracy. kindly tell me what is happening? i should correct my batch size? or any thing else problem in code ?

Your labels have some problem.You can verify this by print(labels.size()).
The size of labels is 3.So legal index is 0, 1 and 2.
Index 3 out of the bounds.

it gives me that answer:


but i have 3 how to correct?

There is a problem with your labels array.If correct, your size should be 3.How do your labels array add values?

see this

 for data in testloader:
        images, labels = data
        images = Variable(images)
        labels = Variable(labels)
        outputs = model(images)
        _, predicted = torch.max(, 1)
        total += labels.size(0)
        correct +=(predicted == labels).sum().item()

Your testloader might only be loading one image and one label at once.
If you want to read multiple images and labels at once, you need to modify the testloader class. If you want to read one image and one label at once.And labels and images record all labels and images that have been read.You may try

images =, data[0])
labels =, data[1])

instead of “images, labels = data”