githubEdit

Contrastive Explanations Method (CEM) applied to MNIST

The Contrastive Explanation Method (CEM) can generate black box model explanations in terms of pertinent positives (PP) and pertinent negatives (PN). For PP, it finds what should be minimally and sufficiently present (e.g. important pixels in an image) to justify its classification. PN on the other hand identify what should be minimally and necessarily absent from the explained instance in order to maintain the original prediction.

The original paper where the algorithm is based on can be found on arXivarrow-up-right.

Note

To enable support for the Contrastive Explanation Method, you may need to run

pip install alibi[tensorflow]
import tensorflow as tf
tf.get_logger().setLevel(40) # suppress deprecation messages
tf.compat.v1.disable_v2_behavior() # disable TF2 behaviour as alibi code still relies on TF1 constructs
import tensorflow.keras as keras
from tensorflow.keras import backend as K
from tensorflow.keras.layers import Conv2D, Dense, Dropout, Flatten, MaxPooling2D, Input, UpSampling2D
from tensorflow.keras.models import Model, load_model
from tensorflow.keras.utils import to_categorical

import matplotlib
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import os
from alibi.explainers import CEM

print('TF version: ', tf.__version__)
print('Eager execution enabled: ', tf.executing_eagerly()) # False

Load and prepare MNIST data

png

Prepare data: scale, reshape and categorize

Define and train CNN model

Evaluate the model on test set

Define and train auto-encoder

Compare original with decoded images

png

Generate contrastive explanation with pertinent negative

Explained instance:

png

Model prediction:

CEM parameters:

Generate pertinent negative:

Pertinent negative:

png

Generate pertinent positive

Pertinent positive:

png

Clean up:

Last updated

Was this helpful?