Skip to content

Instantly share code, notes, and snippets.

@dipanjanS
Created August 15, 2019 08:05
Show Gist options
  • Save dipanjanS/cb22c436845b17c34317e4215c837a0d to your computer and use it in GitHub Desktop.
Save dipanjanS/cb22c436845b17c34317e4215c837a0d to your computer and use it in GitHub Desktop.
# utility function to pass inputs to specific model layers
def map2layer(x, layer):
feed_dict = dict(zip([model.layers[0].input], [preprocess_input(x.copy())]))
return K.get_session().run(model.layers[layer].input, feed_dict)
# focus on the 7th layer of CNN model
print(model.layers[7].input)
Out [46]: <tf.Tensor 'block2_pool_2/MaxPool:0' shape=(?, 56, 56, 128) dtype=float32>
# make model predictions
e = shap.GradientExplainer((model.layers[7].input, model.layers[-1].output),
map2layer(preprocess_input(X.copy()), 7))
shap_values, indexes = e.shap_values(map2layer(to_predict, 7), ranked_outputs=2)
index_names = np.vectorize(lambda x: class_names[str(x)][1])(indexes)
print(index_names)
Out [47]: array([['chain', 'chain_mail'],
['great_grey_owl', 'prairie_chicken'],
['desktop_computer', 'screen'],
['Egyptian_cat', 'tabby']], dtype='<U16')
# visualize model decisions
visualize_model_decisions(shap_values=shap_values, x=to_predict,
labels=index_names, figsize=(20, 40))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment