Abstract
Earlier approaches indirectly studied the information captured by the hidden states of recurrent and non-recurrent neural machine translation models by feeding them into different classifiers. In this paper, we look at the encoder hidden states of both transformer and recurrent machine translation models from the nearest neighbors perspective. We investigate to what extent the nearest neighbors share information with the underlying word embeddings as well as related WordNet entries. Additionally, we study the underlying syntactic structure of the nearest neighbors to shed light on the role of syntactic similarities in bringing the neighbors together. We compare transformer and recurrent models in a more intrinsic way in terms of capturing lexical semantics and syntactic structures, in contrast to extrinsic approaches used by previous works. In agreement with the extrinsic evaluations in the earlier works, our experimental results show that transformers are superior in capturing lexical semantics, but not necessarily better in capturing the underlying syntax. Additionally, we show that the backward recurrent layer in a recurrent model learns more about the semantics of words, whereas the forward recurrent layer encodes more context.
Introduction
The internal representations of Neural Machine Translation (NMT) models, such as Transformers and LSTMs, are often treated as "black boxes." While these models achieve high performance, understanding the specific linguistic information—lexical, semantic, or syntactic—encoded in their intermediate hidden states is crucial for model interpretability and design. Previous research has largely relied on extrinsic methods, where auxiliary classifiers (probes) are trained on hidden states to predict linguistic features like part-of-speech tags or dependency relations.
This work proposes an intrinsic evaluation framework. By analyzing the nearest neighbors of word representations in the hidden state space, the authors investigate how different architectures inherently organize information. This approach avoids the potential biases introduced by training external classifiers and directly probes the geometry of the representation space.
Methodology
The study compares two primary architectures: a bidirectional LSTM and a 6-layer Transformer, both trained on the WMT15 English-German and German-English translation tasks. Hidden states are extracted from a held-out set of 100,000 sentences.
Nearest Neighbor Analysis
For every word occurrence in the dataset, the authors identify its 10 nearest neighbors in the hidden state space using cosine similarity. This allows for a direct comparison of how "contextualized" a word's representation becomes relative to its static input embedding.
Evaluation Metrics
To quantify the information captured, three metrics are employed:
- Embedding Coverage: The overlap between the hidden-state neighbors and the neighbors of the static, uncontextualized word embeddings.
- WordNet Coverage: The proportion of neighbors that share a semantic relation (e.g., synonymy, hypernymy) with the target word according to WordNet or GermaNet.
- Syntactic Similarity: The authors use a constituent parser to extract the smallest phrase constituent subtree above each word. Syntactic similarity between a word and its neighbors is then measured using the PARSEVAL metric on these subtrees.
Results and Discussion
The analysis reveals fundamental differences in how Transformers and LSTMs process language.
Lexical Semantics vs. Syntax
Transformers demonstrate a significantly higher capacity for capturing lexical semantics. For English-German, Transformer hidden states showed a 67% overlap with WordNet relations, compared to only 24% for the recurrent model. Conversely, LSTMs appear more focused on syntactic structure. The average PARSEVAL score for LSTMs (0.38) was higher than for Transformers (0.31), suggesting that recurrence is particularly effective at clustering words with similar hierarchical roles.
Representation Drift
Hidden states in both models drift away from static embeddings as context is integrated, but the degree of drift varies. Transformers maintain a much higher overlap with static embeddings (37% overall) than LSTMs (18%). This suggests that the non-recurrent, self-attention mechanism provides a more "direct path" from input to output, whereas recurrence more aggressively transforms the representation.
Directional Roles in LSTMs
In bidirectional LSTMs, the forward and backward layers serve distinct functions. The backward layer's neighbors have a higher overlap with static embeddings (24%), behaving more like traditional lexical representations. The forward layer, however, shows decreasing embedding coverage as it processes a sentence, indicating it prioritizes the accumulation of long-distance context over the preservation of the original word identity.
Conclusion
This intrinsic analysis confirms that Transformers excel at lexical semantic modeling, while recurrent models are more adept at capturing local syntactic structures. These findings align with previous extrinsic probing studies but offer a more direct view of the models' internal organization. Future work could explore hybrid architectures that leverage the semantic strengths of self-attention alongside the structural modeling capabilities of recurrence.
Summary updated