How do I get the miss predictions of a tensorflow model

Viewed 36

When using TensorFlow I automatically generate predictions when using the method.fit and training it for a specific number of epochs. I want to know how I can get the correctly predicted and the incorrectly predicted images from the model. as reference this is the tutorial I followed https://www.tensorflow.org/tutorials/images/classification

I tried looking if there was a way to access predictions by just calling the output of seeing if I could print a confusion matrix but I could not find such a library

1 Answers

If I understand correctly, you have trained the model and now you are testing the model performance by predicting on a test-set. Tensorflow (Keras) provides an API for calculating prediction metrics called tf.keras.metrics. You can choose however to use the scikit-learn library. After training your model: Given you have a test set x_test, y_test.

from sklearn import metrics
y_pred = model.predict(x_test)
print(metrics.classification_report(y_test, y_pred)

The metrics API has many aditional metrics to choose from.

Now you can filter the predictions for miss-classified samples.


import pandas as pd 
df = pd.DataFrame(x_test)
df['y_true'] = y_test
df['y_pred'] = y_pred
df[df['y_pred'] != df['y_true']]
df
Related