how to identify wrong classification with batches in pytorch
I have a script like this where batches of images were used 

correct = 0
total = 0
incorrect_classification=[]
for (i, [images, labels]) in enumerate(test_loader):
  images = Variable(images.view(-1, n_pixel*n_pixel))
  outputs = net(images)
  _, predicted = torch.min(outputs.data, 1)
  total += labels.size(0)                    
  correct += (predicted == labels).sum() 
print('Accuracy: %d %%' %
      (100 * correct / total))


With a batch size of 10, each enumerate returns 10 x image size tensor. How can I save all the wrong classifications into the array incorrect_classification or wrong img and their probability into an dictionary so I use can plt.imshow to inspect them later? 

If batch size is 1 I could use this:

if (predicted==labels).item()==0:
    incorrect_examples.append(images.numpy())


But with a batch size specified (like 100 images per batch) how should I save the wrong classifications?

Thanks in advance for any answers.

As already said in the comment of @zihaozhihao, images[predicted==labels] should do the work.

In other words, you will get a mask of indexes and then access the images you want with this mask:

correct = 0
total = 0
incorrect_examples=[]
for (i, [images, labels]) in enumerate(test_loader):
    images = Variable(images.view(-1, n_pixel*n_pixel))
    outputs = net(images)
    _, predicted = torch.min(outputs.data, 1)
    total += labels.size(0)                    
    correct += (predicted == labels).sum() 
    print('Accuracy: %d %%' % (100 * correct / total))

    # if (predicted==labels).item()==0:
    #     incorrect_examples.append(images.numpy())

    idxs_mask = (predicted == labels).view(-1)
    incorrect_examples.append(images[idxs_mask].numpy()) 


The view(-1) will flatten the mask which will be used to mask the batch channel of the images tensor. 

At the end of the loop (out of it), the itens in the list incorrect_examples will have shape [batch_size, n_pixel, n_pixel], and for convenience, you can group all of them in one tensor by concatenating them:

incorrect_images = torch.cat(incorrect_examples)
# incorrect_images.size() -&gt; (n_incorrect_images, n_pixel, n_pixel)

