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.
Blog posts:
- Deconstructing BERT, Part 2: Visualizing the Inner Workings of Attention
- OpenAI GPT-2: Understanding Language Generation through Visualization
- Deconstructing BERT: Distilling 6 Patterns from 100 Million Parameters
Paper:
The attention-head view visualizes the attention patterns produced by one or more attention heads in a given transformer layer.
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)
The model view provides a birds-eye view of attention across all of the model’s layers and heads.
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)
The neuron view visualizes the individual neurons in the query and key vectors and shows how they are used to compute attention.
The neuron view supports the following three models:
BERT: [Notebook]
[Colab]
GPT-2
[Notebook]
[Colab]
RoBERTa
[Notebook]
- Transformers (version required depends on models used)
- PyTorch >=1.0.0
- Jupyter
- tqdm
- boto3
- IPython
- requests
- regex
- sentencepiece
(See requirements.txt)
git clone https://github.com/jessevig/bertviz.git
cd bertviz
jupyter notebook
NOTE: If you wish to run BertViz using Colab, please see the example Colab scripts above, as they differ slightly from the Jupyter notebook versions.
When referencing BertViz, please cite this paper.
@article{vig2019transformervis,
author = {Jesse Vig},
title = {A Multiscale Visualization of Attention in the Transformer Model},
journal = {arXiv preprint arXiv:1906.05714},
year = {2019},
url = {https://arxiv.org/abs/1906.05714}
}
This project is licensed under the Apache 2.0 License - see the LICENSE file for details
This project incorporates code from the following repos: