On the Practical Ability of Recurrent Neural Networks to Recognize Hierarchical Languages

Satwik Bhattamishra, Kabir Ahuja, Navin Goyal

Abstract

While recurrent models have been effective in NLP tasks, their performance on context-free languages (CFLs) has been found to be quite weak. Given that CFLs are believed to capture important phenomena such as hierarchical structure in natural languages, this discrepancy in performance calls for an explanation. We study the performance of recurrent models on Dyck-n languages, a particularly important and well-studied class of CFLs. We find that while recurrent models generalize nearly perfectly if the lengths of the training and test strings are from the same range, they perform poorly if the test strings are longer. At the same time, we observe that recurrent models are expressive enough to recognize Dyck words of arbitrary lengths in finite precision if their depths are bounded. Hence, we evaluate our models on samples generated from Dyck languages with bounded depth and find that they are indeed able to generalize to much higher lengths. Since natural language datasets have nested dependencies of bounded depth, this may help explain why they perform well in modeling hierarchical dependencies in natural language data despite prior works indicating poor generalization performance on Dyck languages. We perform probing studies to support our results and provide comparisons with Transformers.

Introduction: The Paradox of Hierarchical Languages

In computational linguistics, natural languages are often modeled as Context-Free Languages (CFLs) due to their inherent hierarchical structures, such as nested clauses. While Recurrent Neural Networks (RNNs) are theoretically Turing-complete with infinite precision, empirical evidence has frequently suggested they struggle to generalize on formal CFLs like the Dyck-n language—the language of balanced parentheses with $n$ types. This creates a paradox: if LSTMs are deficient at processing formal hierarchical languages, why do they excel at modeling natural language?

Hierarchical dependencies in natural language (top) can be mapped to the nested structure of Dyck languages (bottom).

This paper bridges this gap by investigating the relationship between sequence length, nesting depth, and model expressivity. We hypothesize that the perceived failure of RNNs is not a failure of length generalization per se, but rather a failure to handle the increasing precision requirements of deeper nesting.

Methodology

We evaluate models on the Next Character Prediction (NCP) task using synthetic datasets based on Dyck-2, Dyck-3, and Dyck-4. Given a prefix, the model must predict all valid subsequent characters.

Model Architectures

We compare standard LSTMs against Transformers (GPT architecture). Theoretically, we prove that an RNN with ReLU activation can simulate a Deterministic Pushdown Automaton (PDA), though numerical precision must increase with stack depth.

Experimental Setup

To test generalization, we structure data into "bins":

  • Training Set: Strings of lengths 2–50.
  • Bin-A (Unbounded Depth): Test strings of lengths 52–100 with no depth restrictions.
  • Bin-B (Bounded Depth): Test strings of lengths 52–100 and 102–150, with nesting depth restricted to $\le 10$, mirroring the bounded depth found in human languages.

Key Insights: Depth as the Bottleneck

Our primary finding is that nesting depth, rather than sequence length, is the critical bottleneck for LSTMs. Because precision requirements scale with stack depth, a fixed-precision LSTM eventually lacks the representational capacity for deep stacks. However, human languages rarely feature deep nesting (e.g., five levels of nested "which" clauses). By bounding the depth in our experiments to reflect natural language constraints, we find that LSTMs generalize to longer strings almost perfectly.

Surprisingly, LSTMs outperformed Transformers on length generalization. We attribute this to the Transformer's reliance on positional encodings; performance degrades when encountering position indices unseen during training. LSTMs, being recursive, do not suffer from this "out-of-vocabulary" position issue.

Probing and Internal Representations

To understand how LSTMs manage these tasks, we trained auxiliary linear classifiers to probe their hidden states. We found that LSTMs develop emergent data structures that track stack depth with 100% accuracy and can identify specific elements deep within the hidden stack.

LSTMs maintain high accuracy and recall when extracting stack elements, even as sequence length increases significantly beyond the training range.

Results

  • Bounded Depth Generalization: On Dyck-2, LSTMs trained on lengths up to 50 achieved 99.6% accuracy on length 100 and 96.0% on length 150, provided depth was bounded. Without the bound, accuracy on length 100 dropped to 75.1%.
  • Transformer Performance: On the same bounded-depth task, Transformers dropped from 92.1% accuracy at length 100 to just 36.3% at length 150.
  • Robustness: LSTMs remained accurate even under distribution shifts in the test data, suggesting they learn underlying structural rules rather than statistical shortcuts.

Conclusion

This study demonstrates that LSTMs are remarkably capable of recognizing hierarchical languages when the nesting depth is bounded, a condition satisfied by natural language. While LSTMs show superior length generalization compared to Transformers in this context, future work should investigate if alternative positional encodings might improve Transformer performance.

Summary updated

BibTeX
@inproceedings{bhattamishra2020practical,
  author = {Satwik Bhattamishra and Kabir Ahuja and Navin Goyal},
  title = {{On the Practical Ability of Recurrent Neural Networks to Recognize Hierarchical Languages}},
  booktitle = {International Conference on Computational Linguistics},
  year = {2020},
  url = {https://openreview.net/forum?id=WtOB2UKlsl},
}
Figure