How to store wrong predictions during evaluation on the CNN
During evaluation, I want to store unique ids that are wrongly predicted to do some more processing.
It is a multiclass prediction problem
Here is the code during the evaluation:
 outputs = model(imgs)
 loss = criterion(outputs, targets)  # Prediction error

 val_loss += loss.item()
 predicted = torch.argmax(outputs, dim=1)
 t_predicted +=predicted.cpu().tolist()

 total += targets.size(0)
 good_answers = (predicted == targets)
 correct += good_answers.sum().item()


Knowing that ids is a list of the ids of the images,  When I try to get the ids that are wrong:
wrong_ids += ids[~(good_answers.to('cpu'))]

I get this error:
add(): argument 'other' (position 1) must be Tensor


The question contained a tensorflow tag, so I was preparing an answer. After completing my write up, I've found that this tag is removed. However, I believe my answer can give insight into this general question of whether they're using tf or pytorch.

Data
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()

# train set / data 
x_train = x_train.astype('float32') / 255

# validation set / data 
x_test = x_test.astype('float32') / 255

# train set / target 
y_train = tf.keras.utils.to_categorical(y_train, num_classes=10)
# validation set / target 
y_test = tf.keras.utils.to_categorical(y_test, num_classes=10)

print(x_train.shape, y_train.shape)
print(x_test.shape, y_test.shape)  

Train
import tensorflow as tf 

# declare input shape 
input = tf.keras.Input(shape=(32,32,3))
# Block 1
x = tf.keras.layers.Conv2D(32, 3, strides=2, activation=&quot;relu&quot;)(input)
x = tf.keras.layers.MaxPooling2D(3)(x)
# Now that we apply global max pooling.
gap = tf.keras.layers.GlobalMaxPooling2D()(x)
# Finally, we add a classification layer.
output = tf.keras.layers.Dense(10, activation='softmax')(gap)
# bind all
func_model = tf.keras.Model(input, output)

print('\nFunctional API')
func_model.compile(
          metrics=['accuracy'],
          loss      = 'categorical_crossentropy',
          optimizer = tf.keras.optimizers.Adam()
          )

func_model.fit(x_train, y_train)

Error Prediction
# Predict the values from the validation dataset
y_pred = func_model.predict(x_test)
# Convert predictions classes to one hot vectors 
y_pred_classes = np.argmax(y_pred, axis = 1) 
y_test = np.argmax(y_test, axis=1)

# Errors are difference between predicted labels and true labels
errors = (y_pred_classes - y_test != 0)

y_pred_classes_errors = y_pred_classes[errors]
y_pred_errors = y_pred[errors]
y_true_errors = y_test[errors]
x_test_errors = x_test[errors]

# Probabilities of the wrong predicted numbers
y_pred_errors_prob = np.max(y_pred_errors, axis = 1)

# Predicted probabilities of the true values in the error set
true_prob_errors = np.diagonal(np.take(y_pred_errors, y_true_errors, axis=1))

# Difference between the probability of the predicted label and the true label
delta_pred_true_errors = y_pred_errors_prob - true_prob_errors

# Sorted list of the delta prob errors
sorted_dela_errors = np.argsort(delta_pred_true_errors)

# Top 6 errors 
most_important_errors = sorted_dela_errors[-6:]

Display
import matplotlib.pyplot as plt

def display_errors(errors_index,img_errors,pred_errors, obs_errors):
    &quot;&quot;&quot; This function shows 6 images with their predicted and real labels&quot;&quot;&quot;
    n = 0
    nrows = 2
    ncols = 3
    fig, ax = plt.subplots(nrows,ncols,sharex=True,sharey=True)
    for row in range(nrows):
        for col in range(ncols):
            error = errors_index[n]
            ax[row,col].imshow((img_errors[error]).reshape((32,32,3)))
            ax[row,col].set_title(&quot;Predicted label :{}\nTrue label :{}&quot;.format(pred_errors[error],obs_errors[error]))
            n += 1

# Show the top 6 errors
display_errors(most_important_errors, x_test_errors, y_pred_classes_errors, y_true_errors)


