/bertviz

Tool for visualizing attention in the Transformer model (BERT, GPT-2, Albert, XLNet, RoBERTa, CTRL, etc.)

Primary LanguageJupyter NotebookApache License 2.0Apache-2.0

BertViz

BertViz is a tool for visualizing attention in the Transformer model, supporting all models from the transformers library (BERT, GPT-2, XLNet, RoBERTa, XLM, CTRL, etc.). It extends the Tensor2Tensor visualization tool by Llion Jones and the transformers library from HuggingFace.

Resources

🕹️ Colab tutorial

✍️ Blog post

📖 Paper

Overview

Head view

The head view visualizes the attention patterns produced by one or more attention heads in a given transformer layer. It is based on the excellent Tensor2Tensor visualization tool by Llion Jones.

Try out this interactive Colab Notebook with the head view pre-loaded.

Head view

The attention view supports all models from the Transformers library, including:
BERT: [Notebook] [Colab]
GPT-2: [Notebook] [Colab]
XLNet: [Notebook]
RoBERTa: [Notebook]
XLM: [Notebook]
ALBERT: [Notebook]
DistilBERT: [Notebook]
(and others)

Model view

The model view provides a birds-eye view of attention across all of the model’s layers and heads.

Try out this interactive Colab Notebook with the model view pre-loaded.

Model view

The model view supports all models from the Transformers library, including:
BERT: [Notebook] [Colab]
GPT2: [Notebook] [Colab]
XLNet: [Notebook]
RoBERTa: [Notebook]
XLM: [Notebook]
ALBERT: [Notebook]
DistilBERT: [Notebook]
(and others)

Neuron view

The neuron view visualizes the individual neurons in the query and key vectors and shows how they are used to compute attention.

Try out this interactive Colab Notebook with the neuron view pre-loaded (requires Chrome).

Neuron view

The neuron view supports the following three models:
BERT: [Notebook] [Colab]
GPT-2 [Notebook] [Colab]
RoBERTa [Notebook]

Execution

Running locally

git clone https://github.com/jessevig/bertviz.git
cd bertviz
<Install from requirements.txt as needed>
jupyter notebook

Click on any of the sample notebooks. You can view a notebook's cached output visualizations by selecting File > Trust Notebook (and confirming in dialog) or you can run the notebook yourself. Note that the sample notebooks do not cover all Huggingface models, but the code should be similar for those not included.

Running from Colab

Click on any of the Colab links above, and scroll to the bottom of the page. It should be pre-loaded with the visualization.

If you write your own code for executing BertViz in Colab, note that some of the steps are different from those in the Jupyter notebooks (see Colab examples above).

Limitations

Tool

  • The visualizations works best with shorter sentences and may run slowly if the input text is very long, especially for the Model View.
  • When running on Colab, some of the visualizations will fail (runtime disconnection) when the input text is long.
  • If you have issues running the tool in Jupyter Lab, try running with a plain Jupyter notebook.
  • The Neuron View only supports BERT, GPT-2, and RoBERTa models. This view needs access to the query and key vectors, which requires modifying the model code (see transformers_neuron_view directory), which has only been done for these three models. Also, only one Neuron View may be included per notebook.

Attention as "explanation"

Visualizing attention weights illuminates a particular mechanism within the model architecture but does not necessarily provide a direct explanation for model predictions. See [1], [2], [3].

Authors

Jesse Vig

Citation

When referencing BertViz, please cite this paper.

@inproceedings{vig-2019-multiscale,
    title = "A Multiscale Visualization of Attention in the Transformer Model",
    author = "Vig, Jesse",
    booktitle = "Proceedings of the 57th Annual Meeting of the Association for Computational Linguistics: System Demonstrations",
    month = jul,
    year = "2019",
    address = "Florence, Italy",
    publisher = "Association for Computational Linguistics",
    url = "https://www.aclweb.org/anthology/P19-3007",
    doi = "10.18653/v1/P19-3007",
    pages = "37--42",
}

License

This project is licensed under the Apache 2.0 License - see the LICENSE file for details

Acknowledgments

We are grateful to the authors of the following projects, which are incorporated into this repo: