Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

A toolbox to innvestigate neural networks' predictions.

Project description

# [iNNvestigate neural networks!]( [![Tweet](](!&url=,artificialintelligence,machinelearning,deeplearning,datascience)

[![GitHub package version](](
[![Keras package version](](
[![License: BSD-2](](
[![Build Status](](
[![Dev-Build Status](](]]

![Different explanation methods on ImageNet.](

## Table of contents

* [Introduction](#introduction)
* [Installation](#installation)
* [Usage and Examples](#usage-and-examples)
* [More documentation](#more-documentation)
* [Contributing](#contributing)
* [Releases](#releases)

## Introduction

In the recent years neural networks furthered the state of the art in many domains like, e.g., object detection and speech recognition.
Despite the success neural networks are typically still treated as black boxes. Their internal workings are not fully understood and the basis for their predictions is unclear.
In the attempt to understand neural networks better several methods were proposed, e.g., Saliency, Deconvnet, GuidedBackprop, SmoothGrad, IntergratedGradients, LRP, PatternNet\&-Attribution.
Due to the lack of a reference implementations comparing them is a major effort.
This library addresses this by providing a common interface and out-of-the-box implementation for many analysis methods.
Our goal is to make analyzing neural networks' predictions easy!

### If you use this code please star the repository and cite the following paper:

**["iNNvestigate neural networks!"]([]( by Maximilian Alber, Sebastian Lapuschkin, Philipp Seegerer, Miriam Hägele, Kristof T. Schütt, Grégoire Montavon, Wojciech Samek, Klaus-Robert Müller, Sven Dähne, Pieter-Jan Kindermans**

## Installation

iNNvestigate can be installed with the following commands.
The library is based on Keras and therefore requires a supported [Keras-backend](
o(Currently only the Tensorflow backend is supported. We test with Python 3.6, Tensorflow 1.12 and Cuda 9.x.):

pip install innvestigate
# Installing Keras backend
pip install [tensorflow | theano | cntk]

To use the example scripts and notebooks one additionally needs to install the package matplotlib:

pip install matplotlib

The library's tests can be executed via:
git clone
cd innvestigate
python test

## Usage and Examples

The iNNvestigate library contains implementations for the following methods:

* *function:*
* **gradient:** The gradient of the output neuron with respect to the input.
* **smoothgrad:** [SmoothGrad]( averages the gradient over number of inputs with added noise.
* *signal:*
* **deconvnet:** [DeConvNet]( applies a ReLU in the gradient computation instead of the gradient of a ReLU.
* **guided:** [Guided BackProp]( applies a ReLU in the gradient computation additionally to the gradient of a ReLU.
* **** [PatternNet]( estimates the input signal of the output neuron.
* *attribution:*
* **input_t_gradient:** Input \* Gradient
* **deep_taylor[.bounded]:** [DeepTaylor]( computes for each neuron a rootpoint, that is close to the input, but which's output value is 0, and uses this difference to estimate the attribution of each neuron recursively.
* **pattern.attribution:** [PatternAttribution]( applies Deep Taylor by searching rootpoints along the singal direction of each neuron.
* **lrp.\*:** [LRP]( attributes recursively to each neuron's input relevance proportional to its contribution of the neuron output.
* **integrated_gradients:** [IntegratedGradients]( integrates the gradient along a path from the input to a reference.
* **deeplift.wrapper:** [DeepLIFT (wrapper around original code, slower)]( computes a backpropagation based on "finite" gradients.
* *miscellaneous:*
* **input:** Returns the input.
* **random:** Returns random Gaussian noise.

**The intention behind iNNvestigate is to make it easy to use analysis methods, but it is not to explain the underlying concepts and assumptions. Please, read the according publication(s) when using a certain method and when publishing please cite the according paper(s) (as well as the [iNNvestigate paper]( Thank you!**

All the available methods have in common that they try to analyze the output of a specific neuron with respect to input to the neural network.
Typically one analyses the neuron with the largest activation in the output layer.
For example, given a Keras model, one can create a 'gradient' analyzer:

import innvestigate

model = create_keras_model()

analyzer = innvestigate.create_analyzer("gradient", model)

and analyze the influence of the neural network's input on the output neuron by:

analysis = analyzer.analyze(inputs)

To analyze a neuron with the index i, one can use the following scheme:

analyzer = innvestigate.create_analyzer("gradient",
analysis = analyzer.analyze(inputs, i)

Let's look at an example ([code]( with VGG16 and this image:

![Input image.](

import innvestigate
import innvestigate.utils
import keras.applications.vgg16 as vgg16

# Get model
model, preprocess = vgg16.VGG16(), vgg16.preprocess_input
# Strip softmax layer
model = innvestigate.utils.model_wo_softmax(model)

# Create analyzer
analyzer = innvestigate.create_analyzer("deep_taylor", model)

# Add batch axis and preprocess
x = preprocess(image[None])
# Apply analyzer w.r.t. maximum activated output-neuron
a = analyzer.analyze(x)

# Aggregate along color channels and normalize to [-1, 1]
a = a.sum(axis=np.argmax(np.asarray(a.shape) == 3))
a /= np.max(np.abs(a))
# Plot
plt.imshow(a[0], cmap="seismic", clim=(-1, 1))

![Analysis image.](

#### Trainable methods

Some methods like PatternNet and PatternAttribution are data-specific and need to be trained.
Given a data set with train and test data, this can be done in the following way:

import innvestigate

analyzer = innvestigate.create_analyzer("", model)
analysis = analyzer.analyze(X_test)

### Tutorials

In the directory [examples]( one can find different examples as Python scripts and as Jupyter notebooks:

* **[Introduction to iNNvestigate](** shows how to use **iNNvestigate**.
* **[Comparing methods on MNIST](** shows how to train and compare analyzers on MNIST.
* **[Comparing output neurons on MNIST](** shows how to analyze the prediction of different classes on MNIST.
* **[Comparing methods on ImageNet](** shows how to compare analyzers on ImageNet.
* **[Comparing networks on ImageNet](** shows how to compare analyzes for different networks on ImageNet.
* **[Sentiment Analysis](**.
* **[Development with iNNvestigate](** shows how to develop with **iNNvestigate**.
* **[Perturbation Analysis](**.

**To use the ImageNet examples please download the example images first ([script](**

## More documentation

... can be found here: [](

## Contributing

If you would like to contribute or add your analysis method please get in touch with us!

## Releases

[Can be found here.](

Project details

Release history Release notifications

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for innvestigate, version 1.0.8
Filename, size File type Python version Upload date Hashes
Filename, size innvestigate-1.0.8-py3-none-any.whl (97.4 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size innvestigate-1.0.8.tar.gz (73.3 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page