Transformer visualization via dictionary learning: contextualized embedding as a linear superposition of transformer factors
Zeyu Yun, Yubei Chen, Bruno A Olshausen, Yann LeCun
TL;DR
This work addresses the interpretability of transformer representations by modeling contextualized embeddings as sparse, non-negative combinations of latent 'transformer factors' learned via dictionary learning. A single factor dictionary Φ is learned across all layers using non-negative sparse coding, enabling x ≈ Φ α with α ≥ 0, and visualized through top-activation sequences and LIME-based token saliency. Empirically, the approach reveals a hierarchy of patterns: low-level word sense disambiguation, mid-level sentence or phrase patterns, and high-level long-range dependencies, with some factors aligning with linguistic expectations and others yielding novel insights. The method provides a complementary, visualization-based alternative to probing tasks for understanding how transformers organize information across layers, and it includes an interactive resource to explore the discovered factors.
Abstract
Transformer networks have revolutionized NLP representation learning since they were introduced. Though a great effort has been made to explain the representation in transformers, it is widely recognized that our understanding is not sufficient. One important reason is that there lack enough visualization tools for detailed analysis. In this paper, we propose to use dictionary learning to open up these "black boxes" as linear superpositions of transformer factors. Through visualization, we demonstrate the hierarchical semantic structures captured by the transformer factors, e.g., word-level polysemy disambiguation, sentence-level pattern formation, and long-range dependency. While some of these patterns confirm the conventional prior linguistic knowledge, the rest are relatively unexpected, which may provide new insights. We hope this visualization tool can bring further knowledge and a better understanding of how transformer networks work. The code is available at https://github.com/zeyuyun1/TransformerVis
