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

i am stuck here.it 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:

print(labels.size())

torch.Size([1])
but i have 3 labels.now 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(outputs.data, 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 = torch.cat(images, data[0])
labels = torch.cat(labels, data[1])

instead of “images, labels = data”