All Projects → hila-chefer → Transformer Explainability

hila-chefer / Transformer Explainability

Licence: mit
[CVPR 2021] Official PyTorch implementation for Transformer Interpretability Beyond Attention Visualization, a novel method to visualize classifications by Transformer based networks.

Projects that are alternatives of or similar to Transformer Explainability

Mvpose
Code for "Fast and Robust Multi-Person 3D Pose Estimation from Multiple Views" in CVPR'19
Stars: ✭ 326 (+5.5%)
Mutual labels:  jupyter-notebook
Artificial Intelligence
Awesome Artificial Intelligence Projects
Stars: ✭ 330 (+6.8%)
Mutual labels:  jupyter-notebook
Amazon Sagemaker Workshop
Amazon SageMaker workshops: Introduction, TensorFlow in SageMaker, and more
Stars: ✭ 332 (+7.44%)
Mutual labels:  jupyter-notebook
Pytorch
Deep Learning Zero to All - Pytorch
Stars: ✭ 328 (+6.15%)
Mutual labels:  jupyter-notebook
Learning
The data is the future of oil, digging the potential value of the data is very meaningful. This library records my road of machine learning study.
Stars: ✭ 330 (+6.8%)
Mutual labels:  jupyter-notebook
Deepreinforcementlearninginaction
Code from the Deep Reinforcement Learning in Action book from Manning, Inc
Stars: ✭ 329 (+6.47%)
Mutual labels:  jupyter-notebook
Baal
Using approximate bayesian posteriors in deep nets for active learning
Stars: ✭ 328 (+6.15%)
Mutual labels:  jupyter-notebook
Supervisely
AI for everyone! 🎉 Neural networks, tools and a library we use in Supervisely
Stars: ✭ 332 (+7.44%)
Mutual labels:  jupyter-notebook
Openhownet
Core Data of HowNet and OpenHowNet Python API
Stars: ✭ 329 (+6.47%)
Mutual labels:  jupyter-notebook
Music Synthesizer For Android
Automatically exported from code.google.com/p/music-synthesizer-for-android
Stars: ✭ 332 (+7.44%)
Mutual labels:  jupyter-notebook
Styletransfertrilogy
风格迁移三部曲
Stars: ✭ 330 (+6.8%)
Mutual labels:  jupyter-notebook
Pytorch notebooks
tutorial notebooks
Stars: ✭ 330 (+6.8%)
Mutual labels:  jupyter-notebook
Quantumkatas
Tutorials and programming exercises for learning Q# and quantum computing
Stars: ✭ 3,713 (+1101.62%)
Mutual labels:  jupyter-notebook
Graphic Detail Data
Data and code behind the Economist's Graphic Detail section.
Stars: ✭ 328 (+6.15%)
Mutual labels:  jupyter-notebook
Gtc2017 Numba
Numba tutorial for GTC 2017 conference
Stars: ✭ 334 (+8.09%)
Mutual labels:  jupyter-notebook
Kale
Kubeflow’s superfood for Data Scientists
Stars: ✭ 324 (+4.85%)
Mutual labels:  jupyter-notebook
Aws Security Workshops
A collection of the latest AWS Security workshops
Stars: ✭ 332 (+7.44%)
Mutual labels:  jupyter-notebook
Tensorly Notebooks
Tensor methods in Python with TensorLy
Stars: ✭ 332 (+7.44%)
Mutual labels:  jupyter-notebook
Ccf2016 sougou
ccf2016 sougou final winner solution
Stars: ✭ 333 (+7.77%)
Mutual labels:  jupyter-notebook
Tts
🤖 💬 Deep learning for Text to Speech (Discussion forum: https://discourse.mozilla.org/c/tts)
Stars: ✭ 5,427 (+1656.31%)
Mutual labels:  jupyter-notebook

PyTorch Implementation of Transformer Interpretability Beyond Attention Visualization [CVPR 2021]

Open In Colab

Updates

March 15 2021: A Colab notebook for BERT for sentiment analysis added!

Feb 28 2021: Our paper was accepted to CVPR 2021!

Feb 17 2021: A Colab notebook with all examples added!

Jan 5 2021: A Jupyter notebook for DeiT added!

Introduction

Official implementation of Transformer Interpretability Beyond Attention Visualization.

We introduce a novel method which allows to visualize classifications made by a Transformer based model for both vision and NLP tasks. Our method also allows to visualize explanations per class.

Method consists of 3 phases:
  1. Calculating relevance for each attention matrix using our novel formulation of LRP.

  2. Backpropagation of gradients for each attention matrix w.r.t. the visualized class. Gradients are used to average attention heads.

  3. Layer aggregation with rollout.

Please notice our Jupyter notebook where you can run the two class specific examples from the paper.

alt text

To add another input image, simply add the image to the samples folder, and use the generate_visualization function for your selected class of interest (using the class_index={class_idx}), not specifying the index will visualize the top class.

Credits

ViT implementation is based on:

BERT implementation is taken from the huggingface Transformers library: https://huggingface.co/transformers/

ERASER benchmark code adapted from the ERASER GitHub implementation: https://github.com/jayded/eraserbenchmark

Text visualizations in supplementary were created using TAHV heatmap generator for text: https://github.com/jiesutd/Text-Attention-Heatmap-Visualization

Reproducing results on ViT

Section A. Segmentation Results

Example:

CUDA_VISIBLE_DEVICES=0 PYTHONPATH=./:$PYTHONPATH python3 baselines/ViT/imagenet_seg_eval.py --method transformer_attribution --imagenet-seg-path /path/to/gtsegs_ijcv.mat

In the exmaple above we run a segmentation test with our method. Notice you can choose which method you wish to run using the --method argument. You must provide a path to imagenet segmentation data in --imagenet-seg-path.

Section B. Perturbation Results

Example:

CUDA_VISIBLE_DEVICES=0 PYTHONPATH=./:$PYTHONPATH python3 baselines/ViT/generate_visualizations.py --method transformer_attribution --imagenet-validation-path /path/to/imagenet_validation_directory

Notice that you can choose to visualize by target or top class by using the --vis-cls argument.

Now to run the perturbation test run the following command:

CUDA_VISIBLE_DEVICES=0 PYTHONPATH=./:$PYTHONPATH python3 baselines/ViT/pertubation_eval_from_hdf5.py --method transformer_attribution

Notice that you can use the --neg argument to run either positive or negative perturbation.

Reproducing results on BERT

  1. Download the pretrained weights:
  1. Download the dataset pkl file:
  1. Download the dataset:
  1. Now you can run the model.

Example:

CUDA_VISIBLE_DEVICES=0 PYTHONPATH=./:$PYTHONPATH python3 BERT_rationale_benchmark/models/pipeline/bert_pipeline.py --data_dir data/movies/ --output_dir bert_models/movies/ --model_params BERT_params/movies_bert.json

To control which algorithm to use for explanations change the method variable in BERT_rationale_benchmark/models/pipeline/bert_pipeline.py (Defaults to 'transformer_attribution' which is our method). Running this command will create a directory for the method in bert_models/movies/<method_name>.

In order to run f1 test with k, run the following command:

PYTHONPATH=./:$PYTHONPATH python3 BERT_rationale_benchmark/metrics.py --data_dir data/movies/ --split test --results bert_models/movies/<method_name>/identifier_results_k.json

Also, in the method directory there will be created .tex files containing the explanations extracted for each example. This corresponds to our visualizations in the supplementary.

Citing our paper

If you find this work useful please consider citing it:

@article{chefer2020transformerInterpretability,
  title={Transformer Interpretability Beyond Attention Visualization},
  author={Chefer, Hila and Gur, Shir and Wolf, Lior},
  journal={arXiv preprint arXiv:2012.09838},
  year={2020}
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].