Recognizing Long Grammatical Sequences using Recurrent Networks Augmented with an External Differentiable Stack

Ankur Arjun Mali, Alexander Ororbia, Daniel Kifer, C. Lee Giles

Abstract

Recurrent neural networks (RNNs) are widely used for sequence modeling, generation, and prediction. Despite success in applications such as machine translation and voice recognition, these stateful models often struggle with long-term dependencies and hierarchical structures. This paper introduces the DiffStk-RNN family, which augments RNNs with an external differentiable stack and specialized training techniques—like noise injection and "carry-forward" state updates—to successfully classify grammatical strings up to length 160, far outperforming traditional LSTMs and GRUs.

Introduction

While RNNs excel at many sequence tasks, they face a "memory wall" when processing Context-Free Languages (CFLs). CFLs involve hierarchical or nested structures, such as matching parentheses in code or nested clauses in natural language. The simplest version is the Dyck language ($D_n$), consisting of balanced strings of $n$ types of parentheses. While an LSTM can learn to "count" one type of bracket ($D_1$), it struggles with multiple types ($D_n$ where $n > 1$) because counting is insufficient; a stack is required to track which specific bracket must be closed next. Furthermore, standard RNNs often fail to generalize to sequences longer than those seen during training.

Methods: The DiffStk Architecture

The authors propose the "DiffStk" (Differentiable Stack) models, coupling a controller (such as an RNN or LSTM) with a stack supporting three operations: PUSH, POP, and NO-OP.

The DiffStk-RNN architecture showing the interaction between the controller hidden state ($z_t$), input ($x_t$), and the external stack ($S_t$) across time steps.

Key innovations include:

  • Next-Step Prediction Schema: The model predicts the next token and string validity at every time step, allowing it to recover from mistakes during training.
  • Carry-Forward State Update: During a NO-OP (no stack action), the model can "carry forward" its previous hidden state, preventing state drift during long periods of stack inactivity.
  • Weight Noise Injection: Adding Gaussian noise to hidden weights during training acts as a regularizer, significantly improving generalization to longer sequences.
  • Multiplicative Integration (MRNN/MIRNN): Multiplicative interactions between input and hidden states allow the RNN to better approximate the discrete transition rules of a formal Pushdown Automaton (PDA).
  • Hard Negative Sampling: Training on "difficult" negative examples—strings that are nearly valid—forces the model to learn underlying grammar rather than surface patterns.

Datasets and Evaluation

The models were evaluated on several formal and natural language benchmarks:

  1. Dyck Languages ($D_2, D_3, D_6$): Nested parentheses with 2, 3, and 6 different types of brackets.
  2. Palindrome Language: Strings that read the same forwards and backwards.
  3. Penn Treebank (PTB): A standard real-world dataset for word-level language modeling to ensure the stack-augmented models maintain performance on non-synthetic data.

Key Insights and Results

The research highlights that physical memory structures (stacks) are fundamental for true structural recognition, as "counting" mechanisms in LSTMs are insufficient for hierarchical languages. The DiffStk-MRNN variant emerged as the top performer, using multiplicative gates to "switch" behavior based on input, mimicking discrete state transitions.

Performance Highlights: * Generalization: On $D_2$, standard RNNs and LSTMs achieved near 0% accuracy on long strings (length 160). In contrast, DiffStk-MRNN achieved 90% accuracy. * Complexity: On the complex $D_6$ grammar, DiffStk-MRNN maintained 99% accuracy on test sets, while traditional models failed. * Natural Language: On the Penn Treebank dataset, DiffStk-MRNN achieved a test perplexity of 115, matching standard LSTMs and proving that the stack augmentation does not degrade performance on standard natural language tasks.

Conclusion

The DiffStk-RNN family demonstrates that augmenting recurrent networks with differentiable memory structures and robust training techniques allows for the recognition of long, complex grammatical sequences. While accuracy begins to dip as both bracket types and string lengths increase, the stability provided by NO-OP operations and noise injection offers a path forward for more interpretable and scalable memory-augmented neural networks. Future work may explore discrete stacks trained via reinforcement learning to further improve stability and optimization efficiency.

Summary updated

BibTeX
@inproceedings{mali2021recognizing,
  author = {Ankur Arjun Mali and Alexander Ororbia and Daniel Kifer and C. Lee Giles},
  title = {{Recognizing Long Grammatical Sequences using Recurrent Networks Augmented with an External Differentiable Stack}},
  booktitle = {ICGI},
  year = {2021},
  url = {https://openreview.net/forum?id=7l0Rg53od5v},
}
Figure