NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Inferring Algorithmic Patterns with Stack-Augmented Recurrent Nets

Armand Joulin, Tomás MikolovNeurIPS 2015

Introduction

While deep learning has revolutionized fields like computer vision and natural language processing, standard models still struggle with "algorithmic" reasoning. For example, a standard Recurrent Neural Network (RNN) might learn to predict the next character in a simple sequence like $a^n b^n$ (e.g., aaabbb) for small values of $n$, but it often fails to generalize when $n$ exceeds the lengths seen during training.

The core issue is that standard RNNs and Long Short-Term Memory (LSTM) networks use a "flat" hidden state to store information. This architecture makes it difficult for them to perform operations that require precise counting or long-term, structured memorization. Researchers from Facebook AI Research (FAIR) address this by augmenting RNNs with a differentiable, structured memory—specifically a Stack—creating a model that learns the underlying "program" rather than just memorizing data.

The Stack-Augmented Recurrent Network

The authors introduce the Stack-Augmented Recurrent Network (Stack RNN). The key innovation is connecting a standard RNN controller to an external, structured memory that behaves like a Last-In, First-Out (LIFO) stack.

  • Differentiable Operations: To train the network using standard backpropagation, the discrete stack operations (PUSH, POP, and NO-OP) are made continuous. The network outputs a probability distribution over these actions at each time step.
  • The Controller: At each step, the RNN controller observes the current input and the top elements of the stack. It then decides which stack action to take and what value to push.
  • Rounding Procedure: Continuous operations on a stack can lead to numerical "drift" over very long sequences. The authors implement a rounding and fine-tuning strategy that encourages the controller to make discrete (0 or 1) decisions, significantly improving stability and generalization.

The researchers also experimented with a List RNN (using a doubly-linked list memory) and architectures utilizing multiple stacks working in parallel.

Key Insights: Inductive Bias and Stack Interaction

Providing a network with a stack introduces a powerful "inductive bias" for tasks involving nested structures or counting. Instead of forcing raw neurons to reinvent the concept of a stack, the architecture inherently provides the right tools for the job.

This structure encourages true generalization over mere memorization. Standard RNNs often "cheat" by memorizing specific sequence lengths. The Stack RNN, however, learns the underlying rule (e.g., "Push an 'a' for every input, then Pop an 'a' for every 'b'").

For complex tasks, the model learns to use multiple stacks in a highly coordinated way.

Stack operations during binary addition

As seen in the binary addition task, the network dynamically allocates different stacks for distinct sub-tasks—for example, using one stack to store the first number, another for the second number, and a separate stack to handle the "carry" bit.

Experimental Results

The models were evaluated on synthetic algorithmic patterns (counting, memorization, binary addition) and the Penn Treebank (PTB) dataset for language modeling.

  • Algorithmic Superiority: On the $a^n b^n c^n$ counting task, the Stack RNN achieved 100% accuracy on test sequences significantly longer than the training sequences. In contrast, a standard RNN achieved only 13.3%, and an LSTM achieved 68.3%.
  • Generalization in Binary Addition and Memorization: The Stack RNN successfully learned to perform binary addition and sequence memorization, maintaining near-perfect accuracy even as the sequences grew much larger than those seen in training.
Accuracy vs. sequence length for memorization and binary addition
  • Language Modeling: On the PTB dataset, the Stack RNN outperformed a standard RNN and was competitive with LSTMs, suggesting that stack-like structures—which naturally align with the nested nature of human grammar—are useful for real-world language tasks.

Limitations and Future Directions

While powerful, Stack RNNs come with optimization challenges. Training the controller to use the stack correctly is more difficult than training a standard RNN, often requiring several random restarts to find a model that successfully learns the target logic.

Additionally, the current model assumes a fixed memory topology (a stack or a list). A promising future direction is developing models that can dynamically learn the optimal memory structure—deciding whether a task requires a stack, a queue, or a grid—directly from the data. Finally, the model remains somewhat sensitive to input formatting, highlighting an open challenge in making algorithmic neural networks more robust.

Summary updated

BibTeX
@inproceedings{joulin2015inferring,
  author = {Armand Joulin and Tomás Mikolov},
  title = {{Inferring Algorithmic Patterns with Stack-Augmented Recurrent Nets}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2015},
}
Figure