NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

AttentionViz: A Global View of Transformer Attention

Catherine Yeh, Yida Chen, Aoyu Wu, Cynthia Chen, Fernanda B. Viégas, Martin Wattenberg

TL;DR

This paper introduces AttentionViz, a novel interactive visualization tool that provides a global view of transformer self-attention mechanisms by visualizing joint query-key embeddings. This approach allows researchers to explore attention patterns across multiple input sequences and heads simultaneously, offering deeper insights into how transformers learn contextual relationships in both language and vision tasks.

What Problem Does This Paper Address?

Transformer models, despite their widespread success in NLP and computer vision, remain largely "black boxes." Understanding their inner workings, particularly the self-attention mechanism, is crucial for building more reliable systems, troubleshooting issues, and driving further innovation. Existing visualization techniques often focus on single input sequences or individual attention heads, making it difficult to grasp global patterns and compare behavior across the entire model or multiple inputs. This limits researchers' ability to understand how different attention heads contribute to the model's overall behavior and to identify potential anomalies or redundancies.

Methods

The core of AttentionViz lies in visualizing a joint embedding space of query and key vectors for each attention head. Here's a breakdown of their approach:

  • Joint Query-Key Embedding: For each token (word or image patch), the transformer generates a query vector and a key vector. AttentionViz projects these vectors into a lower-dimensional space (using t-SNE, UMAP, or PCA) to create a scatterplot where each point represents a query or key. Queries are typically shown in green and keys in pink.
  • Key Translation: To improve the visual comparison between queries and keys, the key vectors are translated so that the query and key distributions share the same centroid. This is mathematically sound because the softmax function is translation-invariant.
  • Query/Key Scaling: To ensure that distances in the embedding space better reflect attention weights (higher dot products should correspond to closer distances), query and key vectors are scaled. This is achieved by finding a scaling factor that maximizes the correlation between query-key dot products and their distances in the embedding space.
  • Interactive Views: AttentionViz offers three main views:
  • Matrix View: A "global" overview displaying small multiples of all attention heads across transformer layers, allowing for quick scanning and comparison.
  • Single View: A detailed view of a single attention head, enabling users to zoom in and explore specific query-key interactions, with options to project attention lines.
  • Sentence/Image View: Provides fine-grained visualizations for individual sentences or images, showing attention patterns at a token or patch level.

Datasets

The paper uses a variety of datasets for their experiments:

  • Language:
    • Wiki-Auto [23]: Used for general input sentences.
  • SuperGLUE AX [56]: Used to explore task-specific attention patterns for textual entailment.
  • Vision:
    • ImageNet [44]
    • Microsoft COCO [28]
    • Synthetic image data (color and brightness gradient images, sinusoidal signals with varying frequencies and orientations).

Key Insights and Ideas

  • "Visual Traces" of Attention: The joint query-key embedding space reveals distinct geometric patterns ("visual traces") that correspond to different types of attention behavior. These traces can be linked to specific functions of attention heads.
  • Hue/Brightness Specialization in Vision Transformers: AttentionViz identified specific attention heads in ViT that specialize in either color-based or brightness-based patterns, suggesting a form of visual feature specialization.
  • Positional Attention Signatures: Language transformers exhibit distinct "spiral" or "clump" patterns in their attention heads, which are strongly correlated with positional information. Deeper layers in vision transformers show broader attention across rows and columns, suggesting an "elongated filter" behavior.
  • "Look at Self" Heads: A surprising finding in ViT was the identification of attention heads where image patches primarily attend to themselves, with learned query and key projection layers showing redundant parameters.
  • Norm Disparities in GPT-2: Early layers of GPT-2 showed significant disparities between query and key norms, a phenomenon that was not easily explained and has since been linked to training instability in other research.
  • "Attend to First" Pattern in GPT-2: Later layers of GPT-2 frequently exhibit an "attend to first" pattern, where attention is heavily directed towards the initial token, suggesting a potential area for further investigation into how GPT-2 handles positional information.

Key Results

  • Qualitative Insights: AttentionViz successfully revealed novel insights into transformer behavior, including:
  • Visual attention heads specializing in hue and brightness.
  • Positional attention patterns in both language and vision transformers.
  • "Look at self" attention heads in ViT.
  • Norm disparities in GPT-2 query/key vectors.
  • A prevalent "attend to first" pattern in GPT-2.
  • Expert Feedback: Domain experts found the "global" perspective of Matrix View highly valuable for comparing embeddings at scale and expressed interest in using the tool for hypothesis testing, model pruning, and debugging.
  • Quantitative Findings: The paper reports strong correlations between query-key distances and dot products in the joint embedding space across models like BERT, GPT-2, and ViT, validating the effectiveness of their visualization approach. For example, the mean correlation for BERT on Wiki-Auto data was -0.938.

Limitations and Future Work

  • Projection Distortion: The authors acknowledge that dimensionality reduction techniques like t-SNE and UMAP can introduce distortions, leading to potential skepticism about the trustworthiness of the visualizations. Future work could focus on better quantifying this trust or developing methods to support hypothesis testing based on visual insights.
  • Scalability and Usability: While AttentionViz is designed to be flexible, managing the complexity of visualizing all heads and layers can still be overwhelming. Future work could explore ways to summarize information, focus on specific tasks, or provide quicker labeling of heads. The current system also has limitations in data pre-computation times and memory requirements, with plans to improve scalability and allow on-the-fly input.
  • Incorporating Value Vectors: The current visualization focuses on queries and keys. Future research could explore how to incorporate value vectors into the visualization to gain a more complete understanding of the attention mechanism.
  • Beyond Exploration: Adapting AttentionViz for hypothesis testing and causal tracing is suggested as a promising direction for practical model debugging.

Summary updated

BibTeX
@article{yeh2024attentionviz,
  author = {Catherine Yeh and Yida Chen and Aoyu Wu and Cynthia Chen and Fernanda B. Viégas and Martin Wattenberg},
  title = {{AttentionViz: A Global View of Transformer Attention}},
  journal = {IEEE Trans. Vis. Comput. Graph.},
  year = {2024},
  doi = {10.1109/tvcg.2023.3327163},
}
Figure