Abstract
Transformers have revolutionized natural language processing, yet their internal representations remain notoriously difficult to interpret. This paper presents a novel visualization method using dictionary learning to decompose contextualized embeddings into a sparse set of "transformer factors." By analyzing a pre-trained BERT model, we reveal a clear hierarchy of learning: the model progressively moves from simple word-sense disambiguation in early layers to complex sentence patterns and long-range dependencies in later ones. This unsupervised approach identifies linguistic structures without pre-defined labels, offering a more granular understanding of the model's latent space.
Introduction
Despite their dominance, Transformers are often treated as "black boxes." Traditional interpretability methods, such as probing tasks, rely on supervised classifiers to detect known linguistic structures. However, these methods are limited by human-defined categories and fail to explain exactly how representations are formed. We propose treating a word's contextualized embedding not as a single point, but as a linear superposition of many latent "transformer factors," allowing for the discovery of the model's internal vocabulary in an unsupervised manner.
Methodology
We collected embeddings from all 12 layers of a pre-trained BERT-base model and applied non-negative sparse coding (specifically the FISTA algorithm) to learn a dictionary of factors. Each embedding $x$ is represented as $x = \Phi\alpha + \epsilon$, where $\Phi$ is the dictionary and $\alpha$ is a sparse vector of activations. To track the emergence of concepts, we calculated an Importance Score (IS) for each factor at every layer. Factors were interpreted by analyzing the top 1000 sequences that triggered them and using LIME (Local Interpretable Model-agnostic Explanations) to highlight influential tokens.
The Hierarchy of Meaning
Our analysis suggests that BERT follows a "bottom-up" approach to language processing. Factors can be categorized by the depth at which their Importance Scores peak:
- Low-level (Layers 0-4): Focuses on word-level polysemy, such as distinguishing between different senses of the word "left."
- Mid-level (Layers 4-8): Captures sentence-level patterns, such as consecutive adjectives or specific syntactic formats.
- High-level (Layers 8-12): Detects abstract structures and long-range dependencies, such as identifying the start of a biography.
Progressive Disambiguation
A key insight is that disambiguation is a progressive process rather than a single event. For example, the word "left" can function as a direction or the past tense of "leave." Dictionary learning identifies specific factors that become increasingly distinct as the representation moves through the network.
As shown in the activation patterns, the model's internal representation for the verb sense of "left" peaks early. A single transformer factor was found to identify "left" as a verb with an F1 score of 96.5%, outperforming a standard POS tagger (94.1%) and nearly matching a fully fine-tuned BERT model (96.3%).
Conclusion
Dictionary learning provides a powerful, unsupervised lens into the internal workings of Transformers. By decomposing complex embeddings into sparse factors, we can observe the emergence of linguistic hierarchy and the mechanics of disambiguation. This method offers a robust alternative to traditional probing, enabling the discovery of unexpected patterns that human linguists might not otherwise consider.
Summary updated