A Multiscale Visualization of Attention in the Transformer Model
Paper · 2019 · Jesse Vig
Summary
Transformer models such as BERT and GPT-2 compute attention in many layers and heads at once, which makes their attention hard to inspect. BERT-Large alone produces 384 separate attention structures for each input. Vig extends an earlier multi-head visualizer by Jones (2017) and releases bertviz, an open-source tool that shows attention at three levels of detail:
- Attention-head view: draws weighted lines between tokens for chosen heads in one layer, with optional token and sentence-pair filters.
- Model view: lays out every layer and head as a grid of small thumbnails.
- Neuron view: opens up the query and key vectors to show how individual dimensions build the dot product and the final softmax score.
The tool was adapted from the original encoder-decoder setting to encoder-only BERT and decoder-only GPT-2. Three case studies show what it can do: exposing gender bias in a GPT-2 head that behaves like coreference, finding cross-sentence heads useful for paraphrase detection, and tracing a distance-decaying head to a few neurons. The tool matters because it makes a model's internal behaviour browsable and points to places where one could intervene.
Key points
- The tool is called bertviz and is available on GitHub. It was demonstrated on GPT-2 small and BERT-base (uncased).
- The paper cites BERT-Large's 24 layers and 16 heads, which give 384 attention patterns per input, as the core visualization difficulty.
- Observed head behaviours include:
- attending to the previous word;
- spreading attention evenly over earlier words;
- focusing on list items, verbs, acronyms, named entities, paired punctuation and subject-verb pairs.
- Bias case: with greedy decoding, the prompt "The doctor asked the nurse a question" plus She leads to a continuation tied to nurse, while He leads to one tied to doctor. One head shows the same split in its attention.
- In the model view, between-sentence heads stand out by a cross-hatch shape (e.g. layer 3, head 0). In GPT-2 layer 1, head 10, a few neuron positions drive attention that fades with distance, largely regardless of content.
Interesting ideas
- I1. Showing every layer and head at once as a "small multiples" grid of thumbnails lets you spot unusual patterns by eye before zooming in on one.
- I2. Splitting a dot-product score into its element-wise terms shows which individual dimensions push a decision up or down.
- I3. Probing with minimal pairs that differ in a single word (here, pronoun gender) separates a model's bias from its real reasoning.
- I4. Filtering attention by sentence (only A→B links) isolates how one text relates to another.
- I5. Noticing that a component behaves almost the same whatever the input reveals it as purely positional machinery.
For Mova
How these ideas could serve Mova, our small open language model — its files, stages and tests:
- I1 (tree): gparse could dump a layer×head thumbnail grid per sentence so we can quickly see which heads track UD arcs such as nsubj or obj.
- I2 (tree): the perceptron parser could display per-feature contributions to each arc score in the same column layout, which keeps every attachment explainable.
- I3 (frames): coreference could be tested on such swapped pairs, flagging any antecedent choice that flips only because of gender.
- I4 (answer): when matching a question to a fable, we could show only question→passage alignments to explain which words grounded the answer.
- I5 (learn): during evaluation, we could check whether a learned rule fires identically on scrambled inputs, as a negative control against shallow position-only cues.
Institutions
Fields, topics and concepts
Shelves 5The field of science this work belongs to (OpenAlex fields).Computer Science · 19,503 worksArtificial Intelligence · 18,805 worksArts and Humanities · 17,959 worksLanguage and Linguistics · 17,789 worksComputer Vision and Pattern Recognition · 3,621 works
Cite
Cite as Jesse Vig. 2019. A Multiscale Visualization of Attention in the Transformer Model. arXiv:1906.05714.
@misc{vig2019multiscale,
title = {{A Multiscale Visualization of Attention in the Transformer Model}},
author = {Jesse Vig},
year = {2019},
doi = {10.48550/arxiv.1906.05714},
eprint = {1906.05714},
archivePrefix = {arXiv},
url = {https://doi.org/10.48550/arxiv.1906.05714}
}References
- Dzmitry Bahdanau, Kyunghyun Cho, and Yoshua Bengio. 2015. Neural machine translation by jointly learning to align and translate (http://arxiv.org/abs/1409.0473). In Proc. ICLR. our card
- Anthony Bau, Yonatan Belinkov, Hassan Sajjad, Nadir Durrani, Fahim Dalvi, and James Glass. 2019. Identifying and controlling important neurons in neural machine translation. (https://arxiv.org/abs/1811.01157) In Proc. ICLR. our card
- Yonatan Belinkov and James Glass. 2019. Analysis methods in neural language processing: A survey (https://arxiv.org/abs/1812.08951). TACL. our card
- Jacob Devlin, Ming-Wei Chang, Kenton Lee, and Kristina Toutanova. 2018. BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding (https://arxiv.org/pdf/1810.04805.pdf). ArXiv Computation and Language. our card
- Sarthak Jain and Byron C. Wallace. 2019. Attention is not explanation (http://arxiv.org/abs/1902.10186). CoRR, abs/1902.10186. our card
- Llion Jones. 2017. Tensor2tensor transformer visualization. https://github.com/tensorflow/tensor2tensor/tree/master/tensor2tensor/visualization. OpenAlex ↗ · Scholar ↗
- Jaesong Lee, Joong-Hwi Shin, and Jun-Seok Kim. 2017. Interactive visualization and manipulation of attention-based neural machine translation (https://doi.org/10.18653/v1/D17-2021). In EMNLP: System Demonstrations. OpenAlex ↗ · Scholar ↗
- Shusen Liu, Tao Li, Zhimin Li, Vivek Srikumar, Valerio Pascucci, and Peer-Timo Bremer. 2018. Visual interrogation of attention-based models for natural language inference and machine comprehension (https://www.aclweb.org/anthology/D18-2007). In EMNLP: System Demonstrations. OpenAlex ↗ · Scholar ↗
- Kaiji Lu, Piotr Mardziel, Fangjing Wu, Preetam Amancharla, and Anupam Datta. 2018. Gender bias in neural natural language processing (http://arxiv.org/abs/1807.11714). CoRR, abs/1807.11714. our card
- Alec Radford, Jeffrey Wu, Rewon Child, David Luan, Dario Amodei, and Ilya Sutskever. 2019. Language models are unsupervised multitask learners (https://d4mucfpksywv.cloudfront.net/better-language-models/language_models_are_unsupervised_multitask_learners.pdf). Technical report. OpenAlex ↗ · Scholar ↗
- Alessandro Raganato and Jörg Tiedemann. 2018. An analysis of encoder representations in transformer-based machine translation (https://www.aclweb.org/anthology/W18-5431). In EMNLP Workshop: BlackboxNLP. our card
- Tim Rocktäschel, Edward Grefenstette, Karl Moritz Hermann, Tomas Kocisky, and Phil Blunsom. 2016. Reasoning about entailment with neural attention (https://arxiv.org/abs/1509.06664). In Proc. ICLR. our card
- Alexander M. Rush, Sumit Chopra, and Jason Weston. 2015. A neural attention model for abstractive sentence summarization (https://doi.org/10.18653/v1/D15-1044). In Proc. EMNLP. our card
- H. Strobelt, S. Gehrmann, M. Behrisch, A. Perer, H. Pfister, and A. M. Rush. 2018. Seq2Seq-Vis: A Visual Debugging Tool for Sequence-to-Sequence Models (http://arxiv.org/abs/1804.09299v1). ArXiv e-prints. OpenAlex ↗ · Scholar ↗
- Edward Tufte. 1990. Envisioning Information. Graphics Press, Cheshire, CT, USA. OpenAlex ↗ · Scholar ↗
- Ashish Vaswani, Samy Bengio, Eugene Brevdo, Francois Chollet, Aidan N. Gomez, Stephan Gouws, Llion Jones, Łukasz Kaiser, Nal Kalchbrenner, Niki Parmar, Ryan Sepassi, Noam Shazeer, and Jakob Uszkoreit. 2018. Tensor2tensor for neural machine translation (http://arxiv.org/abs/1803.07416). CoRR, abs/1803.07416. our card
- Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N Gomez, Ł ukasz Kaiser, and Illia Polosukhin. 2017. Attention is all you need (https://arxiv.org/pdf/1706.03762.pdf). arXiv preprint arXiv:1706.03762. our card
- Jesse Vig. 2019. BertViz: A tool for visualizing multi-head self-attention in the BERT model (https://debug-ml-iclr2019.github.io/cameraready/DebugML-19_paper_2.pdf). In ICLR Workshop: Debugging Machine Learning Models. OpenAlex ↗ · Scholar ↗
- Jesse Vig and Yonatan Belinkov. 2019. Analyzing the structure of attention in a transformer language model. In ACL Workshop: BlackboxNLP. our card
- Elena Voita, David Talbot, Fedor Moiseev, Rico Sennrich, and Ivan Titov. 2019. Analyzing multi-head self-attention: Specialized heads do the heavy lifting, the rest can be pruned (https://arxiv.org/abs/1905.09418). arXiv preprint arXiv:1905.09418. our card
- Jieyu Zhao, Tianlu Wang, Mark Yatskar, Vicente Ordonez, and Kai-Wei Chang. 2018. Gender bias in coreference resolution: Evaluation and debiasing methods (https://doi.org/10.18653/v1/N18-2003). In NAACL-HLT. our card
This page in Markdown https://md.research.elamur.ai/free/en/papers/vig-2019-multiscale-visualization-attention-transformer-model/