Abstract
In modern machine (ML) learning systems, Transformer-based architectures have achieved milestone success across a broad spectrum of tasks, yet understanding their operational mechanisms remains an open problem. To improve the transparency of ML systems, automata extraction methods, which interpret stateful ML models as automata typically through formal languages, have proven effective for explaining the mechanism of recurrent neural networks (RNNs). However, few works have been applied to this paradigm to Transformer models. In particular, understanding their processing of formal languages and identifying their limitations in this area remains unexplored. In this paper, we propose an automata extraction algorithm specifically designed for Transformer models. Treating the Transformer model as a black-box system, we track the model through the transformation process of their internal latent representations during their operations, and then use classical pedagogical approaches like L* algorithm to interpret them as deterministic finite-state automata (DFA). Overall, our study reveals how the Transformer model comprehends the structure of formal languages, which not only enhances the interpretability of the Transformer-based ML systems but also marks a crucial step toward a deeper understanding of how ML systems process formal languages. Code and data are available at https://github.com/Zhang-Yihao/Transfomer2DFA.
Introduction
Transformers have revolutionized the field of artificial intelligence, yet they remain notoriously difficult to interpret. Unlike Recurrent Neural Networks (RNNs), which process data sequentially and maintain an explicit hidden state, Transformers process sequences in parallel. This architecture makes them highly efficient but creates a "black box" problem when trying to understand how they learn the rigid syntax of formal languages. This paper introduces a novel framework to bridge this gap by extracting human-readable Deterministic Finite Automata (DFAs) from encoder-only Transformers.
Methodology: The Extraction Pipeline
The proposed method treats the Transformer as a black box and employs a two-step distillation process to map its high-dimensional latent space into a discrete state machine.
- Transformer to DCSA: The framework utilizes the
[CLS]token embedding as a proxy for the model's "state." By applying Representation Engineering and a specific alignment loss, the researchers force a Deterministic Continuous State Automaton (DCSA) to mimic the Transformer’s internal latent transitions. - DCSA to DFA: The continuous states are then discretized using the L* algorithm, a classic active learning approach. This step "interrogates" the DCSA to build a finite set of states and transitions that represent the model's learned logic.
Experimental Evaluation
The researchers evaluated their method on three sets of formal languages: Tomita languages, Mod $n$ languages (representing binary numbers divisible by $n$), and a diverse set of Transformer-specific formal languages. The results demonstrate that for languages the Transformer can learn successfully, the extracted DFAs achieve near-perfect consistency with the original model.
Memorization vs. Generalization
A key insight from the study is the relationship between model performance and DFA complexity. When a Transformer fails to generalize—such as with the Mod 3 or Mod 5 languages—the extracted DFA becomes "voluminous," often containing dozens of states. This visual evidence suggests that the model is not learning the underlying rule but is instead attempting to memorize specific patterns. This provides a "smoking gun" for identifying overfitting in Transformer architectures.
Conclusion
By converting complex mathematical embeddings into simple visual flowcharts, this research provides a vital tool for auditing Transformer-based systems. The ability to extract DFAs allows researchers to verify whether a model has truly mastered a language's syntax or is merely relying on memorization. Future work will look toward extending this methodology to decoder-only models and scaling it for the complexities of natural language processing.
Summary updated