Abstract
Transformer models have become the cornerstone of modern natural language processing, yet they remain largely "black boxes." This study introduces a novel interpretability framework based on minimal token perturbations. By replacing tokens with their nearest semantic neighbors in the embedding space, we investigate the model's sensitivity to specific inputs and the evolution of information across layers. Our results demonstrate that rare words induce significantly larger internal shifts than common words. Furthermore, we observe a consistent trend of information intermixing through the network's depth, culminating in a task-specific convergence in the final layer.
Introduction
Despite the widespread adoption of Transformers, tracing the influence of individual words on a model's final decision remains a significant challenge. Traditional interpretability methods often rely on perturbations, such as masking or deleting tokens. However, these techniques frequently generate "out-of-distribution" (OOD) sequences that do not reflect natural language, potentially leading to misleading conclusions about model behavior. This research addresses this gap by proposing a framework of minimal token perturbations that preserves the naturalness of the input while revealing the underlying dynamics of the embedding space and subsequent layers.
Methodology
The researchers utilize a BERT model fine-tuned on the IMDb Movie Reviews dataset (25,000 labeled reviews) for sentiment analysis. The core of the approach is the identification of minimal token perturbations: replacing a target token ($Tok_i$) with its "nearest neighbor"—the word in the vocabulary with the highest cosine similarity in the model's own embedding space.
The study evaluates sensitivity using several metrics:
- Optimization of Shifts: Measuring the change in the embedding space ($||E(x) - E(x')||$).
- Norm Comparison: Testing the consistency of results across $l_1, l_2,$ and $l_\infty$ norms.
- Commonness Metric: Developing a "commonness score" based on log-transformed and normalized word frequencies.
- Layer-wise Analysis: Tracking the propagation of these minimal shifts through the 12 layers of the BERT architecture.
Results and Analysis
Sensitivity and Word Frequency
A primary finding is the inverse relationship between a word's frequency and its embedding stability. Common tokens, such as punctuation marks ('.' and ',') and frequent function words, possess many close neighbors in the embedding space, making the model robust to their substitution. In contrast, rare words are underrepresented and have fewer close neighbors, meaning even a "minimal" change results in a substantial shift in the internal state.
Information Propagation and Intermixing
As data moves through the Transformer's layers, individual token identities begin to blur. By the middle layers (around layer 8), the hidden state at a specific position is no longer a clear representation of the original input token but rather a blend of the surrounding context. The distance between the original and perturbed sequences increases steadily through layer 11.
Interestingly, this distance often decreases at layer 12. This "U-turn" suggests that the final layer discards general linguistic nuances to focus purely on the classification task (e.g., determining sentiment), causing different but semantically related inputs to converge toward a similar task-specific representation.
Norm Invariance
The study found a near-perfect correlation ($r=0.99$) between results obtained using different mathematical norms ($l_1, l_2, l_\infty$). This suggests that the observed sensitivity patterns are an inherent property of the model's architecture and training, rather than an artifact of the measurement method.
Discussion and Limitations
While providing valuable insights into Transformer internals, this study has several limitations. The analysis focused on single-token replacements; however, real-world linguistic variations often involve complex structural changes or multi-token shifts. Additionally, the findings are based on a BERT model trained for sentiment analysis. Future research should investigate whether these patterns—particularly the layer-wise intermixing and the final layer convergence—hold true for models trained on different objectives, such as machine translation or summarization.
Summary updated