SkillAgentSearch skills...

DeepExplain

A unified framework of perturbation and gradient-based attribution methods for Deep Neural Networks interpretability. DeepExplain also includes support for Shapley Values sampling. (ICLR 2018)

Install / Use

/learn @marcoancona/DeepExplain
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

DeepExplain: attribution methods for Deep Learning Build Status

DeepExplain provides a unified framework for state-of-the-art gradient and perturbation-based attribution methods. It can be used by researchers and practitioners for better undertanding the recommended existing models, as well for benchmarking other attribution methods.

It supports Tensorflow as well as Keras with Tensorflow backend. Only Tensorflow V1 is supported. For V2, there is an open pull-request, that works if eager execution is disabled.

Implements the following methods:

Gradient-based attribution methods

Methods marked with (*) are implemented as modified chain-rule, as better explained in Towards better understanding of gradient-based attribution methods for Deep Neural Networks, Ancona et al, ICLR 2018. As such, the result might be slightly different from the original implementation.

Pertubration-based attribution methods

What are attributions?

Consider a network and a specific input to this network (eg. an image, if the network is trained for image classification). The input is multi-dimensional, made of several features. In the case of images, each pixel can be considered a feature. The goal of an attribution method is to determine a real value R(x_i) for each input feature, with respect to a target neuron of interest (for example, the activation of the neuron corresponsing to the correct class).

When the attributions of all input features are arranged together to have the same shape of the input sample we talk about attribution maps (as in the picture below), where red and blue colors indicate respectively features that contribute positively to the activation of the target output and features having a suppressing effect on it. Attribution methods comparison on InceptionV3

This can help to better understand the network behavior, which features mostly contribute to the output and possible reasons for missclassification.

DeepExplain Quickstart

Installation

pip install -e git+https://github.com/marcoancona/DeepExplain.git#egg=deepexplain

Notice that DeepExplain assumes you already have installed Tensorflow > 1.0 and (optionally) Keras > 2.0.

Usage

Working examples for Tensorflow and Keras can be found in the example folder of the repository. DeepExplain consists of a single method: explain(method_name, target_tensor, input_tensor, samples, ...args).

Parameter name | Short name | Type | Description ---------------|------|------|------------ method_name | | string, required | Name of the method to run (see Which method to use?). target_tensor | T | Tensor, required | Tensorflow Tensor object representing the output of the model for which attributions are seeked (see Which tensor to target?). input_tensor | X | Tensor, required | Symbolic input to the network. input_data | xs | numpy array, required | Batch of input samples to be fed to X and for which attributions are seeked. Notice that the first dimension must always be the batch size. target_weights | ys | numpy array, optional | Batch of weights to be applied to T if this has more than one output. Usually necessary on classification problems where there are multiple output units and we need to target a specific one to generate explanations for. In this case, ys can be provided with the one-hot encoding of the desired unit. batch_size | |int, optional| By default, DeepExplain will try to evaluate the model using all data in xs at the same time. If xs contains many samples, it might be necessary to split the processing in batches. In this case, providing a batch_size greater than zero will automatically split the evaluation into chunks of the given size. ...args | | various, optional | Method-specific parameters (see below).

The method explain must be called within a DeepExplain context:

# Pseudo-code
from deepexplain.tensorflow import DeepExplain

# Option 1. Create and train your model within a DeepExplain context

with DeepExplain(session=...) as de:  # < enter DeepExplain context
    model = init_model()  # < construct the model
    model.fit()           # < train the model

    attributions = de.explain(...)  # < compute attributions

# Option 2. First create and train your model, then apply DeepExplain.
# IMPORTANT: in order to work correctly, the graph to analyze
# must always be (re)constructed within the context!

model = init_model()  # < construct the model
model.fit()           # < train the model

with DeepExplain(session=...) as de:  # < enter DeepExplain context
    new_model = init_model()  # < assumes init_model() returns a *new* model with the weights of `model`
    attributions = de.explain(...)  # < compute attributions

When initializing the context, make sure to pass the session parameter:

# With Tensorflow
import tensorflow as tf
# ...build model
sess = tf.Session()
# ... use session to train your model if necessary
with DeepExplain(session=sess) as de:
    ...

# With Keras
import keras
from keras import backend as K

model = Sequential()  # functional API is also supported
# ... build model and train

with DeepExplain(session=K.get_session()) as de:
    ...

See concrete examples here.

Which method to use?

DeepExplain supports several methods. The main partition is between gradient-based methods and perturbation-based methods. The former are faster, given that they estimate attributions with a few forward and backward iterations through the network. The latter perturb the input and measure the change in output with respect to the original input. This requires to sequentially test each feature (or group of features) and therefore takes more time, but tends to produce smoother results.

Cooperative game theory suggests Shapley Values as a unique way to distribute attribution to features such that some important theoretical properties are satisfied. Unfortunately, computing Shapley Values exactly is prohibitively expensive, therefore DeepExplain provides a sampling-based approximation. By changing the samples parameters, one can adjust the trade-off between performance and error. Notice that this method will still be significantly slower than other methods in this library.

Some methods allow tunable parameters. See the table below.

Method | method_name | Optional parameters | Notes ---------------|:------|:------------|----- Saliency | saliency | | [Gradient] Only positive attributions. Gradient * Input | grad*input | | [Gradient] Fast. May be affected by noisy gradients and saturation of the nonlinerities. Integrated Gradients | intgrad |steps, baseline | [Gradient] Similar to Gradient * Input, but performs steps iterations (default: 100) though the network, varying the input from baseline (default: zero) to the actual provided sample. When provided, baseline must be a numpy array with the size of the input (but no batch dimension since the same baseline will be used for all inputs in the batch). epsilon-LRP | elrp | epsilon | [Gradient]Computes Layer-wise Relevance Propagation. Only recommanded with ReLU or Tanh nonlinearities. Value for epsilon must be greater than zero (default: .0001). DeepLIFT (Rescale) | deeplift | baseline | [Gradient] In most cases a faster approximation of Integrated Gradients. Do not apply to networks with multiplicative units (ie. LSTM or GRU). When provided, baseline must be a numpy array with the size of the input, without the batch dimension (default: zero). Occlusion | occlusion | window_shape, step | [Perturbation] Computes rolling window view of the input array and replace each window with zero values, measuring the effect of the perturbation on the target output. The optional parameters window_shape and step behave like in skimage. By default, each feature is tested independently (window_shape=1 and step=1), however this might be extremely slow for large inputs (such as ImageNet images). When the input presents some local coherence (eg. images), you might prefer larger values for window_shape. In this case the attributions of the features in each window will be summed up. Notice that the result might vary significantly for different window sizes. Shapley Value sampling | shapley_sampling | samples, sampling_dims | [Perturbation] Computes approximate Shapley Values by sampling samples times each input feature. Notice that this method can be significantly slower than all the others as it runs the network samples*n times, where n is the number of input features in your input. The parameter sampling_dims (a list of integers) can be used to select which dimensions should be sampled. For example, if the inputs are RGB images, sampling_dims=[1,2] would sample pixels considering the three color channels atomic. Ins

View on GitHub
GitHub Stars760
CategoryCustomer
Updated9d ago
Forks135

Languages

Python

Security Score

95/100

Audited on Mar 23, 2026

No findings